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 TS_TRACE("testUnmarshallingWithAttributes");
\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
79 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
82 auto_ptr<SimpleXMLObject> sxObject(
\r
83 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
85 TS_ASSERT(sxObject.get()!=NULL);
\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
91 void testUnmarshallingWithElementContent() {
\r
92 TS_TRACE("testUnmarshallingWithElementContent");
\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
99 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
100 TS_ASSERT(b!=NULL);
\r
102 auto_ptr<SimpleXMLObject> sxObject(
\r
103 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
105 TS_ASSERT(sxObject.get()!=NULL);
\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
111 void testUnmarshallingWithChildElements() {
\r
112 TS_TRACE("testUnmarshallingWithChildElements");
\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
119 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
120 TS_ASSERT(b!=NULL);
\r
122 auto_ptr<SimpleXMLObject> sxObject(
\r
123 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
125 TS_ASSERT(sxObject.get()!=NULL);
\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
133 void testUnmarshallingWithClone() {
\r
134 TS_TRACE("testUnmarshallingWithClone");
\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
141 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
142 TS_ASSERT(b!=NULL);
\r
144 auto_ptr<SimpleXMLObject> sxObject(
\r
145 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
\r
147 TS_ASSERT(sxObject.get()!=NULL);
\r
149 sxObject->releaseThisAndChildrenDOM();
\r
150 auto_ptr<SimpleXMLObject> clonedObject(sxObject->clone());
\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
158 void testUnmarshallingWithUnknownChild() {
\r
159 TS_TRACE("testUnmarshallingWithUnknownChild");
\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
166 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
\r
167 TS_ASSERT(b!=NULL);
\r
169 TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);
\r