2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
21 #include "XMLObjectBaseTestCase.h"
24 #include <xercesc/util/XMLUniDefs.hpp>
26 const XMLCh SimpleXMLObject::NAMESPACE[] = {
27 chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash,
28 chLatin_w, chLatin_w, chLatin_w, chPeriod,
29 chLatin_e, chLatin_x, chLatin_a, chLatin_m, chLatin_p, chLatin_l, chLatin_e, chPeriod,
30 chLatin_o, chLatin_r, chLatin_g, chForwardSlash,
31 chLatin_t, chLatin_e, chLatin_s, chLatin_t,
32 chLatin_O, chLatin_b, chLatin_j, chLatin_e, chLatin_c, chLatin_t, chLatin_s, chNull
35 const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = {
36 chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull
39 const XMLCh SimpleXMLObject::LOCAL_NAME[] = {
40 chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,
41 chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull
44 const XMLCh SimpleXMLObject::DERIVED_NAME[] = {
45 chLatin_D, chLatin_e, chLatin_r, chLatin_i, chLatin_v, chLatin_e, chLatin_d,
46 chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull
49 const XMLCh SimpleXMLObject::TYPE_NAME[] = {
50 chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,
51 chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t,
52 chLatin_T, chLatin_y, chLatin_p, chLatin_e, chNull
55 const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = {
56 chLatin_I, chLatin_d, chNull
59 class UnmarshallingTest : public CxxTest::TestSuite {
62 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
63 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
64 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
65 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
69 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
70 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
71 XMLObjectBuilder::deregisterBuilder(qname);
72 XMLObjectBuilder::deregisterBuilder(qtype);
75 void testUnmarshallingWithAttributes() {
76 string path=data_path + "SimpleXMLObjectWithAttribute.xml";
77 ifstream fs(path.c_str());
78 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
79 TS_ASSERT(doc!=nullptr);
81 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
82 TS_ASSERT(b!=nullptr);
84 auto_ptr<SimpleXMLObject> sxObject(
85 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
87 TS_ASSERT(sxObject.get()!=nullptr);
89 auto_ptr_XMLCh expected("Firefly");
90 TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId()));
93 void testUnmarshallingWithElementContent() {
94 string path=data_path + "SimpleXMLObjectWithContent.xml";
95 ifstream fs(path.c_str());
96 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
97 TS_ASSERT(doc!=nullptr);
99 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
100 TS_ASSERT(b!=nullptr);
102 auto_ptr<SimpleXMLObject> sxObject(
103 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
105 TS_ASSERT(sxObject.get()!=nullptr);
107 auto_ptr_XMLCh expected("Sample Content");
108 TSM_ASSERT("Element content was not expected value", XMLString::equals(expected.get(), sxObject->getValue()));
111 void testUnmarshallingWithChildElements() {
112 string path=data_path + "SimpleXMLObjectWithChildren.xml";
113 ifstream fs(path.c_str());
114 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
115 TS_ASSERT(doc!=nullptr);
117 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
118 TS_ASSERT(b!=nullptr);
120 auto_ptr<SimpleXMLObject> sxObject(
121 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
123 TS_ASSERT(sxObject.get()!=nullptr);
125 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
126 TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
127 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
128 TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
131 void testUnmarshallingWithClone() {
132 string path=data_path + "SimpleXMLObjectWithChildren.xml";
133 ifstream fs(path.c_str());
134 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
135 TS_ASSERT(doc!=nullptr);
137 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
138 TS_ASSERT(b!=nullptr);
140 auto_ptr<SimpleXMLObject> sxObject(
141 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
143 TS_ASSERT(sxObject.get()!=nullptr);
145 sxObject->releaseThisAndChildrenDOM();
146 auto_ptr<SimpleXMLObject> clonedObject(dynamic_cast<SimpleXMLObject*>(sxObject->clone()));
148 VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects();
149 TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
150 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
151 TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
154 void testUnmarshallingWithUnknownChild() {
155 string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";
156 ifstream fs(path.c_str());
157 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
158 TS_ASSERT(doc!=nullptr);
160 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
161 TS_ASSERT(b!=nullptr);
163 TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);