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