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