1 /*-----------------------------------------------------------------------------
  2 Name:      TestConnect.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Login/logout test for xmlBlaster
  6 -----------------------------------------------------------------------------*/
  7 
  8 /**
  9  * This client does test connect and disconnect.<br />
 10  * login/logout combinations are checked with subscribe()/publish() calls
 11  * <p />
 12  * This client may be invoked multiple time on the same xmlBlaster server,
 13  * as it cleans up everything after his tests are done.
 14  * <p>
 15  * Invoke examples:<br />
 16  * <pre>
 17  *    java -jar lib/xmlBlaster.jar    (Server)
 18  *
 19  *    TestConnect                     (Client)
 20  * </pre>
 21  */
 22 #include "TestSuite.h"
 23 #include <util/qos/ConnectQosFactory.h>
 24 #include <iostream>
 25 #ifdef XMLBLASTER_MICO
 26 #  include <mico/version.h>
 27 #endif
 28 
 29 using namespace std;
 30 using namespace org::xmlBlaster::util;
 31 using namespace org::xmlBlaster::util::qos;
 32 using namespace org::xmlBlaster::util::thread;
 33 using namespace org::xmlBlaster::client;
 34 using namespace org::xmlBlaster::client::key;
 35 using namespace org::xmlBlaster::client::qos;
 36 
 37 namespace org { namespace xmlBlaster { namespace test {
 38 
 39 class TestConnect : public virtual client::I_Callback, public TestSuite
 40 {
 41 
 42 private:
 43 
 44    string            publishReturnQos, secondOid_;
 45    string            oid_;
 46    string            qos1_, qos2_;
 47    string            senderContent_;
 48    XmlBlasterAccess* conn2_;
 49    MessageUnit*      msgUnit_;
 50    int               numReceived_; // error checking
 51    string            contentMime_;
 52    string            contentMimeExtended_;
 53 
 54 public:
 55    /**
 56     * Constructs the TestLogin object.
 57     * <p />
 58     * @param testName   The name used in the test suite
 59     * @param loginName  The name to login to the xmlBlaster
 60     * @param secondName The name to login to the xmlBlaster again
 61     */
 62    TestConnect(int args, char * argv[], const string &qos1, const string &qos2)
 63       : TestSuite(args, argv, "TestConnect")
 64    {
 65       qos1_                = qos1;
 66       qos2_                = qos2;
 67       publishReturnQos     = "";
 68       secondOid_           = "SecondOid";
 69       oid_                 = "TestLogin";
 70       numReceived_         = 0;
 71       contentMime_         = "text/plain";
 72       contentMimeExtended_ = "1.0";
 73       msgUnit_             = NULL;
 74       conn2_               = NULL;
 75    }
 76 
 77    ~TestConnect() {
 78       cout << "Destructor for TestConnect invoked" << endl;
 79       delete conn2_;
 80       conn2_ = 0;
 81       delete msgUnit_;
 82       msgUnit_ = 0;
 83    }
 84 
 85 
 86    /**
 87     * This is the callback method (I_Callback) invoked from CorbaConnection
 88     * informing the client in an asynchronous mode about a new message.
 89     * <p />
 90     * The raw CORBA-BlasterCallback.update() is unpacked and for each arrived
 91     * message this update is called.
 92     *
 93     * @param sessionId The sessionId to authenticate the callback
 94     *                  This sessionId was passed on subscription
 95     *                  we can use it to decide if we trust this update()
 96     * @param updateKey The arrived key
 97     * @param content   The arrived message content
 98     * @param qos       Quality of Service of the MessageUnit
 99     * @return The status string
100     */
101    string update(const string &/*sessionId*/,
102                  UpdateKey &/*updateKey*/,
103                  const unsigned char * /*content*/, 
104                  long /*contentSize*/,
105                  UpdateQos &/*updateQos*/) 
106    {
107       if (log_.call()) log_.call(ME, "Receiving update of a message ...");
108       numReceived_++;
109       return "<qos><state id='OK'/></qos>";
110    }
111 
112    /**
113     * Sets up the fixture. <p />
114     * Connect to xmlBlaster and login
115     */
116 
117    void setUp() 
118    {
119       TestSuite::setUp();
120       try {
121 
122          // Login to xmlBlaster
123          ConnectQosFactory factory(global_);
124          if (conn2_) delete conn2_;
125          ConnectQosRef connectQos2 = factory.readObject(qos2_);
126          conn2_ = new XmlBlasterAccess(global_);
127          conn2_->connect(*connectQos2, NULL);
128 
129          conn2_->disconnect(DisconnectQos(global_));
130          delete conn2_;
131          conn2_ = NULL;
132 
133          ConnectQosRef connectQos1 = factory.readObject(qos1_);
134          connection_.connect(*connectQos1, this);
135 
136       }
137       catch (XmlBlasterException &e) {
138          log_.error(ME, e.toXml());
139          usage();
140       }
141    }
142 
143 
144    void testPubSub()
145    {
146       try {
147          log_.info(ME, "testPubSub");
148 
149 
150          PublishKey pubKey(global_);
151          pubKey.setOid("testConnect");
152          PublishQos pubQos(global_);
153          MessageUnit msgUnit(pubKey, "This is a happy day!", pubQos);
154          connection_.publish(msgUnit);
155          Thread::sleepSecs(1);
156 
157          SubscribeKey subKey(global_);
158          subKey.setOid("testConnect");
159 
160          SubscribeQos subQos(global_);
161          string subscribeOid_ = connection_.subscribe(subKey, subQos).getSubscriptionId();
162          log_.info(ME, string("Success: Subscribe subscription-id=") + subscribeOid_ + " done");
163 
164          Thread::sleepSecs(2);
165          assertEquals(log_, ME, 1, numReceived_, "reconnecting when communication down and giving positive publicSessionId: no exception expected");
166 
167          EraseKey key(global_);
168          key.setOid("testConnect");
169          EraseQos qos(global_);
170          connection_.erase(key, qos);
171          Thread::sleepSecs(1);
172          log_.info(ME, "testPubSub successfully completed");
173       }
174       catch (XmlBlasterException &e) {
175          log_.error(ME, e.toXml());
176          usage();
177       }
178    }
179 
180 
181    /**
182     * Tears down the fixture.
183     * <p />
184     * cleaning up .... erase() the previous message OID and logout
185     */
186    void tearDown() 
187    {
188       TestSuite::tearDown();
189       connection_.disconnect(DisconnectQos(global_));
190 //      conn2_->disconnect(DisconnectQos(global_));
191    }
192 
193 };
194 
195 }}} // namespace
196 
197 using namespace org::xmlBlaster::test;
198 
199 int main(int args, char *argc[]) {
200    org::xmlBlaster::util::Object_Lifetime_Manager::init();
201 
202 # ifdef XMLBLASTER_MICO
203    if (MICO_BIN_VERSION < 0x02030b) {
204         std::cout << " !!!!! THIS TEST CAN NOT BE RUN WITH MICO SINCE AN ORB WHICH IS SHUTDOWN CAN NOT BE REUSED !!!!" << std::endl;
205         std::cout << " !!!!! IT HAS BEEN TESTED AND IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8                  !!!!" << std::endl;
206         std::cout << " !!!!! IT IS PROVEN TO FAIL WITH MICO 2.3.7 AND 2.3.8                                      !!!!" << std::endl;
207         std::cout << " !!!!! TRY IT WITH ANOTHER CORBA IMPLEMENTATION (for example TAO)                          !!!!" << std::endl;
208         exit(-1);
209    }
210    else {
211       std::cout << "MICO Version " << MICO_VERSION << " should run fine" << std::endl;
212    }
213 # endif
214 
215    string qos1 =
216       string("<qos>\n") +
217       string("   <securityService type='htpasswd' version='1.0'>\n") +
218       string("     <![CDATA[\n") +
219       string("     <user>ticheta</user>\n") +
220       string("     <passwd>secret</passwd>\n") +
221       string("     ]]>\n") +
222       string("   </securityService>\n") +
223       string("   <session name='ticheta'/>\n") +
224       string("   <ptp>false</ptp>\n") +
225       string("</qos>\n");
226 
227    string qos2 =
228       string("<qos>\n") +
229       string("   <securityService type='htpasswd' version='1.0'>\n") +
230       string("     <![CDATA[\n") +
231       string("     <user>tacatitac</user>\n") +
232       string("     <passwd>secret</passwd>\n") +
233       string("     ]]>\n") +
234       string("   </securityService>\n") +
235       string("   <session name='tacatitac'/>\n") +
236       string("   <ptp>false</ptp>\n") +
237       string("</qos>\n");
238 
239    Global& glob = Global::getInstance();
240    glob.initialize(args, argc);
241    TestConnect *testConnect = new TestConnect(args, argc, qos1, qos2);
242    testConnect->setUp();
243    testConnect->testPubSub();
244    testConnect->tearDown();
245    delete testConnect;
246    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
247    return 0;
248 }


syntax highlighted by Code2HTML, v. 0.9.1