ctkServiceTrackerTestSuite.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkServiceTrackerTestSuite_p.h"
  16. #include <ctkPlugin.h>
  17. #include <ctkPluginContext.h>
  18. #include <ctkPluginException.h>
  19. #include <ctkServiceTracker.h>
  20. #include <ctkPluginFrameworkTestUtil.h>
  21. #include <QTest>
  22. //----------------------------------------------------------------------------
  23. ctkServiceTrackerTestSuite::ctkServiceTrackerTestSuite(ctkPluginContext* pc)
  24. : pc(pc), p(pc->getPlugin())
  25. {
  26. }
  27. //----------------------------------------------------------------------------
  28. void ctkServiceTrackerTestSuite::initTestCase()
  29. {
  30. // load and start pluginS_test, that may be prodded to
  31. // register/unregister some services. At start it registers one
  32. // service, org.commontk.pluginStest.TestPluginSService0
  33. pS = ctkPluginFrameworkTestUtil::installPlugin(pc, "pluginS_test");
  34. QVERIFY(pS);
  35. }
  36. //----------------------------------------------------------------------------
  37. void ctkServiceTrackerTestSuite::cleanupTestCase()
  38. {
  39. pS->uninstall();
  40. }
  41. //----------------------------------------------------------------------------
  42. void ctkServiceTrackerTestSuite::runTest()
  43. {
  44. try
  45. {
  46. pS->start();
  47. qDebug() << "started service plugin";
  48. }
  49. catch (const ctkPluginException& pexcS)
  50. {
  51. QString msg = QString("Test plugin: ") + pexcS.what();
  52. QFAIL(msg.toAscii());
  53. }
  54. // 1. Create a ctkServiceTracker with ctkServiceTrackerCustomizer == null
  55. QString s1("org.commontk.pluginStest.TestPluginSService");
  56. ctkServiceReference servref = pc->getServiceReference(s1 + "0");
  57. QVERIFY2(servref != 0, "Test if registered service of id org.commontk.pluginStest.TestPluginSService0");
  58. connect(this, SIGNAL(serviceControl(int,QString,long)),
  59. pc->getService(servref), SLOT(controlService(int,QString,long)));
  60. ctkServiceTracker<>* st1 = new ctkServiceTracker<>(pc, servref);
  61. // 2. Check the size method with an unopened service tracker
  62. QVERIFY2(st1->size() == 0, "Test if size == 0");
  63. // 3. Open the service tracker and see what it finds,
  64. // expect to find one instance of the implementation,
  65. // "org.commontk.pluginStest.TestPluginSService0"
  66. st1->open();
  67. QString expName = "ctkTestPluginS";
  68. QList<ctkServiceReference> sa2 = st1->getServiceReferences();
  69. QVERIFY(sa2.size() == 1);
  70. QString name = pc->getService(sa2[0])->metaObject()->className();
  71. QVERIFY(name == expName);
  72. // 5. Close this service tracker
  73. st1->close();
  74. // 6. Check the size method, now when the servicetracker is closed
  75. QVERIFY(st1->size() == 0);
  76. // 7. Check if we still track anything , we should get null
  77. sa2 = st1->getServiceReferences();
  78. QVERIFY(sa2.empty());
  79. // 8. A new Servicetracker, this time with a filter for the object
  80. QString fs = QString("(") + ctkPluginConstants::OBJECTCLASS + "=" + s1 + "*" + ")";
  81. ctkLDAPSearchFilter f1(fs);
  82. delete st1;
  83. st1 = new ctkServiceTracker<>(pc, f1);
  84. // add a service
  85. emit serviceControl(1, "register", 7);
  86. // 9. Open the service tracker and see what it finds,
  87. // expect to find two instances of references to
  88. // "org.commontk.pluginStest.TestPluginSService*"
  89. // i.e. they refer to the same piece of code
  90. st1->open();
  91. sa2 = st1->getServiceReferences();
  92. QVERIFY(sa2.size() == 2);
  93. for (int i = 0; i < sa2.size(); ++i)
  94. {
  95. QString name = pc->getService(sa2[i])->metaObject()->className();
  96. QVERIFY(name == expName);
  97. }
  98. // 10. Get pluginS_test to register one more service and see if it appears
  99. emit serviceControl(2, "register", 1);
  100. sa2 = st1->getServiceReferences();
  101. QVERIFY(sa2.size() == 3);
  102. for (int i = 0; i < sa2.size(); ++i)
  103. {
  104. QString name = pc->getService(sa2[i])->metaObject()->className();
  105. QVERIFY(name == expName);
  106. }
  107. // 11. Get pluginS_test to register one more service and see if it appears
  108. emit serviceControl(3, "register", 2);
  109. sa2 = st1->getServiceReferences();
  110. QVERIFY(sa2.size() == 4);
  111. for (int i = 0; i < sa2.size(); ++i)
  112. {
  113. QString name = pc->getService(sa2[i])->metaObject()->className();
  114. QVERIFY(name == expName);
  115. }
  116. // 12. Get pluginS_test to unregister one service and see if it disappears
  117. emit serviceControl(3, "unregister", 0);
  118. sa2 = st1->getServiceReferences();
  119. QVERIFY(sa2.size() == 3);
  120. for (int i = 0; i < sa2.size(); ++i)
  121. {
  122. QString name = pc->getService(sa2[i])->metaObject()->className();
  123. QVERIFY(name == expName);
  124. }
  125. // 13. Get the highest ranking service reference, it should have ranking 7
  126. ctkServiceReference h1 = st1->getServiceReference();
  127. long rank = h1.getProperty(ctkPluginConstants::SERVICE_RANKING).toLongLong();
  128. QVERIFY(rank == 7);
  129. // 14. Get the service of the highest ranked service reference
  130. QObject* o1 = st1->getService(h1);
  131. QVERIFY(o1 != 0);
  132. // 14a Get the highest ranked service, directly this time
  133. QObject* o3 = st1->getService();
  134. QVERIFY(o3 != 0);
  135. QVERIFY(o1 == o3);
  136. // 15. Now release the tracking of that service and then try to get it
  137. // from the servicetracker, which should yield a null object
  138. emit serviceControl (1, "unregister", 7);
  139. QObject* o2 = st1->getService(h1);
  140. QVERIFY(o2 == 0);
  141. // 16. Get all service objects this tracker tracks, it should be 2
  142. QList<QObject*> ts1 = st1->getServices();
  143. QVERIFY(ts1.size() == 2);
  144. // 17. Test the remove method.
  145. // First register another service, then remove it being tracked
  146. emit serviceControl(1, "register", 7);
  147. h1 = st1->getServiceReference();
  148. QList<ctkServiceReference> sa3 = st1->getServiceReferences();
  149. QVERIFY(sa3.size() == 3);
  150. for (int i = 0; i < sa3.size(); ++i)
  151. {
  152. QString name = pc->getService(sa3[i])->metaObject()->className();
  153. QVERIFY(name == expName);
  154. }
  155. st1->remove(h1); // remove tracking on one servref
  156. sa2 = st1->getServiceReferences();
  157. QVERIFY(sa2.size() == 2);
  158. // 18. Test the addingService method,add a service reference
  159. // 19. Test the removedService method, remove a service reference
  160. // 20. Test the waitForService method
  161. QObject* o9 = st1->waitForService(50);
  162. QVERIFY(o9 != 0);
  163. // 21. Test the waitForService method across threads
  164. ctkServiceTrackerTestWorker worker(pc);
  165. worker.start();
  166. QTest::qWait(100);
  167. // register "org.commontk.pluginStest.TestPluginSService3"
  168. emit serviceControl(3, "register", 2);
  169. // wait until the thread is finished
  170. QVERIFY(worker.wait());
  171. QVERIFY(worker.waitSuccess);
  172. delete st1;
  173. }
  174. ctkServiceTrackerTestWorker::ctkServiceTrackerTestWorker(ctkPluginContext* pc)
  175. : waitSuccess(false), pc(pc)
  176. {
  177. }
  178. void ctkServiceTrackerTestWorker::run()
  179. {
  180. ctkServiceTracker<> tracker(pc, "org.commontk.pluginStest.TestPluginSService3");
  181. tracker.open();
  182. // the tracker should initially be empty
  183. QVERIFY(tracker.isEmpty());
  184. QObject* obj = tracker.waitForService(1000);
  185. if (obj != 0) waitSuccess = true;
  186. quit();
  187. }