00001 /*----------------------------------------------------------------------------- 00002 Name: TestGet.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Testing get() 00006 -----------------------------------------------------------------------------*/ 00007 #include "TestSuite.h" 00008 #include <iostream> 00009 00010 using namespace std; 00011 using namespace org::xmlBlaster::util; 00012 using namespace org::xmlBlaster::util::qos; 00013 using namespace org::xmlBlaster::util::thread; 00014 using namespace org::xmlBlaster::client; 00015 using namespace org::xmlBlaster::client::qos; 00016 using namespace org::xmlBlaster::client::key; 00017 using org::xmlBlaster::authentication::SecurityQos; 00018 00027 namespace org { namespace xmlBlaster { namespace test { 00028 00029 class TestGet : public TestSuite 00030 { 00031 00032 private: 00033 string publishOid_; 00034 string loginName_; 00035 string senderContent_; 00036 string contentMime_; 00037 string contentMimeExtended_; 00038 int numReceived_; // error checking 00039 00045 public: 00046 TestGet(int args, char *argc[], const string &loginName) 00047 : TestSuite(args, argc, "TestGet") 00048 { 00049 loginName_ = loginName; 00050 publishOid_ = "TestGet"; 00051 senderContent_ = "A test message"; 00052 contentMime_ = "text/xml"; 00053 contentMimeExtended_ = "1.0"; 00054 numReceived_ = 0; 00055 } 00056 00057 ~TestGet() 00058 { 00059 } 00060 00066 void setUp() 00067 { 00068 log_.info(ME, "Trying to connect to xmlBlaster with C++ client lib " + Global::getVersion() + " from " + Global::getBuildTimestamp()); 00069 TestSuite::setUp(); 00070 try { 00071 string passwd = "secret"; 00072 SecurityQos secQos(global_, loginName_, passwd); 00073 ConnectQos connQos(global_); 00074 connQos.setSecurityQos(secQos); 00075 connection_.connect(connQos, NULL); 00076 log_.info(ME, "Successful connection"); 00077 } 00078 catch (XmlBlasterException &ex) { 00079 log_.error(ME, ex.toXml()); 00080 usage(); 00081 assert(0); 00082 } 00083 } 00084 00085 00091 void tearDown() { 00092 EraseKey eraseKey(global_); 00093 eraseKey.setOid(publishOid_); 00094 00095 EraseQos eraseQos(global_); 00096 00097 vector<EraseReturnQos> returnQosArr; 00098 try { 00099 returnQosArr = connection_.erase(eraseKey, eraseQos); 00100 log_.info(ME, "Success, erased a message"); 00101 } 00102 catch(XmlBlasterException &e) { 00103 log_.error(ME, "XmlBlasterException: " + e.toXml()); 00104 } 00105 if (returnQosArr.size() != 1) { 00106 log_.error(ME, "Erased " + lexical_cast<string>(returnQosArr.size()) + " messages"); 00107 } 00108 // this is still old fashion ... 00109 connection_.disconnect(DisconnectQos(global_)); 00110 // Give the server some millis to finish the iiop handshake ... 00111 Thread::sleep(200); 00112 log_.info(ME, "Success, logged out"); 00113 00114 TestSuite::tearDown(); 00115 } 00116 00117 00123 void testGet() 00124 { 00125 if (log_.trace()) log_.trace(ME, "1. Get a not existing message " + publishOid_ + " ..."); 00126 try { 00127 GetKey getKey(global_); 00128 getKey.setOid(publishOid_); 00129 GetQos getQos(global_); 00130 vector<util::MessageUnit> msgVec = connection_.get(getKey, getQos); 00131 log_.info(ME, "Success, got array of size " + lexical_cast<string>(msgVec.size()) + 00132 " for trying to get unknown message"); 00133 assert(msgVec.size() == 0); 00134 } 00135 catch(XmlBlasterException &e) { 00136 log_.error(ME, "get of not existing message " + publishOid_ + ": " + e.getMessage()); 00137 usage(); 00138 assert(0); 00139 } 00140 00141 if (log_.trace()) log_.trace(ME, "2. Publish a message ..."); 00142 00143 try { 00144 PublishKey publishKey(global_); 00145 publishKey.setOid(publishOid_); 00146 publishKey.setContentMime("text/plain"); 00147 00148 PublishQos publishQos(global_); 00149 MessageUnit msgUnit(publishKey, senderContent_, publishQos); 00150 connection_.publish(msgUnit); 00151 log_.info(ME, "Success, published a message"); 00152 } 00153 catch(XmlBlasterException &e) { 00154 log_.error(ME, "publish - XmlBlasterException: " + e.toXml()); 00155 usage(); 00156 assert(0); 00157 } 00158 00159 if (log_.trace()) log_.trace(ME, "3. Get an existing message ..."); 00160 try { 00161 GetKey getKey(global_); 00162 getKey.setOid(publishOid_); 00163 GetQos getQos(global_); 00164 vector<MessageUnit> msgVec = connection_.get(getKey, getQos); 00165 log_.info(ME, "Success, got " + lexical_cast<string>(msgVec.size()) + " message"); 00166 assert(msgVec.size() == 1); 00167 string str = msgVec[0].getContentStr(); 00168 if (senderContent_ != str) { 00169 log_.error(ME, "Corrupted content expected '" + senderContent_ + "' size=" + 00170 lexical_cast<string>(senderContent_.size()) + " but was '" + str + 00171 "' size=" + lexical_cast<string>(str.size()) + " and contentLen=" + 00172 lexical_cast<string>(msgVec[0].getContentLen())); 00173 usage(); 00174 assert(0); 00175 } 00176 } 00177 catch(XmlBlasterException &e) { 00178 log_.error(ME, string("XmlBlasterException for trying to get ") 00179 + "a message: " + e.toXml()); 00180 usage(); 00181 assert(0); 00182 } 00183 } 00184 00185 00189 void testMany() 00190 { 00191 int num = 50; 00192 log_.info(ME, "Get " + lexical_cast<string>(num) + " not existing messages ..."); 00193 GetKey getKey(global_); 00194 getKey.setOid("NotExistingMessage"); 00195 GetQos getQos(global_); 00196 for (int i=0; i < num; i++) { 00197 try { 00198 vector<MessageUnit> msgVec = connection_.get(getKey, getQos); 00199 assert(msgVec.size() == 0); 00200 log_.info(ME, string("Success")); 00201 } 00202 catch(XmlBlasterException &e) { 00203 log_.error(ME, "Exception for a not existing message: " + e.toXml()); 00204 assert(0); 00205 } 00206 } 00207 log_.info(ME, "Get " + lexical_cast<string>(num) + " not existing messages done"); 00208 } 00209 00210 void usage() const 00211 { 00212 TestSuite::usage(); 00213 log_.plain(ME, "----------------------------------------------------------"); 00214 log_.plain(ME, "Testing C++/CORBA access to xmlBlaster with a synchronous get()"); 00215 log_.plain(ME, "Usage:"); 00216 XmlBlasterAccess::usage(); 00217 log_.usage(); 00218 log_.plain(ME, "Example:"); 00219 log_.plain(ME, " TestGet -bootstrapHostname serverHost.myCompany.com"); 00220 log_.plain(ME, "----------------------------------------------------------"); 00221 } 00222 }; 00223 00224 }}} // namespace 00225 00226 using namespace org::xmlBlaster::test; 00227 00228 00229 int main(int args, char *argc[]) 00230 { 00231 int ret = -1; 00232 try { 00233 org::xmlBlaster::util::Object_Lifetime_Manager::init(); 00234 TestGet *testObj = new TestGet(args, argc, "Tim"); 00235 testObj->setUp(); 00236 testObj->testMany(); 00237 testObj->testGet(); 00238 testObj->tearDown(); 00239 delete testObj; 00240 testObj = NULL; 00241 ret = 0; 00242 } 00243 catch (XmlBlasterException& err) { 00244 cout << "exception occurred in main string = " << err.getMessage() << endl; 00245 } 00246 catch (const exception& err) { 00247 cout << "exception occurred in main string = " << err.what() << endl; 00248 } 00249 catch (const string& err) { 00250 cout << "exception occurred in main string = " << err << endl; 00251 } 00252 catch (const char* err) { 00253 cout << "exception occurred in main char* = " << err << endl; 00254 } 00255 catch (...) { 00256 cout << "exception occurred in main"<< endl; 00257 } 00258 org::xmlBlaster::util::Object_Lifetime_Manager::fini(); 00259 return ret; 00260 } 00261