InitTermTest.cpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:20k
源码类别:

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 2001, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Id: InitTermTest.cpp,v 1.6 2003/02/05 18:55:21 tng Exp $
  58.  */
  59. // ---------------------------------------------------------------------------
  60. //  This program tests the XMLPlatformUtils::Initialize/Terminate() pair
  61. //  by calling the pair a number of times.
  62. // ---------------------------------------------------------------------------
  63. // ---------------------------------------------------------------------------
  64. //  Includes
  65. // ---------------------------------------------------------------------------
  66. #include <xercesc/util/PlatformUtils.hpp>
  67. #include <xercesc/sax/SAXException.hpp>
  68. #include <xercesc/sax/SAXParseException.hpp>
  69. #include <xercesc/parsers/XercesDOMParser.hpp>
  70. #include <xercesc/parsers/SAXParser.hpp>
  71. #include <xercesc/sax2/SAX2XMLReader.hpp>
  72. #include <xercesc/sax2/XMLReaderFactory.hpp>
  73. #include <xercesc/dom/DOMException.hpp>
  74. #include "InitTermTest.hpp"
  75. #include <string.h>
  76. #include <stdlib.h>
  77. #include <fstream.h>
  78. #include <limits.h>
  79. // ---------------------------------------------------------------------------
  80. //  Local Enum to switch theState
  81. // ---------------------------------------------------------------------------
  82. enum Teststate {
  83.     Once,
  84.     Multiple,
  85.     UnEven,
  86.     Limit,
  87.     ExceedLimit
  88. };
  89. // ---------------------------------------------------------------------------
  90. //  Declare functions
  91. // ---------------------------------------------------------------------------
  92. int TestInit4DOM(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState);
  93. int TestInit4SAX(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState);
  94. int TestInit4SAX2(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState);
  95. // ---------------------------------------------------------------------------
  96. //  Define macro
  97. // ---------------------------------------------------------------------------
  98. #define TESTINITPRE                                               
  99.     long times = 1;                                               
  100.     switch (theState) {                                           
  101.         case Multiple:                                            
  102.         case UnEven:                                              
  103.             times = 100;                                           
  104.             break;                                                
  105.         case Limit:                                               
  106.         case ExceedLimit:                                         
  107.             times = LONG_MAX;                                     
  108.             break;                                                
  109.         case Once:                                                
  110.         default:                                                  
  111.             times = 1;                                            
  112.     }                                                             
  113.     long i = 0;                                                   
  114.     for (i = 0; i < times; i++) {                                 
  115.         try                                                       
  116.         {                                                         
  117.             XMLPlatformUtils::Initialize();                       
  118.         }                                                         
  119.                                                                   
  120.         catch (const XMLException& toCatch)                       
  121.         {                                                         
  122.              cerr << "Error during initialization! :n"           
  123.                   << StrX(toCatch.getMessage()) << endl;          
  124.              return 1;                                            
  125.         }                                                         
  126.     }                                                             
  127.                                                                   
  128.     if (theState == ExceedLimit) {                                
  129.         try                                                       
  130.         {                                                         
  131.             XMLPlatformUtils::Initialize();                       
  132.         }                                                         
  133.                                                                   
  134.         catch (const XMLException& toCatch)                       
  135.         {                                                         
  136.              cerr << "Error during initialization! :n"           
  137.                   << StrX(toCatch.getMessage()) << endl;          
  138.              return 1;                                            
  139.         }                                                         
  140.     }
  141. #define TESTINITPOST                                              
  142.     InitTermTestErrorHandler errorHandler;                        
  143.     parser->setErrorHandler(&errorHandler);                       
  144.     bool   errorOccurred = false;                                 
  145.                                                                   
  146.     errorHandler.resetErrors();                                   
  147.                                                                   
  148.     try                                                           
  149.     {                                                             
  150.         parser->parse(xmlFile);                                   
  151.     }                                                             
  152.                                                                   
  153.     catch (const XMLException& toCatch)                           
  154.     {                                                             
  155.         cerr << "nError during parsing: '" << xmlFile << "'n"   
  156.              << "Exception message is:  n"                       
  157.              << StrX(toCatch.getMessage()) << "n" << endl;       
  158.         errorOccurred = true;                                     
  159.     }                                                             
  160.     catch (const DOMException& toCatch)                           
  161.     {                                                             
  162.         cerr << "nDOM Error during parsing: '" << xmlFile        
  163.              << "nDOMException code is:  n"                     
  164.              << toCatch.code << "n" << endl;                     
  165.         errorOccurred = true;                                     
  166.     }                                                             
  167.     catch (...)                                                   
  168.     {                                                             
  169.         cerr << "nUnexpected exception during parsing: '"        
  170.         << xmlFile << "'n";                                      
  171.         errorOccurred = true;                                     
  172.     }                                                             
  173.                                                                   
  174.     if (errorHandler.getSawErrors())                              
  175.     {                                                             
  176.         cout << "nErrors occurred, no output availablen" << endl;
  177.         errorOccurred = true;                                     
  178.     }                                                             
  179.                                                                   
  180.     delete parser;                                                
  181.                                                                   
  182.     for (i = 0; i < times; i++) {                                 
  183.         XMLPlatformUtils::Terminate();                            
  184.     }                                                             
  185.                                                                   
  186.     if (theState == ExceedLimit || theState == UnEven) {          
  187.         XMLPlatformUtils::Terminate();                            
  188.     }                                                             
  189.                                                                   
  190.     if (errorOccurred)                                            
  191.         return 4;                                                 
  192.     else                                                          
  193.         return 0;
  194. // ---------------------------------------------------------------------------
  195. //  DOM Parser
  196. // ---------------------------------------------------------------------------
  197. int TestInit4DOM(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState)
  198. {
  199.     TESTINITPRE;
  200.     XercesDOMParser* parser = new XercesDOMParser;
  201.     parser->setDoNamespaces(gDoNamespaces);
  202.     parser->setDoSchema(gDoSchema);
  203.     parser->setValidationSchemaFullChecking(gSchemaFullChecking);
  204.     TESTINITPOST;
  205. }
  206. // ---------------------------------------------------------------------------
  207. //  SAX Parser
  208. // ---------------------------------------------------------------------------
  209. int TestInit4SAX(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState)
  210. {
  211.     TESTINITPRE;
  212.     SAXParser* parser = new SAXParser;
  213.     parser->setDoNamespaces(gDoNamespaces);
  214.     parser->setDoSchema(gDoSchema);
  215.     parser->setValidationSchemaFullChecking(gSchemaFullChecking);
  216.     TESTINITPOST;
  217. }
  218. // ---------------------------------------------------------------------------
  219. //  SAX2 XML Reader
  220. // ---------------------------------------------------------------------------
  221. int TestInit4SAX2(const char* xmlFile, bool gDoNamespaces, bool gDoSchema, bool gSchemaFullChecking, Teststate theState)
  222. {
  223.     TESTINITPRE;
  224.     SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  225.     XMLCh* doNamespaceFeature = XMLString::transcode("http://xml.org/sax/features/namespaces");
  226.     parser->setFeature(doNamespaceFeature, gDoNamespaces);
  227.     XMLCh* doSchemaFeature = XMLString::transcode("http://apache.org/xml/features/validation/schema");
  228.     parser->setFeature(doSchemaFeature, gDoSchema);
  229.     XMLCh* fullSchemaCheckFeature = XMLString::transcode("http://apache.org/xml/features/validation/schema-full-checking");
  230.     parser->setFeature(fullSchemaCheckFeature, gSchemaFullChecking);
  231.     XMLString::release(&doNamespaceFeature);
  232.     XMLString::release(&doSchemaFeature);
  233.     XMLString::release(&fullSchemaCheckFeature);
  234.     TESTINITPOST;
  235. }
  236. // ---------------------------------------------------------------------------
  237. //
  238. //  Usage()
  239. //
  240. // ---------------------------------------------------------------------------
  241. void usage()
  242. {
  243.     cout << "nUsage:n"
  244.             "    InitTermTest [options] <XML file>nn"
  245.             "This program tests the XMLPlatformUtils::Initialize()/Terminate()n"
  246.             "pair by calling it a number of times.n"
  247.             "Options:n"
  248.             "    -n          Enable namespace processing. Default is off.n"
  249.             "    -s          Enable schema processing. Default is off.n"
  250.             "    -f          Enable full schema constraint checking. Defaults to off.n"
  251.       "    -?          Show this help.n"
  252.           <<  endl;
  253. }
  254. // ---------------------------------------------------------------------------
  255. //  Main
  256. // ---------------------------------------------------------------------------
  257. int main(int argC, char* argV[]) {
  258.     // ---------------------------------------------------------------------------
  259.     //  Local data
  260.     //
  261.     //  gDoNamespaces
  262.     //      Indicates whether namespace processing should be done.
  263.     //
  264.     //  gDoSchema
  265.     //      Indicates whether schema processing should be done.
  266.     //
  267.     //  gSchemaFullChecking
  268.     //      Indicates whether full schema constraint checking should be done.
  269.     //
  270.     // ---------------------------------------------------------------------------
  271.     bool gDoNamespaces          = false;
  272.     bool gDoSchema              = false;
  273.     bool gSchemaFullChecking    = false;
  274.     // Check command line and extract arguments.
  275.     if (argC < 2)
  276.     {
  277.         usage();
  278.         return 1;
  279.     }
  280.     // See if non validating dom parser configuration is requested.
  281.     int parmInd;
  282.     for (parmInd = 1; parmInd < argC; parmInd++)
  283.     {
  284.         // Break out on first parm not starting with a dash
  285.         if (argV[parmInd][0] != '-')
  286.             break;
  287.         // Watch for special case help request
  288.         if (!strcmp(argV[parmInd], "-?"))
  289.         {
  290.             usage();
  291.             return 2;
  292.         }
  293.          else if (!strcmp(argV[parmInd], "-n")
  294.               ||  !strcmp(argV[parmInd], "-N"))
  295.         {
  296.             gDoNamespaces = true;
  297.         }
  298.          else if (!strcmp(argV[parmInd], "-s")
  299.               ||  !strcmp(argV[parmInd], "-S"))
  300.         {
  301.             gDoSchema = true;
  302.         }
  303.          else if (!strcmp(argV[parmInd], "-f")
  304.               ||  !strcmp(argV[parmInd], "-F"))
  305.         {
  306.             gSchemaFullChecking = true;
  307.         }
  308.          else
  309.         {
  310.             cerr << "Unknown option '" << argV[parmInd]
  311.                  << "', ignoring it.n" << endl;
  312.         }
  313.     }
  314.     //
  315.     //  And now we have to have only one parameter left and it must be
  316.     //  the file name.
  317.     //
  318.     if (parmInd + 1 != argC)
  319.     {
  320.         usage();
  321.         return 1;
  322.     }
  323.     char* xmlFile = argV[parmInd];
  324.     bool error = false;
  325.     //
  326.     // Calling Init/Term pair a number of times
  327.     //
  328.     unsigned int i = 0;
  329.     for (i=0; i < 100; i++) {
  330.         if (error) {
  331.             cout << "Test Failed" << endl;
  332.             return 4;
  333.         }
  334.         if (TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Once))
  335.             error = true;
  336.         if (TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Once))
  337.             error = true;
  338.         if (TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Once))
  339.             error = true;
  340.     }
  341.     if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Multiple))
  342.         error = true;
  343.     if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Multiple))
  344.         error = true;
  345.     if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Multiple))
  346.         error = true;
  347. /*
  348.  * The following Limit test is a stress test that can run a long time
  349.  * Commented out for regular sanity test
  350.  */
  351. /*
  352.     if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Limit))
  353.         error = true;
  354.     if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Limit))
  355.         error = true;
  356.     if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, Limit))
  357.         error = true;
  358.     if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, ExceedLimit))
  359.         error = true;
  360.     if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, ExceedLimit))
  361.         error = true;
  362.     if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, ExceedLimit))
  363.         error = true;
  364. */
  365.     if (error || TestInit4DOM(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, UnEven))
  366.         error = true;
  367.     if (error || TestInit4SAX(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, UnEven))
  368.         error = true;
  369.     if (error || TestInit4SAX2(xmlFile, gDoNamespaces, gDoSchema, gSchemaFullChecking, UnEven))
  370.         error = true;
  371.     if (error) {
  372.         cout << "Test Failed" << endl;
  373.         return 4;
  374.     }
  375.     cout << "Test Run Successfully" << endl;
  376.     return 0;
  377. }
  378. // ---------------------------------------------------------------------------
  379. //  InitTermTestErrorHandler
  380. // ---------------------------------------------------------------------------
  381. InitTermTestErrorHandler::InitTermTestErrorHandler() :
  382.     fSawErrors(false)
  383. {
  384. }
  385. InitTermTestErrorHandler::~InitTermTestErrorHandler()
  386. {
  387. }
  388. void InitTermTestErrorHandler::error(const SAXParseException& e)
  389. {
  390.     fSawErrors = true;
  391.     cerr << "nError at file " << StrX(e.getSystemId())
  392.          << ", line " << e.getLineNumber()
  393.          << ", char " << e.getColumnNumber()
  394.          << "n  Message: " << StrX(e.getMessage()) << endl;
  395. }
  396. void InitTermTestErrorHandler::fatalError(const SAXParseException& e)
  397. {
  398.     fSawErrors = true;
  399.     cerr << "nFatal Error at file " << StrX(e.getSystemId())
  400.          << ", line " << e.getLineNumber()
  401.          << ", char " << e.getColumnNumber()
  402.          << "n  Message: " << StrX(e.getMessage()) << endl;
  403. }
  404. void InitTermTestErrorHandler::warning(const SAXParseException& e)
  405. {
  406.     cerr << "nWarning at file " << StrX(e.getSystemId())
  407.          << ", line " << e.getLineNumber()
  408.          << ", char " << e.getColumnNumber()
  409.          << "n  Message: " << StrX(e.getMessage()) << endl;
  410. }
  411. void InitTermTestErrorHandler::resetErrors()
  412. {
  413.     fSawErrors = false;
  414. }