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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "XMLObjectBaseTestCase.h"
24 #include <cxxtest/GlobalFixture.h>
25 #include <xmltooling/XMLToolingConfig.h>
26 #include <xmltooling/util/ParserPool.h>
28 //#define XMLTOOLINGTEST_LEAKCHECK
30 std::string data_path = "../xmltoolingtest/data/";
32 class ToolingFixture : public CxxTest::GlobalFixture
36 XMLToolingConfig::getConfig().log_config();
38 if (getenv("XMLTOOLINGTEST_DATA"))
39 data_path=std::string(getenv("XMLTOOLINGTEST_DATA")) + "/";
40 XMLToolingConfig::getConfig().catalog_path = data_path + "catalog.xml";
42 if (!XMLToolingConfig::getConfig().init())
44 if (!XMLToolingConfig::getConfig().init()) // should be a no-op
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();
64 //bool setUp() { printf( "</test>" ); return true; }
65 //bool tearDown() { printf( "</test>" ); return true; }
68 static ToolingFixture globalFixture;
70 class GlobalTest : public CxxTest::TestSuite
74 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
78 XMLObjectBuilder::deregisterDefaultBuilder();
82 ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");
83 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
84 TS_ASSERT(doc!=nullptr);
87 XMLHelper::serialize(doc->getDocumentElement(), buf1);
89 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());
90 TS_ASSERT(b!=nullptr);
92 auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document
93 TS_ASSERT(xmlObject.get()!=nullptr);
95 auto_ptr<XMLObject> clonedObject(xmlObject->clone());
96 TS_ASSERT(clonedObject.get()!=nullptr);
98 DOMElement* rootElement=clonedObject->marshall();
99 TS_ASSERT(rootElement!=nullptr);
102 TS_ASSERT(rootElement==clonedObject->marshall());
105 XMLHelper::serialize(rootElement, buf2);
106 TS_ASSERT_EQUALS(buf1,buf2);
109 void testUnknownWithDocChange() {
110 ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");
111 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
112 TS_ASSERT(doc!=nullptr);
115 XMLHelper::serialize(doc->getDocumentElement(), buf1);
117 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(doc->getDocumentElement());
118 TS_ASSERT(b!=nullptr);
120 auto_ptr<XMLObject> xmlObject(b->buildFromDocument(doc)); // bind document
121 TS_ASSERT(xmlObject.get()!=nullptr);
123 DOMDocument* newDoc=XMLToolingConfig::getConfig().getParser().newDocument();
124 DOMElement* rootElement=xmlObject->marshall(newDoc);
125 TS_ASSERT(rootElement!=nullptr);
128 XMLHelper::serialize(rootElement, buf2);
129 TS_ASSERT_EQUALS(buf1,buf2);