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 const XMLCh SimpleXMLObject::NAMESPACE[] = { 27 chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash, 28 chLatin_w, chLatin_w, chLatin_w, chPeriod, 29 chLatin_e, chLatin_x, chLatin_a, chLatin_m, chLatin_p, chLatin_l, chLatin_e, chPeriod, 30 chLatin_o, chLatin_r, chLatin_g, chForwardSlash, 31 chLatin_t, chLatin_e, chLatin_s, chLatin_t, 32 chLatin_O, chLatin_b, chLatin_j, chLatin_e, chLatin_c, chLatin_t, chLatin_s, chNull 33 }; 34 35 const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = { 36 chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull 37 }; 38 39 const XMLCh SimpleXMLObject::LOCAL_NAME[] = { 40 chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e, 41 chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull 42 }; 43 44 const XMLCh SimpleXMLObject::DERIVED_NAME[] = { 45 chLatin_D, chLatin_e, chLatin_r, chLatin_i, chLatin_v, chLatin_e, chLatin_d, 46 chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull 47 }; 48 49 const XMLCh SimpleXMLObject::TYPE_NAME[] = { 50 chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e, 51 chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, 52 chLatin_T, chLatin_y, chLatin_p, chLatin_e, chNull 53 }; 54 55 const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = { 56 chLatin_I, chLatin_d, chNull 57 }; 58 59 class UnmarshallingTest : public CxxTest::TestSuite { 60 public: setUp()61 void setUp() { 62 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME); 63 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME); 64 XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder()); 65 XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder()); 66 } 67 tearDown()68 void tearDown() { 69 xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME); 70 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME); 71 XMLObjectBuilder::deregisterBuilder(qname); 72 XMLObjectBuilder::deregisterBuilder(qtype); 73 } 74 testUnmarshallingWithDTD()75 void testUnmarshallingWithDTD() { 76 string path=data_path + "DTD.xml"; 77 ifstream fs(path.c_str()); 78 TS_ASSERT_THROWS(XMLToolingConfig::getConfig().getParser().parse(fs),XMLParserException); 79 } 80 testUnmarshallingWithAttributes()81 void testUnmarshallingWithAttributes() { 82 string path=data_path + "SimpleXMLObjectWithAttribute.xml"; 83 ifstream fs(path.c_str()); 84 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs); 85 TS_ASSERT(doc!=nullptr); 86 87 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement()); 88 TS_ASSERT(b!=nullptr); 89 90 scoped_ptr<SimpleXMLObject> sxObject( 91 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc)) 92 ); 93 TS_ASSERT(sxObject.get()!=nullptr); 94 95 auto_ptr_XMLCh expected("Firefly"); 96 TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId())); 97 } 98 testUnmarshallingWithElementContent()99 void testUnmarshallingWithElementContent() { 100 string path=data_path + "SimpleXMLObjectWithContent.xml"; 101 ifstream fs(path.c_str()); 102 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs); 103 TS_ASSERT(doc!=nullptr); 104 105 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement()); 106 TS_ASSERT(b!=nullptr); 107 108 scoped_ptr<SimpleXMLObject> sxObject( 109 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc)) 110 ); 111 TS_ASSERT(sxObject.get()!=nullptr); 112 113 auto_ptr_XMLCh expected("Sample Content"); 114 TSM_ASSERT("Element content was not expected value", XMLString::equals(expected.get(), sxObject->getValue())); 115 } 116 testUnmarshallingWithChildElements()117 void testUnmarshallingWithChildElements() { 118 string path=data_path + "SimpleXMLObjectWithChildren.xml"; 119 ifstream fs(path.c_str()); 120 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs); 121 TS_ASSERT(doc!=nullptr); 122 123 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement()); 124 TS_ASSERT(b!=nullptr); 125 126 scoped_ptr<SimpleXMLObject> sxObject( 127 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc)) 128 ); 129 TS_ASSERT(sxObject.get()!=nullptr); 130 131 VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects(); 132 TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size()); 133 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME); 134 TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType())); 135 } 136 testUnmarshallingWithClone()137 void testUnmarshallingWithClone() { 138 string path=data_path + "SimpleXMLObjectWithChildren.xml"; 139 ifstream fs(path.c_str()); 140 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs); 141 TS_ASSERT(doc!=nullptr); 142 143 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement()); 144 TS_ASSERT(b!=nullptr); 145 146 scoped_ptr<SimpleXMLObject> sxObject( 147 dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc)) 148 ); 149 TS_ASSERT(sxObject.get()!=nullptr); 150 151 sxObject->releaseThisAndChildrenDOM(); 152 scoped_ptr<SimpleXMLObject> clonedObject(dynamic_cast<SimpleXMLObject*>(sxObject->clone())); 153 154 VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects(); 155 TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size()); 156 xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME); 157 TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType())); 158 } 159 testUnmarshallingWithUnknownChild()160 void testUnmarshallingWithUnknownChild() { 161 string path=data_path + "SimpleXMLObjectWithUnknownChild.xml"; 162 ifstream fs(path.c_str()); 163 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs); 164 TS_ASSERT(doc!=nullptr); 165 166 const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement()); 167 TS_ASSERT(b!=nullptr); 168 169 TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException); 170 doc->release(); 171 } 172 }; 173