Browse Source

Fix test ctkAbstractLibraryFactoryTest1

Julien Finet 14 years ago
parent
commit
1ad8ca4506

+ 2 - 1
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -96,7 +96,8 @@ ENDMACRO( SIMPLE_TEST  )
 #
 
 SIMPLE_TEST( ctkAbstractFactoryTest1 )
-SIMPLE_TEST( ctkAbstractLibraryFactoryTest1 )
+ADD_TEST( ctkAbstractLibraryFactoryTest1 ${KIT_TESTS} ctkAbstractLibraryFactoryTest1 ${ctkDummyPluginPATH})
+SET_PROPERTY(TEST ctkAbstractLibraryFactoryTest1 PROPERTY LABELS ${PROJECT_NAME})
 SIMPLE_TEST( ctkAbstractObjectFactoryTest1 )
 ADD_TEST( ctkAbstractPluginFactoryTest1 ${KIT_TESTS} ctkAbstractPluginFactoryTest1 ${ctkDummyPluginPATH})
 SET_PROPERTY(TEST ctkAbstractPluginFactoryTest1 PROPERTY LABELS ${PROJECT_NAME})

+ 88 - 29
Libs/Core/Testing/Cpp/ctkAbstractLibraryFactoryTest1.cpp

@@ -20,50 +20,46 @@
 
 // Qt includes
 #include <QApplication>
+#include <QPushButton>
 
 // CTK includes
 #include "ctkAbstractLibraryFactory.h"
+#include "ctkDummyPlugin.h"
 
 // STD includes
 #include <cstdlib>
 #include <iostream>
 
-int ObjectConstructed = 0; 
-
-//-----------------------------------------------------------------------------
-class Object
+class ctkDummyLibrary
 {
-public:
-  Object()
-  {
-    ++ObjectConstructed;
-  }
-  ~Object()
-  {
-    --ObjectConstructed;
-  }
 };
 
-//-----------------------------------------------------------------------------
-class ObjectFactoryItem : public ctkFactoryLibraryItem<Object>
+class ctkDummyLibraryItem: public ctkFactoryLibraryItem<ctkDummyLibrary>
 {
 protected:
-  virtual Object* instanciator(){return new Object;}
+  virtual ctkDummyLibrary* instanciator()
+  {
+    // Using a scoped pointer ensures the memory will be cleaned if instanciator
+    // fails before returning the module. See QScopedPointer::take()
+    QScopedPointer<ctkDummyLibrary> module(new ctkDummyLibrary());
+    foreach(QString symbol, this->Symbols)
+      {
+      void* res = this->symbolAddress(symbol);
+      if (!res)
+        {
+        }
+      }
+    return module.take();
+  }
 };
 
-//-----------------------------------------------------------------------------
-class ObjectFactory : public ctkAbstractLibraryFactory<Object>
+class ctkDummyLibraryFactoryItem: public ctkAbstractLibraryFactory<ctkDummyLibrary>
 {
-public:
-  virtual void registerItems()
-  {
-    qDebug() << "Registering items";
-  }
-  
 protected:
-  virtual ctkAbstractFactoryItem<Object>* createFactoryFileBasedItem()
+  //-----------------------------------------------------------------------------
+  ctkAbstractFactoryItem<ctkDummyLibrary>* createFactoryFileBasedItem()
   {
-    return new ObjectFactoryItem();
+    return new ctkDummyLibraryItem();
   }
 };
 
@@ -72,9 +68,72 @@ int ctkAbstractLibraryFactoryTest1(int argc, char * argv [])
 {
   QApplication app(argc, argv);
 
-  ObjectFactory factory;
-  factory.registerItems();
-
+  if (argc <= 1)
+    {
+    std::cerr << "Missing argument" << std::endl;
+    return EXIT_FAILURE;
+    }
+  QString filePath(argv[1]);
+  QFileInfo file(filePath);
+  while (filePath.contains("$(OutDir)"))
+    {
+    QString debugFilePath = filePath;
+    debugFilePath.replace("$(OutDir)","Debug");
+    if (QFile::exists(QString(debugFilePath)))
+      {
+      file = QFileInfo(debugFilePath);
+      break;
+      }
+    QString releaseFilePath = filePath;
+    releaseFilePath.replace("$(OutDir)","Release");
+    if (QFile::exists(QString(releaseFilePath)))
+      {
+      file = QFileInfo(releaseFilePath);
+      break;
+      }
+    return EXIT_FAILURE;
+    }
+  ctkDummyLibraryFactoryItem libraryFactory;
+  libraryFactory.setVerbose(true);
+
+  bool res = libraryFactory.registerFileItem("fail", QFileInfo("foo/bar.txt"));
+  if (res)
+    {
+    std::cerr << "ctkAbstractLibraryFactory::registerLibrary() registered bad file"
+              << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  res = libraryFactory.registerFileItem("lib", file);
+  if (!res || libraryFactory.keys().count() != 1)
+    {
+    std::cerr << "ctkAbstractLibraryFactory::registerLibrary() failed"
+              << libraryFactory.keys().count() << std::endl;
+    return EXIT_FAILURE;
+    }
+  // register twice must return false
+  res = libraryFactory.registerFileItem("lib", file);
+  if (res || libraryFactory.keys().count() != 1)
+    {
+    std::cerr << "ctkAbstractLibraryFactory::registerLibrary() failed"
+              << libraryFactory.keys().count() << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (QFileInfo(libraryFactory.path("lib")) != file)
+    {
+    std::cerr << "ctkAbstractLibraryFactory::registerLibrary() failed"
+              << libraryFactory.path("lib").toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  ctkDummyLibrary* library = libraryFactory.instantiate("lib");
+  if (library == 0)
+    {
+    std::cerr << "ctkAbstractLibraryFactory::instantiate() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  libraryFactory.uninstantiate("lib");
   return EXIT_SUCCESS;
 }
 

+ 1 - 1
Libs/Core/ctkAbstractLibraryFactory.h

@@ -58,7 +58,7 @@ public:
   /// Get symbol address
   void* symbolAddress(const QString& symbol)const;
 
-private:
+protected:
   QLibrary              Library;
   QHash<QString, void*> ResolvedSymbols;
   QStringList           Symbols;