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