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 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
25 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
26 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
27 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
31 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
32 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
33 XMLObjectBuilder::deregisterBuilder(qname);
34 XMLObjectBuilder::deregisterBuilder(qtype);
37 void testMarshallingWithAttributes() {
38 auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
39 TS_ASSERT(sxObject.get()!=NULL);
40 auto_ptr_XMLCh expected("Firefly");
41 sxObject->setId(expected.get());
43 DOMElement* rootElement = sxObject->marshall();
45 string path=data_path + "SimpleXMLObjectWithAttribute.xml";
46 ifstream fs(path.c_str());
47 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
50 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
54 void testMarshallingWithElementContent() {
55 auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
56 TS_ASSERT(sxObject.get()!=NULL);
57 auto_ptr_XMLCh expected("Sample Content");
58 sxObject->setValue(expected.get());
60 DOMElement* rootElement = sxObject->marshall();
62 string path=data_path + "SimpleXMLObjectWithContent.xml";
63 ifstream fs(path.c_str());
64 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
67 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
71 void testMarshallingWithChildElements() {
72 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
73 const SimpleXMLObjectBuilder* b=dynamic_cast<const SimpleXMLObjectBuilder*>(XMLObjectBuilder::getBuilder(qname));
76 auto_ptr<SimpleXMLObject> sxObject(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
77 TS_ASSERT(sxObject.get()!=NULL);
78 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
79 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
80 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
81 kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
83 // Test some collection stuff
84 auto_ptr_XMLCh foo("Foo");
85 auto_ptr_XMLCh bar("Bar");
86 auto_ptr_XMLCh baz("Baz");
87 kids.begin()->setId(foo.get());
88 kids.at(2)->setValue(bar.get());
89 kids.erase(kids.begin()+1);
90 TS_ASSERT(XMLString::equals(kids.back()->getValue(), bar.get()));
92 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
94 dynamic_cast<SimpleXMLObject*>(
95 b->buildObject(SimpleXMLObject::NAMESPACE,SimpleXMLObject::DERIVED_NAME,SimpleXMLObject::NAMESPACE_PREFIX,&qtype)
98 kids.back()->setValue(baz.get());
100 DOMElement* rootElement = sxObject->marshall();
102 string path=data_path + "SimpleXMLObjectWithChildren.xml";
103 ifstream fs(path.c_str());
104 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
105 TS_ASSERT(doc!=NULL);
107 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));