1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | * $Id: InitTermTest.cpp 833057 2009-11-05 15:25:10Z borisk $ |
---|
20 | */ |
---|
21 | |
---|
22 | // --------------------------------------------------------------------------- |
---|
23 | // This program tests the XMLPlatformUtils::Initialize/Terminate() pair |
---|
24 | // by calling the pair a number of times. |
---|
25 | // --------------------------------------------------------------------------- |
---|
26 | |
---|
27 | // --------------------------------------------------------------------------- |
---|
28 | // Includes |
---|
29 | // --------------------------------------------------------------------------- |
---|
30 | #include <icxercesc/util/PlatformUtils.hpp> |
---|
31 | #include <xercesc/sax/SAXException.hpp> |
---|
32 | #include <xercesc/sax/SAXParseException.hpp> |
---|
33 | |
---|
34 | #include <xercesc/parsers/XercesDOMParser.hpp> |
---|
35 | #include <icxercesc/parsers/SAXParser.hpp> |
---|
36 | #include <xercesc/sax2/SAX2XMLReader.hpp> |
---|
37 | #include <xercesc/sax2/XMLReaderFactory.hpp> |
---|
38 | #include <xercesc/util/OutOfMemoryException.hpp> |
---|
39 | #include <xercesc/dom/DOMException.hpp> |
---|
40 | #include "InitTermTest.hpp" |
---|
41 | |
---|
42 | #include <string.h> |
---|
43 | #include <stdlib.h> |
---|
44 | |
---|
45 | #if defined(XERCES_NEW_IOSTREAMS) |
---|
46 | #include <fstream> |
---|
47 | #else |
---|
48 | #include <fstream.h> |
---|
49 | #endif |
---|
50 | #include <limits.h> |
---|
51 | |
---|
52 | |
---|
53 | // --------------------------------------------------------------------------- |
---|
54 | // Local Enum to switch theState |
---|
55 | // --------------------------------------------------------------------------- |
---|
56 | enum Teststate { |
---|
57 | Once, |
---|
58 | Multiple, |
---|
59 | UnEven, |
---|
60 | Limit, |
---|
61 | ExceedLimit |
---|
62 | }; |
---|
63 | |
---|
64 | // --------------------------------------------------------------------------- |
---|
65 | // Declare functions |
---|
66 | // --------------------------------------------------------------------------- |
---|
67 | int TestInit4DOM(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState); |
---|
68 | int TestInit4SAX(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState); |
---|
69 | int TestInit4SAX2(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState); |
---|
70 | |
---|
71 | // --------------------------------------------------------------------------- |
---|
72 | // Define macro |
---|
73 | // --------------------------------------------------------------------------- |
---|
74 | #define TESTINITPRE \ |
---|
75 | long times = 1; \ |
---|
76 | switch (theState) { \ |
---|
77 | case Multiple: \ |
---|
78 | case UnEven: \ |
---|
79 | times = 100; \ |
---|
80 | break; \ |
---|
81 | case Limit: \ |
---|
82 | case ExceedLimit: \ |
---|
83 | times = LONG_MAX; \ |
---|
84 | break; \ |
---|
85 | case Once: \ |
---|
86 | default: \ |
---|
87 | times = 1; \ |
---|
88 | } \ |
---|
89 | long i = 0; \ |
---|
90 | for (i = 0; i < times; i++) { \ |
---|
91 | try \ |
---|
92 | { \ |
---|
93 | XMLPlatformUtils::Initialize(); \ |
---|
94 | } \ |
---|
95 | \ |
---|
96 | catch (const XMLException& toCatch) \ |
---|
97 | { \ |
---|
98 | XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n" \ |
---|
99 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; \ |
---|
100 | return 1; \ |
---|
101 | } \ |
---|
102 | } \ |
---|
103 | \ |
---|
104 | if (theState == ExceedLimit) { \ |
---|
105 | try \ |
---|
106 | { \ |
---|
107 | XMLPlatformUtils::Initialize(); \ |
---|
108 | } \ |
---|
109 | \ |
---|
110 | catch (const XMLException& toCatch) \ |
---|
111 | { \ |
---|
112 | XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n" \ |
---|
113 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl; \ |
---|
114 | return 1; \ |
---|
115 | } \ |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | #define TESTINITPOST \ |
---|
120 | InitTermTestErrorHandler errorHandler; \ |
---|
121 | parser->setErrorHandler(&errorHandler); \ |
---|
122 | bool errorOccurred = false; \ |
---|
123 | \ |
---|
124 | errorHandler.resetErrors(); \ |
---|
125 | \ |
---|
126 | try \ |
---|
127 | { \ |
---|
128 | parser->parse(xmlFile); \ |
---|
129 | } \ |
---|
130 | catch (const OutOfMemoryException&) \ |
---|
131 | { \ |
---|
132 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; \ |
---|
133 | errorOccurred = true; \ |
---|
134 | } \ |
---|
135 | catch (const XMLException& toCatch) \ |
---|
136 | { \ |
---|
137 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n" \ |
---|
138 | << "Exception message is: \n" \ |
---|
139 | << StrX(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl; \ |
---|
140 | errorOccurred = true; \ |
---|
141 | } \ |
---|
142 | catch (const DOMException& toCatch) \ |
---|
143 | { \ |
---|
144 | XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '" << xmlFile \ |
---|
145 | << "\nDOMException code is: \n" \ |
---|
146 | << toCatch.code << "\n" << XERCES_STD_QUALIFIER endl; \ |
---|
147 | errorOccurred = true; \ |
---|
148 | } \ |
---|
149 | catch (...) \ |
---|
150 | { \ |
---|
151 | XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" \ |
---|
152 | << xmlFile << "'\n"; \ |
---|
153 | errorOccurred = true; \ |
---|
154 | } \ |
---|
155 | \ |
---|
156 | if (errorHandler.getSawErrors()) \ |
---|
157 | { \ |
---|
158 | XERCES_STD_QUALIFIER cout << "\nErrors occurred, no output available\n" << XERCES_STD_QUALIFIER endl;\ |
---|
159 | errorOccurred = true; \ |
---|
160 | } \ |
---|
161 | \ |
---|
162 | delete parser; \ |
---|
163 | \ |
---|
164 | for (i = 0; i < times; i++) { \ |
---|
165 | XMLPlatformUtils::Terminate(); \ |
---|
166 | } \ |
---|
167 | \ |
---|
168 | if (theState == ExceedLimit || theState == UnEven) { \ |
---|
169 | XMLPlatformUtils::Terminate(); \ |
---|
170 | } \ |
---|
171 | \ |
---|
172 | if (errorOccurred) \ |
---|
173 | return 4; \ |
---|
174 | else \ |
---|
175 | return 0; |
---|
176 | |
---|
177 | // --------------------------------------------------------------------------- |
---|
178 | // DOM Parser |
---|
179 | // --------------------------------------------------------------------------- |
---|
180 | int TestInit4DOM(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState) |
---|
181 | { |
---|
182 | TESTINITPRE; |
---|
183 | XercesDOMParser* parser = new XercesDOMParser; |
---|
184 | parser->setDoNamespaces(gDoNamespaces); |
---|
185 | parser->setDoSchema(gDoSchema); |
---|
186 | parser->setHandleMultipleImports (true); |
---|
187 | parser->setValidationSchemaFullChecking(gSchemaFullChecking); |
---|
188 | TESTINITPOST; |
---|
189 | } |
---|
190 | |
---|
191 | // --------------------------------------------------------------------------- |
---|
192 | // SAX Parser |
---|
193 | // --------------------------------------------------------------------------- |
---|
194 | int TestInit4SAX(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState) |
---|
195 | { |
---|
196 | TESTINITPRE; |
---|
197 | SAXParser* parser = new SAXParser; |
---|
198 | parser->setDoNamespaces(gDoNamespaces); |
---|
199 | parser->setDoSchema(gDoSchema); |
---|
200 | parser->setHandleMultipleImports (true); |
---|
201 | parser->setValidationSchemaFullChecking(gSchemaFullChecking); |
---|
202 | TESTINITPOST; |
---|
203 | } |
---|
204 | |
---|
205 | // --------------------------------------------------------------------------- |
---|
206 | // SAX2 XML Reader |
---|
207 | // --------------------------------------------------------------------------- |
---|
208 | int TestInit4SAX2(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState) |
---|
209 | { |
---|
210 | TESTINITPRE; |
---|
211 | SAX2XMLReader* parser = XMLReaderFactory::createXMLReader(); |
---|
212 | |
---|
213 | XMLCh* doNamespaceFeature = XMLString::transcode("http://xml.org/sax/features/namespaces"); |
---|
214 | parser->setFeature(doNamespaceFeature, gDoNamespaces); |
---|
215 | |
---|
216 | XMLCh* doSchemaFeature = XMLString::transcode("http://apache.org/xml/features/validation/schema"); |
---|
217 | parser->setFeature(doSchemaFeature, gDoSchema); |
---|
218 | |
---|
219 | XMLCh* handleMultipleImportsFeature = XMLString::transcode("http://apache.org/xml/features/validation/schema/handle-multiple-imports"); |
---|
220 | parser->setFeature(handleMultipleImportsFeature, true); |
---|
221 | |
---|
222 | XMLCh* fullSchemaCheckFeature = XMLString::transcode("http://apache.org/xml/features/validation/schema-full-checking"); |
---|
223 | parser->setFeature(fullSchemaCheckFeature, gSchemaFullChecking); |
---|
224 | |
---|
225 | XMLString::release(&doNamespaceFeature); |
---|
226 | XMLString::release(&doSchemaFeature); |
---|
227 | XMLString::release(&handleMultipleImportsFeature); |
---|
228 | XMLString::release(&fullSchemaCheckFeature); |
---|
229 | |
---|
230 | TESTINITPOST; |
---|
231 | } |
---|
232 | |
---|
233 | // --------------------------------------------------------------------------- |
---|
234 | // |
---|
235 | // Usage() |
---|
236 | // |
---|
237 | // --------------------------------------------------------------------------- |
---|
238 | void usage() |
---|
239 | { |
---|
240 | XERCES_STD_QUALIFIER cout << "\nUsage:\n" |
---|
241 | " InitTermTest [options] <XML file>\n\n" |
---|
242 | "This program tests the XMLPlatformUtils::Initialize()/Terminate()\n" |
---|
243 | "pair by calling it a number of times.\n" |
---|
244 | "Options:\n" |
---|
245 | " -n Enable namespace processing. Default is off.\n" |
---|
246 | " -s Enable schema processing. Default is off.\n" |
---|
247 | " -f Enable full schema constraint checking. Defaults to off.\n" |
---|
248 | " -? Show this help.\n" |
---|
249 | << XERCES_STD_QUALIFIER endl; |
---|
250 | } |
---|
251 | |
---|
252 | // --------------------------------------------------------------------------- |
---|
253 | // Main |
---|
254 | // --------------------------------------------------------------------------- |
---|
255 | int main(int argC, char* argV[]) { |
---|
256 | |
---|
257 | // --------------------------------------------------------------------------- |
---|
258 | // Local data |
---|
259 | // |
---|
260 | // gDoNamespaces |
---|
261 | // Indicates whether namespace processing should be done. |
---|
262 | // |
---|
263 | // gDoSchema |
---|
264 | // Indicates whether schema processing should be done. |
---|
265 | // |
---|
266 | // gSchemaFullChecking |
---|
267 | // Indicates whether full schema constraint checking should be done. |
---|
268 | // |
---|
269 | // --------------------------------------------------------------------------- |
---|
270 | bool gDoNamespaces = false; |
---|
271 | bool gDoSchema = false; |
---|
272 | bool gSchemaFullChecking = false; |
---|
273 | |
---|
274 | // Check command line and extract arguments. |
---|
275 | if (argC < 2) |
---|
276 | { |
---|
277 | usage(); |
---|
278 | return 1; |
---|
279 | } |
---|
280 | |
---|
281 | // See if non validating dom parser configuration is requested. |
---|
282 | int parmInd; |
---|
283 | for (parmInd = 1; parmInd < argC; parmInd++) |
---|
284 | { |
---|
285 | // Break out on first parm not starting with a dash |
---|
286 | if (argV[parmInd][0] != '-') |
---|
287 | break; |
---|
288 | |
---|
289 | // Watch for special case help request |
---|
290 | if (!strcmp(argV[parmInd], "-?")) |
---|
291 | { |
---|
292 | usage(); |
---|
293 | return 2; |
---|
294 | } |
---|
295 | else if (!strcmp(argV[parmInd], "-n") |
---|
296 | || !strcmp(argV[parmInd], "-N")) |
---|
297 | { |
---|
298 | gDoNamespaces = true; |
---|
299 | } |
---|
300 | else if (!strcmp(argV[parmInd], "-s") |
---|
301 | || !strcmp(argV[parmInd], "-S")) |
---|
302 | { |
---|
303 | gDoSchema = true; |
---|
304 | } |
---|
305 | else if (!strcmp(argV[parmInd], "-f") |
---|
306 | || !strcmp(argV[parmInd], "-F")) |
---|
307 | { |
---|
308 | gSchemaFullChecking = true; |
---|
309 | } |
---|
310 | else |
---|
311 | { |
---|
312 | XERCES_STD_QUALIFIER cerr << "Unknown option '" << argV[parmInd] |
---|
313 | << "', ignoring it.\n" << XERCES_STD_QUALIFIER endl; |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | // |
---|
318 | // And now we have to have only one parameter left and it must be |
---|
319 | // the file name. |
---|
320 | // |
---|
321 | if (parmInd + 1 != argC) |
---|
322 | { |
---|
323 | usage(); |
---|
324 | return 1; |
---|
325 | } |
---|
326 | |
---|
327 | char* xmlFile = argV[parmInd]; |
---|
328 | bool error = false; |
---|
329 | |
---|
330 | // |
---|
331 | // Calling Init/Term pair a number of times |
---|
332 | // |
---|
333 | unsigned int i = 0; |
---|
334 | for (i=0; i < 100; i++) { |
---|
335 | if (error) { |
---|
336 | XERCES_STD_QUALIFIER cout << "Test Failed" << XERCES_STD_QUALIFIER endl; |
---|
337 | return 4; |
---|
338 | } |
---|
339 | |
---|
340 | if (TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Once)) |
---|
341 | error = true; |
---|
342 | if (TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Once)) |
---|
343 | error = true; |
---|
344 | if (TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Once)) |
---|
345 | error = true; |
---|
346 | } |
---|
347 | |
---|
348 | if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Multiple)) |
---|
349 | error = true; |
---|
350 | if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Multiple)) |
---|
351 | error = true; |
---|
352 | if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Multiple)) |
---|
353 | error = true; |
---|
354 | /* |
---|
355 | * The following Limit test is a stress test that can run a long time |
---|
356 | * Commented out for regular sanity test |
---|
357 | */ |
---|
358 | /* |
---|
359 | if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Limit)) |
---|
360 | error = true; |
---|
361 | if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Limit)) |
---|
362 | error = true; |
---|
363 | if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Limit)) |
---|
364 | error = true; |
---|
365 | |
---|
366 | if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, ExceedLimit)) |
---|
367 | error = true; |
---|
368 | if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, ExceedLimit)) |
---|
369 | error = true; |
---|
370 | if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, ExceedLimit)) |
---|
371 | error = true; |
---|
372 | */ |
---|
373 | |
---|
374 | if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, UnEven)) |
---|
375 | error = true; |
---|
376 | if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, UnEven)) |
---|
377 | error = true; |
---|
378 | if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, UnEven)) |
---|
379 | error = true; |
---|
380 | |
---|
381 | if (error) { |
---|
382 | XERCES_STD_QUALIFIER cout << "Test Failed" << XERCES_STD_QUALIFIER endl; |
---|
383 | return 4; |
---|
384 | } |
---|
385 | |
---|
386 | XERCES_STD_QUALIFIER cout << "Test Run Successfully" << XERCES_STD_QUALIFIER endl; |
---|
387 | return 0; |
---|
388 | } |
---|
389 | |
---|
390 | // --------------------------------------------------------------------------- |
---|
391 | // InitTermTestErrorHandler |
---|
392 | // --------------------------------------------------------------------------- |
---|
393 | InitTermTestErrorHandler::InitTermTestErrorHandler() : |
---|
394 | |
---|
395 | fSawErrors(false) |
---|
396 | { |
---|
397 | } |
---|
398 | |
---|
399 | InitTermTestErrorHandler::~InitTermTestErrorHandler() |
---|
400 | { |
---|
401 | } |
---|
402 | |
---|
403 | void InitTermTestErrorHandler::error(const SAXParseException& e) |
---|
404 | { |
---|
405 | fSawErrors = true; |
---|
406 | XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId()) |
---|
407 | << ", line " << e.getLineNumber() |
---|
408 | << ", char " << e.getColumnNumber() |
---|
409 | << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl; |
---|
410 | } |
---|
411 | |
---|
412 | void InitTermTestErrorHandler::fatalError(const SAXParseException& e) |
---|
413 | { |
---|
414 | fSawErrors = true; |
---|
415 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId()) |
---|
416 | << ", line " << e.getLineNumber() |
---|
417 | << ", char " << e.getColumnNumber() |
---|
418 | << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl; |
---|
419 | } |
---|
420 | |
---|
421 | void InitTermTestErrorHandler::warning(const SAXParseException& e) |
---|
422 | { |
---|
423 | XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId()) |
---|
424 | << ", line " << e.getLineNumber() |
---|
425 | << ", char " << e.getColumnNumber() |
---|
426 | << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl; |
---|
427 | } |
---|
428 | |
---|
429 | void InitTermTestErrorHandler::resetErrors() |
---|
430 | { |
---|
431 | fSawErrors = false; |
---|
432 | } |
---|