Selaa lähdekoodia

Adapt code to use default constructor of ctkServiceReference.

Sascha Zelzer 14 vuotta sitten
vanhempi
commit
43121850a7

+ 8 - 14
Libs/PluginFramework/Testing/FrameworkTest/ctkPluginFrameworkTestSuite.cpp

@@ -175,20 +175,20 @@ void ctkPluginFrameworkTestSuite::frame010a()
   qDebug() << "PCACTIVE:" << pstate;
 }
 
-//Test result of getServiceReference(). Should throw ctkServiceException
+//Test result of getService(ctkServiceReference()). Should throw std::invalid_argument
 void ctkPluginFrameworkTestSuite::frame018a()
 {
   try
   {
-    ctkServiceReference ref = pc->getServiceReference("illegalname");
-    qDebug() << "Got service reference =" << ref << ", excpected ctkServiceException";
-    QFAIL("Got service reference, excpected NullPointerException");
+    QObject* obj = pc->getService(ctkServiceReference());
+    qDebug() << "Got service object =" << obj->metaObject()->className() << ", excpected std::invalid_argument exception";
+    QFAIL("Got service object, excpected std::invalid_argument exception");
   }
-  catch (const ctkServiceException& )
+  catch (const std::invalid_argument& )
   {}
   catch (...)
   {
-       QFAIL("Got wrong exception, expected ctkServiceException");
+    QFAIL("Got wrong exception, expected std::invalid_argument");
   }
 }
 
@@ -214,15 +214,12 @@ void ctkPluginFrameworkTestSuite::frame020a()
   QCOMPARE(iter.value(), QString("pluginA.test"));
 
   // Check that no service reference exist yet.
-  try
+  ctkServiceReference sr1 = pc->getServiceReference("org.commontk.TestPluginAService");
+  if (sr1)
   {
-    pc->getServiceReference("org.commontk.TestPluginAService");
     QFAIL("framework test plugin, service from test plugin A unexpectedly found");
   }
-  catch (ctkServiceException& /*e*/)
-  {
 
-  }
 
   // check the listeners for events, expect only a plugin event,
   // of type installation
@@ -301,9 +298,6 @@ void ctkPluginFrameworkTestSuite::frame030b()
   {
     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)
   {

+ 5 - 0
Libs/PluginFramework/ctkPluginContext.cpp

@@ -143,6 +143,11 @@ QObject* ctkPluginContext::getService(ctkServiceReference reference)
 {
   Q_D(ctkPluginContext);
   d->isPluginContextValid();
+
+  if (!reference)
+  {
+    throw std::invalid_argument("Default constructed ctkServiceReference is not a valid input to getService()");
+  }
   return reference.d_func()->getService(d->plugin->q_func());
 }
 

+ 4 - 2
Libs/PluginFramework/ctkPluginContext.h

@@ -431,7 +431,8 @@ public:
    *         longer valid.
    * @throws std::invalid_argument If the specified
    *         <code>ctkServiceReference</code> was not created by the same
-   *         framework instance as this <code>ctkPluginContext</code>.
+   *         framework instance as this <code>ctkPluginContext</code> or
+   *         if it is invalid (default constructed).
    * @see #ungetService(const ctkServiceReference&)
    * @see ctkServiceFactory
    */
@@ -454,7 +455,8 @@ public:
    *         longer valid.
    * @throws std::invalid_argument If the specified
    *         <code>ctkServiceReference</code> was not created by the same
-   *         framework instance as this <code>ctkPluginContext</code>.
+   *         framework instance as this <code>ctkPluginContext</code> or
+   *         if it is invalid (default constructed).
    * @see #getService(ctkServiceReference)
    * @see #ungetService(const ctkServiceReference&)
    * @see ctkServiceFactory

+ 1 - 1
Libs/PluginFramework/ctkServices.cpp

@@ -171,7 +171,7 @@ ctkServiceReference ctkServices::get(ctkPluginPrivate* plugin, const QString& cl
   catch (const std::invalid_argument& )
   { }
 
-  throw ctkServiceException(QString("No service registered for: ") + clazz);
+  return ctkServiceReference();
 }
 
 

+ 0 - 2
Libs/PluginFramework/ctkServices_p.h

@@ -136,8 +136,6 @@ public:
    * @param plugin The plugin requesting reference
    * @param clazz The class name of the requested service.
    * @return A {@link ctkServiceReference} object.
-   * @throws ctkServiceException if no service was registered under the given
-   *         clazz name.
    */
   ctkServiceReference get(ctkPluginPrivate* plugin, const QString& clazz) const;