00001 /*----------------------------------------------------------------------------- 00002 Name: TestConnect.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Login/logout test for xmlBlaster 00006 -----------------------------------------------------------------------------*/ 00007 00022 #include "TestSuite.h" 00023 #include <util/qos/ConnectQosFactory.h> 00024 #include <iostream> 00025 #ifdef XMLBLASTER_MICO 00026 # include <mico/version.h> 00027 #endif 00028 00029 using namespace std; 00030 using namespace org::xmlBlaster::util; 00031 using namespace org::xmlBlaster::util::qos; 00032 using namespace org::xmlBlaster::util::thread; 00033 using namespace org::xmlBlaster::client; 00034 using namespace org::xmlBlaster::client::key; 00035 using namespace org::xmlBlaster::client::qos; 00036 00037 namespace org { namespace xmlBlaster { namespace test { 00038 00039 class TestConnect : public virtual client::I_Callback, public TestSuite 00040 { 00041 00042 private: 00043 00044 string publishReturnQos, secondOid_; 00045 string oid_; 00046 string qos1_, qos2_; 00047 string senderContent_; 00048 XmlBlasterAccess* conn2_; 00049 MessageUnit* msgUnit_; 00050 int numReceived_; // error checking 00051 string contentMime_; 00052 string contentMimeExtended_; 00053 00054 public: 00062 TestConnect(int args, char * argv[], const string &qos1, const string &qos2) 00063 : TestSuite(args, argv, "TestConnect") 00064 { 00065 qos1_ = qos1; 00066 qos2_ = qos2; 00067 publishReturnQos = ""; 00068 secondOid_ = "SecondOid"; 00069 oid_ = "TestLogin"; 00070 numReceived_ = 0; 00071 contentMime_ = "text/plain"; 00072 contentMimeExtended_ = "1.0"; 00073 msgUnit_ = NULL; 00074 conn2_ = NULL; 00075 } 00076 00077 ~TestConnect() { 00078 cout << "Destructor for TestConnect invoked" << endl; 00079 delete conn2_; 00080 conn2_ = 0; 00081 delete msgUnit_; 00082 msgUnit_ = 0; 00083 } 00084 00085 00101 string update(const string &/*sessionId*/, 00102 UpdateKey &/*updateKey*/, 00103 const unsigned char * /*content*/, 00104 long /*contentSize*/, 00105 UpdateQos &/*updateQos*/) 00106 { 00107 if (log_.call()) log_.call(ME, "Receiving update of a message ..."); 00108 numReceived_++; 00109 return "<qos><state id='OK'/></qos>"; 00110 } 00111 00117 void setUp() 00118 { 00119 TestSuite::setUp(); 00120 try { 00121 00122 // Login to xmlBlaster 00123 ConnectQosFactory factory(global_); 00124 if (conn2_) delete conn2_; 00125 ConnectQosRef connectQos2 = factory.readObject(qos2_); 00126 conn2_ = new XmlBlasterAccess(global_); 00127 conn2_->connect(*connectQos2, NULL); 00128 00129 conn2_->disconnect(DisconnectQos(global_)); 00130 delete conn2_; 00131 conn2_ = NULL; 00132 00133 ConnectQosRef connectQos1 = factory.readObject(qos1_); 00134 connection_.connect(*connectQos1, this); 00135 00136 } 00137 catch (XmlBlasterException &e) { 00138 log_.error(ME, e.toXml()); 00139 usage(); 00140 } 00141 } 00142 00143 00144 void testPubSub() 00145 { 00146 try { 00147 log_.info(ME, "testPubSub"); 00148 00149 00150 PublishKey pubKey(global_); 00151 pubKey.setOid("testConnect"); 00152 PublishQos pubQos(global_); 00153 MessageUnit msgUnit(pubKey, "This is a happy day!", pubQos); 00154 connection_.publish(msgUnit); 00155 Thread::sleepSecs(1); 00156 00157 SubscribeKey subKey(global_); 00158 subKey.setOid("testConnect"); 00159 00160 SubscribeQos subQos(global_); 00161 string subscribeOid_ = connection_.subscribe(subKey, subQos).getSubscriptionId(); 00162 log_.info(ME, string("Success: Subscribe subscription-id=") + subscribeOid_ + " done"); 00163 00164 Thread::sleepSecs(2); 00165 assertEquals(log_, ME, 1, numReceived_, "reconnecting when communication down and giving positive publicSessionId: no exception expected"); 00166 00167 EraseKey key(global_); 00168 key.setOid("testConnect"); 00169 EraseQos qos(global_); 00170 connection_.erase(key, qos); 00171 Thread::sleepSecs(1); 00172 log_.info(ME, "testPubSub successfully completed"); 00173 } 00174 catch (XmlBlasterException &e) { 00175 log_.error(ME, e.toXml()); 00176 usage(); 00177 } 00178 } 00179 00180 00186 void tearDown() 00187 { 00188 TestSuite::tearDown(); 00189 connection_.disconnect(DisconnectQos(global_)); 00190 // conn2_->disconnect(DisconnectQos(global_)); 00191 } 00192 00193 }; 00194 00195 }}} // namespace 00196 00197 using namespace org::xmlBlaster::test; 00198 00199 int main(int args, char *argc[]) { 00200 org::xmlBlaster::util::Object_Lifetime_Manager::init(); 00201 00202 # ifdef XMLBLASTER_MICO 00203 if (MICO_BIN_VERSION < 0x02030b) { 00204 std::cout << " !!!!! THIS TEST CAN NOT BE RUN WITH MICO SINCE AN ORB WHICH IS SHUTDOWN CAN NOT BE REUSED !!!!" << std::endl; 00205 std::cout << " !!!!! IT HAS BEEN TESTED AND IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8 !!!!" << std::endl; 00206 std::cout << " !!!!! IT IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8 !!!!" << std::endl; 00207 std::cout << " !!!!! TRY IT WITH ANOTHER CORBA IMPLEMENTATION (for example TAO) !!!!" << std::endl; 00208 exit(-1); 00209 } 00210 else { 00211 std::cout << "MICO Version " << MICO_VERSION << " should run fine" << std::endl; 00212 } 00213 # endif 00214 00215 string qos1 = 00216 string("<qos>\n") + 00217 string(" <securityService type='htpasswd' version='1.0'>\n") + 00218 string(" <![CDATA[\n") + 00219 string(" <user>ticheta</user>\n") + 00220 string(" <passwd>secret</passwd>\n") + 00221 string(" ]]>\n") + 00222 string(" </securityService>\n") + 00223 string(" <session name='ticheta'/>\n") + 00224 string(" <ptp>false</ptp>\n") + 00225 string("</qos>\n"); 00226 00227 string qos2 = 00228 string("<qos>\n") + 00229 string(" <securityService type='htpasswd' version='1.0'>\n") + 00230 string(" <![CDATA[\n") + 00231 string(" <user>tacatitac</user>\n") + 00232 string(" <passwd>secret</passwd>\n") + 00233 string(" ]]>\n") + 00234 string(" </securityService>\n") + 00235 string(" <session name='tacatitac'/>\n") + 00236 string(" <ptp>false</ptp>\n") + 00237 string("</qos>\n"); 00238 00239 Global& glob = Global::getInstance(); 00240 glob.initialize(args, argc); 00241 TestConnect *testConnect = new TestConnect(args, argc, qos1, qos2); 00242 testConnect->setUp(); 00243 testConnect->testPubSub(); 00244 testConnect->tearDown(); 00245 delete testConnect; 00246 org::xmlBlaster::util::Object_Lifetime_Manager::fini(); 00247 return 0; 00248 } 00249