testsuite/src/c++/TestSubXPath.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      TestSubXPath.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Testing the Timeout Features
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;
00018 
00019 namespace org { namespace xmlBlaster { namespace test {
00020 
00021 class TestSubXPath : public TestSuite, public virtual I_Callback
00022 {
00023 private:
00024    Mutex             updateMutex_;
00025    int               numOfUpdates_;
00026 
00027    void subscribeXPath(const string& query) 
00028    {
00029       if (log_.trace()) log_.trace(ME, "Subscribing using XPath syntax ...");
00030       SubscribeKey subKey(global_);
00031       subKey.setQueryString(query);
00032       SubscribeQos qos(global_);
00033       string subscribeOid = "";
00034       try {
00035          subscribeOid = connection_.subscribe(subKey, qos).getSubscriptionId();
00036          log_.info(ME, string("Success: Subscribe on ") + subscribeOid + " done:\n" + subKey.toXml());
00037       } catch(XmlBlasterException& e) {
00038          log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
00039          assertEquals(log_, ME, true, false, string("subscribe - XmlBlasterException: ") + e.toXml());
00040       }
00041       assertEquals(log_, ME, false, subscribeOid.empty(), "returned emty subscribeOid");
00042    }
00043 
00044 
00045 public:
00046    TestSubXPath(int args, char *argc[]) 
00047       :  TestSuite(args, argc, "TestSubXPath"), updateMutex_()
00048    {
00049       numOfUpdates_ = 0;
00050    }
00051 
00052    void tearDown()
00053    {
00054       TestSuite::tearDown();
00055    }
00056 
00057    virtual ~TestSubXPath()
00058    {
00059    }
00060 
00061    void setUp()
00062    {
00063       TestSuite::setUp();
00064       try {   
00065 //         ConnectQos connQos(global_, "Tim", "secret");
00066          ConnectQos connQos(global_);
00067          log_.info(ME, string("connecting to xmlBlaster. Connect qos: ") + connQos.toXml());
00068 
00069          ConnectReturnQos retQos = connection_.connect(connQos, this);
00070          log_.info(ME, "successfully connected to xmlBlaster. Return qos: " + retQos.toXml());
00071 
00072       }
00073       catch (XmlBlasterException& ex) {
00074          log_.error(ME, string("exception occurred in setUp. ") + ex.toXml());
00075          assert(0);
00076       }
00077 
00078    }
00079 
00080 
00081 
00086    void testInitial()  
00087    {
00088       ME = "TestSubXPath:testInitial()";
00089       string oid = "INITIAL";
00090       subscribeXPath("//demo");
00091       Thread::sleep(1000);
00092 
00093       try {
00094          PublishKey pk(global_, oid, "text/xml", "1.0");
00095          pk.setClientTags("<org.xmlBlaster><demo/></org.xmlBlaster>");
00096          PublishQos pq(global_);
00097          MessageUnit msgUnit(pk, "Hi", pq);
00098          PublishReturnQos tmp = connection_.publish(msgUnit);
00099          if (oid != tmp.getKeyOid()) {
00100             log_.error(ME, string("wrong oid. It should be '") + oid + "' but is '" + tmp.getKeyOid());
00101             assert(0);
00102          }
00103          Thread::sleep(3000);                                   
00104          assertEquals(log_, ME, 1, numOfUpdates_, "checking number of updates");
00105       }
00106       catch (XmlBlasterException& e) {
00107          log_.error(ME, e.getMessage());
00108          assert(0);
00109       }
00110 
00111       try {
00112          EraseKey key(global_);
00113          key.setOid(oid);
00114          EraseQos qos(global_);
00115          vector<EraseReturnQos> arr = connection_.erase(key, qos);
00116          assertEquals(log_, ME, (size_t)1, arr.size(), "Erase");
00117       } 
00118       catch(XmlBlasterException& e) { 
00119                         log_.error(ME, "Erase problem: " + e.getMessage());
00120          assert(0);
00121       }
00122    }
00123 
00124    string update(const string&, UpdateKey&, const unsigned char*, long, UpdateQos&)
00125    {
00126       Lock lock(updateMutex_);
00127       log_.info(ME, "update invoked");
00128       numOfUpdates_++;
00129       return "";
00130    }
00131 
00132 };
00133 
00134 }}} // namespaces
00135 
00136 using namespace org::xmlBlaster::test;
00137 
00145 int main(int args, char ** argv)
00146 {
00147    try {
00148       org::xmlBlaster::util::Object_Lifetime_Manager::init();
00149       TestSubXPath *testSubXpath = new TestSubXPath(args, argv);
00150       testSubXpath->setUp();
00151       testSubXpath->testInitial();
00152       testSubXpath->tearDown();
00153       Thread::sleepSecs(1);
00154       delete testSubXpath;
00155       org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00156    }
00157    catch (XmlBlasterException& ex) {
00158       std::cout << ex.toXml() << std::endl;
00159    }
00160    catch (bad_exception& ex) {
00161       cout << "bad_exception: " << ex.what() << endl;
00162    }
00163    catch (exception& ex) {
00164       cout << " exception: " << ex.what() << endl;
00165    }
00166    catch (string& ex) {
00167       cout << "string: " << ex << endl;
00168    }
00169    catch (char* ex) {
00170       cout << "char* :  " << ex << endl;
00171    }
00172 
00173    catch (...)
00174    {
00175       cout << "unknown exception occured" << endl;
00176       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
00177       cout << e.toXml() << endl;
00178    }
00179 
00180    return 0;
00181 }