2 * Copyright 2001-2005 Internet2
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 #include "XMLObjectBaseTestCase.h"
\r
20 #include <xercesc/util/XMLUniDefs.hpp>
\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
31 const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = {
\r
32 chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull
\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
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
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
51 const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = {
\r
52 chLatin_I, chLatin_d, chNull
\r
55 class UnmarshallingTest : public CxxTest::TestSuite {
\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
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
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
77 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
80 auto_ptr<SimpleXMLObject> sxObject(
\r
81 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
83 TS_ASSERT(sxObject.get()!=NULL);
\r
85 auto_ptr_XMLCh expected("Firefly");
\r
86 TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId()));
\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
95 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
98 auto_ptr<SimpleXMLObject> sxObject(
\r
99 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
101 TS_ASSERT(sxObject.get()!=NULL);
\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
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
113 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
114 TS_ASSERT(b!=NULL);
\r
116 auto_ptr<SimpleXMLObject> sxObject(
\r
117 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
119 TS_ASSERT(sxObject.get()!=NULL);
\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
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
133 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
134 TS_ASSERT(b!=NULL);
\r
136 auto_ptr<SimpleXMLObject> sxObject(
\r
137 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
139 TS_ASSERT(sxObject.get()!=NULL);
\r
141 sxObject->releaseThisAndChildrenDOM();
\r
142 auto_ptr<SimpleXMLObject> clonedObject(sxObject->clone());
\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
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
156 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
157 TS_ASSERT(b!=NULL);
\r
159 TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);
\r