Browse Source

More PluginFramework tests.

Sascha Zelzer 14 years ago
parent
commit
2ee07bd8ff

+ 1 - 1
Libs/PluginFramework/Testing/Cpp/ctkPluginFrameworkTestUtil.h

@@ -42,7 +42,7 @@ public:
     if (pluginInfo.exists() && pluginInfo.isFile() &&
         QLibrary::isLibrary(pluginInfo.absoluteFilePath()))
     {
-      return pc->installPlugin(QUrl::fromLocalFile(pluginInfo.absoluteFilePath()).toString());
+      return pc->installPlugin(QUrl::fromLocalFile(pluginInfo.absoluteFilePath()));
     }
     else
     {

+ 223 - 0
Libs/PluginFramework/Testing/FrameworkTest/ctkPluginFrameworkTestSuite.cpp

@@ -143,6 +143,54 @@ void ctkPluginFrameworkTestSuite::frame005a()
   QCOMPARE(QString("http://www.apache.org/licenses/LICENSE-2.0.html"), headers.value(k));
 }
 
+// Extract all information from the getProperty in the ctkPluginContext class
+void ctkPluginFrameworkTestSuite::frame007a()
+{
+  QList<QString> NNList;
+  NNList << ctkPluginConstants::FRAMEWORK_VERSION
+         << ctkPluginConstants::FRAMEWORK_VENDOR;
+
+  foreach(QString k, NNList)
+  {
+    QVariant v = pc->getProperty(k);
+    if(!v.isValid())
+    {
+      QString msg("'%1' not set");
+      QFAIL(qPrintable(msg.arg(k)));
+    }
+  }
+}
+
+// Get context id, location and status of the plugin
+void ctkPluginFrameworkTestSuite::frame010a()
+{
+  qlonglong contextid = p->getPluginId();
+  qDebug() << "CONTEXT ID:" << contextid;
+
+  QString location = p->getLocation();
+  qDebug() << "LOCATION:" << location;
+
+  ctkPlugin::State pstate = p->getState();
+  qDebug() << "PCACTIVE:" << pstate;
+}
+
+//Test result of getServiceReference(). Should throw ctkServiceException
+void ctkPluginFrameworkTestSuite::frame018a()
+{
+  try
+  {
+    ctkServiceReference ref = pc->getServiceReference("illegalname");
+    qDebug() << "Got service reference =" << ref << ", excpected ctkServiceException";
+    QFAIL("Got service reference, excpected NullPointerException");
+  }
+  catch (const ctkServiceException& )
+  {}
+  catch (...)
+  {
+       QFAIL("Got wrong exception, expected ctkServiceException");
+  }
+}
+
 // Load pluginA_test and check that it exists and that its expected service does not exist,
 // also check that the expected events in the framework occur
 void ctkPluginFrameworkTestSuite::frame020a()
@@ -242,6 +290,161 @@ void ctkPluginFrameworkTestSuite::frame025b()
   }
 }
 
