2 * Copyright 2001-2005 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::newSimpleXMLObject());
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::newSimpleXMLObject());
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);
69 TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
73 void testMarshallingWithChildElements() {
74 QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
75 const SimpleXMLObjectBuilder* b=dynamic_cast<const SimpleXMLObjectBuilder*>(XMLObjectBuilder::getBuilder(qname));
78 auto_ptr<SimpleXMLObject> sxObject(b->buildObject());
79 TS_ASSERT(sxObject.get()!=NULL);
80 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
81 kids.push_back(b->buildObject());
82 kids.push_back(b->buildObject());
83 kids.push_back(b->buildObject());
85 // Test some collection stuff
86 auto_ptr_XMLCh foo("Foo");
87 auto_ptr_XMLCh bar("Bar");
88 auto_ptr_XMLCh baz("Baz");
89 kids.begin()->setId(foo.get());
90 kids.at(2)->setValue(bar.get());
91 kids.erase(kids.begin()+1);
92 TS_ASSERT(XMLString::equals(kids.back()->getValue(), bar.get()));
94 QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
96 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()));