/*============================================================================= Library: CTK Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #include "ctkCmdLineModuleManager.h" #include "ctkCmdLineModuleBackend.h" #include "ctkException.h" #include "ctkCmdLineModuleFuture.h" #include "ctkTest.h" #include #include #include #include #if (QT_VERSION < QT_VERSION_CHECK(4,7,0)) extern int qHash(const QUrl& url); #endif namespace { class BackendMockUp : public ctkCmdLineModuleBackend { public: void addModule(const QUrl& location, const QByteArray& xml) { this->UrlToXml[location] = xml; } virtual QString name() const { return "Mockup"; } virtual QString description() const { return "Test Mock-up"; } virtual QList schemes() const { return QList() << "test"; } virtual qint64 timeStamp(const QUrl& /*location*/) const { return 0; } virtual QByteArray rawXmlDescription(const QUrl& location) { return UrlToXml[location]; } protected: virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* /*frontend*/) { return ctkCmdLineModuleFuture(); } private: QHash UrlToXml; }; } //----------------------------------------------------------------------------- class ctkCmdLineModuleManagerTester : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void testStrictValidation(); void testWeakValidation(); void testSkipValidation(); private: QByteArray validXml; QByteArray invalidXml; }; //----------------------------------------------------------------------------- void ctkCmdLineModuleManagerTester::initTestCase() { validXml = "\n" " My Filter\n" " Awesome filter\n" " \n" " \n" " bla\n" " \n" " param\n" " i\n" " bla\n" " \n" " \n" " \n" "\n"; invalidXml = "\n" " Awesome filter\n" " My Filter\n" " \n" " \n" " bla\n" " \n" " param\n" " i\n" " bla\n" " \n" " \n" " \n" "\n"; } //----------------------------------------------------------------------------- void ctkCmdLineModuleManagerTester::testStrictValidation() { BackendMockUp backend; backend.addModule(QUrl("test://validXml"), validXml); backend.addModule(QUrl("test://invalidXml"), invalidXml); ctkCmdLineModuleManager manager; manager.registerBackend(&backend); ctkCmdLineModuleReference moduleRef = manager.registerModule(QUrl("test://validXml")); QVERIFY(moduleRef); QVERIFY(moduleRef.xmlValidationErrorString().isEmpty()); try { manager.registerModule(QUrl("test://invalidXml")); QFAIL("Succeeded in registering invalid module"); } catch (const ctkInvalidArgumentException&) { } } //----------------------------------------------------------------------------- void ctkCmdLineModuleManagerTester::testWeakValidation() { BackendMockUp backend; backend.addModule(QUrl("test://validXml"), validXml); backend.addModule(QUrl("test://invalidXml"), invalidXml); ctkCmdLineModuleManager manager(ctkCmdLineModuleManager::WEAK_VALIDATION); manager.registerBackend(&backend); ctkCmdLineModuleReference moduleRef = manager.registerModule(QUrl("test://validXml")); QVERIFY(moduleRef); QVERIFY(moduleRef.xmlValidationErrorString().isEmpty()); ctkCmdLineModuleReference moduleRef2 = manager.registerModule(QUrl("test://invalidXml")); QVERIFY(moduleRef2); QVERIFY(!moduleRef2.xmlValidationErrorString().isEmpty()); } //----------------------------------------------------------------------------- void ctkCmdLineModuleManagerTester::testSkipValidation() { BackendMockUp backend; backend.addModule(QUrl("test://validXml"), validXml); backend.addModule(QUrl("test://invalidXml"), invalidXml); ctkCmdLineModuleManager manager(ctkCmdLineModuleManager::SKIP_VALIDATION); manager.registerBackend(&backend); ctkCmdLineModuleReference moduleRef = manager.registerModule(QUrl("test://validXml")); QVERIFY(moduleRef); QVERIFY(moduleRef.xmlValidationErrorString().isEmpty()); ctkCmdLineModuleReference moduleRef2 = manager.registerModule(QUrl("test://invalidXml")); QVERIFY(moduleRef2); QVERIFY(moduleRef2.xmlValidationErrorString().isEmpty()); } // ---------------------------------------------------------------------------- CTK_TEST_MAIN(ctkCmdLineModuleManagerTest) #include "moc_ctkCmdLineModuleManagerTest.cpp"