Moved DOM methods up the tree, add copy c'tors, KeyInfo sample
[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     QName m_qname;\r
57     QName m_qtype;\r
58 public:\r
59     UnmarshallingTest() : m_qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME,SimpleXMLObject::NAMESPACE_PREFIX),\r
60         m_qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX) {}\r
61 \r
62     void setUp() {\r
63         XMLObjectBuilder::registerBuilder(m_qname, new SimpleXMLObjectBuilder());\r
64         XMLObjectBuilder::registerBuilder(m_qtype, new SimpleXMLObjectBuilder());\r
65     }\r
66 \r
67     void tearDown() {\r
68         XMLObjectBuilder::deregisterBuilder(m_qname);\r
69         XMLObjectBuilder::deregisterBuilder(m_qtype);\r
70     }\r
71 \r
72     void testUnmarshallingWithAttributes() {\r
73         TS_TRACE("testUnmarshallingWithAttributes");\r
74 \r
75         string path=data_path + "SimpleXMLObjectWithAttribute.xml";\r
76         ifstream fs(path.c_str());\r
77         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
78         TS_ASSERT(doc!=NULL);\r
79 \r
80         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
81         TS_ASSERT(b!=NULL);\r
82 \r
83         auto_ptr<SimpleXMLObject> sxObject(\r
84             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
85             );\r
86         TS_ASSERT(sxObject.get()!=NULL);\r
87 \r
88         auto_ptr_XMLCh expected("Firefly");\r
89         TSM_ASSERT_SAME_DATA("ID was not expected value", expected.get(), sxObject->getId(), XMLString::stringLen(expected.get()));\r
90     }\r
91 \r
92     void testUnmarshallingWithElementContent() {\r
93         TS_TRACE("testUnmarshallingWithElementContent");\r
94 \r
95         string path=data_path + "SimpleXMLObjectWithContent.xml";\r
96         ifstream fs(path.c_str());\r
97         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
98         TS_ASSERT(doc!=NULL);\r
99 \r
100         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
101         TS_ASSERT(b!=NULL);\r
102 \r
103         auto_ptr<SimpleXMLObject> sxObject(\r
104             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
105             );\r
106         TS_ASSERT(sxObject.get()!=NULL);\r
107 \r
108         auto_ptr_XMLCh expected("Sample Content");\r
109         TSM_ASSERT_SAME_DATA("Element content was not expected value", expected.get(), sxObject->getValue(), XMLString::stringLen(expected.get()));\r
110     }\r
111 \r
112     void testUnmarshallingWithChildElements() {\r
113         TS_TRACE("testUnmarshallingWithChildElements");\r
114 \r
115         string path=data_path + "SimpleXMLObjectWithChildren.xml";\r
116         ifstream fs(path.c_str());\r
117         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
118         TS_ASSERT(doc!=NULL);\r
119 \r
120         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
121         TS_ASSERT(b!=NULL);\r
122 \r
123         auto_ptr<SimpleXMLObject> sxObject(\r
124             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))\r
125             );\r
126         TS_ASSERT(sxObject.get()!=NULL);\r
127 \r
128         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();\r
129         TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());\r
130         TSM_ASSERT_EQUALS("Child's schema type was not expected value", m_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=nonvalidatingPool->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         TSM_ASSERT_EQUALS("Child's schema type was not expected value", m_qtype, *(kids.back()->getSchemaType()));\r
155     }\r
156 \r
157     void testUnmarshallingWithUnknownChild() {\r
158         TS_TRACE("testUnmarshallingWithUnknownChild");\r
159 \r
160         string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";\r
161         ifstream fs(path.c_str());\r
162         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
163         TS_ASSERT(doc!=NULL);\r
164 \r
165         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());\r
166         TS_ASSERT(b!=NULL);\r
167 \r
168         TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);\r
169         doc->release();\r
170     }\r
171 };\r