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