ChimeraTK-DeviceAccess  03.18.00
testBackendFactory.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #define BOOST_TEST_DYN_LINK
5 #define BOOST_TEST_MODULE BackendFactoryTest
6 #include <boost/test/unit_test.hpp>
7 using namespace boost::unit_test_framework;
8 
9 #include "BackendFactory.h"
10 #include "DeviceAccessVersion.h"
11 #include "DummyBackend.h"
12 #include "Exception.h"
13 
14 #include <boost/make_shared.hpp>
15 
16 #include <cstdio>
17 namespace ChimeraTK {
18  using namespace ChimeraTK;
19 }
20 using namespace ChimeraTK;
21 
22 //#undef TEST_DMAP_FILE_PATH
23 //#define TEST_DMAP_FILE_PATH "/testDummies.dmap"
24 
25 struct NewBackend : public DummyBackend {
26  using DummyBackend::DummyBackend;
27 
28  static boost::shared_ptr<DeviceBackend> createInstance(
29  std::string instance, std::map<std::string, std::string> parameters) {
30  return returnInstance<NewBackend>(instance, convertPathRelativeToDmapToAbs(parameters["map"]));
31  }
32 
33  // no registerer, we do it manually
34 };
35 
36 BOOST_AUTO_TEST_SUITE(BackendFactoryTestSuite)
37 
38 BOOST_AUTO_TEST_CASE(testCreateBackend) {
39  BackendFactory::getInstance().setDMapFilePath("");
40  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("test"), ChimeraTK::logic_error);
41 
42  std::string testFilePath = TEST_DMAP_FILE_PATH;
43  std::string oldtestFilePath = std::string(TEST_DMAP_FILE_PATH) + "Old";
44  std::string invalidtestFilePath = std::string(TEST_DMAP_FILE_PATH) + "disabled";
45  BOOST_CHECK_THROW(BackendFactory::getInstance().setDMapFilePath(invalidtestFilePath), ChimeraTK::logic_error);
46  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("test"),
47  ChimeraTK::logic_error); // dmap file not found
48  // exception .
49  BackendFactory::getInstance().setDMapFilePath(oldtestFilePath);
50  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("test"),
51  ChimeraTK::logic_error); // file found but not
52  // an existing alias.
53  boost::shared_ptr<DeviceBackend> testPtr;
54  BOOST_CHECK_NO_THROW(testPtr = BackendFactory::getInstance().createBackend("DUMMYD0")); // entry in old dummies.dmap
55  BOOST_CHECK(testPtr);
56  testPtr.reset();
57  BackendFactory::getInstance().setDMapFilePath(testFilePath);
58  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("test"),
59  ChimeraTK::logic_error); // not an existing
60  // alias.
61  BOOST_CHECK_NO_THROW(testPtr = BackendFactory::getInstance().createBackend("DUMMYD9")); // entry in dummies.dmap
62  BOOST_CHECK(testPtr);
63  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("FAKE1"),
64  ChimeraTK::logic_error); // entry in
65  // dummies.dmap for
66  // unregistered
67  // device
68  boost::shared_ptr<DeviceBackend> testPtr2;
69  BOOST_CHECK_NO_THROW(testPtr2 = BackendFactory::getInstance().createBackend("DUMMYD9")); // open existing backend
70  // again
71  BOOST_CHECK(testPtr2 == testPtr); // must be same
72 }
73 
74 BOOST_AUTO_TEST_CASE(testPluginMechanism) {
75  // check the registation of a new backed, called NewBackend ;-)
76  // Throws with the wrong version (00.18 did not have the feature yet, so its
77  // safe to use it) It however is only happening when the backend is tried to
78  // be instantiated because otherwise we would end up in uncatchable exceptions
79  // while loading a dmap file with a broken backend.
80  BOOST_CHECK_NO_THROW(ChimeraTK::BackendFactory::getInstance().registerBackendType(
81  "newBackendWrongVersion", &NewBackend::createInstance, {"map"}, "00.18"));
82 
83  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("(newBackendWrongVersion?map=goodMapFile.map)"),
85 
86  BOOST_CHECK_NO_THROW(ChimeraTK::BackendFactory::getInstance().registerBackendType(
87  "newBackend", &NewBackend::createInstance, {"map"}, CHIMERATK_DEVICEACCESS_VERSION));
88 
89  BOOST_CHECK_NO_THROW(BackendFactory::getInstance().createBackend("(newBackend?map=goodMapFile.map)"));
90 
91  BOOST_CHECK_THROW(
92  ChimeraTK::BackendFactory::getInstance().loadPluginLibrary("notExisting.so"), ChimeraTK::logic_error);
93 
94  BOOST_CHECK_NO_THROW(ChimeraTK::BackendFactory::getInstance().loadPluginLibrary("./libWorkingBackend.so"));
95  // check that the backend really is registered
96  BOOST_CHECK_NO_THROW(BackendFactory::getInstance().createBackend("(working?map=goodMapFile.map)"));
97  BOOST_CHECK_NO_THROW(BackendFactory::getInstance().createBackend("(working?map=goodMapFile.map)"));
98 
99  BOOST_CHECK_THROW(
100  ChimeraTK::BackendFactory::getInstance().loadPluginLibrary("libNotRegisteringPlugin.so"), ChimeraTK::logic_error);
101  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("(notRegisteringPlugin?map=goodMapFile.map)"),
103 
104  BOOST_CHECK_NO_THROW(ChimeraTK::BackendFactory::getInstance().loadPluginLibrary("./libWrongVersionBackend.so"));
105  BOOST_CHECK_THROW(
106  BackendFactory::getInstance().createBackend("(wrongVersionBackend?map=goodMapFile.map)"), ChimeraTK::logic_error);
107 
108  BOOST_CHECK_NO_THROW(ChimeraTK::BackendFactory::getInstance().loadPluginLibrary("./libWrongVersionBackendCompat.so"));
109  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("(ibWrongVersionBackendCompat?map=goodMapFile.map)"),
111 
112  BOOST_CHECK_THROW(BackendFactory::getInstance().createBackend("(unregisteredBackend)"), ChimeraTK::logic_error);
113 }
114 
115 BOOST_AUTO_TEST_CASE(testCreateFromUri) {
116  // this has to work without dmap file
117  BackendFactory::getInstance().setDMapFilePath("");
118 
119  boost::shared_ptr<DeviceBackend> testPtr;
120  testPtr = BackendFactory::getInstance().createBackend("(dummy?map=mtcadummy.map)"); // get some dummy
121  // just check that something has been created. That it's the correct thing is
122  // another test.
123  BOOST_CHECK(testPtr);
124 }
125 
126 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testCreateBackend)
Definition: testBackendFactory.cpp:38
ChimeraTK::BackendFactory::getInstance
static BackendFactory & getInstance()
Static function to get an instance of factory.
Definition: BackendFactory.cc:191
DummyBackend.h
NewBackend::createInstance
static boost::shared_ptr< DeviceBackend > createInstance(std::string instance, std::map< std::string, std::string > parameters)
Definition: testBackendFactory.cpp:28
NewBackend
Definition: testBackendFactory.cpp:25
BackendFactory.h
ChimeraTK::DummyBackend
The dummy device opens a mapping file instead of a device, and implements all registers defined in th...
Definition: DummyBackend.h:45
TEST_DMAP_FILE_PATH
#define TEST_DMAP_FILE_PATH
Definition: BackendFactory.h:18
Exception.h
ChimeraTK::setDMapFilePath
void setDMapFilePath(std::string dmapFilePath)
Set the location of the dmap file.
Definition: Utilities.cpp:327
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51