From: cantor Date: Thu, 2 Mar 2006 21:46:28 +0000 (+0000) Subject: Marshalling tests. X-Git-Tag: 1.4.1~785 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fxmltooling.git;a=commitdiff_plain;h=4fc572c4d189687adb01c1e5d716b59f46639396 Marshalling tests. git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@38 de75baf8-a10c-0410-a50a-987c0e22f00f --- 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 {