2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <cxxtest/TestSuite.h>
20 #include <saml/exceptions.h>
21 #include <saml/SAMLConfig.h>
22 #include <saml/util/SAMLConstants.h>
23 #include <xmltooling/XMLObject.h>
24 #include <xmltooling/XMLObjectBuilder.h>
26 using namespace opensaml;
27 using namespace xmltooling;
30 extern string data_path;
32 class SAMLObjectBaseTestCase
35 /** Location of file containing a single element with NO optional attributes */
36 string singleElementFile;
38 /** Location of file containing a single element with all optional attributes */
39 string singleElementOptionalAttributesFile;
41 /** Location of file containing a single element with child elements */
42 string childElementsFile;
44 /** The expected result of a marshalled single element with no optional attributes */
45 DOMDocument* expectedDOM;
47 /** The expected result of a marshalled single element with all optional attributes */
48 DOMDocument* expectedOptionalAttributesDOM;
50 /** The expected result of a marshalled single element with child elements */
51 DOMDocument* expectedChildElementsDOM;
54 * Unmarshalls an element file into its SAML XMLObject.
56 * @return the SAML XMLObject from the file
58 XMLObject* unmarshallElement(string elementFile) {
60 ParserPool& p=XMLToolingConfig::getConfig().getParser();
61 ifstream fs(elementFile.c_str());
62 DOMDocument* doc = p.parse(fs);
63 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
64 return b->buildFromDocument(doc);
66 catch (XMLToolingException& e) {
67 TS_TRACE(typeid(e).name());
73 void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject) {
74 DOMElement* generatedDOM = xmlObject->marshall();
75 if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
77 XMLHelper::serialize(generatedDOM, buf);
78 TS_TRACE(buf.c_str());
80 XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
81 TS_TRACE(buf.c_str());
82 TSM_ASSERT(failMessage, false);
86 void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject) {
87 assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject);
93 ParserPool& p=XMLToolingConfig::getConfig().getParser();
94 if (!singleElementFile.empty()) {
95 ifstream fs(singleElementFile.c_str());
96 expectedDOM = p.parse(fs);
99 if (!singleElementOptionalAttributesFile.empty()) {
100 ifstream fs(singleElementOptionalAttributesFile.c_str());
101 expectedOptionalAttributesDOM = p.parse(fs);
104 if (!childElementsFile.empty()) {
105 ifstream fs(childElementsFile.c_str());
106 expectedChildElementsDOM = p.parse(fs);
111 if (expectedDOM) expectedDOM->release();
112 if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
113 if (expectedChildElementsDOM) expectedChildElementsDOM->release();