ctkPluginFrameworkTestRunner.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "ctkPluginFrameworkTestRunner.h"
  16. #include "ctkTestSuiteInterface.h"
  17. #include <ctkPluginFrameworkFactory.h>
  18. #include <ctkPluginFramework.h>
  19. #include <ctkPluginFrameworkLauncher.h>
  20. #include <ctkPluginContext.h>
  21. #include <ctkPluginException.h>
  22. #include <QStringList>
  23. #include <QPair>
  24. #include <QCoreApplication>
  25. #include <QDirIterator>
  26. #include <QTest>
  27. #include <QThread>
  28. #include <QDebug>
  29. //----------------------------------------------------------------------------
  30. class TestRunner : public QThread
  31. {
  32. public:
  33. typedef QPair<long, ctkPlugin::StartOptions> StartPluginPair;
  34. TestRunner(ctkPluginContext* context, const QSet<StartPluginPair>& startPlugins, int argc, char** argv)
  35. : context(context), startPluginInfos(startPlugins),
  36. argc(argc), argv(argv)
  37. {
  38. }
  39. void run()
  40. {
  41. // start the specified plugins which may register the test suites (QObject classes)
  42. foreach(StartPluginPair pluginInfo, startPluginInfos)
  43. {
  44. QSharedPointer<ctkPlugin> plugin = context->getPlugin(pluginInfo.first);
  45. plugin->start(pluginInfo.second);
  46. }
  47. QList<ctkServiceReference> refs = context->getServiceReferences<ctkTestSuiteInterface>();
  48. int result = 0;
  49. int count = 0;
  50. foreach(ctkServiceReference ref, refs)
  51. {
  52. result = QTest::qExec(context->getService(ref), argc, argv);
  53. if (result > 0) break;
  54. ++count;
  55. }
  56. // stop the framework
  57. QSharedPointer<ctkPluginFramework> fw = qSharedPointerCast<ctkPluginFramework>(context->getPlugin(0));
  58. fw->stop();
  59. // wait for 30 secs
  60. ctkPluginFrameworkEvent event = fw->waitForStop(30000);
  61. if (result == 0 && event.getType() == ctkPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT)
  62. {
  63. qWarning() << "Framework shutdown wait timed out";
  64. result = 1;
  65. }
  66. qDebug() << "#########" << count << "out of" << refs.size() << "test suites passed #########";
  67. if (result > 0)
  68. {
  69. QCoreApplication::exit(result);
  70. qApp->thread()->wait();
  71. }
  72. }
  73. private:
  74. ctkPluginContext* context;
  75. QSet<StartPluginPair> startPluginInfos;
  76. int argc;
  77. char** argv;
  78. };
  79. //----------------------------------------------------------------------------
  80. class ctkPluginFrameworkTestRunnerPrivate
  81. {
  82. public:
  83. typedef QPair<QString, bool> PluginPathPair;
  84. QList<PluginPathPair> pluginPaths;
  85. typedef QPair<QString, QString> InstallCandPair;
  86. QList<InstallCandPair> installCandidates;
  87. typedef QPair<QString, ctkPlugin::StartOptions> ActivatePair;
  88. QList<ActivatePair> activatePlugins;
  89. typedef QPair<long, ctkPlugin::StartOptions> StartPluginPair;
  90. QSet<StartPluginPair> startPlugins;
  91. ctkPluginContext* context;
  92. ctkPluginFrameworkFactory* fwFactory;
  93. //----------------------------------------------------------------------------
  94. ctkPluginFrameworkTestRunnerPrivate()
  95. : context(0), fwFactory(0)
  96. {
  97. pluginLibFilter << "*.dll" << "*.so" << "*.dylib";
  98. }
  99. //----------------------------------------------------------------------------
  100. void installPlugins(const QString& path)
  101. {
  102. QDirIterator dirIter(path, pluginLibFilter, QDir::Files);
  103. while(dirIter.hasNext())
  104. {
  105. dirIter.next();
  106. try
  107. {
  108. QSharedPointer<ctkPlugin> plugin = context->installPlugin(QUrl::fromLocalFile(dirIter.filePath()));
  109. long pluginId = plugin->getPluginId();
  110. QString symbolicName = plugin->getSymbolicName();
  111. foreach(ActivatePair activatePlugin, activatePlugins)
  112. {
  113. if (activatePlugin.first == symbolicName)
  114. {
  115. startPlugins.insert(qMakePair(pluginId, activatePlugin.second));
  116. activatePlugins.removeAll(activatePlugin);
  117. break;
  118. }
  119. }
  120. }
  121. catch (const ctkPluginException& e)
  122. {
  123. qCritical() << e.what();
  124. }
  125. }
  126. }
  127. //----------------------------------------------------------------------------
  128. void installPlugin(const QString& path, const QString& name)
  129. {
  130. QDirIterator dirIter(path, pluginLibFilter, QDir::Files);
  131. while(dirIter.hasNext())
  132. {
  133. dirIter.next();
  134. QString fileName = dirIter.fileName().mid(3); // strip the "lib" prefix
  135. fileName.truncate(fileName.lastIndexOf('.')); // remove the suffix
  136. if (fileName == name)
  137. {
  138. try
  139. {
  140. QSharedPointer<ctkPlugin> plugin = context->installPlugin(QUrl::fromLocalFile(dirIter.filePath()));
  141. QString symbolicName = plugin->getSymbolicName();
  142. long pluginId = plugin->getPluginId();
  143. foreach(ActivatePair activatePlugin, activatePlugins)
  144. {
  145. if (activatePlugin.first == symbolicName)
  146. {
  147. startPlugins.insert(qMakePair(pluginId, activatePlugin.second));
  148. activatePlugins.removeAll(activatePlugin);
  149. break;
  150. }
  151. }
  152. break;
  153. }
  154. catch (const ctkPluginException& e)
  155. {
  156. qCritical() << e.what();
  157. }
  158. }
  159. }
  160. }
  161. private:
  162. QStringList pluginLibFilter;
  163. };
  164. //----------------------------------------------------------------------------
  165. ctkPluginFrameworkTestRunner::ctkPluginFrameworkTestRunner()
  166. : d_ptr(new ctkPluginFrameworkTestRunnerPrivate())
  167. {
  168. }
  169. //----------------------------------------------------------------------------
  170. ctkPluginFrameworkTestRunner::~ctkPluginFrameworkTestRunner()
  171. {
  172. Q_D(ctkPluginFrameworkTestRunner);
  173. delete d->fwFactory;
  174. }
  175. //----------------------------------------------------------------------------
  176. void ctkPluginFrameworkTestRunner::addPluginPath(const QString& path, bool install)
  177. {
  178. Q_D(ctkPluginFrameworkTestRunner);
  179. d->pluginPaths.push_back(qMakePair(path, install));
  180. ctkPluginFrameworkLauncher::appendPathEnv(path);
  181. }
  182. //----------------------------------------------------------------------------
  183. void ctkPluginFrameworkTestRunner::addPlugin(const QString &path, const QString &name)
  184. {
  185. Q_D(ctkPluginFrameworkTestRunner);
  186. d->installCandidates.push_back(qMakePair(path, name));
  187. }
  188. //----------------------------------------------------------------------------
  189. void ctkPluginFrameworkTestRunner::startPluginOnRun(const QString& pluginId, ctkPlugin::StartOptions opts)
  190. {
  191. Q_D(ctkPluginFrameworkTestRunner);
  192. d->activatePlugins.push_back(qMakePair(pluginId, opts));
  193. }
  194. //----------------------------------------------------------------------------
  195. void ctkPluginFrameworkTestRunner::init(const ctkProperties& fwProps)
  196. {
  197. Q_D(ctkPluginFrameworkTestRunner);
  198. d->fwFactory = new ctkPluginFrameworkFactory(fwProps);
  199. QSharedPointer<ctkPluginFramework> framework = d->fwFactory->getFramework();
  200. framework->start();
  201. d->context = framework->getPluginContext();
  202. foreach(ctkPluginFrameworkTestRunnerPrivate::PluginPathPair path,
  203. d->pluginPaths)
  204. {
  205. QCoreApplication::addLibraryPath(path.first);
  206. if (path.second) d->installPlugins(path.first);
  207. }
  208. foreach(ctkPluginFrameworkTestRunnerPrivate::InstallCandPair candidate,
  209. d->installCandidates)
  210. {
  211. qDebug() << "Installing" << candidate.first << "," << candidate.second;
  212. d->installPlugin(candidate.first, candidate.second);
  213. }
  214. }
  215. //----------------------------------------------------------------------------
  216. int ctkPluginFrameworkTestRunner::run(int argc, char** argv)
  217. {
  218. Q_D(ctkPluginFrameworkTestRunner);
  219. if (!d->activatePlugins.isEmpty())
  220. {
  221. qCritical() << "The following plugins will not be started, because"
  222. << "they could not be installed:";
  223. foreach(ctkPluginFrameworkTestRunnerPrivate::ActivatePair p,
  224. d->activatePlugins)
  225. {
  226. qCritical() << " -" << p.first;
  227. }
  228. return EXIT_FAILURE;
  229. }
  230. TestRunner runner(d->context, d->startPlugins, argc, argv);
  231. runner.connect(&runner, SIGNAL(finished()), qApp, SLOT(quit()));
  232. runner.start();
  233. return qApp->exec();
  234. }