2 * Copyright 2001-2010 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 "XMLObjectBaseTestCase.h"
20 #include <xercesc/util/XMLUniDefs.hpp>
22 class NonVisibleNamespaceTest : public CxxTest::TestSuite {
25 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
26 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
27 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
28 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
32 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
33 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
34 XMLObjectBuilder::deregisterBuilder(qname);
35 XMLObjectBuilder::deregisterBuilder(qtype);
38 void testNamespacesAfterBuilding() {
39 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME,SimpleXMLObject::NAMESPACE_PREFIX);
40 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(qtype);
41 TS_ASSERT(b!=nullptr);
42 auto_ptr<SimpleXMLObject> sxObject(
43 dynamic_cast<SimpleXMLObject*>(b->buildObject(SimpleXMLObject::NAMESPACE, SimpleXMLObject::LOCAL_NAME, nullptr, &qtype))
45 TS_ASSERT(sxObject.get()!=nullptr);
46 static_cast<AttributeExtensibleXMLObject*>(sxObject.get())->setAttribute(
47 xmltooling::QName(nullptr, "attr1"), xmltooling::QName("http://www.example.org/testObjects/ext", "Value1", "test2")
50 static const XMLCh TEST2_PREFIX[] = { chLatin_t, chLatin_e, chLatin_s, chLatin_t, chDigit_2, chNull };
52 const set<Namespace>& namespaces = sxObject->getNamespaces();
53 bool cond1=false, cond2=false, cond3 = false;
54 for (set<Namespace>::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) {
55 if (XMLString::equals(ns->getNamespacePrefix(), SimpleXMLObject::NAMESPACE_PREFIX)) {
56 TSM_ASSERT("'test' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
59 else if (XMLString::equals(ns->getNamespacePrefix(), TEST2_PREFIX)) {
60 TSM_ASSERT("'test2' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
63 else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) {
64 TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed);
68 TSM_ASSERT("'test' namespace was missing.", cond1);
69 TSM_ASSERT("'test2' namespace was missing.", cond2);
70 TSM_ASSERT("Default namespace was missing.", cond3);
73 void testNamespacesAfterUnmarshalling() {
74 string path=data_path + "SimpleXMLObjectWithNonVisible.xml";
75 ifstream fs(path.c_str());
76 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
77 TS_ASSERT(doc!=nullptr);
79 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
80 TS_ASSERT(b!=nullptr);
82 auto_ptr<SimpleXMLObject> sxObject(
83 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
85 TS_ASSERT(sxObject.get()!=nullptr);
87 const set<Namespace>& namespaces = sxObject->getNamespaces();
88 bool cond1=false, cond2=false, cond3=false;
89 for (set<Namespace>::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) {
90 if (XMLString::equals(ns->getNamespacePrefix(), SimpleXMLObject::NAMESPACE_PREFIX)) {
91 TSM_ASSERT("'test' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
94 else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) {
95 TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed);
99 TSM_ASSERT("Default or 'test' namespace missing.", cond1 && cond2);
100 for (set<Namespace>::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) {
101 static const XMLCh TEST2_PREFIX[] = { chLatin_t, chLatin_e, chLatin_s, chLatin_t, chDigit_2, chNull };
102 TSM_ASSERT("'test2' namespace was noted during unmarshalling", !XMLString::equals(ns->getNamespacePrefix(), TEST2_PREFIX));