1 /*-----------------------------------------------------------------------------
  2 Name:      TestSuite.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing helper
  6 See:       Other C++ test tools: http://c2.com/cgi/wiki?TestingFramework
  7 -----------------------------------------------------------------------------*/
  8 
  9 #ifndef _TESTSUITE_H
 10 #define _TESTSUITE_H
 11 
 12 #include <assert.h> // windows
 13 #include <iostream>
 14 #include <cstring>
 15 #include <client/XmlBlasterAccess.h>
 16 #include <util/EmbeddedServer.h>
 17 #include <util/XmlBlasterException.h>
 18 #include <util/Global.h>
 19 #include <util/thread/ThreadImpl.h>
 20 #include <util/Timestamp.h>
 21 #include <util/lexical_cast.h>
 22 #include <cstring> // strcmp()
 23 
 24 namespace org { namespace xmlBlaster { namespace test {
 25 
 26 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);
 27 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);
 28 #if __GNUC__ == 2
 29 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);
 30 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);
 31 #endif
 32 
 33 /**
 34  * Supports comparing for example char[] with string. 
 35  * @param should const char [13]
 36  * @param is     std::string
 37  */
 38 template <class T1, class T2> 
 39 void assertEquals(org::xmlBlaster::util::I_Log& log, const std::string& who, const T1& should, const T2& is, const std::string& txt)
 40 {
 41    //if (should != is) {
 42    if (org::xmlBlaster::util::lexical_cast<std::string>(should) != org::xmlBlaster::util::lexical_cast<std::string>(is)) {
 43       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) + "'");
 44       assert(0);
 45    }
 46    else {
 47       log.info(who, txt + " OK");
 48    }
 49 }
 50 
 51 template <class T1, class T2> 
 52 void assertDifferes(org::xmlBlaster::util::I_Log& log, const std::string& who, const T1& should, const T2& is, const std::string& txt)
 53 {
 54    //if (should == is) {
 55    if (org::xmlBlaster::util::lexical_cast<std::string>(should) == org::xmlBlaster::util::lexical_cast<std::string>(is)) {
 56       log.error(who, txt + " FAILED: value is '" + org::xmlBlaster::util::lexical_cast<std::string>(is) + "' in both cases but they should be different");
 57       assert(0);
 58    }
 59    else {
 60       log.info(who, txt + " OK");
 61    }
 62 }
 63 
 64 
 65 #if __GNUC__ == 2
 66 // for example g++ 2.95.3
 67 // specific implementation for the string since the org::xmlBlaster::util::lexical_cast from string to string causes problems.
 68 void assertEquals(org::xmlBlaster::util::I_Log& log, const std::string& who, const std::string& should, const std::string& is, const std::string& txt)
 69 {
 70    if (should != is) {
 71       log.error(who, txt + " FAILED: value is '" + is + "' but should be '" + should + "'");
 72       assert(0);
 73    }
 74    else {
 75       log.info(who, txt + " OK");
 76    }
 77 }
 78 void assertDifferes(org::xmlBlaster::util::I_Log& log, const std::string& who, const std::string& should, const std::string& is, const std::string& txt)
 79 {
 80    if (should == is) {
 81       log.error(who, txt + " FAILED: value is '" + is + "' for both cases but they should be different");
 82       assert(0);
 83    }
 84    else {
 85       log.info(who, txt + " OK");
 86    }
 87 }
 88 #endif
 89 
 90 
 91 class TestSuite
 92 {
 93 protected:
 94    std::string           ME;
 95    std::string           applName_;
 96    org::xmlBlaster::util::Global&          global_;
 97    org::xmlBlaster::util::I_Log&             log_;
 98    bool             useEmbeddedServer_;
 99    org::xmlBlaster::client::XmlBlasterAccess connection_;
100    org::xmlBlaster::util::EmbeddedServer*  embeddedServer_;
101    bool             needsHelp_;
102    bool             doEmbeddedServerCheck_;
103 
104 public:
105 
106    TestSuite(int args, const char * const argv[], const std::string& name, bool doEmbeddedServerCheck=true) 
107       : ME(name), 
108         applName_(ME), 
109         global_(org::xmlBlaster::util::Global::getInstance().initialize(args, argv)), 
110         log_(global_.getLog("test")),
111         connection_(global_),
112         doEmbeddedServerCheck_(doEmbeddedServerCheck)
113    {
114       needsHelp_ = false;
115       for (int ii=0; ii<args; ii++) {
116          if (strcmp(argv[ii], "-?")==0 || strcmp(argv[ii], "-h")==0 || strcmp(argv[ii], "-help")==0) {
117             needsHelp_ = true;
118             break;
119          }
120       }
121 
122       if (!doEmbeddedServerCheck_)
123          return;
124 
125       if ( log_.call() ) log_.call(ME, "Entering TestSuite base class, initializing XML environment");
126       embeddedServer_    = NULL;
127       useEmbeddedServer_ = global_.getProperty().getBoolProperty("embeddedServer", false);
128       if (useEmbeddedServer_) {
129          log_.info(ME, "the embedded server is switched ON (you could switch it off with '-embeddedServer false' on the command line)");
130       }
131       else {
132          log_.warn(ME, "the embedded server is switched OFF (you will need an external xmlBlaster running), sleeping for 2 sec now ...");
133          org::xmlBlaster::util::thread::Thread::sleep(2000);
134      }
135      if (useEmbeddedServer_) {
136 # ifdef XMLBLASTER_MICO
137         std::cout << " !!!!! THIS TEST CAN NOT BE RUN WITH MICO SINCE AN ORB WHICH IS SHUTDOWN CAN NOT BE REUSED !!!!" << std::endl;
138         std::cout << " !!!!! IT HAS BEEN TESTED AND IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8                  !!!!" << std::endl;
139         std::cout << " !!!!! IT IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8                                      !!!!" << std::endl;
140         std::cout << " !!!!! TRY IT WITH ANOTHER CORBA IMPLEMENTATION (for example TAO)                          !!!!" << std::endl;
141         exit(-1);
142 # endif
143         if ( log_.call() ) log_.call(ME, "Entering TestSuite base class, useEmbeddedServer_=true");
144         std::string cmdLine = global_.getProperty().getStringProperty("embeddedServer.cmdLine", "> /dev/null");
145         std::string jvmArgs = global_.getProperty().getStringProperty("embeddedServer.jvmArgs", "");
146         embeddedServer_ = new org::xmlBlaster::util::EmbeddedServer(global_, jvmArgs, cmdLine, &connection_);
147         embeddedServer_->start(true);
148 //        org::xmlBlaster::util::thread::Thread::sleepSecs(5); // let the xmlBlaster server start ...
149         // don't need to wait anymore since 
150      }
151    }
152 
153    virtual ~TestSuite()
154    {
155       if (log_.call()) log_.call(ME, "destructor");
156       if (doEmbeddedServerCheck_) {
157          delete embeddedServer_;
158          embeddedServer_ = NULL;
159       }
160       if (log_.trace()) log_.trace(ME, "destructor ended");
161    }
162 
163    virtual void setUp(/*int args=0, char *argv[]=0*/)
164    {
165       if (needsHelp_) {
166          usage();
167          exit(0);
168       }
169    }
170 
171    virtual void tearDown() 
172    {
173    }
174 
175    void startEmbeddedServer()
176    {
177       if (embeddedServer_) {
178          log_.trace(ME, "starting now the embedded server");
179          embeddedServer_->start();
180       }
181       else {
182          log_.warn(ME, "could not start the embedded server since you are running without it");
183       }
184    }
185 
186    void stopEmbeddedServer()
187    {
188       if (embeddedServer_) {
189          embeddedServer_->stop();
190       }
191       else {
192          log_.warn(ME, "could not stop the embedded server since you are running without it");
193       }
194    }
195 
196    virtual void usage() const
197    {
198       log_.plain(applName_, std::string("usage: ") + applName_ + "\n with the following attributes:\n");
199       log_.plain(applName_, "-embeddedServer false: [default] an external xmlBlaster will be needed");
200       log_.plain(applName_, "-embeddedServer true :           an internal xmlBlaster will be used, stop all external xmlBlaster");
201       log_.plain(applName_, "-embeddedServer.cmdLine : defaults to \"/dev/null\"");
202       log_.plain(applName_, "-embeddedServer.jvmArgs : defaults to \"\"");
203       log_.plain(applName_, "");
204       log_.plain(applName_, std::string("for example: ") + applName_ + " -embeddedServer true -embeddedServer.cmdLine \"call -true\" -embbededServer.jvmArgs \"-Dwhatever \"");
205    }
206 
207 };
208 
209 
210 }}} // namespace
211 
212 
213 #endif


syntax highlighted by Code2HTML, v. 0.9.1