Information
We are currently investigating an issue with the editor of some pages. Please save your work and avoid to create new pages until this banner is gone.
...
The C++ prototype extends the ContainerServices class. The important methods to consider here, are the activateComponent/deactivateComponent which are to be used by the setUp and cleanUp methods in Unit testing.
Code Block | ||||
---|---|---|---|---|
| ||||
class MockContainerServices : public maci::ContainerServices { public: MockContainerServices(ACE_CString& componentName, PortableServer::POA_ptr poa); ~MockContainerServices(); virtual CORBA::Object* getCORBAComponent(const char* name); //... template<class TAdvancedObject> void activateComponent(const char* name) { TAdvancedObject* obj = new TAdvancedObject(name, this); obj->initialize(); obj->execute(); this->comps[name] = obj }; virtual deactivateComponent(const char* name) { acscomponent::ACSComponentImpl* obj = dynamic_cast<acscomponent::ACSComponentImpl*>(this->comps[name]); try { obj->cleanUp(); } catch (...) { obj->aboutToAbort(); } this->comps.erase(name); } protected: std::map<std::string, CORBA::Object_var> comps; }; |
The getCORBAComponent method was also extended in order to return the component from 'comps' the std::map:
Code Block | ||||
---|---|---|---|---|
| ||||
CORBA::Object* MockContainerServices::getCORBAComponent(const char* name){ if (comps.find(name) == comps.end()) { maciErrType::CannotGetComponentExImpl ex(__FILE__, __LINE__, "MockContainerServices::getComponent"); ex.setCURL(name); throw ex; } return CORBA::Object::_duplicate(comps[name].in()); } |
Later on, in the test implementation we would do something similar to:
Code Block | ||||
---|---|---|---|---|
| ||||
MockContainerServices* mcs = nullptr; setUp() { mcs = new MockContainerServices(cn, nullptr); mcs->activateComponent<MaciTestComponentImpl>("TEST_COMP1"); mcs->activateComponent<MaciTestComponentImpl>("TEST_COMP2"); } cleanUp() { mcs.deactivateComponent("TEST_COMP1"); mcs.deactivateComponent("TEST_COMP2"); delete mcs; mcs = nullptr; } //The tests should not know anything about the changes we've done, but they rely on this mcs instance just for convenience test_example() { MACI_TEST::MaciTestComponent_var comp = mcs->getComponent<MACI_TEST::MaciTestComponent>("TEST_COMP1"); comp->some_method(...); //If some_method retrieves a component, it should also work normally, since on creation, we passed an instance of MockContainerServices } |
...