/*************************************************************************** * This file is part of the KDE project * copyright (C) 2006 by Bernd Steindorff (bernd@itii.de) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * You should have received a copy of the GNU Library General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. ***************************************************************************/ #include "xmlhandlertests.h" #include "testaction.h" #include "komacrotestbase.h" #include "../lib/action.h" #include "../lib/manager.h" #include "../lib/macro.h" #include "../lib/variable.h" #include "../lib/macroitem.h" #include #include #include #include #include #include using namespace KUnitTest; using namespace KoMacroTest; namespace KoMacroTest { /** * Register KoMacroTest::CommonTests as TestSuite. */ KUNITTEST_SUITE("KoMacroTestSuite") KUNITTEST_REGISTER_TESTER(XMLHandlerTests); class XMLHandlerTests::Private { public: /** * An KXMLGUIClient instance created on @a setUp() and * passed to the @a KoMacro::Manager to bridge to the * app-functionality. */ KXMLGUIClient* xmlguiclient; /** * An @a TestObject instance used internaly to test * handling and communication with from TQObject * inheritated instances. */ TDESharedPtr testaction; Private() : xmlguiclient(0) , testaction(0) { } }; } XMLHandlerTests::XMLHandlerTests() : KUnitTest::SlotTester() , d( new Private() ) // create the private d-pointer instance. { } XMLHandlerTests::~XMLHandlerTests() { delete d->xmlguiclient; delete d; } void XMLHandlerTests::setUp() { d->xmlguiclient = new KXMLGUIClient(); //Singelton more or less ... if (::KoMacro::Manager::self() == 0) { ::KoMacro::Manager::init( d->xmlguiclient ); } d->testaction = new TestAction(); ::KoMacro::Manager::self()->publishAction(d->testaction); } void XMLHandlerTests::tearDown() { delete d->xmlguiclient; } /** * Test the @a KoMacro::XMLHandler parseXML() and toXML()-function. */ void XMLHandlerTests::testParseAndToXML() { kdDebug()<<"===================== testParseAndToXML() ======================" << endl; // 1.Test - Correct DomElement. testCorrectDomElement(); // 2.Test - XML-document with bad root element. testBadRoot(); // 3.Test - XML-document with a missing Variable. testMissingVariable(); // 4.Test - One more Variable in XML-Document. testMoreVariables(); // 5.Test - XML-document with wrong macro-xmlversion. testWrongVersion(); // 6.Test - XML-document if it has a wrong structure like wrong parathesis // or missing end tag. testWrongXMLStruct(); // 7.Test-XML-document with maximum field-size. testMaxNum(); // 8.Test-XML-document with maximum+1 field-size. testMaxNum2(); // 9.Test-XML-document with minimum field-size. testMinNum(); // 10.Test-XML-document with minimum-1 field-size. testMinNum2(); // 11.Test - With a to big number. testBigNumber(); // 12.Test - With two MacroItems. testTwoMacroItems(); } /*************************************************************************** * Begin of Sub-methos of testParseXML(). ***************************************************************************/ // 1.Test - Correct DomElement. void XMLHandlerTests::testCorrectDomElement() { // Local Init TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; // Part 1: From XML to a Macro. // Test-XML-document with normal allocated variables. const TQString xml = TQString("" "" "" "test_string" "0" "true" "0.6" "" ""); // Set the XML-document with the above string. doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); // Is our XML parseable by calling parseXML()? KOMACROTEST_ASSERT(macro->parseXML(elem),true); // Is the parsen content in the Macro correct ? TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); // Transform back by calling toXML(). const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); // Test the Compare-method when a Variable will change, it must fail. macro->items().first()->variable("teststring")->setVariant("bla"); isvariableok.replace("teststring",false); assertMacroContentEqToXML(macro,elem,false,true,isvariableok); } // 2.Test - XML-document with bad root element. void XMLHandlerTests::testBadRoot() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" "0" "true" "0.6" "" ""); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_XASSERT(macro->parseXML(elem),true); //no assertMacroContentEqToXML(), because parsing failed. assertMacroContentEqToXML(macro,elem,true,false,TQMap()); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,true,false,TQMap()); } // 3.Test - XML-document with a missing Variable. void XMLHandlerTests::testMissingVariable() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" "0" "0.6" "" ""); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 4.Test - One more Variable in XML-Document. void XMLHandlerTests::testMoreVariables() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" "0" "true" "0.6" "somethingwrong" "" ""); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; isvariableok["testbla"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 5.Test - XML-document with wrong macro-xmlversion. void XMLHandlerTests::testWrongVersion() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" "0" "true" "0.6" "" ""); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_XASSERT(macro->parseXML(elem),true); //no assertMacroContentEqToXML(), because parsing failed. assertMacroContentEqToXML(macro,elem,true,false,TQMap()); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,true,false,TQMap()); } // 6.Test - XML-document if it has a wrong structure like wrong parathesis // or missing end tag. void XMLHandlerTests::testWrongXMLStruct() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "macro xmlversion=\"1\">>" "" "test_string" "0" "" ""); KOMACROTEST_XASSERT(doomdocument.setContent(xml),true); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_XASSERT(macro->parseXML(elem),true); //no assertMacroContentEqToXML(), because parsing failed. assertMacroContentEqToXML(macro,elem,true,false,TQMap()); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,true,false,TQMap()); } // 7.Test-XML-document with maximum field-size. void XMLHandlerTests::testMaxNum() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" " %1 " "true" " %2 " "" "").arg(INT_MAX).arg(DBL_MAX); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 8.Test-XML-document with maximum+1 field-size. void XMLHandlerTests::testMaxNum2() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" " %1 " "true" " %2 " "" "").arg(INT_MAX+1).arg(DBL_MAX+1); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 9.Test-XML-document with minimum field-size. void XMLHandlerTests::testMinNum() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" " %1 " "true" " %2 " "" "").arg(INT_MIN).arg(DBL_MIN); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 10.Test-XML-document with minimum+1 field-size. void XMLHandlerTests::testMinNum2() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" " %1 " "true" " %2 " "" "").arg(INT_MIN-1).arg(DBL_MIN-1); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 11.Test - With a to big number. void XMLHandlerTests::testBigNumber() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" " 0123456789012345678901234567890123456789 " "true" " %1 " "" "").arg(DBL_MAX+1); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } // 12.Test - With two MacroItems. void XMLHandlerTests::testTwoMacroItems() { TDESharedPtr macro = KoMacro::Manager::self()->createMacro("testMacro"); TQDomDocument doomdocument; const TQString xml = TQString("" "" "" "test_string" "0" "true" "0.6" "somethingwrong" "" "" "testBBstring2" "4" "false" "0.7" "somethingwrong2" "" ""); doomdocument.setContent(xml); const TQDomElement elem = doomdocument.documentElement(); KOMACROTEST_ASSERT(macro->parseXML(elem),true); TQMap isvariableok; isvariableok["teststring"] = true; isvariableok["testint"] = true; isvariableok["testbool"] = true; isvariableok["testdouble"] = true; assertMacroContentEqToXML(macro,elem,false,true,isvariableok); const TQDomElement elem2 = macro->toXML(); assertMacroContentEqToXML(macro,elem2,false,true,isvariableok); } /*************************************************************************** * End of Sub-methos of testParseAndToXML(). ***************************************************************************/ /** * Compares a XML-Element with a Macro. Call sub-asserts. * @p macro The parsen @a Macro. * @p elem The given @a TQDomElement which is parsen. * @p isitemsempty Bool for expectation of an empty @a MacroItem -List. * @p isactionset Bool for expectation that the @a Action -names are equal. * @p isvariableok TQMap of Bools for comparing each @a Variable . */ void XMLHandlerTests::assertMacroContentEqToXML(const TDESharedPtr macro, const TQDomElement& elem, const bool isitemsempty, const bool isactionset, const TQMap isvariableok) { // Make an Iterator over the MacroItems of the Macro. const TQValueList > macroitems = macro->items(); TQValueList >::ConstIterator mit(macroitems.constBegin()), end(macroitems.constEnd()); //1.comparison - Is the MacroItem-list empty? { if( isitemsempty ) { KOMACROTEST_XASSERT(macroitems.empty(),false); kdDebug() << "There is no correct MacroItem parsen." << endl; return; } else { KOMACROTEST_ASSERT(macroitems.empty(),false); } } // Got to the first item-elements of the elem (there is only one in the tests). TQDomNode itemnode = elem.firstChild(); // Iterate over the MacroItems and item-elements. while(mit != end && ! itemnode.isNull()) { const TDESharedPtr macroitem = *mit; const TQDomElement itemelem = itemnode.toElement(); //2.comparison - Is the Action-name equal? { if( ! isactionset) { KOMACROTEST_XASSERT(macroitem->action()->name() == itemelem.attribute("action"),true); kdDebug() << "Action-name not equal: " << macroitem->action()->name() << " != " << itemelem.attribute("action") << endl; return; } else { KOMACROTEST_ASSERT(macroitem->action()->name() == itemelem.attribute("action"),true); } } // Go down to MacroItem->Variable and item->variable and compare them. TQMap > mvariables = macroitem->variables(); TQDomNode varnode = itemelem.firstChild(); while ( ! varnode.isNull()) { const TQDomElement varelem = varnode.toElement(); const TDESharedPtr varitem = mvariables.find(varelem.attribute("name")).data(); //3.comparison - Is the content of the Variable // in the MacroItem and and item equal? { const bool var = *isvariableok.find(varelem.attribute("name")); if( ! var ) { KOMACROTEST_XASSERT(varitem->variant() == TQVariant(varelem.text()), !var); kdDebug() << "The content of the Variable: " << varitem->name() << " is not equal." << varitem->variant() << "!=" << varelem.text() << endl; } else { KOMACROTEST_ASSERT(varitem->variant() == TQVariant(varelem.text()), var); } } // Erase the MacroItem from the map, because it is parsen correctly. mvariables.erase(varitem->name()); // Go to next Variable in node-tree. varnode = varnode.nextSibling(); } //4.comparison - Is every MacroItem parsen? { KOMACROTEST_ASSERT(mvariables.empty(),true); kdDebug() << "There are non-filled variable in the MacroItem: " << mvariables.count() < > mvariables, const TQString s) { //TQValueList::ConstIterator kit (keys.constBegin()), end(keys.constEnd()); TQMap >::ConstIterator mvit (mvariables.constBegin()), end(mvariables.constEnd()); while(mvit != end){ const KoMacro::Variable * v = *mvit; kdDebug() << s << ": " << v->name() << endl; mvit++; } } #include "xmlhandlertests.moc"