Set fourth file version digit to signify rebuild.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / NonVisibleNamespaceTest.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 #include "XMLObjectBaseTestCase.h"
22
23 #include <fstream>
24 #include <xercesc/util/XMLUniDefs.hpp>
25
26 class NonVisibleNamespaceTest : public CxxTest::TestSuite {
27 public:
28     void setUp() {
29         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
30         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
31         XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
32         XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
33     }
34
35     void tearDown() {
36         xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
37         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
38         XMLObjectBuilder::deregisterBuilder(qname);
39         XMLObjectBuilder::deregisterBuilder(qtype);
40     }
41
42     void testNamespacesAfterBuilding() {
43         xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
44         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(qtype);
45         TS_ASSERT(b!=nullptr);
46         auto_ptr<SimpleXMLObject> sxObject(
47             dynamic_cast<SimpleXMLObject*>(b->buildObject(SimpleXMLObject::NAMESPACE, SimpleXMLObject::LOCAL_NAME, nullptr, &qtype))
48             );
49         TS_ASSERT(sxObject.get()!=nullptr);
50         static_cast<AttributeExtensibleXMLObject*>(sxObject.get())->setAttribute(
51             xmltooling::QName(nullptr, "attr1"), xmltooling::QName("http://www.example.org/testObjects/ext", "Value1", "test2")
52             );
53
54         static const XMLCh TEST2_PREFIX[] = { chLatin_t, chLatin_e, chLatin_s, chLatin_t, chDigit_2, chNull };
55
56         const set<Namespace>& namespaces = sxObject->getNamespaces();
57         bool cond1=false, cond2=false, cond3 = false;
58         for (set<Namespace>::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) {
59             if (XMLString::equals(ns->getNamespacePrefix(), SimpleXMLObject::NAMESPACE_PREFIX)) {
60                 TSM_ASSERT("'test' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
61                 cond1 = true;
62             }
63             else if (XMLString::equals(ns->getNamespacePrefix(), TEST2_PREFIX)) {
64                 TSM_ASSERT("'test2' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
65                 cond2 = true;
66             }
67             else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) {
68                 TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed);
69                 cond3 = true;
70             }
71         }
72         TSM_ASSERT("'test' namespace was missing.", cond1);
73         TSM_ASSERT("'test2' namespace was missing.", cond2);
74         TSM_ASSERT("Default namespace was missing.", cond3);
75     }
76
77     void testNamespacesAfterUnmarshalling() {
78         string path=data_path + "SimpleXMLObjectWithNonVisible.xml";
79         ifstream fs(path.c_str());
80         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
81         TS_ASSERT(doc!=nullptr);
82
83         const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
84         TS_ASSERT(b!=nullptr);
85
86         auto_ptr<SimpleXMLObject> sxObject(
87             dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
88             );
89         TS_ASSERT(sxObject.get()!=nullptr);
90
91         const set<Namespace>& namespaces = sxObject->getNamespaces();
92         bool cond1=false, cond2=false;
93         for (set<Namespace>::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) {
94             if (XMLString::equals(ns->getNamespacePrefix(), SimpleXMLObject::NAMESPACE_PREFIX)) {
95                 TSM_ASSERT("'test' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
96                 cond1 = true;
97             }
98             else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) {
99                 TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed);
100                 cond2 = true;
101             }
102         }
103         TSM_ASSERT("Default or 'test' namespace missing.", cond1 && cond2);
104         for (set<Namespace>::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) {
105             static const XMLCh TEST2_PREFIX[] = { chLatin_t, chLatin_e, chLatin_s, chLatin_t, chDigit_2, chNull };
106             TSM_ASSERT("'test2' namespace was noted during unmarshalling", !XMLString::equals(ns->getNamespacePrefix(), TEST2_PREFIX));
107         }
108     }
109 };