From 2eecfd1fd6e3bf556f42d7f4e334063f3d530986 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Thu, 2 Mar 2006 21:46:28 +0000 Subject: [PATCH] Marshalling tests. --- xmltoolingtest/MarshallingTest.h | 98 ++++++++++++++++++++++++++++++++++ xmltoolingtest/XMLObjectBaseTestCase.h | 3 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 xmltoolingtest/MarshallingTest.h diff --git a/xmltoolingtest/MarshallingTest.h b/xmltoolingtest/MarshallingTest.h new file mode 100644 index 0000000..d8294b1 --- /dev/null +++ b/xmltoolingtest/MarshallingTest.h @@ -0,0 +1,98 @@ +/* + * Copyright 2001-2005 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "XMLObjectBaseTestCase.h" + +#include + +class MarshallingTest : public CxxTest::TestSuite { + QName m_qname; +public: + MarshallingTest() : m_qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME) {} + + void setUp() { + XMLObjectBuilder::registerBuilder(m_qname, new SimpleXMLObjectBuilder()); + Marshaller::registerMarshaller(m_qname, new SimpleXMLObjectMarshaller()); + Unmarshaller::registerUnmarshaller(m_qname, new SimpleXMLObjectUnmarshaller()); + } + + void tearDown() { + XMLObjectBuilder::deregisterBuilder(m_qname); + Marshaller::deregisterMarshaller(m_qname); + Unmarshaller::deregisterUnmarshaller(m_qname); + } + + void testMarshallingWithAttributes() { + TS_TRACE("testMarshallingWithAttributes"); + + auto_ptr_XMLCh expected("Firefly"); + auto_ptr sxObject(dynamic_cast(XMLObjectBuilder::getBuilder(m_qname)->buildObject())); + TS_ASSERT(sxObject.get()!=NULL); + sxObject->setId(expected.get()); + + DOMElement* rootElement = Marshaller::getMarshaller(sxObject.get())->marshall(sxObject.get()); + + string path=data_path + "SimpleXMLObjectWithAttribute.xml"; + ifstream fs(path.c_str()); + DOMDocument* doc=nonvalidatingPool->parse(fs); + TS_ASSERT(doc!=NULL); + + TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement())); + doc->release(); + } + + void testMarshallingWithElementContent() { + TS_TRACE("testMarshallingWithElementContent"); + + auto_ptr_XMLCh expected("Sample Content"); + auto_ptr sxObject(dynamic_cast(XMLObjectBuilder::getBuilder(m_qname)->buildObject())); + TS_ASSERT(sxObject.get()!=NULL); + sxObject->setValue(expected.get()); + + DOMElement* rootElement = Marshaller::getMarshaller(sxObject.get())->marshall(sxObject.get()); + + string path=data_path + "SimpleXMLObjectWithContent.xml"; + ifstream fs(path.c_str()); + DOMDocument* doc=nonvalidatingPool->parse(fs); + TS_ASSERT(doc!=NULL); + + TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement())); + doc->release(); + } + + void testMarshallingWithChildElements() { + TS_TRACE("testMarshallingWithChildElements"); + + const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(m_qname); + TS_ASSERT(b!=NULL); + + auto_ptr sxObject(dynamic_cast(b->buildObject())); + TS_ASSERT(sxObject.get()!=NULL); + sxObject->getSimpleXMLObjects().push_back(dynamic_cast(b->buildObject())); + sxObject->getSimpleXMLObjects().push_back(dynamic_cast(b->buildObject())); + + DOMElement* rootElement = Marshaller::getMarshaller(sxObject.get())->marshall(sxObject.get()); + + string path=data_path + "SimpleXMLObjectWithChildren.xml"; + ifstream fs(path.c_str()); + DOMDocument* doc=nonvalidatingPool->parse(fs); + TS_ASSERT(doc!=NULL); + + TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement())); + doc->release(); + } + +}; diff --git a/xmltoolingtest/XMLObjectBaseTestCase.h b/xmltoolingtest/XMLObjectBaseTestCase.h index ff3f3a8..a6e999d 100644 --- a/xmltoolingtest/XMLObjectBaseTestCase.h +++ b/xmltoolingtest/XMLObjectBaseTestCase.h @@ -59,7 +59,8 @@ public: const XMLCh* getValue() const { return m_value; } void setValue(const XMLCh* value) { m_value=prepareForAssignment(m_value,value); } - const list& getSimpleXMLObjects() const { return m_children; } + // TODO: Leave non-const, but wrap STL container to intercept adds. + list& getSimpleXMLObjects() { return m_children; } bool hasChildren() const { return !m_children.empty(); } size_t getOrderedChildren(vector& children) const { -- 2.1.4