3687ea3edd5c664aeb6f4f8afbf644140f7762c9
[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         TS_TRACE("testUnmarshallingWithAttributes");\r
73 \r
74         string path=data_path + "SimpleXMLObjectWithAttribute.xml";\r
75         ifstream fs(path.c_str());\r
76         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
77         TS_ASSERT(doc!=NULL);\r
78 \r
79         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
80         TS_ASSERT(b!=NULL);\r
81 \r
82         auto_ptr<SimpleXMLObject> sxObject(\r
83             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
84             );\r
85         TS_ASSERT(sxObject.get()!=NULL);\r
86 \r
87         auto_ptr_XMLCh expected("Firefly");\r
88         TSM_ASSERT_SAME_DATA("ID was not expected value", expected.get(), sxObject->getId(), XMLString::stringLen(expected.get()));\r
89     }\r
90 \r
91     void testUnmarshallingWithElementContent() {\r
92         TS_TRACE("testUnmarshallingWithElementContent");\r
93 \r
94         string path=data_path + "SimpleXMLObjectWithContent.xml";\r
95         ifstream fs(path.c_str());\r
96         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
97         TS_ASSERT(doc!=NULL);\r
98 \r
99         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
100         TS_ASSERT(b!=NULL);\r
101 \r
102         auto_ptr<SimpleXMLObject> sxObject(\r
103             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
104             );\r
105         TS_ASSERT(sxObject.get()!=NULL);\r
106 \r
107         auto_ptr_XMLCh expected("Sample Content");\r
108         TSM_ASSERT_SAME_DATA("Element content was not expected value", expected.get(), sxObject->getValue(), XMLString::stringLen(expected.get()));\r
109     }\r
110 \r
111     void testUnmarshallingWithChildElements() {\r
112         TS_TRACE("testUnmarshallingWithChildElements");\r
113 \r
114         string path=data_path + "SimpleXMLObjectWithChildren.xml";\r
115         ifstream fs(path.c_str());\r
116         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
117         TS_ASSERT(doc!=NULL);\r
118 \r
119         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
120         TS_ASSERT(b!=NULL);\r
121 \r
122         auto_ptr<SimpleXMLObject> sxObject(\r
123             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
124             );\r
125         TS_ASSERT(sxObject.get()!=NULL);\r
126 \r
127         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();\r
128         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());\r
129         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);\r
130         TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));\r
131     }\r
132 \r
133     void testUnmarshallingWithClone() {\r
134         TS_TRACE("testUnmarshallingWithClone");\r
135 \r
136         string path=data_path + "SimpleXMLObjectWithChildren.xml";\r
137         ifstream fs(path.c_str());\r
138         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
139         TS_ASSERT(doc!=NULL);\r
140 \r
141         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
142         TS_ASSERT(b!=NULL);\r
143 \r
144         auto_ptr<SimpleXMLObject> sxObject(\r
145             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
146             );\r
147         TS_ASSERT(sxObject.get()!=NULL);\r
148 \r
149         sxObject->releaseThisAndChildrenDOM();\r
150         auto_ptr<SimpleXMLObject> clonedObject(sxObject->clone());\r
151 \r
152         VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects();\r
153         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());\r
154         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);\r
155         TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));\r
156     }\r
157 \r
158     void testUnmarshallingWithUnknownChild() {\r
159         TS_TRACE("testUnmarshallingWithUnknownChild");\r
160 \r
161         string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";\r
162         ifstream fs(path.c_str());\r
163         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
164         TS_ASSERT(doc!=NULL);\r
165 \r
166         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
167         TS_ASSERT(b!=NULL);\r
168 \r
169         TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);\r
170         doc->release();\r
171     }\r
172 };\r