00001 /*----------------------------------------------------------------------------- 00002 Name: TestSuite.h 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Testing helper 00006 See: Other C++ test tools: http://c2.com/cgi/wiki?TestingFramework 00007 -----------------------------------------------------------------------------*/ 00008 00009 #ifndef _TESTSUITE_H 00010 #define _TESTSUITE_H 00011 00012 #include <assert.h> // windows 00013 #include <iostream> 00014 #include <client/XmlBlasterAccess.h> 00015 #include <util/EmbeddedServer.h> 00016 #include <util/XmlBlasterException.h> 00017 #include <util/Global.h> 00018 #include <util/thread/ThreadImpl.h> 00019 #include <util/Timestamp.h> 00020 #include <util/lexical_cast.h> 00021 00022 namespace org { namespace xmlBlaster { namespace test { 00023 00024 template <class T1, class T2> extern void assertEquals(org::xmlBlaster::util::I_Log& log, const std::string& who, const T1& should, const T2& is, const std::string& txt); 00025 template <class T1, class T2> extern void assertDifferes(org::xmlBlaster::util::I_Log& log, const std::string& who, const T1& should, const T2& is, const std::string& txt); 00026 #if __GNUC__ == 2 00027 extern void assertEquals(org::xmlBlaster::util::I_Log& log, const std::string& who, const std::string& should, const std::string& is, const std::string& txt); 00028 extern void assertDifferes(org::xmlBlaster::util::I_Log& log, const std::string& who, const std::string& should, const std::string& is, const std::string& txt); 00029 #endif 00030 00036 template <class T1, class T2> 00037 void assertEquals(org::xmlBlaster::util::I_Log& log, const std::string& who, const T1& should, const T2& is, const std::string& txt) 00038 { 00039 //if (should != is) { 00040 if (org::xmlBlaster::util::lexical_cast<std::string>(should) != org::xmlBlaster::util::lexical_cast<std::string>(is)) { 00041 log.error(who, txt + " FAILED: value is '" + org::xmlBlaster::util::lexical_cast<std::string>(is) + "' but should be '" + org::xmlBlaster::util::lexical_cast<std::string>(should) + "'"); 00042 assert(0); 00043 } 00044 else { 00045 log.info(who, txt + " OK"); 00046 } 00047 } 00048 00049 template <class T1, class T2> 00050 void assertDifferes(org::xmlBlaster::util::I_Log& log, const std::string& who, const T1& should, const T2& is, const std::string& txt) 00051 { 00052 //if (should == is) { 00053 if (org::xmlBlaster::util::lexical_cast<std::string>(should) == org::xmlBlaster::util::lexical_cast<std::string>(is)) { 00054 log.error(who, txt + " FAILED: value is '" + org::xmlBlaster::util::lexical_cast<std::string>(is) + "' in both cases but they should be different"); 00055 assert(0); 00056 } 00057 else { 00058 log.info(who, txt + " OK"); 00059 } 00060 } 00061 00062 00063 #if __GNUC__ == 2 00064 // for example g++ 2.95.3 00065 // specific implementation for the string since the org::xmlBlaster::util::lexical_cast from string to string causes problems. 00066 void assertEquals(org::xmlBlaster::util::I_Log& log, const std::string& who, const std::string& should, const std::string& is, const std::string& txt) 00067 { 00068 if (should != is) { 00069 log.error(who, txt + " FAILED: value is '" + is + "' but should be '" + should + "'"); 00070 assert(0); 00071 } 00072 else { 00073 log.info(who, txt + " OK"); 00074 } 00075 } 00076 void assertDifferes(org::xmlBlaster::util::I_Log& log, const std::string& who, const std::string& should, const std::string& is, const std::string& txt) 00077 { 00078 if (should == is) { 00079 log.error(who, txt + " FAILED: value is '" + is + "' for both cases but they should be different"); 00080 assert(0); 00081 } 00082 else { 00083 log.info(who, txt + " OK"); 00084 } 00085 } 00086 #endif 00087 00088 00089 class TestSuite 00090 { 00091 protected: 00092 std::string ME; 00093 std::string applName_; 00094 org::xmlBlaster::util::Global& global_; 00095 org::xmlBlaster::util::I_Log& log_; 00096 bool useEmbeddedServer_; 00097 org::xmlBlaster::client::XmlBlasterAccess connection_; 00098 org::xmlBlaster::util::EmbeddedServer* embeddedServer_; 00099 bool needsHelp_; 00100 bool doEmbeddedServerCheck_; 00101 00102 public: 00103 00104 TestSuite(int args, const char * const argv[], const std::string& name, bool doEmbeddedServerCheck=true) 00105 : ME(name), 00106 applName_(ME), 00107 global_(org::xmlBlaster::util::Global::getInstance().initialize(args, argv)), 00108 log_(global_.getLog("test")), 00109 connection_(global_), 00110 doEmbeddedServerCheck_(doEmbeddedServerCheck) 00111 { 00112 needsHelp_ = false; 00113 for (int ii=0; ii<args; ii++) { 00114 if (strcmp(argv[ii], "-?")==0 || strcmp(argv[ii], "-h")==0 || strcmp(argv[ii], "-help")==0) { 00115 needsHelp_ = true; 00116 break; 00117 } 00118 } 00119 00120 if (!doEmbeddedServerCheck_) 00121 return; 00122 00123 if ( log_.call() ) log_.call(ME, "Entering TestSuite base class, initializing XML environment"); 00124 embeddedServer_ = NULL; 00125 useEmbeddedServer_ = global_.getProperty().getBoolProperty("embeddedServer", false); 00126 if (useEmbeddedServer_) { 00127 log_.info(ME, "the embedded server is switched ON (you could switch it off with '-embeddedServer false' on the command line)"); 00128 } 00129 else { 00130 log_.warn(ME, "the embedded server is switched OFF (you will need an external xmlBlaster running), sleeping for 2 sec now ..."); 00131 org::xmlBlaster::util::thread::Thread::sleep(2000); 00132 } 00133 if (useEmbeddedServer_) { 00134 # ifdef XMLBLASTER_MICO 00135 std::cout << " !!!!! THIS TEST CAN NOT BE RUN WITH MICO SINCE AN ORB WHICH IS SHUTDOWN CAN NOT BE REUSED !!!!" << std::endl; 00136 std::cout << " !!!!! IT HAS BEEN TESTED AND IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8 !!!!" << std::endl; 00137 std::cout << " !!!!! IT IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8 !!!!" << std::endl; 00138 std::cout << " !!!!! TRY IT WITH ANOTHER CORBA IMPLEMENTATION (for example TAO) !!!!" << std::endl; 00139 exit(-1); 00140 # endif 00141 if ( log_.call() ) log_.call(ME, "Entering TestSuite base class, useEmbeddedServer_=true"); 00142 std::string cmdLine = global_.getProperty().getStringProperty("embeddedServer.cmdLine", "> /dev/null"); 00143 std::string jvmArgs = global_.getProperty().getStringProperty("embeddedServer.jvmArgs", ""); 00144 embeddedServer_ = new org::xmlBlaster::util::EmbeddedServer(global_, jvmArgs, cmdLine, &connection_); 00145 embeddedServer_->start(true); 00146 // org::xmlBlaster::util::thread::Thread::sleepSecs(5); // let the xmlBlaster server start ... 00147 // don't need to wait anymore since 00148 } 00149 } 00150 00151 virtual ~TestSuite() 00152 { 00153 if (log_.call()) log_.call(ME, "destructor"); 00154 if (doEmbeddedServerCheck_) { 00155 delete embeddedServer_; 00156 embeddedServer_ = NULL; 00157 } 00158 if (log_.trace()) log_.trace(ME, "destructor ended"); 00159 } 00160 00161 virtual void setUp(/*int args=0, char *argv[]=0*/) 00162 { 00163 if (needsHelp_) { 00164 usage(); 00165 exit(0); 00166 } 00167 } 00168 00169 virtual void tearDown() 00170 { 00171 } 00172 00173 void startEmbeddedServer() 00174 { 00175 if (embeddedServer_) { 00176 log_.trace(ME, "starting now the embedded server"); 00177 embeddedServer_->start(); 00178 } 00179 else { 00180 log_.warn(ME, "could not start the embedded server since you are running without it"); 00181 } 00182 } 00183 00184 void stopEmbeddedServer() 00185 { 00186 if (embeddedServer_) { 00187 embeddedServer_->stop(); 00188 } 00189 else { 00190 log_.warn(ME, "could not stop the embedded server since you are running without it"); 00191 } 00192 } 00193 00194 virtual void usage() const 00195 { 00196 log_.plain(applName_, std::string("usage: ") + applName_ + "\n with the following attributes:\n"); 00197 log_.plain(applName_, "-embeddedServer false: [default] an external xmlBlaster will be needed"); 00198 log_.plain(applName_, "-embeddedServer true : an internal xmlBlaster will be used, stop all external xmlBlaster"); 00199 log_.plain(applName_, "-embeddedServer.cmdLine : defaults to \"/dev/null\""); 00200 log_.plain(applName_, "-embeddedServer.jvmArgs : defaults to \"\""); 00201 log_.plain(applName_, ""); 00202 log_.plain(applName_, std::string("for example: ") + applName_ + " -embeddedServer true -embeddedServer.cmdLine \"call -true\" -embbededServer.jvmArgs \"-Dwhatever \""); 00203 } 00204 00205 }; 00206 00207 00208 }}} // namespace 00209 00210 00211 #endif 00212 00213