testsuite/src/c++/TestLeaveServer.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      TestLeaveServer.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Demo code for a client using xmlBlaster
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 namespace org::xmlBlaster::authentication;
00018 
00019 namespace org { namespace xmlBlaster { namespace test {
00020 
00024 class TestLeaveServer: public TestSuite, public virtual I_Callback 
00025 {
00026 private:
00027    string loginName_;
00028    ConnectReturnQos returnQos_;
00029 
00031    enum TestType {
00032       TEST_ONEWAY, TEST_PUBLISH, TEST_ARRAY
00033    };
00034 
00041  public:
00042    TestLeaveServer(int args, char *argc[], const string &loginName)
00043       : TestSuite(args, argc, "TestLeaveServer"), returnQos_(global_)
00044    {
00045       loginName_ = loginName;
00046    }
00047 
00048    virtual ~TestLeaveServer() 
00049    {
00050    }
00051 
00056    void setUp() 
00057    {
00058       log_.info(ME, "Trying to connect to xmlBlaster with C++ client lib " + Global::getVersion() + " from " + Global::getBuildTimestamp());
00059       TestSuite::setUp();
00060       try {
00061          string passwd = "secret";
00062          SecurityQos secQos(global_, loginName_, passwd);
00063          ConnectQos connQos(global_);
00064          connQos.getSessionQosRef()->setPubSessionId(3L);
00065          returnQos_ = connection_.connect(connQos, this);
00066          string name = returnQos_.getSessionQos().getAbsoluteName();
00067          string name1 = returnQos_.getSessionQosRef()->getAbsoluteName();
00068          assertEquals(log_, ME, name, name1, string("name comparison for reference"));
00069 
00070          log_.info(ME, string("connection setup: the session name is '") + name + "'");
00071          // Login to xmlBlaster
00072       }
00073       catch (XmlBlasterException &e) {
00074          log_.error(ME, string("Login failed: ") + e.toXml());
00075          assert(0);
00076       }
00077    }
00078 
00079 
00084    void tearDown() 
00085    {
00086       TestSuite::tearDown();
00087    }
00088 
00089 
00093    void testLeaveServer()
00094    {
00095       log_.info(ME, "testLeaveServer() ...");
00096 
00097       try {
00098          GetKey getKey(global_);
00099          getKey.setOid("__cmd:?freeMem");
00100          GetQos getQos(global_);
00101          vector<util::MessageUnit> msgVec = connection_.get(getKey, getQos);
00102          log_.info(ME, "Success, got array of size " + lexical_cast<string>(msgVec.size()) +
00103                          " for trying to get __cmd:?freeMem");
00104          assert(msgVec.size() == 1);
00105       }
00106       catch(XmlBlasterException &e) {
00107          log_.error(ME, "get of '__cmd:?freeMem' failed: " + e.toString());
00108          assert(0);
00109       }
00110 
00111       try {
00112          StringMap map;
00113          connection_.leaveServer(map);
00114          log_.info(ME, string("Success: leaveServer()"));
00115       }
00116       catch(XmlBlasterException &e) {
00117          log_.warn(ME, string("XmlBlasterException: ")+e.toXml());
00118          assert(0);
00119       }
00120 
00121       try {
00122          StringMap map;
00123          connection_.leaveServer(map);
00124          log_.error(ME, string("leaveServer(): Leaving server twice should fail"));
00125                         assert(0);
00126       }
00127       catch(XmlBlasterException &e) {
00128          log_.info(ME, string("SUCCESS: Expected XmlBlasterException: ")+e.toString());
00129       }
00130 
00131 
00132       try {
00133          GetKey getKey(global_);
00134          getKey.setOid("__cmd:?freeMem");
00135          GetQos getQos(global_);
00136          vector<util::MessageUnit> msgVec = connection_.get(getKey, getQos);
00137          log_.error(ME, string("leaveServer(): Calling get() after leaving server should fail, msgs=") + lexical_cast<string>(msgVec.size()));
00138          assert(0);
00139       }
00140       catch(XmlBlasterException &e) {
00141          log_.info(ME, string("SUCCESS: Expected XmlBlasterException: ")+e.toString());
00142       }
00143 
00144       log_.info(ME, "SUCCESS: testLeaveServer() DONE");
00145    }
00146 
00147 
00148    string update(const string &sessionId,
00149                UpdateKey &updateKey,
00150                const unsigned char * /*content*/, long /*contentSize*/,
00151                UpdateQos &updateQos) 
00152    {
00153       log_.error(ME, string("Receiving update of message oid=") +
00154                 updateKey.getOid() + " state=" + updateQos.getState() +
00155                 " authentication sessionId=" + sessionId + " ...");
00156       return "<qos><state id='OK'/></qos>";
00157    }
00158 };
00159 
00160 }}} // namespace
00161 
00162 using namespace org::xmlBlaster::test;
00163 
00164 int main(int args, char *argc[]) 
00165 {
00166    try {
00167       org::xmlBlaster::util::Object_Lifetime_Manager::init();
00168       TestLeaveServer testSub(args, argc, "TestLeaveServer");
00169       testSub.setUp();
00170       testSub.testLeaveServer();
00171       testSub.tearDown();
00172    }
00173    catch (XmlBlasterException& ex) {
00174       std::cout << ex.toXml() << std::endl;
00175    }
00176    catch (bad_exception& ex) {
00177       cout << "bad_exception: " << ex.what() << endl;
00178    }
00179    catch (exception& ex) {
00180       cout << " exception: " << ex.what() << endl;
00181    }
00182    catch (string& ex) {
00183       cout << "string: " << ex << endl;
00184    }
00185    catch (char* ex) {
00186       cout << "char* :  " << ex << endl;
00187    }
00188    catch (...)
00189    {
00190       cout << "unknown exception occured" << endl;
00191       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
00192       cout << e.toXml() << endl;
00193    }
00194 
00195    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00196    return 0;
00197 }
00198 
00199