ctkCmdLineModuleManagerTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "ctkCmdLineModuleManager.h"
  16. #include "ctkCmdLineModuleBackend.h"
  17. #include "ctkException.h"
  18. #include "ctkCmdLineModuleFuture.h"
  19. #include "ctkTest.h"
  20. #include <QCoreApplication>
  21. #include <QBuffer>
  22. #include <QDataStream>
  23. #include <QDebug>
  24. #if (QT_VERSION < QT_VERSION_CHECK(4,7,0))
  25. extern int qHash(const QUrl& url);
  26. #endif
  27. namespace {
  28. class BackendMockUp : public ctkCmdLineModuleBackend
  29. {
  30. public:
  31. void addModule(const QUrl& location, const QByteArray& xml)
  32. {
  33. this->UrlToXml[location] = xml;
  34. }
  35. virtual QString name() const { return "Mockup"; }
  36. virtual QString description() const { return "Test Mock-up"; }
  37. virtual QList<QString> schemes() const { return QList<QString>() << "test"; }
  38. virtual qint64 timeStamp(const QUrl& /*location*/) const { return 0; }
  39. virtual QByteArray rawXmlDescription(const QUrl& location)
  40. {
  41. return UrlToXml[location];
  42. }
  43. protected:
  44. virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* /*frontend*/)
  45. {
  46. return ctkCmdLineModuleFuture();
  47. }
  48. private:
  49. QHash<QUrl, QByteArray> UrlToXml;
  50. };
  51. }
  52. //-----------------------------------------------------------------------------
  53. class ctkCmdLineModuleManagerTester : public QObject
  54. {
  55. Q_OBJECT
  56. private Q_SLOTS:
  57. void initTestCase();
  58. void testStrictValidation();
  59. void testWeakValidation();
  60. void testSkipValidation();
  61. private:
  62. QByteArray validXml;
  63. QByteArray invalidXml;
  64. };
  65. //-----------------------------------------------------------------------------
  66. void ctkCmdLineModuleManagerTester::initTestCase()
  67. {
  68. validXml = "<executable>\n"
  69. " <title>My Filter</title>\n"
  70. " <description>Awesome filter</description>\n"
  71. " <parameters>\n"
  72. " <label>bla</label>\n"
  73. " <description>bla</description>\n"
  74. " <integer>\n"
  75. " <name>param</name>\n"
  76. " <flag>i</flag>\n"
  77. " <description>bla</description>\n"
  78. " <label>bla</label>\n"
  79. " </integer>\n"
  80. " </parameters>\n"
  81. "</executable>\n";
  82. invalidXml = "<executable>\n"
  83. " <description>Awesome filter</description>\n"
  84. " <title>My Filter</title>\n"
  85. " <parameters>\n"
  86. " <label>bla</label>\n"
  87. " <description>bla</description>\n"
  88. " <integer>\n"
  89. " <name>param</name>\n"
  90. " <flag>i</flag>\n"
  91. " <description>bla</description>\n"
  92. " <label>bla</label>\n"
  93. " </integer>\n"
  94. " </parameters>\n"
  95. "</executable>\n";
  96. }
  97. //-----------------------------------------------------------------------------
  98. void ctkCmdLineModuleManagerTester::testStrictValidation()
  99. {
  100. BackendMockUp backend;
  101. backend.addModule(QUrl("test://validXml"), validXml);
  102. backend.addModule(QUrl("test://invalidXml"), invalidXml);
  103. ctkCmdLineModuleManager manager;
  104. manager.registerBackend(&backend);
  105. ctkCmdLineModuleReference moduleRef = manager.registerModule(QUrl("test://validXml"));
  106. QVERIFY(moduleRef);
  107. QVERIFY(moduleRef.xmlValidationErrorString().isEmpty());
  108. try
  109. {
  110. manager.registerModule(QUrl("test://invalidXml"));
  111. QFAIL("Succeeded in registering invalid module");
  112. }
  113. catch (const ctkInvalidArgumentException&)
  114. {
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. void ctkCmdLineModuleManagerTester::testWeakValidation()
  119. {
  120. BackendMockUp backend;
  121. backend.addModule(QUrl("test://validXml"), validXml);
  122. backend.addModule(QUrl("test://invalidXml"), invalidXml);
  123. ctkCmdLineModuleManager manager(ctkCmdLineModuleManager::WEAK_VALIDATION);
  124. manager.registerBackend(&backend);
  125. ctkCmdLineModuleReference moduleRef = manager.registerModule(QUrl("test://validXml"));
  126. QVERIFY(moduleRef);
  127. QVERIFY(moduleRef.xmlValidationErrorString().isEmpty());
  128. ctkCmdLineModuleReference moduleRef2 = manager.registerModule(QUrl("test://invalidXml"));
  129. QVERIFY(moduleRef2);
  130. QVERIFY(!moduleRef2.xmlValidationErrorString().isEmpty());
  131. }
  132. //-----------------------------------------------------------------------------
  133. void ctkCmdLineModuleManagerTester::testSkipValidation()
  134. {
  135. BackendMockUp backend;
  136. backend.addModule(QUrl("test://validXml"), validXml);
  137. backend.addModule(QUrl("test://invalidXml"), invalidXml);
  138. ctkCmdLineModuleManager manager(ctkCmdLineModuleManager::SKIP_VALIDATION);
  139. manager.registerBackend(&backend);
  140. ctkCmdLineModuleReference moduleRef = manager.registerModule(QUrl("test://validXml"));
  141. QVERIFY(moduleRef);
  142. QVERIFY(moduleRef.xmlValidationErrorString().isEmpty());
  143. ctkCmdLineModuleReference moduleRef2 = manager.registerModule(QUrl("test://invalidXml"));
  144. QVERIFY(moduleRef2);
  145. QVERIFY(moduleRef2.xmlValidationErrorString().isEmpty());
  146. }
  147. // ----------------------------------------------------------------------------
  148. CTK_TEST_MAIN(ctkCmdLineModuleManagerTest)
  149. #include "moc_ctkCmdLineModuleManagerTest.cpp"