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