1 /*-----------------------------------------------------------------------------
  2 Name:      TestLeaveServer.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo code for a client using xmlBlaster
  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 namespace org::xmlBlaster::authentication;
 18 
 19 namespace org { namespace xmlBlaster { namespace test {
 20 
 21 /**
 22  * This client tests the method leaveServer(). 
 23  */
 24 class TestLeaveServer: public TestSuite, public virtual I_Callback 
 25 {
 26 private:
 27    string loginName_;
 28    ConnectReturnQos returnQos_;
 29 
 30    /** Publish tests */
 31    enum TestType {
 32       TEST_ONEWAY, TEST_PUBLISH, TEST_ARRAY
 33    };
 34 
 35    /**
 36     * Constructs the TestLeaveServer object.
 37     * <p />
 38     * @param testName  The name used in the test suite
 39     * @param loginName The name to login to the xmlBlaster
 40     */
 41  public:
 42    TestLeaveServer(int args, char *argc[], const string &loginName)
 43       : TestSuite(args, argc, "TestLeaveServer"), returnQos_(global_)
 44    {
 45       loginName_ = loginName;
 46    }
 47 
 48    virtual ~TestLeaveServer() 
 49    {
 50    }
 51 
 52    /**
 53     * Sets up the fixture. <p />
 54     * Connect to xmlBlaster and login
 55     */
 56    void setUp() 
 57    {
 58       log_.info(ME, "Trying to connect to xmlBlaster with C++ client lib " + Global::getVersion() + " from " + Global::getBuildTimestamp());
 59       TestSuite::setUp();
 60       try {
 61          string passwd = "secret";
 62          SecurityQos secQos(global_, loginName_, passwd);
 63          ConnectQos connQos(global_);
 64          connQos.getSessionQosRef()->setPubSessionId(3L);
 65          returnQos_ = connection_.connect(connQos, this);
 66          string name = returnQos_.getSessionQos().getAbsoluteName();
 67          string name1 = returnQos_.getSessionQosRef()->getAbsoluteName();
 68          assertEquals(log_, ME, name, name1, string("name comparison for reference"));
 69 
 70          log_.info(ME, string("connection setup: the session name is '") + name + "'");
 71          // Login to xmlBlaster
 72       }
 73       catch (XmlBlasterException &e) {
 74          log_.error(ME, string("Login failed: ") + e.toXml());
 75          assert(0);
 76       }
 77    }
 78 
 79 
 80    /**
 81     * Tears down the fixture. <p />
 82     * cleaning up .... erase() the previous message OID and logout
 83     */
 84    void tearDown() 
 85    {
 86       TestSuite::tearDown();
 87    }
 88 
 89 
 90    /**
 91     * TEST: 
 92     */
 93    void testLeaveServer()
 94    {
 95       log_.info(ME, "testLeaveServer() ...");
 96 
 97       try {
 98          GetKey getKey(global_);
 99          getKey.setOid("__cmd:?freeMem");
100          GetQos getQos(global_);
101          vector<util::MessageUnit> msgVec = connection_.get(getKey, getQos);
102          log_.info(ME, "Success, got array of size " + lexical_cast<string>(msgVec.size()) +
103                          " for trying to get __cmd:?freeMem");
104          assert(msgVec.size() == 1);
105       }
106       catch(XmlBlasterException &e) {
107          log_.error(ME, "get of '__cmd:?freeMem' failed: " + e.toString());
108          assert(0);
109       }
110 
111       try {
112          StringMap map;
113          connection_.leaveServer(map);
114          log_.info(ME, string("Success: leaveServer()"));
115       }
116       catch(XmlBlasterException &e) {
117          log_.warn(ME, string("XmlBlasterException: ")+e.toXml());
118          assert(0);
119       }
120 
121       try {
122          StringMap map;
123          connection_.leaveServer(map);
124          log_.error(ME, string("leaveServer(): Leaving server twice should fail"));
125                         assert(0);
126       }
127       catch(XmlBlasterException &e) {
128          log_.info(ME, string("SUCCESS: Expected XmlBlasterException: ")+e.toString());
129       }
130 
131 
132       try {
133          GetKey getKey(global_);
134          getKey.setOid("__cmd:?freeMem");
135          GetQos getQos(global_);
136          vector<util::MessageUnit> msgVec = connection_.get(getKey, getQos);
137          log_.error(ME, string("leaveServer(): Calling get() after leaving server should fail, msgs=") + lexical_cast<string>(msgVec.size()));
138          assert(0);
139       }
140       catch(XmlBlasterException &e) {
141          log_.info(ME, string("SUCCESS: Expected XmlBlasterException: ")+e.toString());
142       }
143 
144       log_.info(ME, "SUCCESS: testLeaveServer() DONE");
145    }
146 
147 
148    string update(const string &sessionId,
149                UpdateKey &updateKey,
150                const unsigned char * /*content*/, long /*contentSize*/,
151                UpdateQos &updateQos) 
152    {
153       log_.error(ME, string("Receiving update of message oid=") +
154                 updateKey.getOid() + " state=" + updateQos.getState() +
155                 " authentication sessionId=" + sessionId + " ...");
156       return "<qos><state id='OK'/></qos>";
157    }
158 };
159 
160 }}} // namespace
161 
162 using namespace org::xmlBlaster::test;
163 
164 int main(int args, char *argc[]) 
165 {
166    try {
167       org::xmlBlaster::util::Object_Lifetime_Manager::init();
168       TestLeaveServer testSub(args, argc, "TestLeaveServer");
169       testSub.setUp();
170       testSub.testLeaveServer();
171       testSub.tearDown();
172    }
173    catch (XmlBlasterException& ex) {
174       std::cout << ex.toXml() << std::endl;
175    }
176    catch (bad_exception& ex) {
177       cout << "bad_exception: " << ex.what() << endl;
178    }
179    catch (exception& ex) {
180       cout << " exception: " << ex.what() << endl;
181    }
182    catch (string& ex) {
183       cout << "string: " << ex << endl;
184    }
185    catch (char* ex) {
186       cout << "char* :  " << ex << endl;
187    }
188    catch (...)
189    {
190       cout << "unknown exception occured" << endl;
191       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
192       cout << e.toXml() << endl;
193    }
194 
195    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
196    return 0;
197 }


syntax highlighted by Code2HTML, v. 0.9.1