Simplify storage context mgmt.
[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 \r
34         if (getenv("XMLTOOLINGTEST_DATA"))\r
35             data_path=std::string(getenv("XMLTOOLINGTEST_DATA")) + "/";\r
36         XMLToolingConfig::getConfig().catalog_path = data_path + "catalog.xml";\r
37 \r
38         if (!XMLToolingConfig::getConfig().init())\r
39             return false;\r
40         \r
41         return true;\r
42     }\r
43     bool tearDownWorld() {\r
44         XMLToolingConfig::getConfig().term();\r
45 #if defined(_MSC_VER ) && defined(XMLTOOLINGTEST_LEAKCHECK)\r
46        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );\r
47        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );\r
48        _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );\r
49        _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );\r
50        _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );\r
51        _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );\r
52        _CrtDumpMemoryLeaks();\r
53 #endif\r
54         return true;\r
55     }\r
56     //bool setUp() { printf( "</test>" ); return true; }\r
57     //bool tearDown() { printf( "</test>" ); return true; }\r
58 };\r
59 \r
60 static ToolingFixture globalFixture;\r
61 \r
62 class GlobalTest : public CxxTest::TestSuite\r
63 {\r
64 public:\r
65     void setUp() {\r
66         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());\r
67     }\r
68 \r
69     void tearDown() {\r
70         XMLObjectBuilder::deregisterDefaultBuilder();\r
71     }\r
72 \r
73     void testUnknown() {\r
74         ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");\r
75         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
76         TS_ASSERT(doc!=NULL);\r
77 \r
78         string buf1;\r
79         XMLHelper::serialize(doc->getDocumentElement(), buf1);\r
80 \r
81         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
82         TS_ASSERT(b!=NULL);\r
83 \r
84         auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document\r
85         TS_ASSERT(xmlObject.get()!=NULL);\r
86 \r
87         auto_ptr<XMLObject> clonedObject(xmlObject->clone());\r
88         TS_ASSERT(clonedObject.get()!=NULL);\r
89 \r
90         DOMElement* rootElement=clonedObject->marshall();\r
91         TS_ASSERT(rootElement!=NULL);\r
92 \r
93         // should reuse DOM\r
94         TS_ASSERT(rootElement==clonedObject->marshall());\r
95 \r
96         string buf2;\r
97         XMLHelper::serialize(rootElement, buf2);\r
98         TS_ASSERT_EQUALS(buf1,buf2);\r
99     }\r
100 \r
101     void testUnknownWithDocChange() {\r
102         ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");\r
103         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
104         TS_ASSERT(doc!=NULL);\r
105 \r
106         string buf1;\r
107         XMLHelper::serialize(doc->getDocumentElement(), buf1);\r
108 \r
109         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
110         TS_ASSERT(b!=NULL);\r
111 \r
112         auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document\r
113         TS_ASSERT(xmlObject.get()!=NULL);\r
114 \r
115         DOMDocument* newDoc=XMLToolingConfig::getConfig().getParser().newDocument();\r
116         DOMElement* rootElement=xmlObject->marshall(newDoc);\r
117         TS_ASSERT(rootElement!=NULL);\r
118 \r
119         string buf2;\r
120         XMLHelper::serialize(rootElement, buf2);\r
121         TS_ASSERT_EQUALS(buf1,buf2);\r
122 \r
123         newDoc->release();\r
124     }\r
125 };\r
126 \r