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