Initial unit test plus fixes
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / UnknownTest.h
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 #include "XMLObjectBaseTestCase.h"\r
18 \r
19 #include <fstream>\r
20 #include <xmltooling/io/Marshaller.h>\r
21 #include <xmltooling/io/Unmarshaller.h>\r
22 \r
23 \r
24 class UnknownTest : public CxxTest::TestSuite {\r
25 public:\r
26 \r
27     void testUnknown() {\r
28         ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");\r
29         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
30         TS_ASSERT(doc!=NULL);\r
31 \r
32         string buf1;\r
33         XMLHelper::serialize(doc->getDocumentElement(), buf1);\r
34 \r
35         const Unmarshaller* u=Unmarshaller::getUnmarshaller(doc->getDocumentElement());\r
36         TS_ASSERT(u!=NULL);\r
37 \r
38         auto_ptr<XMLObject> xmlObject(u->unmarshall(doc->getDocumentElement(),true)); // bind document\r
39         TS_ASSERT(xmlObject.get()!=NULL);\r
40 \r
41         auto_ptr<XMLObject> clonedObject(xmlObject->clone());\r
42         TS_ASSERT(clonedObject.get()!=NULL);\r
43 \r
44         const Marshaller* m=Marshaller::getMarshaller(clonedObject.get());\r
45         TS_ASSERT(m!=NULL);\r
46 \r
47         DOMElement* rootElement=m->marshall(clonedObject.get());\r
48         TS_ASSERT(rootElement!=NULL);\r
49 \r
50         rootElement=m->marshall(clonedObject.get());    // should reuse DOM\r
51         TS_ASSERT(rootElement!=NULL);\r
52 \r
53         string buf2;\r
54         XMLHelper::serialize(rootElement, buf2);\r
55         TS_ASSERT_EQUALS(buf1,buf2);\r
56     }\r
57 \r
58     void testUnknownWithDocChange() {\r
59         ifstream fs("../xmltoolingtest/data/SimpleXMLObjectWithChildren.xml");\r
60         DOMDocument* doc=nonvalidatingPool->parse(fs);\r
61         TS_ASSERT(doc!=NULL);\r
62 \r
63         string buf1;\r
64         XMLHelper::serialize(doc->getDocumentElement(), buf1);\r
65 \r
66         const Unmarshaller* u=Unmarshaller::getUnmarshaller(doc->getDocumentElement());\r
67         TS_ASSERT(u!=NULL);\r
68 \r
69         auto_ptr<XMLObject> xmlObject(u->unmarshall(doc->getDocumentElement(),true)); // bind document\r
70         TS_ASSERT(xmlObject.get()!=NULL);\r
71 \r
72         const Marshaller* m=Marshaller::getMarshaller(xmlObject.get());\r
73         TS_ASSERT(m!=NULL);\r
74 \r
75         DOMDocument* newDoc=nonvalidatingPool->newDocument();\r
76         DOMElement* rootElement=m->marshall(xmlObject.get(), newDoc);\r
77         TS_ASSERT(rootElement!=NULL);\r
78 \r
79         string buf2;\r
80         XMLHelper::serialize(rootElement, buf2);\r
81         TS_ASSERT_EQUALS(buf1,buf2);\r
82 \r
83         newDoc->release();\r
84     }\r
85 };\r