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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "XMLObjectBaseTestCase.h"
24 #include <xercesc/util/XMLUniDefs.hpp>
26 class NonVisibleNamespaceTest : public CxxTest::TestSuite {
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());
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);
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))
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")
54 static const XMLCh TEST2_PREFIX[] = { chLatin_t, chLatin_e, chLatin_s, chLatin_t, chDigit_2, chNull };
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);
63 else if (XMLString::equals(ns->getNamespacePrefix(), TEST2_PREFIX)) {
64 TSM_ASSERT("'test2' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed);
67 else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) {
68 TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed);
72 TSM_ASSERT("'test' namespace was missing.", cond1);
73 TSM_ASSERT("'test2' namespace was missing.", cond2);
74 TSM_ASSERT("Default namespace was missing.", cond3);
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);
83 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
84 TS_ASSERT(b!=nullptr);
86 auto_ptr<SimpleXMLObject> sxObject(
87 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
89 TS_ASSERT(sxObject.get()!=nullptr);
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);
98 else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) {
99 TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed);
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));