Remove some tracing, fix string compares.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / UnmarshallingTest.h
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "XMLObjectBaseTestCase.h"\r
18 \r
19 #include <fstream>\r
20 #include <xercesc/util/XMLUniDefs.hpp>\r
21 \r
22 const XMLCh SimpleXMLObject::NAMESPACE[] = {\r
23     chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash,\r
24     chLatin_w, chLatin_w, chLatin_w, chPeriod,\r
25     chLatin_e, chLatin_x, chLatin_a, chLatin_m, chLatin_p, chLatin_l, chLatin_e, chPeriod,\r
26     chLatin_o, chLatin_r, chLatin_g, chForwardSlash,\r
27     chLatin_t, chLatin_e, chLatin_s, chLatin_t,\r
28     chLatin_O, chLatin_b, chLatin_j, chLatin_e, chLatin_c, chLatin_t, chLatin_s, chNull\r
29 };\r
30 \r
31 const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = {\r
32     chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull\r
33 };\r
34 \r
35 const XMLCh SimpleXMLObject::LOCAL_NAME[] = {\r
36     chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,\r
37     chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull\r
38 };\r
39 \r
40 const XMLCh SimpleXMLObject::DERIVED_NAME[] = {\r
41     chLatin_D, chLatin_e, chLatin_r, chLatin_i, chLatin_v, chLatin_e, chLatin_d,\r
42     chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull\r
43 };\r
44 \r
45 const XMLCh SimpleXMLObject::TYPE_NAME[] = {\r
46     chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,\r
47     chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, \r
48     chLatin_T, chLatin_y, chLatin_p, chLatin_e, chNull\r
49 };\r
50 \r
51 const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = {\r
52     chLatin_I, chLatin_d, chNull\r
53 };\r
54 \r
55 class UnmarshallingTest : public CxxTest::TestSuite {\r
56 public:\r
57     void setUp() {\r
58         QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);\r
59         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);\r
60         XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());\r
61         XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());\r
62     }\r
63 \r
64     void tearDown() {\r
65         QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);\r
66         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);\r
67         XMLObjectBuilder::deregisterBuilder(qname);\r
68         XMLObjectBuilder::deregisterBuilder(qtype);\r
69     }\r
70 \r
71     void testUnmarshallingWithAttributes() {\r
72         string path=data_path + "SimpleXMLObjectWithAttribute.xml";\r
73         ifstream fs(path.c_str());\r
74         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
75         TS_ASSERT(doc!=NULL);\r
76 \r
77         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
78         TS_ASSERT(b!=NULL);\r
79 \r
80         auto_ptr<SimpleXMLObject> sxObject(\r
81             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
82             );\r
83         TS_ASSERT(sxObject.get()!=NULL);\r
84 \r
85         auto_ptr_XMLCh expected("Firefly");\r
86         TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId()));\r
87     }\r
88 \r
89     void testUnmarshallingWithElementContent() {\r
90         string path=data_path + "SimpleXMLObjectWithContent.xml";\r
91         ifstream fs(path.c_str());\r
92         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
93         TS_ASSERT(doc!=NULL);\r
94 \r
95         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
96         TS_ASSERT(b!=NULL);\r
97 \r
98         auto_ptr<SimpleXMLObject> sxObject(\r
99             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
100             );\r
101         TS_ASSERT(sxObject.get()!=NULL);\r
102 \r
103         auto_ptr_XMLCh expected("Sample Content");\r
104         TSM_ASSERT("Element content was not expected value", XMLString::equals(expected.get(), sxObject->getValue()));\r
105     }\r
106 \r
107     void testUnmarshallingWithChildElements() {\r
108         string path=data_path + "SimpleXMLObjectWithChildren.xml";\r
109         ifstream fs(path.c_str());\r
110         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
111         TS_ASSERT(doc!=NULL);\r
112 \r
113         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
114         TS_ASSERT(b!=NULL);\r
115 \r
116         auto_ptr<SimpleXMLObject> sxObject(\r
117             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
118             );\r
119         TS_ASSERT(sxObject.get()!=NULL);\r
120 \r
121         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();\r
122         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());\r
123         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);\r
124         TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));\r
125     }\r
126 \r
127     void testUnmarshallingWithClone() {\r
128         string path=data_path + "SimpleXMLObjectWithChildren.xml";\r
129         ifstream fs(path.c_str());\r
130         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
131         TS_ASSERT(doc!=NULL);\r
132 \r
133         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
134         TS_ASSERT(b!=NULL);\r
135 \r
136         auto_ptr<SimpleXMLObject> sxObject(\r
137             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
138             );\r
139         TS_ASSERT(sxObject.get()!=NULL);\r
140 \r
141         sxObject->releaseThisAndChildrenDOM();\r
142         auto_ptr<SimpleXMLObject> clonedObject(sxObject->clone());\r
143 \r
144         VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects();\r
145         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());\r
146         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);\r
147         TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));\r
148     }\r
149 \r
150     void testUnmarshallingWithUnknownChild() {\r
151         string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";\r
152         ifstream fs(path.c_str());\r
153         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
154         TS_ASSERT(doc!=NULL);\r
155 \r
156         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
157         TS_ASSERT(b!=NULL);\r
158 \r
159         TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);\r
160         doc->release();\r
161     }\r
162 };\r