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"
25 class MarshallingTest : public CxxTest::TestSuite {
28 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
29 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
30 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
31 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
35 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
36 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
37 XMLObjectBuilder::deregisterBuilder(qname);
38 XMLObjectBuilder::deregisterBuilder(qtype);
41 void testMarshallingWithAttributes() {
42 auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
43 TS_ASSERT(sxObject.get()!=nullptr);
44 auto_ptr_XMLCh expected("Firefly");
45 sxObject->setId(expected.get());
47 DOMElement* rootElement = sxObject->marshall();
49 string path=data_path + "SimpleXMLObjectWithAttribute.xml";
50 ifstream fs(path.c_str());
51 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
52 TS_ASSERT(doc!=nullptr);
54 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
58 void testMarshallingWithElementContent() {
59 auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
60 TS_ASSERT(sxObject.get()!=nullptr);
61 auto_ptr_XMLCh expected("Sample Content");
62 sxObject->setValue(expected.get());
64 DOMElement* rootElement = sxObject->marshall();
66 string path=data_path + "SimpleXMLObjectWithContent.xml";
67 ifstream fs(path.c_str());
68 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
69 TS_ASSERT(doc!=nullptr);
71 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
75 void testMarshallingWithChildElements() {
76 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
77 const SimpleXMLObjectBuilder* b=dynamic_cast<const SimpleXMLObjectBuilder*>(XMLObjectBuilder::getBuilder(qname));
78 TS_ASSERT(b!=nullptr);
80 auto_ptr<SimpleXMLObject> sxObject(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
81 TS_ASSERT(sxObject.get()!=nullptr);
82 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
83 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
84 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
85 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
87 // Test some collection stuff
88 auto_ptr_XMLCh foo("Foo");
89 auto_ptr_XMLCh bar("Bar");
90 auto_ptr_XMLCh baz("Baz");
91 kids.begin()->setId(foo.get());
92 kids.at(2)->setValue(bar.get());
93 kids.erase(kids.begin()+1);
94 TS_ASSERT(XMLString::equals(kids.back()->getValue(), bar.get()));
96 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
98 dynamic_cast<SimpleXMLObject*>(
99 b->buildObject(SimpleXMLObject::NAMESPACE,SimpleXMLObject::DERIVED_NAME,SimpleXMLObject::NAMESPACE_PREFIX,&qtype)
102 kids.back()->setValue(baz.get());
104 DOMElement* rootElement = sxObject->marshall();
106 string path=data_path + "SimpleXMLObjectWithChildren.xml";
107 ifstream fs(path.c_str());
108 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
109 TS_ASSERT(doc!=nullptr);
111 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));