int usModuleResourceTest(int /*argc*/, char* /*argv*/[]) { US_TEST_BEGIN("ModuleResourceTest"); ModuleContext* mc = GetModuleContext(); assert(mc); #ifdef US_BUILD_SHARED_LIBS SharedLibrary libR(LIB_PATH, "TestModuleR"); try { libR.Load(); } catch (const std::exception& e) { US_TEST_FAILED_MSG(<< "Load module exception: " << e.what()) } #endif Module* moduleR = ModuleRegistry::GetModule("TestModuleR"); US_TEST_CONDITION_REQUIRED(moduleR != nullptr, "Test for existing module TestModuleR") US_TEST_CONDITION(moduleR->GetName() == "TestModuleR", "Test module name") testInvalidResource(moduleR); testResourceFromExecutable(mc->GetModule()); testResourceTree(moduleR); testResourceOperators(moduleR); testTextResource(moduleR); testTextResourceAsBinary(moduleR); testSpecialCharacters(moduleR); testBinaryResource(moduleR); testCompressedResource(moduleR); ModuleResource foo = moduleR->GetResource("foo.txt"); US_TEST_CONDITION(foo.IsValid() == true, "Valid resource") #ifdef US_BUILD_SHARED_LIBS libR.Unload(); US_TEST_CONDITION(foo.IsValid() == true, "Still valid resource") #endif testResourcesFrom("TestModuleRL"); testResourcesFrom("TestModuleRA"); US_TEST_END() }
US_END_NAMESPACE void TestServiceFactoryModuleScope() { // Install and start test module H, a service factory and test that the methods // in that interface works. SharedLibrary target(LIB_PATH, "TestModuleH"); #ifdef US_BUILD_SHARED_LIBS try { target.Load(); } catch (const std::exception& e) { US_TEST_FAILED_MSG( << "Failed to load module, got exception: " << e.what()) } #endif Module* moduleH = ModuleRegistry::GetModule("TestModuleH"); US_TEST_CONDITION_REQUIRED(moduleH != 0, "Test for existing module TestModuleH") std::vector<ServiceReferenceU> registeredRefs = moduleH->GetRegisteredServices(); US_TEST_CONDITION_REQUIRED(registeredRefs.size() == 2, "# of registered services") US_TEST_CONDITION(registeredRefs[0].GetProperty(ServiceConstants::SERVICE_SCOPE()).ToString() == ServiceConstants::SCOPE_MODULE(), "service scope") US_TEST_CONDITION(registeredRefs[1].GetProperty(ServiceConstants::SERVICE_SCOPE()).ToString() == ServiceConstants::SCOPE_PROTOTYPE(), "service scope") ModuleContext* mc = GetModuleContext(); // Check that a service reference exist const ServiceReferenceU sr1 = mc->GetServiceReference("us::TestModuleH"); US_TEST_CONDITION_REQUIRED(sr1, "Service shall be present.") US_TEST_CONDITION(sr1.GetProperty(ServiceConstants::SERVICE_SCOPE()).ToString() == ServiceConstants::SCOPE_MODULE(), "service scope") InterfaceMap service = mc->GetService(sr1); US_TEST_CONDITION_REQUIRED(service.size() >= 1, "GetService()") InterfaceMap::const_iterator serviceIter = service.find("us::TestModuleH"); US_TEST_CONDITION_REQUIRED(serviceIter != service.end(), "GetService()") US_TEST_CONDITION_REQUIRED(serviceIter->second != nullptr, "GetService()") InterfaceMap service2 = mc->GetService(sr1); US_TEST_CONDITION(service == service2, "Same service pointer") std::vector<ServiceReferenceU> usedRefs = mc->GetModule()->GetServicesInUse(); US_TEST_CONDITION_REQUIRED(usedRefs.size() == 1, "services in use") US_TEST_CONDITION(usedRefs[0] == sr1, "service ref in use") InterfaceMap service3 = moduleH->GetModuleContext()->GetService(sr1); US_TEST_CONDITION(service != service3, "Different service pointer") US_TEST_CONDITION(moduleH->GetModuleContext()->UngetService(sr1), "UngetService()") US_TEST_CONDITION_REQUIRED(mc->UngetService(sr1) == false, "ungetService()") US_TEST_CONDITION_REQUIRED(mc->UngetService(sr1) == true, "ungetService()") target.Unload(); }
US_USE_NAMESPACE int usSharedLibraryTest(int /*argc*/, char* /*argv*/[]) { US_TEST_BEGIN("SharedLibraryTest"); #ifdef US_PLATFORM_WINDOWS const std::string LIB_PATH = US_RUNTIME_OUTPUT_DIRECTORY; const std::string LIB_PREFIX = ""; const std::string LIB_SUFFIX = ".dll"; const char PATH_SEPARATOR = '\\'; #elif defined(US_PLATFORM_APPLE) const std::string LIB_PATH = US_LIBRARY_OUTPUT_DIRECTORY; const std::string LIB_PREFIX = "lib"; const std::string LIB_SUFFIX = ".dylib"; const char PATH_SEPARATOR = '/'; #else const std::string LIB_PATH = US_LIBRARY_OUTPUT_DIRECTORY; const std::string LIB_PREFIX = "lib"; const std::string LIB_SUFFIX = ".so"; const char PATH_SEPARATOR = '/'; #endif const std::string libAFilePath = LIB_PATH + PATH_SEPARATOR + LIB_PREFIX + "TestModuleA" + LIB_SUFFIX; SharedLibrary lib1(libAFilePath); US_TEST_CONDITION(lib1.GetFilePath() == libAFilePath, "Absolute file path") US_TEST_CONDITION(lib1.GetLibraryPath() == LIB_PATH, "Library path") US_TEST_CONDITION(lib1.GetName() == "TestModuleA", "Name") US_TEST_CONDITION(lib1.GetPrefix() == LIB_PREFIX, "Prefix") US_TEST_CONDITION(lib1.GetSuffix() == LIB_SUFFIX, "Suffix") lib1.SetName("bla"); US_TEST_CONDITION(lib1.GetName() == "TestModuleA", "Name after SetName()") lib1.SetLibraryPath("bla"); US_TEST_CONDITION(lib1.GetLibraryPath() == LIB_PATH, "Library path after SetLibraryPath()") lib1.SetPrefix("bla"); US_TEST_CONDITION(lib1.GetPrefix() == LIB_PREFIX, "Prefix after SetPrefix()") lib1.SetSuffix("bla"); US_TEST_CONDITION(lib1.GetSuffix() == LIB_SUFFIX, "Suffix after SetSuffix()") US_TEST_CONDITION(lib1.GetFilePath() == libAFilePath, "File path after setters") lib1.SetFilePath("bla"); US_TEST_CONDITION(lib1.GetFilePath() == "bla", "Invalid file path") US_TEST_CONDITION(lib1.GetLibraryPath().empty(), "Empty lib path") US_TEST_CONDITION(lib1.GetName() == "bla", "Invalid file name") US_TEST_CONDITION(lib1.GetPrefix() == LIB_PREFIX, "Invalid prefix") US_TEST_CONDITION(lib1.GetSuffix() == LIB_SUFFIX, "Invalid suffix") US_TEST_FOR_EXCEPTION(std::runtime_error, lib1.Load()) US_TEST_CONDITION(lib1.IsLoaded() == false, "Is loaded") US_TEST_CONDITION(lib1.GetHandle() == NULL, "Handle") lib1.SetFilePath(libAFilePath); lib1.Load(); US_TEST_CONDITION(lib1.IsLoaded() == true, "Is loaded") US_TEST_CONDITION(lib1.GetHandle() != NULL, "Handle") US_TEST_FOR_EXCEPTION(std::logic_error, lib1.Load()) lib1.SetFilePath("bla"); US_TEST_CONDITION(lib1.GetFilePath() == libAFilePath, "File path") lib1.Unload(); SharedLibrary lib2(LIB_PATH, "TestModuleA"); US_TEST_CONDITION(lib2.GetFilePath() == libAFilePath, "File path") lib2.SetPrefix(""); US_TEST_CONDITION(lib2.GetPrefix().empty(), "Lib prefix") US_TEST_CONDITION(lib2.GetFilePath() == LIB_PATH + PATH_SEPARATOR + "TestModuleA" + LIB_SUFFIX, "File path") SharedLibrary lib3 = lib2; US_TEST_CONDITION(lib3.GetFilePath() == lib2.GetFilePath(), "Compare file path") lib3.SetPrefix(LIB_PREFIX); US_TEST_CONDITION(lib3.GetFilePath() == libAFilePath, "Compare file path") lib3.Load(); US_TEST_CONDITION(lib3.IsLoaded(), "lib3 loaded") US_TEST_CONDITION(!lib2.IsLoaded(), "lib2 not loaded") lib1 = lib3; US_TEST_FOR_EXCEPTION(std::logic_error, lib1.Load()) lib2.SetPrefix(LIB_PREFIX); lib2.Load(); lib3.Unload(); US_TEST_CONDITION(!lib3.IsLoaded(), "lib3 unloaded") US_TEST_CONDITION(!lib1.IsLoaded(), "lib3 unloaded") lib2.Unload(); lib1.Unload(); US_TEST_END() }
#include <usGetModuleContext.h> #include <usModuleContext.h> #include <stdexcept> US_USE_NAMESPACE struct ITestServiceA { virtual ~ITestServiceA() {} }; void TestServiceInterfaceId() { US_TEST_CONDITION(us_service_interface_iid<int>() == "int", "Service interface id int") US_TEST_CONDITION(us_service_interface_iid<ITestServiceA>() == "ITestServiceA", "Service interface id ITestServiceA") } void TestMultipleServiceRegistrations() { struct TestServiceA : public ITestServiceA { }; ModuleContext* context = GetModuleContext(); TestServiceA s1; TestServiceA s2; ServiceRegistration<ITestServiceA> reg1 = context->RegisterService<ITestServiceA>(&s1);
void TestServiceFactoryPrototypeScope() { // Install and start test module H, a service factory and test that the methods // in that interface works. SharedLibrary target(LIB_PATH, "TestModuleH"); #ifdef US_BUILD_SHARED_LIBS try { target.Load(); } catch (const std::exception& e) { US_TEST_FAILED_MSG( << "Failed to load module, got exception: " << e.what()) } Module* moduleH = ModuleRegistry::GetModule("TestModuleH"); US_TEST_CONDITION_REQUIRED(moduleH != 0, "Test for existing module TestModuleH") #endif ModuleContext* mc = GetModuleContext(); // Check that a service reference exist const ServiceReference<TestModuleH2> sr1 = mc->GetServiceReference<TestModuleH2>(); US_TEST_CONDITION_REQUIRED(sr1, "Service shall be present.") US_TEST_CONDITION(sr1.GetProperty(ServiceConstants::SERVICE_SCOPE()).ToString() == ServiceConstants::SCOPE_PROTOTYPE(), "service scope") ServiceObjects<TestModuleH2> svcObjects = mc->GetServiceObjects(sr1); TestModuleH2* prototypeServiceH2 = svcObjects.GetService(); const ServiceReferenceU sr1void = mc->GetServiceReference(us_service_interface_iid<TestModuleH2>()); ServiceObjects<void> svcObjectsVoid = mc->GetServiceObjects(sr1void); InterfaceMap prototypeServiceH2Void = svcObjectsVoid.GetService(); US_TEST_CONDITION_REQUIRED(prototypeServiceH2Void.find(us_service_interface_iid<TestModuleH2>()) != prototypeServiceH2Void.end(), "ServiceObjects<void>::GetService()") #ifdef US_BUILD_SHARED_LIBS // There should be only one service in use US_TEST_CONDITION_REQUIRED(mc->GetModule()->GetServicesInUse().size() == 1, "services in use") #endif TestModuleH2* moduleScopeService = mc->GetService(sr1); US_TEST_CONDITION_REQUIRED(moduleScopeService && moduleScopeService != prototypeServiceH2, "GetService()") US_TEST_CONDITION_REQUIRED(prototypeServiceH2 != prototypeServiceH2Void.find(us_service_interface_iid<TestModuleH2>())->second, "GetService()") svcObjectsVoid.UngetService(prototypeServiceH2Void); TestModuleH2* moduleScopeService2 = mc->GetService(sr1); US_TEST_CONDITION(moduleScopeService == moduleScopeService2, "Same service pointer") #ifdef US_BUILD_SHARED_LIBS std::vector<ServiceReferenceU> usedRefs = mc->GetModule()->GetServicesInUse(); US_TEST_CONDITION_REQUIRED(usedRefs.size() == 1, "services in use") US_TEST_CONDITION(usedRefs[0] == sr1, "service ref in use") #endif std::string filter = "(" + ServiceConstants::SERVICE_ID() + "=" + sr1.GetProperty(ServiceConstants::SERVICE_ID()).ToString() + ")"; const ServiceReference<TestModuleH> sr2 = mc->GetServiceReferences<TestModuleH>(filter).front(); US_TEST_CONDITION_REQUIRED(sr2, "Service shall be present.") US_TEST_CONDITION(sr2.GetProperty(ServiceConstants::SERVICE_SCOPE()).ToString() == ServiceConstants::SCOPE_PROTOTYPE(), "service scope") US_TEST_CONDITION(any_cast<long>(sr2.GetProperty(ServiceConstants::SERVICE_ID())) == any_cast<long>(sr1.GetProperty(ServiceConstants::SERVICE_ID())), "same service id") try { svcObjects.UngetService(moduleScopeService2); US_TEST_FAILED_MSG(<< "std::invalid_argument exception expected") } catch (const std::invalid_argument&) { // this is expected } #ifdef US_BUILD_SHARED_LIBS // There should still be only one service in use usedRefs = mc->GetModule()->GetServicesInUse(); US_TEST_CONDITION_REQUIRED(usedRefs.size() == 1, "services in use") #endif ServiceObjects<TestModuleH2> svcObjects2 = svcObjects; ServiceObjects<TestModuleH2> svcObjects3 = mc->GetServiceObjects(sr1); try { svcObjects3.UngetService(prototypeServiceH2); US_TEST_FAILED_MSG(<< "std::invalid_argument exception expected") } catch (const std::invalid_argument&) { // this is expected } svcObjects2.UngetService(prototypeServiceH2); prototypeServiceH2 = svcObjects2.GetService(); TestModuleH2* prototypeServiceH2_2 = svcObjects3.GetService(); US_TEST_CONDITION_REQUIRED(prototypeServiceH2_2 && prototypeServiceH2_2 != prototypeServiceH2, "prototype service") svcObjects2.UngetService(prototypeServiceH2); svcObjects3.UngetService(prototypeServiceH2_2); US_TEST_CONDITION_REQUIRED(mc->UngetService(sr1) == false, "ungetService()") US_TEST_CONDITION_REQUIRED(mc->UngetService(sr1) == true, "ungetService()") target.Unload(); }
public: MyCustomizer(us::ModuleContext* context) : m_context(context) {} virtual MyInterfaceOne* AddingService(const ServiceReferenceT& reference) { US_TEST_CONDITION_REQUIRED(reference, "AddingService() valid reference") return m_context->GetService(reference); } virtual void ModifiedService(const ServiceReferenceT& reference, MyInterfaceOne* service) { US_TEST_CONDITION(reference, "ModifiedService() valid reference") US_TEST_CONDITION(service, "ModifiedService() valid service") } virtual void RemovedService(const ServiceReferenceT& reference, MyInterfaceOne* service) { US_TEST_CONDITION(reference, "RemovedService() valid reference") US_TEST_CONDITION(service, "RemovedService() valid service") } private: us::ModuleContext* m_context; }; void TestFilterString()