+// Stop pluginA_test and check that it gets state RESOLVED
+void ctkPluginFrameworkTestSuite::frame030b()
+{
+  ctkServiceReference sr1
+      = pc->getServiceReference("ctkTestPluginAService");
+
+  try
+  {
+    pA->stop();
+    QVERIFY2(pA->getState() == ctkPlugin::RESOLVED, "pluginA should be RESOLVED");
+
+    QVERIFY(sr1.getPlugin().isNull());
+    QVERIFY(pc->getService(sr1) == 0);
+  }
+  catch (const std::logic_error& ise)
+  {
+    qDebug() << "Unexpected logic_error exception:" << ise.what();
+    QFAIL("framework test plugin, stop plugin pluginA");
+  }
+  catch (const ctkPluginException& pe)
+  {
+    qDebug() << "Unexpected plugin exception:" << pe;
+    QFAIL("framework test plugin, stop plugin pluginA");
+  }
+
+  QList<ctkPluginEvent> pEvts;
+  pEvts << ctkPluginEvent(ctkPluginEvent::STOPPED, pA);
+
+  QList<ctkServiceEvent> seEvts;
+  seEvts << ctkServiceEvent(ctkServiceEvent::UNREGISTERING, sr1);
+
+  QVERIFY2(checkListenerEvents(QList<ctkPluginFrameworkEvent>(), pEvts, seEvts),
+           "Unexpected events");
+
+  QList<ctkPluginEvent> syncPEvts;
+  syncPEvts << ctkPluginEvent(ctkPluginEvent::STOPPING, pA);
+
+  QVERIFY2(checkSyncListenerEvents(syncPEvts), "Unexpected events");
+}
+
+// Uninstall pluginA_test and check that it gets state UNINSTALLED
+void ctkPluginFrameworkTestSuite::frame035b()
+{
+  try
+  {
+    pA->uninstall();
+    QVERIFY2(pA->getState() == ctkPlugin::UNINSTALLED,
+             "pluginA_test should be UNINSTALLED");
+  }
+  catch (const std::logic_error& ise)
+  {
+    qDebug() << "Unexpected logic_error exception:" << ise.what();
+    QFAIL("framework test plugin, uninstall");
+  }
+  catch (const ctkPluginException& pe)
+  {
+    qDebug() << "Unexpected plugin exception:" << pe;
+    QFAIL("framework test plugin, uninstall pluginA_test");
+  }
+
+
+  QList<ctkPluginEvent> pEvts;
+  pEvts << ctkPluginEvent(ctkPluginEvent::UNRESOLVED, pA);
+  pEvts << ctkPluginEvent(ctkPluginEvent::UNINSTALLED, pA);
+
+  QVERIFY2(checkListenerEvents(QList<ctkPluginFrameworkEvent>(),
+                               pEvts, QList<ctkServiceEvent>()),
+           "Unexpected events");
+
+  QVERIFY2(checkSyncListenerEvents(QList<ctkPluginEvent>()),
+           "Unexpected sync events");
+}
+
+// Install pluginD_test, check that a ctkPluginException is thrown
+// as this plugin is not a Qt plugin
+void ctkPluginFrameworkTestSuite::frame040a()
+{
+  bool teststatus = true;
+  bool exception = false;
+  try
+  {
+    pD = ctkPluginFrameworkTestUtil::installPlugin(pc, "pluginD_test");
+    exception = false;
+  }
+  catch (const ctkPluginException& pe)
+  {
+    // This exception is expected
+    qDebug() << "Expected exception" << pe;
+    exception = true;
+  }
+  //      catch (SecurityException secA) {
+  //        QFAIL("framework test plugin " + secA + " :FRAME040A:FAIL");
+  //        teststatus = false;
+  //        exception = true;
+  //      }
+
+  if (exception == false)
+  {
+    teststatus = false;
+  }
+
+  // check the listeners for events, expect no events
+  bool lStat = checkListenerEvents(false, ctkPluginFrameworkEvent::STARTED,
+                                   false , ctkPluginEvent::INSTALLED,
+                                   false, ctkServiceEvent::MODIFIED,
+                                   pD, 0);
+
+  QVERIFY(teststatus == true && pD.isNull() && lStat == true);
+}
+
+// Install a non-existent plug-in
+void ctkPluginFrameworkTestSuite::frame042a()
+{
+  bool exception = false;
+  try
+  {
+    pc->installPlugin(QUrl("file://no-plugin"));
+    exception = false;
+  }
+  catch (const ctkPluginException& pe)
+  {
+    // This exception is expected
+    qDebug() << "Expected exception" << pe;
+    exception = true;
+  }
+
+  // check the listeners for events, expect no events
+  bool lStat = checkListenerEvents(false, ctkPluginFrameworkEvent::STARTED,
+                                   false , ctkPluginEvent::INSTALLED,
+                                   false, ctkServiceEvent::MODIFIED,
+                                   QSharedPointer<ctkPlugin>(), 0);
+
+  QVERIFY(exception == true && lStat == true);
+}
+
+// Add a service listener with a broken LDAP filter to get an exception
+void ctkPluginFrameworkTestSuite::frame045a()
+{
+  ctkServiceListenerPFW sListen1;
+  QString brokenFilter = "A broken LDAP filter";
+
+  try
+  {
+    pc->connectServiceListener(&sListen1, "serviceChanged", brokenFilter);
+  }
+  catch (const std::invalid_argument& ia)
+  {
+    //assertEquals("InvalidSyntaxException.getFilter should be same as input string", brokenFilter, ise.getFilter());
+  }
+  catch (...)
+  {
+    QFAIL("framework test bundle, wrong exception on broken LDAP filter:");
+  }
+}
+
 void ctkPluginFrameworkTestSuite::frameworkListener(const ctkPluginFrameworkEvent& fwEvent)
 {
   frameworkEvents.push_back(fwEvent);
@@ -493,3 +696,23 @@ bool ctkPluginFrameworkTestSuite::checkSyncListenerEvents(
   syncPluginEvents.clear();
   return listenState;
 }
+
+ctkServiceEvent ctkServiceListenerPFW::getEvent() const
+{
+  return events.size() ? events.last() : ctkServiceEvent();
+}
+
+QList<ctkServiceEvent> ctkServiceListenerPFW::getEvents() const
+{
+  return events;
+}
+void ctkServiceListenerPFW::clearEvent()
+{
+  events.clear();
+}
+
+void ctkServiceListenerPFW::serviceChanged(const ctkServiceEvent& evt)
+{
+  events.push_back(evt);
+  qDebug() << "ctkServiceEvent:" << evt;
+}

+ 29 - 1
Libs/PluginFramework/Testing/FrameworkTest/ctkPluginFrameworkTestSuite_p.h

@@ -55,8 +55,16 @@ private slots:
 
   // test functions
   void frame005a();
+  void frame007a();
+  void frame010a();
+  void frame018a();
   void frame020a();
   void frame025b();
+  void frame030b();
+  void frame035b();
+  void frame040a();
+  void frame042a();
+  void frame045a();
 
 private:
 
@@ -100,10 +108,30 @@ private:
   int eventDelay;
 
   ctkPluginContext* pc;
-  QSharedPointer<ctkPlugin> p;
 
+  QSharedPointer<ctkPlugin> p;
   QSharedPointer<ctkPlugin> pA;
+  QSharedPointer<ctkPlugin> pD;
+
+};
+
+class ctkServiceListenerPFW : public QObject
+{
+  Q_OBJECT
+
+public:
+
+  ctkServiceEvent getEvent() const;
+  QList<ctkServiceEvent> getEvents() const;
+  void clearEvent();
+
+public slots:
+
+  void serviceChanged(const ctkServiceEvent& evt);
+
+private:
 
+  QList<ctkServiceEvent> events;
 };
 
 #endif // CTKPLUGINFRAMEWORKTESTSUITE_P_H

