Set fourth file version digit to signify rebuild.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / UnmarshallingTest.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 #include <xercesc/util/XMLUniDefs.hpp>
25
26 const XMLCh SimpleXMLObject::NAMESPACE[] = {
27     chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash,
28     chLatin_w, chLatin_w, chLatin_w, chPeriod,
29     chLatin_e, chLatin_x, chLatin_a, chLatin_m, chLatin_p, chLatin_l, chLatin_e, chPeriod,
30     chLatin_o, chLatin_r, chLatin_g, chForwardSlash,
31     chLatin_t, chLatin_e, chLatin_s, chLatin_t,
32     chLatin_O, chLatin_b, chLatin_j, chLatin_e, chLatin_c, chLatin_t, chLatin_s, chNull
33 };
34
35 const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = {
36     chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull
37 };
38
39 const XMLCh SimpleXMLObject::LOCAL_NAME[] = {
40     chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,
41     chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull
42 };
43
44 const XMLCh SimpleXMLObject::DERIVED_NAME[] = {
45     chLatin_D, chLatin_e, chLatin_r, chLatin_i, chLatin_v, chLatin_e, chLatin_d,
46     chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull
47 };
48
49 const XMLCh SimpleXMLObject::TYPE_NAME[] = {
50     chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,
51     chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, 
52     chLatin_T, chLatin_y, chLatin_p, chLatin_e, chNull
53 };
54
55 const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = {
56     chLatin_I, chLatin_d, chNull
57 };
58
59 class UnmarshallingTest : public CxxTest::TestSuite {
60 public:
61     void setUp() {
62         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
63         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
64         XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
65         XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
66     }
67
68     void tearDown() {
69         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
70         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
71         XMLObjectBuilder::deregisterBuilder(qname);
72         XMLObjectBuilder::deregisterBuilder(qtype);
73     }
74
75     void testUnmarshallingWithAttributes() {
76         string path=data_path + "SimpleXMLObjectWithAttribute.xml";
77         ifstream fs(path.c_str());
78         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
79         TS_ASSERT(doc!=nullptr);
80
81         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
82         TS_ASSERT(b!=nullptr);
83
84         auto_ptr<SimpleXMLObject> sxObject(
85             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
86             );
87         TS_ASSERT(sxObject.get()!=nullptr);
88
89         auto_ptr_XMLCh expected("Firefly");
90         TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId()));
91     }
92
93     void testUnmarshallingWithElementContent() {
94         string path=data_path + "SimpleXMLObjectWithContent.xml";
95         ifstream fs(path.c_str());
96         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
97         TS_ASSERT(doc!=nullptr);
98
99         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
100         TS_ASSERT(b!=nullptr);
101
102         auto_ptr<SimpleXMLObject> sxObject(
103             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
104             );
105         TS_ASSERT(sxObject.get()!=nullptr);
106
107         auto_ptr_XMLCh expected("Sample Content");
108         TSM_ASSERT("Element content was not expected value", XMLString::equals(expected.get(), sxObject->getValue()));
109     }
110
111     void testUnmarshallingWithChildElements() {
112         string path=data_path + "SimpleXMLObjectWithChildren.xml";
113         ifstream fs(path.c_str());
114         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
115         TS_ASSERT(doc!=nullptr);
116
117         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
118         TS_ASSERT(b!=nullptr);
119
120         auto_ptr<SimpleXMLObject> sxObject(
121             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
122             );
123         TS_ASSERT(sxObject.get()!=nullptr);
124
125         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
126         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
127         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
128         TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
129     }
130
131     void testUnmarshallingWithClone() {
132         string path=data_path + "SimpleXMLObjectWithChildren.xml";
133         ifstream fs(path.c_str());
134         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
135         TS_ASSERT(doc!=nullptr);
136
137         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
138         TS_ASSERT(b!=nullptr);
139
140         auto_ptr<SimpleXMLObject> sxObject(
141             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
142             );
143         TS_ASSERT(sxObject.get()!=nullptr);
144
145         sxObject->releaseThisAndChildrenDOM();
146         auto_ptr<SimpleXMLObject> clonedObject(dynamic_cast<SimpleXMLObject*>(sxObject->clone()));
147
148         VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects();
149         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
150         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
151         TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
152     }
153
154     void testUnmarshallingWithUnknownChild() {
155         string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";
156         ifstream fs(path.c_str());
157         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
158         TS_ASSERT(doc!=nullptr);
159
160         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
161         TS_ASSERT(b!=nullptr);
162
163         TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);
164         doc->release();
165     }
166 };