1 /*-----------------------------------------------------------------------------
  2 Name:      TestCorbaDriver.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the Timeout Features
  6 -----------------------------------------------------------------------------*/
  7 #ifdef COMPILE_CORBA_PLUGIN
  8 
  9 #include <client/protocol/corba/CorbaDriverFactory.h>
 10 #include "TestSuite.h"
 11 #include <iostream>
 12 
 13 using namespace std;
 14 using namespace org::xmlBlaster::util;
 15 using namespace org::xmlBlaster::util::thread;
 16 using namespace org::xmlBlaster::client::protocol::corba;
 17 
 18 
 19 namespace org { namespace xmlBlaster { namespace test {
 20 
 21 class TestCorbaDriver
 22 {
 23 private:
 24    string              ME;
 25    Global&             global_;
 26    I_Log&              log_;
 27    Mutex               updateMutex_;
 28    int                 numOfUpdates_;
 29    CorbaDriverFactory& factory_;
 30 
 31 public:
 32 
 33    TestCorbaDriver(Global& glob) 
 34       : ME("TestCorbaDriver"), 
 35         global_(glob), 
 36         log_(glob.getLog()),
 37         updateMutex_(),
 38         factory_(CorbaDriverFactory::getFactory(glob))
 39    {
 40    }
 41 
 42 
 43    void tearDown()
 44    {
 45    }
 46 
 47    virtual ~TestCorbaDriver()
 48    {
 49    }
 50 
 51    void setUp()
 52    {
 53    }
 54 
 55    void testSingleDriver()
 56    {
 57       log_.info(ME, "testing single driver: start");
 58       CorbaDriver& driver1 = factory_.getDriverInstance(&global_);
 59       CorbaDriver& driver2 = factory_.getDriverInstance(&global_);
 60       CorbaDriver& driver3 = factory_.getDriverInstance(&global_);
 61       // should be three instances with the name 'one' now ...
 62 
 63       assertEquals(log_, ME, &driver1, &driver2, "Both 'one' drivers should share the same address");
 64       assertEquals(log_, ME, &driver2, &driver3, "Both 'one' drivers should share the same address");
 65       assertEquals(log_, ME, 2, factory_.killDriverInstance(&global_), "number of 'one' instances should be 2 (after deletion)");
 66       Thread::sleepSecs(1);
 67       assertEquals(log_, ME, 1, factory_.killDriverInstance(&global_), "number of 'one' instances should be 1 (after deletion)");
 68       Thread::sleepSecs(1);
 69       assertEquals(log_, ME, 0, factory_.killDriverInstance(&global_), "number of 'one' instances should be 0 (after deletion)");
 70       Thread::sleepSecs(1);
 71       assertEquals(log_, ME, -1, factory_.killDriverInstance(&global_), "number of 'one' instances should be -1 (after deletion)");
 72       Thread::sleepSecs(1);
 73       assertEquals(log_, ME, -1, factory_.killDriverInstance(&global_), "number of 'one' instances should be -1 (after deletion)");
 74       log_.info(ME, "testing single driver: end");
 75    }
 76 
 77    void testMultipleDrivers()
 78    {
 79       log_.info(ME, "testing multiple drivers: start");
 80       Global glob2;
 81       Global glob3;
 82       CorbaDriver& driver1 = factory_.getDriverInstance(&global_);
 83       CorbaDriver& driver2 = factory_.getDriverInstance(&glob2);
 84       CorbaDriver& driver3 = factory_.getDriverInstance(&glob3);
 85       factory_.getDriverInstance(&glob2);
 86       factory_.getDriverInstance(&glob3);
 87       // should be three instances with the name 'one' now ...
 88 
 89       assertDifferes(log_, ME, &driver1, &driver2, "'one' and 'two' should NOT share the same address");
 90       assertDifferes(log_, ME, &driver2, &driver3, "Both 'one' drivers should share the same address");
 91       
 92       Thread::sleepSecs(1);
 93       assertEquals(log_, ME, 0, factory_.killDriverInstance(&global_), "number of 'one' instances should be 0 (after deletion)");
 94       Thread::sleepSecs(1);
 95       assertEquals(log_, ME, -1, factory_.killDriverInstance(&global_), "number of 'one' instances should be -1 (after deletion)");
 96       Thread::sleepSecs(1);
 97       assertEquals(log_, ME, 1, factory_.killDriverInstance(&glob2), "number of 'two' instances should be 1 (after deletion)");
 98       Thread::sleepSecs(1);
 99       assertEquals(log_, ME, 1, factory_.killDriverInstance(&glob3), "number of 'three' instances should be 1 (after deletion)");
100       Thread::sleepSecs(1);
101       assertEquals(log_, ME, 0, factory_.killDriverInstance(&glob3), "number of 'three' instances should be 0 (after deletion)");
102       // here the thread still should be running ...
103       Thread::sleepSecs(2);
104       assertEquals(log_, ME, 0, factory_.killDriverInstance(&glob2), "number of 'two' instances should be 0 (after deletion)");
105       log_.info(ME, "testing multiple drivers: end");
106    }
107 
108 };                                
109 
110 }}}
111 
112 /**
113  * Try
114  * <pre>
115  *   java TestCorbaDriver -help
116  * </pre>
117  * for usage help
118  */
119 using namespace org::xmlBlaster::test;
120 
121 int main(int args, char ** argv)
122 {
123    try {
124       org::xmlBlaster::util::Object_Lifetime_Manager::init();
125       Global& glob = Global::getInstance();
126       glob.initialize(args, argv);
127 
128       TestCorbaDriver test(glob);
129       test.setUp();
130       test.testSingleDriver();
131       test.tearDown();
132 
133       test.setUp();
134       test.testMultipleDrivers();
135       test.tearDown();
136       org::xmlBlaster::util::Object_Lifetime_Manager::fini();
137    }
138    catch (XmlBlasterException& ex) {
139       std::cout << ex.toXml() << std::endl;
140    }
141    catch (bad_exception& ex) {
142       cout << "bad_exception: " << ex.what() << endl;
143    }
144    catch (exception& ex) {
145       cout << " exception: " << ex.what() << endl;
146    }
147    catch (string& ex) {
148       cout << "string: " << ex << endl;
149    }
150    catch (char* ex) {
151       cout << "char* :  " << ex << endl;
152    }
153 
154    catch (...)
155    {
156       cout << "unknown exception occured" << endl;
157       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
158       cout << e.toXml() << endl;
159    }
160 
161    return 0;
162 }
163  
164 #else // COMPILE_CORBA_PLUGIN
165 #include <iostream>
166 int main(int args, char ** argv)
167 {
168    ::std::cout << "TestCorbaDriver: COMPILE_CORBA_PLUGIN is not defined, nothing to do" << ::std::endl;
169    return 0;
170 }
171 #endif // COMPILE_CORBA_PLUGIN


syntax highlighted by Code2HTML, v. 0.9.1