ctkManagedServiceFactoryTestSuite.cpp 5.5 KB

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