1 /*-----------------------------------------------------------------------------
  2 Name:      TestGet.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing get()
  6 -----------------------------------------------------------------------------*/
  7 #include "TestSuite.h"
  8 #include <iostream>
  9 
 10 using namespace std;
 11 using namespace org::xmlBlaster::util;
 12 using namespace org::xmlBlaster::util::qos;
 13 using namespace org::xmlBlaster::util::thread;
 14 using namespace org::xmlBlaster::client;
 15 using namespace org::xmlBlaster::client::qos;
 16 using namespace org::xmlBlaster::client::key;
 17 using org::xmlBlaster::authentication::SecurityQos;
 18 
 19 /**
 20  * This client tests the synchronous method get() with its different qos
 21  * variants.<p>
 22  * This client may be invoked multiple time on the same xmlBlaster server,
 23  * as it cleans up everything after his tests are done.
 24  * <p>
 25  */
 26 
 27 namespace org { namespace xmlBlaster { namespace test {
 28 
 29 class TestGet : public TestSuite
 30 {
 31 
 32 private:
 33    string publishOid_;
 34    string loginName_;
 35    string senderContent_;
 36    string contentMime_;
 37    string contentMimeExtended_;
 38    int    numReceived_;  // error checking
 39 
 40    /**
 41     * Constructs the TestGet object.
 42     * <p />
 43     * @param loginName The name to login to the xmlBlaster
 44     */
 45 public:
 46    TestGet(int args, char *argc[], const string &loginName)
 47       : TestSuite(args, argc, "TestGet")
 48    {
 49       loginName_           = loginName;
 50       publishOid_          = "TestGet";
 51       senderContent_       = "A test message";
 52       contentMime_         = "text/xml";
 53       contentMimeExtended_ = "1.0";
 54       numReceived_         = 0;
 55    }
 56 
 57    ~TestGet() 
 58    {
 59    }
 60 
 61    /**
 62     * Sets up the fixture.
 63     * <p />
 64     * Connect to xmlBlaster and login
 65     */
 66    void setUp() 
 67    {
 68       log_.info(ME, "Trying to connect to xmlBlaster with C++ client lib " + Global::getVersion() + " from " + Global::getBuildTimestamp());
 69       TestSuite::setUp();
 70       try {
 71          string passwd = "secret";
 72          SecurityQos secQos(global_, loginName_, passwd);
 73          ConnectQos connQos(global_);
 74          connQos.setSecurityQos(secQos);
 75          connection_.connect(connQos, NULL);
 76          log_.info(ME, "Successful connection");
 77       }
 78       catch (XmlBlasterException &ex) {
 79          log_.error(ME, ex.toXml());
 80          usage();
 81          assert(0);
 82       }
 83    }
 84 
 85 
 86    /**
 87     * Tears down the fixture.
 88     * <p />
 89     * cleaning up .... erase() the previous message OID and logout
 90     */
 91    void tearDown() {
 92       EraseKey eraseKey(global_);
 93       eraseKey.setOid(publishOid_);
 94 
 95       EraseQos eraseQos(global_);
 96 
 97       vector<EraseReturnQos> returnQosArr;
 98       try {
 99          returnQosArr = connection_.erase(eraseKey, eraseQos);
100          log_.info(ME, "Success, erased a message");
101       }
102       catch(XmlBlasterException &e) {
103          log_.error(ME, "XmlBlasterException: " + e.toXml());
104       }
105       if (returnQosArr.size() != 1) {
106          log_.error(ME, "Erased " + lexical_cast<string>(returnQosArr.size()) + " messages");
107       }
108       // this is still old fashion ...
109       connection_.disconnect(DisconnectQos(global_));
110       // Give the server some millis to finish the iiop handshake ...
111       Thread::sleep(200);
112       log_.info(ME, "Success, logged out");
113 
114       TestSuite::tearDown();
115    }
116 
117 
118    /**
119     * TEST: Get an not existing and an existing message
120     * <p />
121     * The returned content is checked
122     */
123    void testGet() 
124    {
125       if (log_.trace()) log_.trace(ME, "1. Get a not existing message " + publishOid_ + " ...");
126       try {
127          GetKey getKey(global_);
128          getKey.setOid(publishOid_);
129          GetQos getQos(global_);
130          vector<util::MessageUnit> msgVec = connection_.get(getKey, getQos);
131          log_.info(ME, "Success, got array of size " + lexical_cast<string>(msgVec.size()) +
132                          " for trying to get unknown message");
133          assert(msgVec.size() == 0);
134       }
135       catch(XmlBlasterException &e) {
136          log_.error(ME, "get of not existing message " + publishOid_ + ": " + e.getMessage());
137          usage();
138          assert(0);
139       }
140 
141       if (log_.trace()) log_.trace(ME, "2. Publish a message ...");
142 
143       try {
144          PublishKey publishKey(global_);
145          publishKey.setOid(publishOid_);
146          publishKey.setContentMime("text/plain");
147 
148          PublishQos publishQos(global_);
149          MessageUnit msgUnit(publishKey, senderContent_, publishQos);
150          connection_.publish(msgUnit);
151          log_.info(ME, "Success, published a message");
152       }
153       catch(XmlBlasterException &e) {
154          log_.error(ME, "publish - XmlBlasterException: " + e.toXml());
155          usage();
156          assert(0);
157       }
158 
159       if (log_.trace()) log_.trace(ME, "3. Get an existing message ...");
160       try {
161          GetKey getKey(global_);
162          getKey.setOid(publishOid_);
163          GetQos getQos(global_);
164          vector<MessageUnit> msgVec = connection_.get(getKey, getQos);
165          log_.info(ME, "Success, got " + lexical_cast<string>(msgVec.size()) + " message");
166          assert(msgVec.size() == 1);
167          string str = msgVec[0].getContentStr();
168          if (senderContent_ != str) {
169             log_.error(ME, "Corrupted content expected '" + senderContent_ + "' size=" +
170                              lexical_cast<string>(senderContent_.size()) + " but was '" + str +
171                              "' size=" + lexical_cast<string>(str.size()) + " and contentLen=" +
172                              lexical_cast<string>(msgVec[0].getContentLen()));
173             usage();
174             assert(0);
175          }
176       }
177       catch(XmlBlasterException &e) {
178          log_.error(ME, string("XmlBlasterException for trying to get ")
179                     + "a message: " + e.toXml());
180          usage();
181          assert(0);
182       }
183    }
184 
185 
186    /**
187     * LOAD TEST: get 50 times a non-existing message
188     */
189    void testMany() 
190    {
191       int num = 50;
192       log_.info(ME, "Get " + lexical_cast<string>(num) + " not existing messages ...");
193       GetKey getKey(global_);
194       getKey.setOid("NotExistingMessage");
195       GetQos getQos(global_);
196       for (int i=0; i < num; i++) {
197          try {
198             vector<MessageUnit> msgVec = connection_.get(getKey, getQos);
199             assert(msgVec.size() == 0);
200             log_.info(ME, string("Success"));
201          }
202          catch(XmlBlasterException &e) {
203             log_.error(ME, "Exception for a not existing message: " + e.toXml());
204             assert(0);
205          }
206       }
207       log_.info(ME, "Get " + lexical_cast<string>(num) + " not existing messages done");
208    }
209 
210    void usage() const
211    {
212                 TestSuite::usage();
213       log_.plain(ME, "----------------------------------------------------------");
214       log_.plain(ME, "Testing C++/CORBA access to xmlBlaster with a synchronous get()");
215       log_.plain(ME, "Usage:");
216       XmlBlasterAccess::usage();
217       log_.usage();
218       log_.plain(ME, "Example:");
219       log_.plain(ME, "   TestGet -bootstrapHostname serverHost.myCompany.com");
220       log_.plain(ME, "----------------------------------------------------------");
221    }
222 };
223 
224 }}} // namespace
225 
226 using namespace org::xmlBlaster::test;
227 
228   
229 int main(int args, char *argc[]) 
230 {
231    int ret = -1;
232    try {
233       org::xmlBlaster::util::Object_Lifetime_Manager::init();
234       TestGet *testObj = new TestGet(args, argc, "Tim");
235       testObj->setUp();
236       testObj->testMany();
237       testObj->testGet();
238       testObj->tearDown();
239       delete testObj;
240       testObj = NULL;
241       ret = 0;
242    }
243    catch (XmlBlasterException& err) {
244       cout << "exception occurred in main string = " << err.getMessage() << endl;
245    }
246    catch (const exception& err) {
247       cout << "exception occurred in main string = " << err.what() << endl;
248    }
249    catch (const string& err) {
250       cout << "exception occurred in main string = " << err << endl;
251    }
252    catch (const char* err) {
253       cout << "exception occurred in main char* = " << err << endl;
254    }
255    catch (...) {
256       cout << "exception occurred in main"<< endl;
257    }
258    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
259    return ret;
260 }


syntax highlighted by Code2HTML, v. 0.9.1