2 * Copyright 2001-2005 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "XMLObjectBaseTestCase.h"
20 #include <xercesc/util/XMLUniDefs.hpp>
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
31 const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = {
32 chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull
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
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
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
51 const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = {
52 chLatin_I, chLatin_d, chNull
55 class UnmarshallingTest : public CxxTest::TestSuite {
58 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
59 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
60 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
61 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
65 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
66 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
67 XMLObjectBuilder::deregisterBuilder(qname);
68 XMLObjectBuilder::deregisterBuilder(qtype);
71 void testUnmarshallingWithAttributes() {
72 string path=data_path + "SimpleXMLObjectWithAttribute.xml";
73 ifstream fs(path.c_str());
74 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
77 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
80 auto_ptr<SimpleXMLObject> sxObject(
81 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
83 TS_ASSERT(sxObject.get()!=NULL);
85 auto_ptr_XMLCh expected("Firefly");
86 TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId()));
89 void testUnmarshallingWithElementContent() {
90 string path=data_path + "SimpleXMLObjectWithContent.xml";
91 ifstream fs(path.c_str());
92 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
95 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
98 auto_ptr<SimpleXMLObject> sxObject(
99 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
101 TS_ASSERT(sxObject.get()!=NULL);
103 auto_ptr_XMLCh expected("Sample Content");
104 TSM_ASSERT("Element content was not expected value", XMLString::equals(expected.get(), sxObject->getValue()));
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!=NULL);
113 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
116 auto_ptr<SimpleXMLObject> sxObject(
117 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
119 TS_ASSERT(sxObject.get()!=NULL);
121 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
122 TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
123 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
124 TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
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!=NULL);
133 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
136 auto_ptr<SimpleXMLObject> sxObject(
137 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
139 TS_ASSERT(sxObject.get()!=NULL);
141 sxObject->releaseThisAndChildrenDOM();
142 auto_ptr<SimpleXMLObject> clonedObject(sxObject->clone());
144 VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects();
145 TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
146 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
147 TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
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!=NULL);
156 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
159 TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);