File: UnmarshallingTest.h

package info (click to toggle)
xmltooling 3.2.0-3%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,936 kB
  • sloc: cpp: 23,231; sh: 4,244; xml: 1,437; makefile: 357; ansic: 27
file content (172 lines) | stat: -rw-r--r-- 7,215 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * Licensed to the University Corporation for Advanced Internet
 * Development, Inc. (UCAID) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */

#include "XMLObjectBaseTestCase.h"

#include <fstream>
#include <xercesc/util/XMLUniDefs.hpp>

const XMLCh SimpleXMLObject::NAMESPACE[] = {
    chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash,
    chLatin_w, chLatin_w, chLatin_w, chPeriod,
    chLatin_e, chLatin_x, chLatin_a, chLatin_m, chLatin_p, chLatin_l, chLatin_e, chPeriod,
    chLatin_o, chLatin_r, chLatin_g, chForwardSlash,
    chLatin_t, chLatin_e, chLatin_s, chLatin_t,
    chLatin_O, chLatin_b, chLatin_j, chLatin_e, chLatin_c, chLatin_t, chLatin_s, chNull
};

const XMLCh SimpleXMLObject::NAMESPACE_PREFIX[] = {
    chLatin_t, chLatin_e, chLatin_s, chLatin_t, chNull
};

const XMLCh SimpleXMLObject::LOCAL_NAME[] = {
    chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,
    chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull
};

const XMLCh SimpleXMLObject::DERIVED_NAME[] = {
    chLatin_D, chLatin_e, chLatin_r, chLatin_i, chLatin_v, chLatin_e, chLatin_d,
    chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull
};

const XMLCh SimpleXMLObject::TYPE_NAME[] = {
    chLatin_S, chLatin_i, chLatin_m, chLatin_p, chLatin_l, chLatin_e,
    chLatin_E, chLatin_l, chLatin_e, chLatin_m, chLatin_e, chLatin_n, chLatin_t, 
    chLatin_T, chLatin_y, chLatin_p, chLatin_e, chNull
};

const XMLCh SimpleXMLObject::ID_ATTRIB_NAME[] = {
    chLatin_I, chLatin_d, chNull
};

class UnmarshallingTest : public CxxTest::TestSuite {
public:
    void setUp() {
        xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
        xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
        XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
        XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
    }

    void tearDown() {
        xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
        xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
        XMLObjectBuilder::deregisterBuilder(qname);
        XMLObjectBuilder::deregisterBuilder(qtype);
    }

    void testUnmarshallingWithDTD() {
        string path=data_path + "DTD.xml";
        ifstream fs(path.c_str());
        TS_ASSERT_THROWS(XMLToolingConfig::getConfig().getParser().parse(fs),XMLParserException);
    }

    void testUnmarshallingWithAttributes() {
        string path=data_path + "SimpleXMLObjectWithAttribute.xml";
        ifstream fs(path.c_str());
        DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
        TS_ASSERT(doc!=nullptr);

        const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
        TS_ASSERT(b!=nullptr);

        scoped_ptr<SimpleXMLObject> sxObject(
            dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
            );
        TS_ASSERT(sxObject.get()!=nullptr);

        auto_ptr_XMLCh expected("Firefly");
        TSM_ASSERT("ID was not expected value", XMLString::equals(expected.get(), sxObject->getId()));
    }

    void testUnmarshallingWithElementContent() {
        string path=data_path + "SimpleXMLObjectWithContent.xml";
        ifstream fs(path.c_str());
        DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
        TS_ASSERT(doc!=nullptr);

        const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
        TS_ASSERT(b!=nullptr);

        scoped_ptr<SimpleXMLObject> sxObject(
            dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
            );
        TS_ASSERT(sxObject.get()!=nullptr);

        auto_ptr_XMLCh expected("Sample Content");
        TSM_ASSERT("Element content was not expected value", XMLString::equals(expected.get(), sxObject->getValue()));
    }

    void testUnmarshallingWithChildElements() {
        string path=data_path + "SimpleXMLObjectWithChildren.xml";
        ifstream fs(path.c_str());
        DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
        TS_ASSERT(doc!=nullptr);

        const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
        TS_ASSERT(b!=nullptr);

        scoped_ptr<SimpleXMLObject> sxObject(
            dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
            );
        TS_ASSERT(sxObject.get()!=nullptr);

        VectorOf(SimpleXMLObject) kids=sxObject->getSimpleXMLObjects();
        TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
        xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
        TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
    }

    void testUnmarshallingWithClone() {
        string path=data_path + "SimpleXMLObjectWithChildren.xml";
        ifstream fs(path.c_str());
        DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
        TS_ASSERT(doc!=nullptr);

        const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
        TS_ASSERT(b!=nullptr);

        scoped_ptr<SimpleXMLObject> sxObject(
            dynamic_cast<SimpleXMLObject*>(b->buildFromDocument(doc))
            );
        TS_ASSERT(sxObject.get()!=nullptr);

        sxObject->releaseThisAndChildrenDOM();
        scoped_ptr<SimpleXMLObject> clonedObject(dynamic_cast<SimpleXMLObject*>(sxObject->clone()));

        VectorOf(SimpleXMLObject) kids=clonedObject->getSimpleXMLObjects();
        TSM_ASSERT_EQUALS("Number of child elements was not expected value", 3, kids.size());
        xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
        TSM_ASSERT_EQUALS("Child's schema type was not expected value", qtype, *(kids.back()->getSchemaType()));
    }

    void testUnmarshallingWithUnknownChild() {
        string path=data_path + "SimpleXMLObjectWithUnknownChild.xml";
        ifstream fs(path.c_str());
        DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);
        TS_ASSERT(doc!=nullptr);

        const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
        TS_ASSERT(b!=nullptr);

        TS_ASSERT_THROWS(b->buildFromDocument(doc),UnmarshallingException);
        doc->release();
    }
};