Move credential usage enum to Credential class.
[shibboleth/xmltooling.git] / xmltoolingtest / MarshallingTest.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "XMLObjectBaseTestCase.h"
18
19 #include <fstream>
20
21 class MarshallingTest : public CxxTest::TestSuite {
22 public:
23     void setUp() {
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());
28     }
29
30     void tearDown() {
31         QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
32         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
33         XMLObjectBuilder::deregisterBuilder(qname);
34         XMLObjectBuilder::deregisterBuilder(qtype);
35     }
36
37     void testMarshallingWithAttributes() {
38         QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
39         auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
40         TS_ASSERT(sxObject.get()!=NULL);
41         auto_ptr_XMLCh expected("Firefly");
42         sxObject->setId(expected.get());
43         
44         DOMElement* rootElement = sxObject->marshall();
45
46         string path=data_path + "SimpleXMLObjectWithAttribute.xml";
47         ifstream fs(path.c_str());
48         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
49         TS_ASSERT(doc!=NULL);
50
51         TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
52         doc->release();
53     }
54
55     void testMarshallingWithElementContent() {
56         QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
57         auto_ptr<SimpleXMLObject> sxObject(SimpleXMLObjectBuilder::buildSimpleXMLObject());
58         TS_ASSERT(sxObject.get()!=NULL);
59         auto_ptr_XMLCh expected("Sample Content");
60         sxObject->setValue(expected.get());
61         
62         DOMElement* rootElement = sxObject->marshall();
63
64         string path=data_path + "SimpleXMLObjectWithContent.xml";
65         ifstream fs(path.c_str());
66         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
67         TS_ASSERT(doc!=NULL);
68
69         string buf;
70         XMLHelper::serialize(rootElement, buf);
71         cout << buf;
72         
73         TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
74         doc->release();
75     }
76
77     void testMarshallingWithChildElements() {
78         QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
79         const SimpleXMLObjectBuilder* b=dynamic_cast<const SimpleXMLObjectBuilder*>(XMLObjectBuilder::getBuilder(qname));
80         TS_ASSERT(b!=NULL);
81         
82         auto_ptr<SimpleXMLObject> sxObject(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
83         TS_ASSERT(sxObject.get()!=NULL);
84         VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
85         kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
86         kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
87         kids.push_back(dynamic_cast<SimpleXMLObject*>(b->buildObject()));
88         
89         // Test some collection stuff
90         auto_ptr_XMLCh foo("Foo");
91         auto_ptr_XMLCh bar("Bar");
92         auto_ptr_XMLCh baz("Baz");
93         kids.begin()->setId(foo.get());
94         kids.at(2)->setValue(bar.get());
95         kids.erase(kids.begin()+1);
96         TS_ASSERT(XMLString::equals(kids.back()->getValue(), bar.get()));
97         
98         QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
99         kids.push_back(
100             dynamic_cast<SimpleXMLObject*>(
101                 b->buildObject(SimpleXMLObject::NAMESPACE,SimpleXMLObject::DERIVED_NAME,SimpleXMLObject::NAMESPACE_PREFIX,&qtype)
102                 )
103             );
104         kids.back()->setValue(baz.get());
105         
106         DOMElement* rootElement = sxObject->marshall();
107
108         string path=data_path + "SimpleXMLObjectWithChildren.xml";
109         ifstream fs(path.c_str());
110         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
111         TS_ASSERT(doc!=NULL);
112
113         TS_ASSERT(rootElement->isEqualNode(doc->getDocumentElement()));
114         doc->release();
115     }
116
117 };