Remove some tracing, fix string compares.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / xmltoolingtest.h
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "XMLObjectBaseTestCase.h"\r
18 \r
19 #include <fstream>\r
20 #include <cxxtest/GlobalFixture.h>\r
21 #include <xmltooling/XMLToolingConfig.h>\r
22 #include <xmltooling/util/ParserPool.h>\r
23 \r
24 //#define XMLTOOLINGTEST_LEAKCHECK\r
25 \r
26 std::string data_path = "../xmltoolingtest/data/";\r
27 \r
28 class ToolingFixture : public CxxTest::GlobalFixture\r
29 {\r
30 public:\r
31     bool setUpWorld() {\r
32         XMLToolingConfig::getConfig().log_config();\r
33         if (!XMLToolingConfig::getConfig().init())\r
34             return false;\r
35         if (getenv("XMLTOOLINGTEST_DATA"))\r
36             data_path=std::string(getenv("XMLTOOLINGTEST_DATA")) + "/";\r
37         std::string catpath=data_path + "catalog.xml";\r
38         auto_ptr_XMLCh temp(catpath.c_str());\r
39         return XMLToolingConfig::getConfig().getValidatingParser().loadCatalog(temp.get());\r
40     }\r
41     bool tearDownWorld() {\r
42         XMLToolingConfig::getConfig().term();\r
43 #if defined(_MSC_VER ) && defined(XMLTOOLINGTEST_LEAKCHECK)\r
44        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );\r
45        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );\r
46        _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );\r
47        _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );\r
48        _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );\r
49        _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );\r
50        _CrtDumpMemoryLeaks();\r
51 #endif\r
52         return true;\r
53     }\r
54     //bool setUp() { printf( "</test>" ); return true; }\r
55     //bool tearDown() { printf( "</test>" ); return true; }\r
56 };\r
57 \r
58 static ToolingFixture globalFixture;\r
59 \r
60 class GlobalTest : public CxxTest::TestSuite\r
61 {\r
62 public:\r
63     void setUp() {\r
64         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());\r
65     }\r
66 \r
67     void tearDown() {\r
68         XMLObjectBuilder::deregisterDefaultBuilder();\r
69     }\r
70 \r
71     void testUnknown() {\r
72         ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");\r
73         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
74         TS_ASSERT(doc!=NULL);\r
75 \r
76         string buf1;\r
77         XMLHelper::serialize(doc->getDocumentElement(), buf1);\r
78 \r
79         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
80         TS_ASSERT(b!=NULL);\r
81 \r
82         auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document\r
83         TS_ASSERT(xmlObject.get()!=NULL);\r
84 \r
85         auto_ptr<XMLObject> clonedObject(xmlObject->clone());\r
86         TS_ASSERT(clonedObject.get()!=NULL);\r
87 \r
88         DOMElement* rootElement=clonedObject->marshall();\r
89         TS_ASSERT(rootElement!=NULL);\r
90 \r
91         // should reuse DOM\r
92         TS_ASSERT(rootElement==clonedObject->marshall());\r
93 \r
94         string buf2;\r
95         XMLHelper::serialize(rootElement, buf2);\r
96         TS_ASSERT_EQUALS(buf1,buf2);\r
97     }\r
98 \r
99     void testUnknownWithDocChange() {\r
100         ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");\r
101         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
102         TS_ASSERT(doc!=NULL);\r
103 \r
104         string buf1;\r
105         XMLHelper::serialize(doc->getDocumentElement(), buf1);\r
106 \r
107         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
108         TS_ASSERT(b!=NULL);\r
109 \r
110         auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document\r
111         TS_ASSERT(xmlObject.get()!=NULL);\r
112 \r
113         DOMDocument* newDoc=XMLToolingConfig::getConfig().getParser().newDocument();\r
114         DOMElement* rootElement=xmlObject->marshall(newDoc);\r
115         TS_ASSERT(rootElement!=NULL);\r
116 \r
117         string buf2;\r
118         XMLHelper::serialize(rootElement, buf2);\r
119         TS_ASSERT_EQUALS(buf1,buf2);\r
120 \r
121         newDoc->release();\r
122     }\r
123 };\r
124 \r