Switch to vector-based API.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / MarshallingTest.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
25 class MarshallingTest : public CxxTest::TestSuite {
26 public:
27     void setUp() {
28         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
29         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
30         XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
31         XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
32     }
33
34     void tearDown() {
35         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
36         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
37         XMLObjectBuilder::deregisterBuilder(qname);
38         XMLObjectBuilder::deregisterBuilder(qtype);
39     }
40
41     void testMarshallingWithAttributes() {
42         auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
43         TS_ASSERT(sxObject.get()!=nullptr);
44         auto_ptr_XMLCh expected("Firefly");
45         sxObject->setId(expected.get());
46         
47         DOMElement* rootElement = sxObject->marshall();
48
49         string path=data_path + "SimpleXMLObjectWithAttribute.xml";
50         ifstream fs(path.c_str());
51         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
52         TS_ASSERT(doc!=nullptr);
53
54         TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
55         doc->release();
56     }
57
58     void testMarshallingWithElementContent() {
59         auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
60         TS_ASSERT(sxObject.get()!=nullptr);
61         auto_ptr_XMLCh expected("Sample Content");
62         sxObject->setValue(expected.get());
63         
64         DOMElement* rootElement = sxObject->marshall();
65
66         string path=data_path + "SimpleXMLObjectWithContent.xml";
67         ifstream fs(path.c_str());
68         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
69         TS_ASSERT(doc!=nullptr);
70
71         TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
72         doc->release();
73     }
74
75     void testMarshallingWithChildElements() {
76         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
77         const SimpleXMLObjectBuilder* b=dynamic_cast<const SimpleXMLObjectBuilder*>(XMLObjectBuilder::getBuilder(qname));
78         TS_ASSERT(b!=nullptr);
79         
80         auto_ptr<SimpleXMLObject> sxObject(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
81         TS_ASSERT(sxObject.get()!=nullptr);
82         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
83         kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
84         kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
85         kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
86         
87         // Test some collection stuff
88         auto_ptr_XMLCh foo("Foo");
89         auto_ptr_XMLCh bar("Bar");
90         auto_ptr_XMLCh baz("Baz");
91         kids.begin()->setId(foo.get());
92         kids.at(2)->setValue(bar.get());
93         kids.erase(kids.begin()+1);
94         TS_ASSERT(XMLString::equals(kids.back()->getValue(), bar.get()));
95         
96         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
97         kids.push_back(
98             dynamic_cast<SimpleXMLObject*>(
99                 b->buildObject(SimpleXMLObject::NAMESPACE,SimpleXMLObject::DERIVED_NAME,SimpleXMLObject::NAMESPACE_PREFIX,&qtype)
100                 )
101             );
102         kids.back()->setValue(baz.get());
103         
104         DOMElement* rootElement = sxObject->marshall();
105
106         string path=data_path + "SimpleXMLObjectWithChildren.xml";
107         ifstream fs(path.c_str());
108         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
109         TS_ASSERT(doc!=nullptr);
110
111         TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
112         doc->release();
113     }
114
115 };