Back to index

nordugrid-arc-nox  1.1.0~rc6
XMLNodeTest.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #include <string>
00004 
00005 #include <cppunit/extensions/HelperMacros.h>
00006 
00007 #include <arc/XMLNode.h>
00008 
00009 class XMLNodeTest
00010   : public CppUnit::TestFixture {
00011 
00012   CPPUNIT_TEST_SUITE(XMLNodeTest);
00013   CPPUNIT_TEST(TestParsing);
00014   CPPUNIT_TEST(TestExchange);
00015   CPPUNIT_TEST(TestMove);
00016   CPPUNIT_TEST_SUITE_END();
00017 
00018 public:
00019   void setUp();
00020   void tearDown();
00021   void TestParsing();
00022   void TestExchange();
00023   void TestMove();
00024 
00025 };
00026 
00027 
00028 void XMLNodeTest::setUp() {
00029 }
00030 
00031 
00032 void XMLNodeTest::tearDown() {
00033 }
00034 
00035 
00036 void XMLNodeTest::TestParsing() {
00037   std::string xml_str(
00038      "<!-- comment must not be visible -->\n"
00039      "<root>\n"
00040      "  <child1>value1</child1>\n"
00041      "  <child2>value2</child2>\n"
00042      "</root>"
00043   );
00044   Arc::XMLNode xml(xml_str);
00045   CPPUNIT_ASSERT((bool)xml);
00046   CPPUNIT_ASSERT_EQUAL(std::string("root"), xml.Name());
00047   CPPUNIT_ASSERT_EQUAL(std::string("value1"), (std::string)xml["child1"]);
00048   CPPUNIT_ASSERT_EQUAL(std::string("value2"), (std::string)xml["child2"]);
00049   std::string s;
00050   xml.GetXML(s);
00051   CPPUNIT_ASSERT_EQUAL(std::string("<root>\n  <child1>value1</child1>\n  <child2>value2</child2>\n</root>"),s);
00052   xml.GetDoc(s);
00053   CPPUNIT_ASSERT_EQUAL("<?xml version=\"1.0\"?>\n"+xml_str+"\n",s);
00054 }
00055 
00056 void XMLNodeTest::TestExchange() {
00057   std::string xml1_str(
00058      "<root1>"
00059       "<child1>value1</child1>"
00060       "<child2>"
00061        "<child3>value3</child3>"
00062       "</child2>"
00063      "</root1>"
00064   );
00065   std::string xml2_str(
00066      "<root2>"
00067       "<child4>value4</child4>"
00068       "<child5>"
00069        "<child6>value6</child6>"
00070       "</child5>"
00071      "</root2>"
00072   );
00073   Arc::XMLNode* xml1 = NULL;
00074   Arc::XMLNode* xml2 = NULL;
00075   Arc::XMLNode node1;
00076   Arc::XMLNode node2;
00077 
00078   // Exchanging ordinary nodes
00079   xml1 = new Arc::XMLNode(xml1_str);
00080   xml2 = new Arc::XMLNode(xml2_str);
00081   node1 = (*xml1)["child2"];
00082   node2 = (*xml2)["child5"];
00083   node1.Exchange(node2);
00084   delete xml2; xml2 = new Arc::XMLNode(xml1_str);
00085   CPPUNIT_ASSERT_EQUAL(std::string("value6"),(std::string)((*xml1)["child5"]["child6"]));
00086   delete xml1;
00087   delete xml2;
00088 
00089   // Exchanging root nodes - operation must fail
00090   xml1 = new Arc::XMLNode(xml1_str);
00091   xml2 = new Arc::XMLNode(xml2_str);
00092   node1 = (*xml1);
00093   node2 = (*xml2);
00094   node1.Exchange(node2);
00095   delete xml2; xml2 = new Arc::XMLNode(xml1_str);
00096   CPPUNIT_ASSERT_EQUAL(std::string("root1"),(*xml1).Name());
00097   delete xml1;
00098   delete xml2;
00099 
00100   // Exchanging documents
00101   xml1 = new Arc::XMLNode(xml1_str);
00102   xml2 = new Arc::XMLNode(xml2_str);
00103   xml1->Exchange(*xml2);
00104   CPPUNIT_ASSERT_EQUAL(std::string("root2"),(*xml1).Name());
00105   CPPUNIT_ASSERT_EQUAL(std::string("root1"),(*xml2).Name());
00106   delete xml1;
00107   delete xml2;
00108 
00109   // Exchanging document and empty node
00110   xml1 = new Arc::XMLNode(xml1_str);
00111   xml2 = new Arc::XMLNode;
00112   xml1->Exchange(*xml2);
00113   CPPUNIT_ASSERT_EQUAL(std::string("root1"),(*xml2).Name());
00114   CPPUNIT_ASSERT(!(*xml1));
00115   delete xml1;
00116   delete xml2;
00117 
00118 }
00119 
00120 void XMLNodeTest::TestMove() {
00121   std::string xml_str(
00122      "<root1>"
00123       "<child1>value1</child1>"
00124       "<child2>"
00125        "<child3>value3</child3>"
00126       "</child2>"
00127      "</root1>"
00128   );
00129   Arc::XMLNode xml1(xml_str);
00130   Arc::XMLNode xml2;
00131   Arc::XMLNode node = xml1["child2"];
00132   node.Move(xml2);
00133   CPPUNIT_ASSERT_EQUAL(std::string("child2"),xml2.Name());
00134   CPPUNIT_ASSERT_EQUAL(std::string("value3"),(std::string)(xml2["child3"]));
00135   CPPUNIT_ASSERT_EQUAL(std::string("root1"),xml1.Name());
00136   CPPUNIT_ASSERT_EQUAL(std::string("value1"),(std::string)(xml1["child1"]));
00137   CPPUNIT_ASSERT(!(xml1["child2"]));
00138 }
00139 
00140 CPPUNIT_TEST_SUITE_REGISTRATION(XMLNodeTest);