Add overloaded assertEquals() for testing XMLCh* string equality.
[shibboleth/cpp-opensaml.git] / samltest / internal.h
1 /*
2  *  Copyright 2001-2006 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 <cxxtest/TestSuite.h>
18
19 #include <fstream>
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>
25
26 using namespace opensaml;
27 using namespace xmltooling;
28 using namespace std;
29
30 extern string data_path;
31
32 class SAMLObjectBaseTestCase
33 {
34 protected:
35     /** Location of file containing a single element with NO optional attributes */
36     string singleElementFile;
37
38     /** Location of file containing a single element with all optional attributes */
39     string singleElementOptionalAttributesFile;
40
41     /** Location of file containing a single element with child elements */
42     string childElementsFile;
43
44     /** The expected result of a marshalled single element with no optional attributes */
45     DOMDocument* expectedDOM;
46
47     /** The expected result of a marshalled single element with all optional attributes */
48     DOMDocument* expectedOptionalAttributesDOM;
49
50     /** The expected result of a marshalled single element with child elements */
51     DOMDocument* expectedChildElementsDOM;
52
53     /**
54      * Unmarshalls an element file into its SAML XMLObject.
55      * 
56      * @return the SAML XMLObject from the file
57      */
58     XMLObject* unmarshallElement(string elementFile) {
59         try {
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);
65         }
66         catch (XMLToolingException& e) {
67             TS_TRACE(typeid(e).name());
68             TS_TRACE(e.what());
69             throw;
70         }
71     }
72
73     void assertEquals(const char* failMessage, DOMDocument* expectedDOM, XMLObject* xmlObject) {
74         DOMElement* generatedDOM = xmlObject->marshall();
75         if (!generatedDOM->isEqualNode(expectedDOM->getDocumentElement())) {
76             string buf;
77             XMLHelper::serialize(generatedDOM, buf);
78             TS_TRACE(buf.c_str());
79             buf.erase();
80             XMLHelper::serialize(expectedDOM->getDocumentElement(), buf);
81             TS_TRACE(buf.c_str());
82             TSM_ASSERT(failMessage, false);
83         }
84     }
85
86     void assertEquals(DOMDocument* expectedDOM, XMLObject* xmlObject) {
87         assertEquals("Marshalled DOM was not the same as the expected DOM", expectedDOM, xmlObject);
88         delete xmlObject;
89     }
90
91     void assertEquals(const char* failMessage, const XMLCh* expectedString, const XMLCh* testString) {
92         char* buf = NULL;
93         if (!XMLString::equals(expectedString, testString)) {
94             buf = XMLString::transcode(testString);
95             TS_TRACE(buf);
96             XMLString::release(&buf);
97             buf = XMLString::transcode(expectedString);
98             TS_TRACE(buf);
99             XMLString::release(&buf);
100             TSM_ASSERT(failMessage, false);
101         }
102     }
103
104 public:
105     void setUp() {
106         ParserPool& p=XMLToolingConfig::getConfig().getParser();
107         if (!singleElementFile.empty()) {
108             ifstream fs(singleElementFile.c_str());
109             expectedDOM = p.parse(fs);
110         }
111
112         if (!singleElementOptionalAttributesFile.empty()) {
113             ifstream fs(singleElementOptionalAttributesFile.c_str());
114             expectedOptionalAttributesDOM = p.parse(fs);
115         }
116
117         if (!childElementsFile.empty()) {
118             ifstream fs(childElementsFile.c_str());
119             expectedChildElementsDOM = p.parse(fs);
120         }
121     }
122     
123     void tearDown() {
124         if (expectedDOM) expectedDOM->release();
125         if (expectedOptionalAttributesDOM) expectedOptionalAttributesDOM->release();
126         if (expectedChildElementsDOM) expectedChildElementsDOM->release();
127     }
128 };