diff --git a/include/class_loader/multi_library_class_loader.h b/include/class_loader/multi_library_class_loader.h index b7a5fe7b..17aa27e1 100644 --- a/include/class_loader/multi_library_class_loader.h +++ b/include/class_loader/multi_library_class_loader.h @@ -73,6 +73,8 @@ class MultiLibraryClassLoader for(unsigned int c = 0; c < active_loaders.size(); c++) { ClassLoader* current = active_loaders.at(c); + if (!current->isLibraryLoaded()) + current->loadLibrary(); if(current->isClassAvailable(class_name)) return(current->createInstance(class_name)); } @@ -113,6 +115,8 @@ class MultiLibraryClassLoader for(unsigned int c = 0; c < active_loaders.size(); c++) { ClassLoader* current = active_loaders.at(c); + if (!current->isLibraryLoaded()) + current->loadLibrary(); if(current->isClassAvailable(class_name)) return(current->createUnmanagedInstance(class_name)); } diff --git a/test/utest.cpp b/test/utest.cpp index b24b172a..a8ec03f6 100644 --- a/test/utest.cpp +++ b/test/utest.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "base.h" #include @@ -297,8 +298,69 @@ TEST(ClassLoaderTest, loadRefCountingLazy) FAIL() << "Did not throw exception as expected.\n"; } + /*****************************************************************************/ +void testMultiClassLoader(bool lazy) +{ + try + { + class_loader::MultiLibraryClassLoader loader(lazy); + loader.loadLibrary(LIBRARY_1); + loader.loadLibrary(LIBRARY_2); + for (int i=0; i < 2; ++i) { + loader.createInstance("Cat")->saySomething(); + loader.createInstance("Dog")->saySomething(); + loader.createInstance("Robot")->saySomething(); + } + } + catch(class_loader::ClassLoaderException& e) + { + FAIL() << "ClassLoaderException: " << e.what() << "\n"; + } + + SUCCEED(); +} + +TEST(MultiClassLoaderTest, lazyLoad) +{ + testMultiClassLoader(true); +} +TEST(MultiClassLoaderTest, lazyLoadSecondTime) +{ + testMultiClassLoader(true); +} +TEST(MultiClassLoaderTest, nonLazyLoad) +{ + testMultiClassLoader(false); +} +TEST(MultiClassLoaderTest, noWarningOnLazyLoad) +{ + try + { + boost::shared_ptr cat, dog, rob; + { + class_loader::MultiLibraryClassLoader loader(true); + loader.loadLibrary(LIBRARY_1); + loader.loadLibrary(LIBRARY_2); + + cat = loader.createInstance("Cat"); + dog = loader.createInstance("Dog"); + rob = loader.createInstance("Robot"); + } + cat->saySomething(); + dog->saySomething(); + rob->saySomething(); + } + catch(class_loader::ClassLoaderException& e) + { + FAIL() << "ClassLoaderException: " << e.what() << "\n"; + } + + SUCCEED(); +} + +/*****************************************************************************/ // Run all the tests that were declared with TEST() int main(int argc, char **argv){