Marshalling tests.
authorScott Cantor <cantor.2@osu.edu>
Thu, 2 Mar 2006 21:46:28 +0000 (21:46 +0000)
committerScott Cantor <cantor.2@osu.edu>
Thu, 2 Mar 2006 21:46:28 +0000 (21:46 +0000)
xmltoolingtest/MarshallingTest.h [new file with mode: 0644]
xmltoolingtest/XMLObjectBaseTestCase.h

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