2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "XMLObjectBaseTestCase.h"
20 #include <cxxtest/GlobalFixture.h>
21 #include <xmltooling/XMLToolingConfig.h>
22 #include <xmltooling/util/ParserPool.h>
24 //#define XMLTOOLINGTEST_LEAKCHECK
26 std::string data_path = "../xmltoolingtest/data/";
28 class ToolingFixture : public CxxTest::GlobalFixture
32 XMLToolingConfig::getConfig().log_config();
34 if (getenv("XMLTOOLINGTEST_DATA"))
35 data_path=std::string(getenv("XMLTOOLINGTEST_DATA")) + "/";
36 XMLToolingConfig::getConfig().catalog_path = data_path + "catalog.xml";
38 if (!XMLToolingConfig::getConfig().init())
40 if (!XMLToolingConfig::getConfig().init()) // should be a no-op
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();
60 //bool setUp() { printf( "</test>" ); return true; }
61 //bool tearDown() { printf( "</test>" ); return true; }
64 static ToolingFixture globalFixture;
66 class GlobalTest : public CxxTest::TestSuite
70 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
74 XMLObjectBuilder::deregisterDefaultBuilder();
78 ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");
79 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
80 TS_ASSERT(doc!=nullptr);
83 XMLHelper::serialize(doc->getDocumentElement(), buf1);
85 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());
86 TS_ASSERT(b!=nullptr);
88 auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document
89 TS_ASSERT(xmlObject.get()!=nullptr);
91 auto_ptr<XMLObject> clonedObject(xmlObject->clone());
92 TS_ASSERT(clonedObject.get()!=nullptr);
94 DOMElement* rootElement=clonedObject->marshall();
95 TS_ASSERT(rootElement!=nullptr);
98 TS_ASSERT(rootElement==clonedObject->marshall());
101 XMLHelper::serialize(rootElement, buf2);
102 TS_ASSERT_EQUALS(buf1,buf2);
105 void testUnknownWithDocChange() {
106 ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");
107 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
108 TS_ASSERT(doc!=nullptr);
111 XMLHelper::serialize(doc->getDocumentElement(), buf1);
113 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());
114 TS_ASSERT(b!=nullptr);
116 auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document
117 TS_ASSERT(xmlObject.get()!=nullptr);
119 DOMDocument* newDoc=XMLToolingConfig::getConfig().getParser().newDocument();
120 DOMElement* rootElement=xmlObject->marshall(newDoc);
121 TS_ASSERT(rootElement!=nullptr);
124 XMLHelper::serialize(rootElement, buf2);
125 TS_ASSERT_EQUALS(buf1,buf2);