ctkManagedServiceTestSuite.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "ctkManagedServiceTestSuite_p.h"
  16. #include <service/cm/ctkConfigurationAdmin.h>
  17. #include <ctkPluginContext.h>
  18. #include <ctkPluginConstants.h>
  19. #include <QTest>
  20. //----------------------------------------------------------------------------
  21. _ManagedServiceUpdateTest::_ManagedServiceUpdateTest(
  22. ctkManagedServiceTestSuite* ts)
  23. : ts(ts)
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. void _ManagedServiceUpdateTest::updated(
  28. const ctkDictionary& properties)
  29. {
  30. Q_UNUSED(properties)
  31. QMutexLocker l(&ts->mutex);
  32. ts->locked = false;
  33. ts->lock.wakeOne();
  34. ts->updateCount++;
  35. }
  36. //----------------------------------------------------------------------------
  37. ctkManagedServiceTestSuite::ctkManagedServiceTestSuite(
  38. ctkPluginContext* pc, long cmPluginId)
  39. : context(pc), cmPluginId(cmPluginId), cm(0), updateCount(0),
  40. locked(false)
  41. {
  42. }
  43. //----------------------------------------------------------------------------
  44. void ctkManagedServiceTestSuite::init()
  45. {
  46. context->getPlugin(cmPluginId)->start();
  47. reference = context->getServiceReference<ctkConfigurationAdmin>();
  48. cm = context->getService<ctkConfigurationAdmin>(reference);
  49. }
  50. //----------------------------------------------------------------------------
  51. void ctkManagedServiceTestSuite::cleanup()
  52. {
  53. context->ungetService(reference);
  54. context->getPlugin(cmPluginId)->stop();
  55. }
  56. //----------------------------------------------------------------------------
  57. void ctkManagedServiceTestSuite::testSamePidManagedService()
  58. {
  59. ctkConfigurationPtr config = cm->getConfiguration("test");
  60. ctkDictionary props;
  61. props.insert("testkey", "testvalue");
  62. config->update(props);
  63. updateCount = 0;
  64. _ManagedServiceUpdateTest ms(this);
  65. ctkDictionary dict;
  66. dict.insert(ctkPluginConstants::SERVICE_PID, "test");
  67. ctkServiceRegistration reg;
  68. {
  69. QMutexLocker l(&mutex);
  70. reg = context->registerService<ctkManagedService>(&ms, dict);
  71. locked = true;
  72. lock.wait(&mutex, 5000);
  73. if (locked)
  74. QFAIL("should have updated");
  75. QCOMPARE(1, updateCount);
  76. }
  77. ctkServiceRegistration reg2;
  78. {
  79. QMutexLocker l(&mutex);
  80. reg2 = context->registerService<ctkManagedService>(&ms, dict);
  81. locked = true;
  82. lock.wait(&mutex, 100);
  83. QVERIFY(locked);
  84. QCOMPARE(1, updateCount);
  85. locked = false;
  86. }
  87. reg.unregister();
  88. reg2.unregister();
  89. config->remove();
  90. }
  91. //----------------------------------------------------------------------------
  92. void ctkManagedServiceTestSuite::testGeneralManagedService()
  93. {
  94. updateCount = 0;
  95. _ManagedServiceUpdateTest ms(this);
  96. ctkDictionary dict;
  97. dict.insert(ctkPluginConstants::SERVICE_PID, "test");
  98. ctkServiceRegistration reg;
  99. {
  100. QMutexLocker l(&mutex);
  101. reg = context->registerService<ctkManagedService>(&ms, dict);
  102. locked = true;
  103. lock.wait(&mutex, 5000);
  104. if (locked)
  105. QFAIL("should have updated");
  106. QCOMPARE(1, updateCount);
  107. }
  108. ctkConfigurationPtr config = cm->getConfiguration("test");
  109. QVERIFY(config->getProperties().isEmpty());
  110. ctkDictionary props;
  111. props.insert("testkey", "testvalue");
  112. {
  113. QMutexLocker l(&mutex);
  114. config->update(props);
  115. locked = true;
  116. lock.wait(&mutex, 5000);
  117. if (locked)
  118. QFAIL("should have updated");
  119. QCOMPARE(2, updateCount);
  120. }
  121. QString location = config->getPluginLocation();
  122. config->setPluginLocation("bogus");
  123. {
  124. QMutexLocker l(&mutex);
  125. config->update();
  126. locked = true;
  127. lock.wait(&mutex, 100);
  128. QVERIFY(locked);
  129. QCOMPARE(2, updateCount);
  130. locked = false;
  131. }
  132. config->setPluginLocation(location);
  133. dict.remove(ctkPluginConstants::SERVICE_PID);
  134. {
  135. QMutexLocker l(&mutex);
  136. reg.setProperties(dict);
  137. props.insert("testkey", "testvalue2");
  138. config->update(props);
  139. locked = true;
  140. lock.wait(&mutex, 100);
  141. QVERIFY(locked);
  142. QCOMPARE(2, updateCount);
  143. locked = false;
  144. }
  145. config->remove();
  146. config = cm->getConfiguration("test2");
  147. dict.insert(ctkPluginConstants::SERVICE_PID, "test2");
  148. {
  149. QMutexLocker l(&mutex);
  150. reg.setProperties(dict);
  151. locked = true;
  152. lock.wait(&mutex, 5000);
  153. if (locked)
  154. QFAIL("should have updated");
  155. QCOMPARE(3, updateCount);
  156. }
  157. {
  158. QMutexLocker l(&mutex);
  159. config->remove();
  160. locked = true;
  161. lock.wait(&mutex, 5000);
  162. if (locked)
  163. QFAIL("should have updated");
  164. QCOMPARE(4, updateCount);
  165. }
  166. reg.unregister();
  167. }