// load a valid module, export a symbol, call it, and unload module void Test1() { DynamicLibrary library(getLibraryFileName(VALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(library.getFileName() == getLibraryFileName(VALID_LIBRARY_NAME)); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); Uint32 (* callme)(void) = (Uint32 (*)(void))library.getSymbol("callme"); PEGASUS_TEST_ASSERT(callme); PEGASUS_TEST_ASSERT(callme() == 0xdeadbeef); Uint32 (* badfunc)(void) = (Uint32 (*)(void))library.getSymbol("badfunc"); PEGASUS_TEST_ASSERT(badfunc == 0); library.unload(); PEGASUS_TEST_ASSERT(!library.isLoaded()); }
// load an invalid module void Test3() { DynamicLibrary library(getLibraryFileName(INVALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(!library.isLoaded()); Boolean loaded = library.load(); PEGASUS_TEST_ASSERT(!loaded); PEGASUS_TEST_ASSERT(!library.isLoaded()); String errorMessage = library.getLoadErrorMessage(); String fileName = library.getFileName(); if (verbose) { cout << "Invalid library name: " << fileName << endl; cout << "Invalid library load error: " << errorMessage << endl; } PEGASUS_TEST_ASSERT(fileName == getLibraryFileName(INVALID_LIBRARY_NAME)); }
// load valid module, assignment void Test2() { DynamicLibrary library(getLibraryFileName(VALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(!library.isLoaded()); { DynamicLibrary library2(library); PEGASUS_TEST_ASSERT(!library2.isLoaded()); } library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); { DynamicLibrary library2(library); PEGASUS_TEST_ASSERT(library2.isLoaded()); } { DynamicLibrary library2; library2 = library; PEGASUS_TEST_ASSERT(library2.isLoaded()); } { DynamicLibrary library2(getLibraryFileName(VALID_LIBRARY_NAME)); library2.load(); PEGASUS_TEST_ASSERT(library2.isLoaded()); library2 = library; PEGASUS_TEST_ASSERT(library2.isLoaded()); } library = library; PEGASUS_TEST_ASSERT(library.isLoaded()); library.unload(); PEGASUS_TEST_ASSERT(!library.isLoaded()); }
// Test reference counting void Test4() { DynamicLibrary library(getLibraryFileName(VALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(!library.isLoaded()); // Load the library Boolean loaded = library.load(); PEGASUS_TEST_ASSERT(loaded); PEGASUS_TEST_ASSERT(library.isLoaded()); // Load the library again loaded = library.load(); PEGASUS_TEST_ASSERT(loaded); PEGASUS_TEST_ASSERT(library.isLoaded()); // Unload the library library.unload(); PEGASUS_TEST_ASSERT(library.isLoaded()); // Unload the library again library.unload(); PEGASUS_TEST_ASSERT(!library.isLoaded()); // Load the library and then assign to a new instance library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); library = DynamicLibrary(getLibraryFileName(VALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(!library.isLoaded()); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); // Call the destructor while the library is loaded }
void PluginManager::loadAllPlugins(EnvisionManager& envisionManager) { int lastCountLoaded = -1; // Holds the ids of plugins which are not yet loaded. QStringList plugins; for (auto p_meta : pluginMetaData) plugins.append(p_meta.id); QTextStream out(stdout); while ( lastCountLoaded != loadedPlugins.length() && loadedPlugins.length() < pluginMetaData.length() ) { lastCountLoaded = loadedPlugins.length(); for (int i = plugins.length() - 1; i >= 0; i--) { QList<PluginDependency> depList = idToMetaDataMap.value(plugins.at(i))->dependencies; bool allDependenciesLoaded = true; for (QList<PluginDependency>::iterator dep = depList.begin(); dep != depList.end(); dep++) { // Check if this dependency is already loaded if ( idToPluginLoaderMap.contains(dep->id) == false ) { allDependenciesLoaded = false; break; } // Check if the version of the non-dependent plugin matches the version the current plugin depends on if ( !idToMetaDataMap.value(dep->id)->version.startsWith(dep->majorVersion + ".", Qt::CaseSensitive) ) throw EnvisionException("Plugin " + plugins.at(i) + " depends on version " + dep->majorVersion + " of " + dep->id + " but a different version is installed."); } if ( allDependenciesLoaded ) { out << "Loading plug-in " << plugins.at(i) << "... " << endl; QPluginLoader* plugin = new QPluginLoader(pluginsDir.absoluteFilePath(getLibraryFileName(plugins.at(i)))); plugin->setLoadHints(QLibrary::ExportExternalSymbolsHint); plugin->load(); if ( plugin->isLoaded() == false ) throw EnvisionException("Could not load plugin: " + plugin->errorString()); EnvisionPlugin* p = qobject_cast<EnvisionPlugin*> (plugin->instance()); p->initialize(envisionManager); out << plugins.at(i) << " loaded OK" << endl; loadedPlugins.append(plugin); idToPluginLoaderMap.insert(plugins.at(i), plugin); plugins.removeAt(i); } } } // Check if there are any plug-ins with unmet dependencies if (plugins.size() > 0) { out<< "Warning: The following plug-ins have not been loaded because their dependencies are not satisfied" << endl; for (int i = 0; i< plugins.size(); ++i) out << " " << plugins.at(i) << endl; } else out << "All plug-ins loaded." << endl; }