1 /*-----------------------------------------------------------------------------
  2 Name:      TestSubXPath.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the Timeout Features
  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;
 18 
 19 namespace org { namespace xmlBlaster { namespace test {
 20 
 21 class TestSubXPath : public TestSuite, public virtual I_Callback
 22 {
 23 private:
 24    Mutex             updateMutex_;
 25    int               numOfUpdates_;
 26 
 27    void subscribeXPath(const string& query) 
 28    {
 29       if (log_.trace()) log_.trace(ME, "Subscribing using XPath syntax ...");
 30       SubscribeKey subKey(global_);
 31       subKey.setQueryString(query);
 32       SubscribeQos qos(global_);
 33       string subscribeOid = "";
 34       try {
 35          subscribeOid = connection_.subscribe(subKey, qos).getSubscriptionId();
 36          log_.info(ME, string("Success: Subscribe on ") + subscribeOid + " done:\n" + subKey.toXml());
 37       } catch(XmlBlasterException& e) {
 38          log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
 39          assertEquals(log_, ME, true, false, string("subscribe - XmlBlasterException: ") + e.toXml());
 40       }
 41       assertEquals(log_, ME, false, subscribeOid.empty(), "returned emty subscribeOid");
 42    }
 43 
 44 
 45 public:
 46    TestSubXPath(int args, char *argc[]) 
 47       :  TestSuite(args, argc, "TestSubXPath"), updateMutex_()
 48    {
 49       numOfUpdates_ = 0;
 50    }
 51 
 52    void tearDown()
 53    {
 54       TestSuite::tearDown();
 55    }
 56 
 57    virtual ~TestSubXPath()
 58    {
 59    }
 60 
 61    void setUp()
 62    {
 63       TestSuite::setUp();
 64       try {   
 65 //         ConnectQos connQos(global_, "Tim", "secret");
 66          ConnectQos connQos(global_);
 67          log_.info(ME, string("connecting to xmlBlaster. Connect qos: ") + connQos.toXml());
 68 
 69          ConnectReturnQos retQos = connection_.connect(connQos, this);
 70          log_.info(ME, "successfully connected to xmlBlaster. Return qos: " + retQos.toXml());
 71 
 72       }
 73       catch (XmlBlasterException& ex) {
 74          log_.error(ME, string("exception occurred in setUp. ") + ex.toXml());
 75          assert(0);
 76       }
 77 
 78    }
 79 
 80 
 81 
 82    /**
 83     * TEST: Construct 5 messages and publish them,<br />
 84     * the previous XPath subscription should match message #3 and send an update.
 85     */
 86    void testInitial()  
 87    {
 88       ME = "TestSubXPath:testInitial()";
 89       string oid = "INITIAL";
 90       subscribeXPath("//demo");
 91       Thread::sleep(1000);
 92 
 93       try {
 94          PublishKey pk(global_, oid, "text/xml", "1.0");
 95          pk.setClientTags("<org.xmlBlaster><demo/></org.xmlBlaster>");
 96          PublishQos pq(global_);
 97          MessageUnit msgUnit(pk, "Hi", pq);
 98          PublishReturnQos tmp = connection_.publish(msgUnit);
 99          if (oid != tmp.getKeyOid()) {
100             log_.error(ME, string("wrong oid. It should be '") + oid + "' but is '" + tmp.getKeyOid());
101             assert(0);
102          }
103          Thread::sleep(3000);                                   
104          assertEquals(log_, ME, 1, numOfUpdates_, "checking number of updates");
105       }
106       catch (XmlBlasterException& e) {
107          log_.error(ME, e.getMessage());
108          assert(0);
109       }
110 
111       try {
112          EraseKey key(global_);
113          key.setOid(oid);
114          EraseQos qos(global_);
115          vector<EraseReturnQos> arr = connection_.erase(key, qos);
116          assertEquals(log_, ME, (size_t)1, arr.size(), "Erase");
117       } 
118       catch(XmlBlasterException& e) { 
119                         log_.error(ME, "Erase problem: " + e.getMessage());
120          assert(0);
121       }
122    }
123 
124    string update(const string&, UpdateKey&, const unsigned char*, long, UpdateQos&)
125    {
126       Lock lock(updateMutex_);
127       log_.info(ME, "update invoked");
128       numOfUpdates_++;
129       return "";
130    }
131 
132 };
133 
134 }}} // namespaces
135 
136 using namespace org::xmlBlaster::test;
137 
138 /**
139  * Try
140  * <pre>
141  *   java TestSubXPath -help
142  * </pre>
143  * for usage help
144  */
145 int main(int args, char ** argv)
146 {
147    try {
148       org::xmlBlaster::util::Object_Lifetime_Manager::init();
149       TestSubXPath *testSubXpath = new TestSubXPath(args, argv);
150       testSubXpath->setUp();
151       testSubXpath->testInitial();
152       testSubXpath->tearDown();
153       Thread::sleepSecs(1);
154       delete testSubXpath;
155       org::xmlBlaster::util::Object_Lifetime_Manager::fini();
156    }
157    catch (XmlBlasterException& ex) {
158       std::cout << ex.toXml() << std::endl;
159    }
160    catch (bad_exception& ex) {
161       cout << "bad_exception: " << ex.what() << endl;
162    }
163    catch (exception& ex) {
164       cout << " exception: " << ex.what() << endl;
165    }
166    catch (string& ex) {
167       cout << "string: " << ex << endl;
168    }
169    catch (char* ex) {
170       cout << "char* :  " << ex << endl;
171    }
172 
173    catch (...)
174    {
175       cout << "unknown exception occured" << endl;
176       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
177       cout << e.toXml() << endl;
178    }
179 
180    return 0;
181 }


syntax highlighted by Code2HTML, v. 0.9.1