InitTermTest.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:16k
源码类别:

xml/soap/webservice

开发平台:

C/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
  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 <util/PlatformUtils.hpp>
  67. #include <sax/SAXException.hpp>
  68. #include <sax/SAXParseException.hpp>
  69. #include <parsers/DOMParser.hpp>
  70. #include <parsers/IDOMParser.hpp>
  71. #include <parsers/SAXParser.hpp>
  72. #include <sax2/SAX2XMLReader.hpp>
  73. #include <sax2/XMLReaderFactory.hpp>
  74. #include <dom/DOM_DOMException.hpp>
  75. #include <idom/IDOM_DOMException.hpp>
  76. #include "InitTermTest.hpp"
  77. #include <string.h>
  78. #include <stdlib.h>
  79. #include <fstream.h>
  80. #include <limits.h>
  81. // ---------------------------------------------------------------------------
  82. //  Local Enum to switch theState
  83. // ---------------------------------------------------------------------------
  84. enum Teststate {
  85.     Once,
  86.     Multiple,
  87.     UnEven,
  88.     Limit,
  89.     ExceedLimit
  90. };
  91. // ---------------------------------------------------------------------------
  92. //  Declare functions
  93. // ---------------------------------------------------------------------------
  94. int TestInit4DOM(const char* xmlFile, Teststate theState);
  95. int TestInit4DOM(const char* xmlFile, Teststate theState);
  96. int TestInit4SAX(const char* xmlFile, Teststate theState);
  97. int TestInit4SAX2(const char* xmlFile, Teststate theState);
  98. // ---------------------------------------------------------------------------
  99. //  Define macro
  100. // ---------------------------------------------------------------------------
  101. #define TESTINITPRE                                               
  102.     long times = 1;                                               
  103.     switch (theState) {                                           
  104.         case Multiple:                                            
  105.         case UnEven:                                              
  106.             times = 100;                                           
  107.             break;                                                
  108.         case Limit:                                               
  109.         case ExceedLimit:                                         
  110.             times = LONG_MAX;                                     
  111.             break;                                                
  112.         case Once:                                                
  113.         default:                                                  
  114.             times = 1;                                            
  115.     }                                                             
  116.     long i = 0;                                                   
  117.     for (i = 0; i < times; i++) {                                 
  118.         try                                                       
  119.         {                                                         
  120.             XMLPlatformUtils::Initialize();                       
  121.         }                                                         
  122.                                                                   
  123.         catch (const XMLException& toCatch)                       
  124.         {                                                         
  125.              cerr << "Error during initialization! :n"           
  126.                   << StrX(toCatch.getMessage()) << endl;          
  127.              return 1;                                            
  128.         }                                                         
  129.     }                                                             
  130.                                                                   
  131.     if (theState == ExceedLimit) {                                
  132.         try                                                       
  133.         {                                                         
  134.             XMLPlatformUtils::Initialize();                       
  135.         }                                                         
  136.                                                                   
  137.         catch (const XMLException& toCatch)                       
  138.         {                                                         
  139.              cerr << "Error during initialization! :n"           
  140.                   << StrX(toCatch.getMessage()) << endl;          
  141.              return 1;                                            
  142.         }                                                         
  143.     }
  144. #define TESTINITPOST                                              
  145.     InitTermTestErrorHandler errorHandler;                        
  146.     parser->setErrorHandler(&errorHandler);                       
  147.     bool   errorOccurred = false;                                 
  148.                                                                   
  149.     errorHandler.resetErrors();                                   
  150.                                                                   
  151.     try                                                           
  152.     {                                                             
  153.         parser->parse(xmlFile);                                   
  154.     }                                                             
  155.                                                                   
  156.     catch (const XMLException& toCatch)                           
  157.     {                                                             
  158.         cerr << "nError during parsing: '" << xmlFile << "'n"   
  159.              << "Exception message is:  n"                       
  160.              << StrX(toCatch.getMessage()) << "n" << endl;       
  161.         errorOccurred = true;                                     
  162.     }                                                             
  163.     catch (const DOM_DOMException& toCatch)                       
  164.     {                                                             
  165.         cerr << "nDOM Error during parsing: '" << xmlFile        
  166.              << "nDOMException code is:  n"                     
  167.              << toCatch.code << "n" << endl;                     
  168.         errorOccurred = true;                                     
  169.     }                                                             
  170.     catch (...)                                                   
  171.     {                                                             
  172.         cerr << "nUnexpected exception during parsing: '"        
  173.         << xmlFile << "'n";                                      
  174.         errorOccurred = true;                                     
  175.     }                                                             
  176.                                                                   
  177.     if (errorHandler.getSawErrors())                              
  178.     {                                                             
  179.         cout << "nErrors occured, no output availablen" << endl;
  180.         errorOccurred = true;                                     
  181.     }                                                             
  182.                                                                   
  183.     delete parser;                                                
  184.                                                                   
  185.     for (i = 0; i < times; i++) {                                 
  186.         XMLPlatformUtils::Terminate();                            
  187.     }                                                             
  188.                                                                   
  189.     if (theState == ExceedLimit || theState == UnEven) {          
  190.         XMLPlatformUtils::Terminate();                            
  191.     }                                                             
  192.                                                                   
  193.     if (errorOccurred)                                            
  194.         return 4;                                                 
  195.     else                                                          
  196.         return 0;
  197. // ---------------------------------------------------------------------------
  198. //  DOM Parser
  199. // ---------------------------------------------------------------------------
  200. int TestInit4DOM(const char* xmlFile, Teststate theState)
  201. {
  202.     TESTINITPRE;
  203.     DOMParser* parser = new DOMParser;
  204.     TESTINITPOST;
  205. }
  206. // ---------------------------------------------------------------------------
  207. //  IDOM Parser
  208. // ---------------------------------------------------------------------------
  209. int TestInit4IDOM(const char* xmlFile, Teststate theState)
  210. {
  211.     TESTINITPRE;
  212.     IDOMParser* parser = new IDOMParser;
  213.     TESTINITPOST;
  214. }
  215. // ---------------------------------------------------------------------------
  216. //  SAX Parser
  217. // ---------------------------------------------------------------------------
  218. int TestInit4SAX(const char* xmlFile, Teststate theState)
  219. {
  220.     TESTINITPRE;
  221.     SAXParser* parser = new SAXParser;
  222.     TESTINITPOST;
  223. }
  224. // ---------------------------------------------------------------------------
  225. //  SAX2 XML Reader
  226. // ---------------------------------------------------------------------------
  227. int TestInit4SAX2(const char* xmlFile, Teststate theState)
  228. {
  229.     TESTINITPRE;
  230.     SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  231.     TESTINITPOST;
  232. }
  233. // ---------------------------------------------------------------------------
  234. //  Main
  235. // ---------------------------------------------------------------------------
  236. int main(int argC, char* argV[]) {
  237.     // Check command line and extract arguments.
  238.     if (argC != 2)
  239.     {
  240.         cout << "nUsage:n"
  241.                 "    InitTermTest <XML file>nn"
  242.                 "This program tests the XMLPlatformUtils::Initialize()/Terminate()n"
  243.                 "pair by calling it a number of times."
  244.              << endl;
  245.         return 1;
  246.     }
  247.     char* xmlFile = argV[1];
  248.     bool error = false;
  249.     //
  250.     // Calling Init/Term pair a number of times
  251.     //
  252.     unsigned int i = 0;
  253.     for (i=0; i < 100; i++) {
  254.         if (error) {
  255.             cout << "Test Failed" << endl;
  256.             return 4;
  257.         }
  258.         if (TestInit4DOM(xmlFile, Once))
  259.             error = true;
  260.         if (TestInit4IDOM(xmlFile, Once))
  261.             error = true;
  262.         if (TestInit4SAX(xmlFile, Once))
  263.             error = true;
  264.         if (TestInit4SAX2(xmlFile, Once))
  265.             error = true;
  266.     }
  267.     if (error || TestInit4DOM(xmlFile, Multiple))
  268.         error = true;
  269.     if (error || TestInit4IDOM(xmlFile, Multiple))
  270.         error = true;
  271.     if (error || TestInit4SAX(xmlFile, Multiple))
  272.         error = true;
  273.     if (error || TestInit4SAX2(xmlFile, Multiple))
  274.         error = true;
  275. /*
  276.  * The following Limit test is a stress test that can run a long time
  277.  * Commented out for regular sanity test
  278.  */
  279. /*
  280.     if (error || TestInit4DOM(xmlFile, Limit))
  281.         error = true;
  282.     if (error || TestInit4IDOM(xmlFile, Limit))
  283.         error = true;
  284.     if (error || TestInit4SAX(xmlFile, Limit))
  285.         error = true;
  286.     if (error || TestInit4SAX2(xmlFile, Limit))
  287.         error = true;
  288.     if (error || TestInit4DOM(xmlFile, ExceedLimit))
  289.         error = true;
  290.     if (error || TestInit4IDOM(xmlFile, ExceedLimit))
  291.         error = true;
  292.     if (error || TestInit4SAX(xmlFile, ExceedLimit))
  293.         error = true;
  294.     if (error || TestInit4SAX2(xmlFile, ExceedLimit))
  295.         error = true;
  296. */
  297.     if (error || TestInit4DOM(xmlFile, UnEven))
  298.         error = true;
  299.     if (error || TestInit4IDOM(xmlFile, UnEven))
  300.         error = true;
  301.     if (error || TestInit4SAX(xmlFile, UnEven))
  302.         error = true;
  303.     if (error || TestInit4SAX2(xmlFile, UnEven))
  304.         error = true;
  305.     if (error) {
  306.         cout << "Test Failed" << endl;
  307.         return 4;
  308.     }
  309.     cout << "Test Run Successfully" << endl;
  310.     return 0;
  311. }
  312. // ---------------------------------------------------------------------------
  313. //  InitTermTestErrorHandler
  314. // ---------------------------------------------------------------------------
  315. InitTermTestErrorHandler::InitTermTestErrorHandler() :
  316.     fSawErrors(false)
  317. {
  318. }
  319. InitTermTestErrorHandler::~InitTermTestErrorHandler()
  320. {
  321. }
  322. void InitTermTestErrorHandler::error(const SAXParseException& e)
  323. {
  324.     fSawErrors = true;
  325.     cerr << "nError at file " << StrX(e.getSystemId())
  326.          << ", line " << e.getLineNumber()
  327.          << ", char " << e.getColumnNumber()
  328.          << "n  Message: " << StrX(e.getMessage()) << endl;
  329. }
  330. void InitTermTestErrorHandler::fatalError(const SAXParseException& e)
  331. {
  332.     fSawErrors = true;
  333.     cerr << "nFatal Error at file " << StrX(e.getSystemId())
  334.          << ", line " << e.getLineNumber()
  335.          << ", char " << e.getColumnNumber()
  336.          << "n  Message: " << StrX(e.getMessage()) << endl;
  337. }
  338. void InitTermTestErrorHandler::warning(const SAXParseException& e)
  339. {
  340.     cerr << "nWarning at file " << StrX(e.getSystemId())
  341.          << ", line " << e.getLineNumber()
  342.          << ", char " << e.getColumnNumber()
  343.          << "n  Message: " << StrX(e.getMessage()) << endl;
  344. }
  345. void InitTermTestErrorHandler::resetErrors()
  346. {
  347.     fSawErrors = false;
  348. }