https://issues.shibboleth.net/jira/browse/CPPXT-77
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / ComplexXMLObjectTest.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 <xercesc/util/XMLUniDefs.hpp>
21
22 class ComplexXMLObjectTest : public CxxTest::TestSuite {
23 public:
24     ComplexXMLObjectTest() {}
25
26     void setUp() {
27         XMLObjectBuilder::registerDefaultBuilder(new AnyElementBuilder());
28     }
29
30     void tearDown() {
31         XMLObjectBuilder::deregisterDefaultBuilder();
32     }
33
34     void testComplexUnmarshalling() {
35         string path=data_path + "ComplexXMLObject.xml";
36         ifstream fs(path.c_str());
37         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
38         TS_ASSERT(doc!=nullptr);
39         XercesJanitor<DOMDocument> janitor(doc);
40
41         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
42         TS_ASSERT(b!=nullptr);
43
44         auto_ptr<ElementProxy> wcObject(
45             dynamic_cast<ElementProxy*>(b->buildFromDocument(doc, false))
46             );
47         TS_ASSERT(wcObject.get()!=nullptr);
48         
49         VectorOf(XMLObject) kids=wcObject->getUnknownXMLObjects();
50         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 2, kids.size());
51         
52         ElementProxy* wc1=dynamic_cast<ElementProxy*>(*(++kids.begin()));
53         ElementProxy* wc2=dynamic_cast<ElementProxy*>(*(++(wc1->getUnknownXMLObjects().begin())));
54         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, wc2->getUnknownXMLObjects().size());
55
56         static const XMLCh html[] = {chLatin_h, chLatin_t, chLatin_m, chLatin_l, chNull};
57         static const XMLCh div[] = {chLatin_d, chLatin_i, chLatin_v, chNull};
58         auto_ptr_XMLCh htmlns("http://www.w3.org/1999/xhtml");
59         xmltooling::QName q(htmlns.get(),div,html);
60         TSM_ASSERT_EQUALS("Element QName unexpected", wc2->getUnknownXMLObjects()[2]->getElementQName(),q);
61
62         DOMElement* rebuilt = wcObject->marshall(XMLToolingConfig::getConfig().getParser().newDocument());
63         wcObject->setDocument(rebuilt->getOwnerDocument());
64         TS_ASSERT(rebuilt->isEqualNode(doc->getDocumentElement()));
65     }
66
67 };