2 * Copyright 2001-2007 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"
21 class MarshallingTest : public CxxTest::TestSuite {
24 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
25 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
26 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
27 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
31 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
32 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
33 XMLObjectBuilder::deregisterBuilder(qname);
34 XMLObjectBuilder::deregisterBuilder(qtype);
37 void testMarshallingWithAttributes() {
38 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
39 auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
40 TS_ASSERT(sxObject.get()!=NULL);
41 auto_ptr_XMLCh expected("Firefly");
42 sxObject->setId(expected.get());
44 DOMElement* rootElement = sxObject->marshall();
46 string path=data_path + "SimpleXMLObjectWithAttribute.xml";
47 ifstream fs(path.c_str());
48 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
51 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
55 void testMarshallingWithElementContent() {
56 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
57 auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
58 TS_ASSERT(sxObject.get()!=NULL);
59 auto_ptr_XMLCh expected("Sample Content");
60 sxObject->setValue(expected.get());
62 DOMElement* rootElement = sxObject->marshall();
64 string path=data_path + "SimpleXMLObjectWithContent.xml";
65 ifstream fs(path.c_str());
66 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
70 XMLHelper::serialize(rootElement, buf);
73 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
77 void testMarshallingWithChildElements() {
78 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
79 const SimpleXMLObjectBuilder* b=dynamic_cast<const SimpleXMLObjectBuilder*>(XMLObjectBuilder::getBuilder(qname));
82 auto_ptr<SimpleXMLObject> sxObject(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
83 TS_ASSERT(sxObject.get()!=NULL);
84 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
85 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
86 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
87 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
89 // Test some collection stuff
90 auto_ptr_XMLCh foo("Foo");
91 auto_ptr_XMLCh bar("Bar");
92 auto_ptr_XMLCh baz("Baz");
93 kids.begin()->setId(foo.get());
94 kids.at(2)->setValue(bar.get());
95 kids.erase(kids.begin()+1);
96 TS_ASSERT(XMLString::equals(kids.back()->getValue(), bar.get()));
98 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
100 dynamic_cast<SimpleXMLObject*>(
101 b->buildObject(SimpleXMLObject::NAMESPACE,SimpleXMLObject::DERIVED_NAME,SimpleXMLObject::NAMESPACE_PREFIX,&qtype)
104 kids.back()->setValue(baz.get());
106 DOMElement* rootElement = sxObject->marshall();
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 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));