+ 35 - 0
Libs/PluginFramework/Testing/TestPlugins/pluginD_test/CMakeLists.txt

@@ -0,0 +1,35 @@
+PROJECT(pluginD_test)
+
+SET(PLUGIN_export_directive "pluginD_test_EXPORT")
+
+SET(PLUGIN_SRCS
+  ctkTestPluginD.cpp
+)
+
+SET(lib_name ${PROJECT_NAME})
+
+INCLUDE(${QT_USE_FILE})
+
+ADD_LIBRARY(${lib_name} SHARED ${PLUGIN_SRCS})
+TARGET_LINK_LIBRARIES(${lib_name} ${QT_LIBRARIES})
+
+# Set the output directory for the plugin
+SET(output_dir_suffix "test_plugins")
+IF(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
+  SET(runtime_output_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${output_dir_suffix}")
+ELSE()
+  SET(runtime_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
+ENDIF()
+IF(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
+  SET(library_output_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${output_dir_suffix}")
+ELSE()
+  SET(library_output_dir "${CMAKE_CURRENT_BINARY_DIR}/${output_dir_suffix}")
+ENDIF()
+
+# Apply properties to the library target.
+SET_TARGET_PROPERTIES(${lib_name} PROPERTIES
+#    COMPILE_FLAGS "-DQT_PLUGIN"
+    RUNTIME_OUTPUT_DIRECTORY ${runtime_output_dir}
+    LIBRARY_OUTPUT_DIRECTORY ${library_output_dir}
+    PREFIX "lib"
+    )

+ 30 - 0
Libs/PluginFramework/Testing/TestPlugins/pluginD_test/ctkTestPluginD.cpp

@@ -0,0 +1,30 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  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.apache.org/licenses/LICENSE-2.0
+
+  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.
+
+=============================================================================*/
+
+#include <QDebug>
+
+class ctkTestPluginD 
+{
+  ctkTestPluginD()
+  {
+    qDebug() << "Not a CTK Plugin, just a normal shared library";
+  }
+};

+ 9 - 0
Libs/PluginFramework/Testing/TestPlugins/pluginD_test/target_libraries.cmake

@@ -0,0 +1,9 @@
+#
+# See CMake/ctkMacroGetTargetLibraries.cmake
+# 
+# This file should list the libraries required to build the current CTK plugin.
+# 
+
+SET(target_libraries
+  
+  )