Merged marshalling/unmarshalling methods into core interface.
[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::ID_ATTRIB_NAME[] = {\r
41     chLatin_I, chLatin_d, chNull\r
42 };\r
43 \r
44 class UnmarshallingTest : public CxxTest::TestSuite {\r
45     QName m_qname;\r
46 public:\r
47     UnmarshallingTest() : m_qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME) {}\r
48 \r
49     void setUp() {\r
50         XMLObjectBuilder::registerBuilder(m_qname, new SimpleXMLObjectBuilder());\r
51     }\r
52 \r
53     void tearDown() {\r
54         XMLObjectBuilder::deregisterBuilder(m_qname);\r
55     }\r
56 \r
57     void testUnmarshallingWithAttributes() {\r
58         TS_TRACE("testUnmarshallingWithAttributes");\r
59 \r
60         string path=data_path + "SimpleXMLObjectWithAttribute.xml";\r
61         ifstream fs(path.c_str());\r
62         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
63         TS_ASSERT(doc!=NULL);\r
64 \r
65         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
66         TS_ASSERT(b!=NULL);\r
67 \r
68         auto_ptr<SimpleXMLObject> sxObject(\r
69             dynamic_cast<SimpleXMLObject*>(b->buildObject()->unmarshall(doc->getDocumentElement(),true))\r
70             );\r
71         TS_ASSERT(sxObject.get()!=NULL);\r
72 \r
73         auto_ptr_XMLCh expected("Firefly");\r
74         TSM_ASSERT_SAME_DATA("ID was not expected value", expected.get(), sxObject->getId(), XMLString::stringLen(expected.get()));\r
75     }\r
76 \r
77     void testUnmarshallingWithElementContent() {\r
78         TS_TRACE("testUnmarshallingWithElementContent");\r
79 \r
80         string path=data_path + "SimpleXMLObjectWithContent.xml";\r
81         ifstream fs(path.c_str());\r
82         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
83         TS_ASSERT(doc!=NULL);\r
84 \r
85         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
86         TS_ASSERT(b!=NULL);\r
87 \r
88         auto_ptr<SimpleXMLObject> sxObject(\r
89             dynamic_cast<SimpleXMLObject*>(b->buildObject()->unmarshall(doc->getDocumentElement(),true))\r
90             );\r
91         TS_ASSERT(sxObject.get()!=NULL);\r
92 \r
93         auto_ptr_XMLCh expected("Sample Content");\r
94         TSM_ASSERT_SAME_DATA("Element content was not expected value", expected.get(), sxObject->getValue(), XMLString::stringLen(expected.get()));\r
95     }\r
96 \r
97     void testUnmarshallingWithChildElements() {\r
98         TS_TRACE("testUnmarshallingWithChildElements");\r
99 \r
100         string path=data_path + "SimpleXMLObjectWithChildren.xml";\r
101         ifstream fs(path.c_str());\r
102         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
103         TS_ASSERT(doc!=NULL);\r
104 \r
105         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
106         TS_ASSERT(b!=NULL);\r
107 \r
108         auto_ptr<SimpleXMLObject> sxObject(\r
109             dynamic_cast<SimpleXMLObject*>(b->buildObject()->unmarshall(doc->getDocumentElement(),true))\r
110             );\r
111         TS_ASSERT(sxObject.get()!=NULL);\r
112 \r
113         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();\r
114         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 2, kids.size());\r
115     }\r
116 \r
117     void testUnmarshallingWithUnknownChild() {\r
118         TS_TRACE("testUnmarshallingWithUnknownChild");\r
119 \r
120         string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";\r
121         ifstream fs(path.c_str());\r
122         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
123         TS_ASSERT(doc!=NULL);\r
124 \r
125         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
126         TS_ASSERT(b!=NULL);\r
127 \r
128         auto_ptr<XMLObject> sxObject(b->buildObject());\r
129         TS_ASSERT_THROWS(sxObject->unmarshall(doc->getDocumentElement(),true),UnmarshallingException);\r
130         doc->release();\r
131     }\r
132 };\r