Forráskód Böngészése

Add test to ctkAbstractPluginFactory

A plugin (ctkDummyPlugin) is created to try the factory.
Julien Finet 14 éve
szülő
commit
de49e1dfd4

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

@@ -1,3 +1,17 @@
+# Dummy plugin
+ADD_DEFINITIONS( -DCTKDummyPlugin)
+ctkMacroBuildLib(
+  NAME "CTKDummyPlugin"
+  EXPORT_DIRECTIVE "CTK_DUMMY_EXPORT"
+  SRCS ctkDummyPlugin.h ctkDummyPlugin.cpp
+  MOC_SRCS "ctkDummyPlugin.h"
+  TARGET_LIBRARIES ${CTK_BASE_LIBRARIES}
+  LIBRARY_TYPE "SHARED"
+  )
+GET_TARGET_PROPERTY(ctkDummyPluginPATH "CTKDummyPlugin" LOCATION)
+REMOVE_DEFINITIONS(-DCTKDummyPlugin)
+
+#normal test
 SET(KIT ${PROJECT_NAME})
 
 CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
@@ -41,8 +55,9 @@ SET(Tests_MOC_SRCS
 SET(Tests_MOC_CPP)
 QT4_WRAP_CPP(Tests_MOC_CPP ${Tests_MOC_SRCS})
 
+# Test library
 ADD_EXECUTABLE(${KIT}CppTests ${Tests} ${Tests_SRCS} ${Tests_MOC_CPP})
-TARGET_LINK_LIBRARIES(${KIT}CppTests ${LIBRARY_NAME} ${CTK_BASE_LIBRARIES})
+TARGET_LINK_LIBRARIES(${KIT}CppTests ${LIBRARY_NAME} ${CTK_BASE_LIBRARIES} CTKDummyPlugin)
 
 SET( KIT_TESTS ${CPP_TEST_PATH}/${KIT}CppTests)
 
@@ -58,7 +73,9 @@ ENDMACRO( SIMPLE_TEST  )
 SIMPLE_TEST( ctkAbstractFactoryTest1 )
 SIMPLE_TEST( ctkAbstractLibraryFactoryTest1 )
 SIMPLE_TEST( ctkAbstractObjectFactoryTest1 )
-SIMPLE_TEST( ctkAbstractPluginFactoryTest1 )
+SIMPLE_TEST( ctkAbstractPluginFactoryTest1 "totto")
+ADD_TEST( ctkAbstractPluginFactoryTest1 ${KIT_TESTS} ctkAbstractPluginFactoryTest1 ${ctkDummyPluginPATH})
+SET_PROPERTY(TEST ctkAbstractPluginFactoryTest1 PROPERTY LABELS ${PROJECT_NAME})
 SIMPLE_TEST( ctkAbstractQObjectFactoryTest1 )
 SIMPLE_TEST( ctkCommandLineParserTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest1 )

+ 51 - 2
Libs/Core/Testing/Cpp/ctkAbstractPluginFactoryTest1.cpp

@@ -23,7 +23,7 @@
 
 // CTK includes
 #include "ctkAbstractPluginFactory.h"
-#include "ctkModelTester.h"
+#include "ctkDummyPlugin.h"
 
 // STD includes
 #include <cstdlib>
@@ -34,9 +34,58 @@ int ctkAbstractPluginFactoryTest1(int argc, char * argv [] )
 {
   QApplication app(argc, argv);
 
-  ctkAbstractPluginFactory< ctkModelTester > ctkObject;
+  ctkAbstractPluginFactory< ctkDummyPlugin > pluginFactory;
+  pluginFactory.setVerbose(true);
+  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;
+    }
+  
+  std::cerr<< "true path: " << file.absoluteFilePath().toStdString() << std::endl;
+  pluginFactory.registerLibrary("lib", file);
+  if (pluginFactory.keys().count() != 1)
+    {
+    std::cerr << "ctkAbstractPluginFactory::registerLibrary() failed"
+              << pluginFactory.keys().count() << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (QFileInfo(pluginFactory.path("lib")) != file)
+    {
+    std::cerr << "ctkAbstractPluginFactory::registerLibrary() failed"
+              << pluginFactory.path("lib").toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
 
+  ctkDummyPlugin* plugin = pluginFactory.instantiate("lib");
+  if (plugin == 0)
+    {
+    std::cerr << "ctkAbstractPluginFactory::instantiate() failed" << std::endl;
+    return EXIT_FAILURE;
+    }
 
+  pluginFactory.uninstantiate("lib");
+  
   return EXIT_SUCCESS;
 }
 

+ 36 - 0
Libs/Core/Testing/Cpp/ctkDummyPlugin.cpp

@@ -0,0 +1,36 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QtPlugin>
+
+// CTK includes
+#include "ctkDummyPlugin.h"
+
+Q_EXPORT_PLUGIN2( ctkDummyPlugin , ctkDummyPlugin)
+
+ctkDummyPlugin::ctkDummyPlugin(QObject* parent)
+  :QObject(parent)
+{
+}
+
+void ctkDummyPlugin::dummyInterface()
+{
+}

+ 41 - 0
Libs/Core/Testing/Cpp/ctkDummyPlugin.h

@@ -0,0 +1,41 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QObject>
+#include "ctkDummyPluginExport.h"
+/*
+class ctkDummyInterface{
+public:
+  virtual ~ctkDummyInterface(){}
+  virtual void dummyInterface() = 0;
+};
+
+Q_DECLARE_INTERFACE ( ctkDummyInterface, "dummy.plugin" )
+*/
+class CTK_DUMMY_EXPORT ctkDummyPlugin: public QObject//, public ctkDummyInterface
+{
+  Q_OBJECT
+//  Q_INTERFACES(ctkDummyInterface)
+public:
+  ctkDummyPlugin(QObject* parent = 0);
+  virtual void dummyInterface();
+};
+

+ 1 - 1
Libs/Core/ctkAbstractPluginFactory.tpp

@@ -80,7 +80,7 @@ BaseClassType* ctkFactoryPluginItem<BaseClassType>::instanciator()
     if (this->verbose())
       {
       qWarning() << "Failed to access interface [" << BaseClassType::staticMetaObject.className()
-               << "] in plugin:" << this->path();
+               << "] in plugin:" << this->path() << "\n instead, got object of type:" << object->metaObject()->className();
       }
     delete object; // Clean memory
     return 0;