1 /*-----------------------------------------------------------------------------
  2 Name:      TestConnectQos.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the Timeout Features
  6 -----------------------------------------------------------------------------*/
  7 #include <util/qos/ConnectQos.h>
  8 #include <util/qos/ConnectQosFactory.h>
  9 #include <util/XmlBlasterException.h>
 10 #include <util/Global.h>
 11 #include "TestSuite.h"
 12 #include <iostream>
 13 
 14 namespace org { namespace xmlBlaster { namespace test {
 15 
 16 using namespace std;
 17 using namespace org::xmlBlaster::util;
 18 using namespace org::xmlBlaster::util::qos;
 19 using namespace org::xmlBlaster::util::qos::storage;
 20 using namespace org::xmlBlaster::util::qos::address;
 21 
 22 class TestConnectQos
 23 {
 24 private:
 25    string  ME;
 26    Global& global_;
 27    I_Log&  log_;
 28    bool useSessionMarker_;  // Remove again at version 2.0
 29 
 30 public:
 31    TestConnectQos(Global& glob) 
 32       : ME("TestConnectQos"), 
 33         global_(glob), 
 34         log_(glob.getLog("test"))
 35    {
 36       SessionName sn(global_, "client/dummy");
 37       useSessionMarker_ = sn.useSessionMarker();
 38       //useSessionMarker_ = global_.getProperty().getBoolProperty("xmlBlaster/useSessionMarker", true);
 39    }
 40 
 41 
 42    void tearDown()
 43    {
 44    }
 45 
 46    virtual ~TestConnectQos()
 47    {
 48    }
 49 
 50    void setUp()
 51    {
 52    }
 53 
 54 
 55    void testSessionQos()
 56    {
 57 
 58       string me = ME + ".testSessionQos";
 59       log_.info(me, "testing creation, parsing and output of SessionQos: start");
 60       log_.info(me, "useSessionMarker=" + lexical_cast<std::string>(useSessionMarker_));
 61 
 62        string qos = string("<session name='/node/http:/client/ticheta/-3' timeout='86400000' maxSessions='10' \n") +
 63                     "         clearSessions='false' sessionId='IIOP:01110728321B0222011028'/>\n";
 64 
 65       SessionQosFactory factory(global_);
 66 
 67       for (int i=0; i<2; i++) {
 68          SessionQosData data = factory.readObject(qos);
 69          if (useSessionMarker_)
 70             assertEquals(log_, me, string("/node/http:/client/ticheta/session/-3"), data.getAbsoluteName(), "absolute name check");
 71          else
 72             assertEquals(log_, me, string("/node/http:/client/ticheta/-3"), data.getAbsoluteName(), "absolute name check");
 73          assertEquals(log_, me, (long)86400000l, data.getTimeout(), "timeout check");
 74          assertEquals(log_, me, 10, data.getMaxSessions(), "maxSessions check");
 75          assertEquals(log_, me, false, data.getClearSessions(), "clearSessions check");
 76          assertEquals(log_, me, string("IIOP:01110728321B0222011028"), data.getSecretSessionId(), "sessionId check");
 77          SessionQosData ref(global_);
 78          ref.setAbsoluteName("/node/http:/client/ticheta/-3");
 79          ref.setTimeout(86400000l);
 80          ref.setMaxSessions(10);
 81          ref.setClearSessions(false);
 82          ref.setSecretSessionId("IIOP:01110728321B0222011028");
 83          string lit1 = data.toXml();
 84          string lit2 = ref.toXml();
 85          if (log_.trace()) {
 86             log_.trace(me, string("xml is: ") + lit1);
 87             log_.trace(me, string("xml should be: ") + lit2);
 88          }
 89          assertEquals(log_, me, lit2, lit1, "sessionId check");
 90       }
 91 
 92       // make sure the property 'session.name' has not been set on the command line or on the prop. file
 93       string name = global_.getProperty().getStringProperty("session.name", "");
 94       assertEquals(log_, me, string(""), name, "non setting of property 'session.name'");
 95       SessionQosData data1(global_, "Fritz", 0);
 96       assertEquals(log_, me, string("client/Fritz"), data1.getAbsoluteName(), "checking constructor with 'user' and 'pubSessionId=0'");
 97       data1 = SessionQosData(global_, "Franz", -3);
 98       if (useSessionMarker_)
 99          assertEquals(log_, me, string("client/Franz/session/-3"), data1.getAbsoluteName(), "checking constructor with relative name");
100       else
101          assertEquals(log_, me, string("client/Franz/-3"), data1.getAbsoluteName(), "checking constructor with relative name");
102 
103       global_.getProperty().setProperty("user.name", "PincoPallino");
104       name = global_.getProperty().getStringProperty("user.name", "");
105       assertEquals(log_, me, string("PincoPallino"), name, "checking if property 'user' has been set correctly");
106 
107       data1 = SessionQosData(global_, "", 6);
108       if (useSessionMarker_)
109          assertEquals(log_, me, string("client/PincoPallino/session/6"), data1.getAbsoluteName(), "checking constructor with empty defaultUserName when 'user' set");
110       else
111          assertEquals(log_, me, string("client/PincoPallino/6"), data1.getAbsoluteName(), "checking constructor with empty defaultUserName when 'user' set");
112 
113       data1 = SessionQosData(global_, "Nisse", 0);
114       assertEquals(log_, me, string("client/Nisse"), data1.getAbsoluteName(), "checking constructor with set defaultUserName when 'user' set");
115 
116       
117       // set the property now
118       global_.getProperty().setProperty("session.name", "/node/australia/client/Martin/4");
119       name = global_.getProperty().getStringProperty("session.name", "");
120       assertEquals(log_, me, string("/node/australia/client/Martin/4"), name, "checking if property 'session.name' has been set correctly");
121       data1 = SessionQosData(global_, "Nisse/3", 0);
122       assertEquals(log_, me, string("/node/australia/client/Martin/4"), name, "checking when 'session.name' is strongest");
123 
124       data1 = SessionQosData(global_);
125       data1.setAbsoluteName("/node/frodo/client/whoMore/3");
126       if (useSessionMarker_)
127          assertEquals(log_, me, string("/node/frodo/client/whoMore/session/3"), data1.getAbsoluteName(), "checking when 'session.name' is weaker");
128       else
129          assertEquals(log_, me, string("/node/frodo/client/whoMore/3"), data1.getAbsoluteName(), "checking when 'session.name' is weaker");
130       log_.info(me, "testing creation, parsing and output of SessionQos: end");
131    }
132 
133 
134    void testQueueProperty()
135    {
136       string me = ME + "::testQueueProperty";
137       log_.info(me, "testing queue properties parsing: start");
138 
139       string qos = string("<queue relating='connection' storeSwapLevel='1468006' storeSwapBytes='524288' ") +
140                    string("     reloadSwapLevel='629145' reloadSwapBytes='524288'>\n") + 
141                    string("  <address type='IOR' bootstrapHostname='127.0.0.2' dispatchPlugin='undef'>") +
142                    string("     <burstMode collectTime='400' maxEntries='20' maxBytes='3900' />") +
143                    string("     http://127.0.0.2:3412\n") +
144                    string("     <attribute name='corrId' type='int'>120001</attribute>\n") +
145                    string("  </address>\n") +
146                    string("</queue>\n");
147       
148       QueuePropertyFactory factory(global_);
149       for (int i=0; i < 2; i++) {
150          QueuePropertyBase propBase = factory.readObject(qos);
151          ClientQueueProperty prop(propBase);
152          assertEquals(log_, me, string("connection"), prop.getRelating(), "relating check");
153          assertEquals(log_, me, 1468006L, prop.getStoreSwapLevel(), "storeSwapLevel check");
154          assertEquals(log_, me, 524288L, prop.getStoreSwapBytes(), "storeSwapBytes check");
155          assertEquals(log_, me, 629145L, prop.getReloadSwapLevel(), "reloadSwapLevel check");
156          assertEquals(log_, me, 524288L, prop.getReloadSwapBytes(), "reloadSwapBytes check");
157 
158          AddressBaseRef address = prop.getCurrentAddress();
159          assertEquals(log_, me, string("IOR"), address->getType(), "address type check");
160          assertEquals(log_, me, string("127.0.0.2"), address->getHostname(), "address hostname check");
161          assertEquals(log_, me, string("undef"), address->getDispatchPlugin(), "address dispatch Plugin check");
162          assertEquals(log_, me, 400L, address->getCollectTime(), "collectTime check");
163          assertEquals(log_, me, 20, address->getBurstModeMaxEntries(), "getBurstModeMaxEntries check");
164          assertEquals(log_, me, 3900L, address->getBurstModeMaxBytes(), "getBurstModeMaxBytes check");
165          org::xmlBlaster::util::qos::address::AddressBase::ClientPropertyMap map = address->getAttributes();
166          assertEquals(log_, me, 1L, map.size(), "no attribute");
167 
168          if (log_.trace()) log_.trace(me, string("the queue property literal: ") + prop.toXml());
169       }
170       log_.info(me, "testing queue properties parsing: end");
171    }
172 
173 
174    void testConnectQos()
175    {
176       string me = ME + "::testConnectQos";
177       log_.info(me, "testing parsing of a return connect qos: start");
178       string defaultBoolStr[2] = { "true", "false" };
179       for (int jj=0; jj<2; jj++) {
180          bool defaultBool = lexical_cast<bool>(defaultBoolStr[jj]);
181          string qos = 
182              string("<qos>\n") +
183              string("  <securityService type='htpasswd' version='1.0'><![CDATA[\n") +
184              string("   <user>Tim</user>\n") +
185              string("   <passwd>secret</passwd>\n") +
186              string("  ]]></securityService>\n") +
187              string("  <ptp>")+defaultBoolStr[jj]+string("</ptp>\n") +
188              string("  <clusterNode>")+defaultBoolStr[jj]+string("</clusterNode>\n") +
189              string("  <instanceId>/xmlBlaster/node/heron/client/Tim/session/-3/instanceId/123445</instanceId>\n") +
190              string("  <reconnected>")+defaultBoolStr[jj]+string("</reconnected>\n") +
191              string("  <refreshSession>")+defaultBoolStr[jj]+string("</refreshSession>\n") +
192              string("  <duplicateUpdates>")+defaultBoolStr[jj]+string("</duplicateUpdates>\n") +
193              string("  <duplicateUpdates>")+defaultBoolStr[jj]+string("</duplicateUpdates>\n") +
194              string("  <session name='/node/http_127_0_0_2_3412/client/Tim/-3' timeout='86400000' maxSessions='10' clearSessions='")+defaultBoolStr[jj]+string("' sessionId='IIOP:01110C332A141532012A0F'/>\n") +
195              string("  <queue relating='connection' storeSwapLevel='1468006' storeSwapBytes='524288' reloadSwapLevel='629145' reloadSwapBytes='524288'>\n") +
196              string("   <address type='IOR' bootstrapHostname='127.0.0.2' dispatchPlugin='undef'>\n") +
197              string("      http://127.0.0.2:3412\n") +
198              string("      <attribute name='someConnectFloat' size='3' type='float'>1.5</attribute>\n") +
199              string("   </address>\n") +
200              string("  </queue>\n") +
201              string("  <queue relating='callback' type='CACHE' version='1.0' maxEntries='10000000' storeSwapLevel='1468006' storeSwapBytes='524288' reloadSwapLevel='629145' reloadSwapBytes='524288'>\n") +
202              string("   <callback type='IOR' bootstrapHostname='127.0.0.1' dispatchPlugin='undef'>\n") +
203              string("      IOR:010000004000000049444c3a6f72672e786d6c426c61737465722e70726f746f636f6c2e636f7262612f636c69656e7449646c2f426c617374657243616c6c6261636b3a312e300002000000000000002f000000010100000c0000006c696e75782e6c6f63616c00a6820000130000002f353936372f313034323232363530392f5f30000100000024000000010000000100000001000000140000000100000001000100000000000901010000000000\n") +
204              string("      <burstMode collectTime='400' maxEntries='20' maxBytes='-1' />\n") +
205              string("      <attribute name='someString'>BlaBla</attribute>\n") +
206              string("      <attribute name='someString2'>BlaBla2</attribute>\n") +
207              string("   </callback>\n") +
208              string("  </queue>\n") +
209              string("  <serverRef type='IOR'>\n") +
210              string("  IOR:000000000000003749444c3a6f72672e786d6c426c61737465722e70726f746f636f6c2e636f7262612f73657276657249646c2f5365727665723a312e300000000000030000000000000043000100000000000a3132372e302e302e320082980000002b5374616e64617264496d706c4e616d652f786d6c426c61737465722d504f412f01110c332a141532012a0f000000000000000048000101000000000a3132372e302e302e320082980000002b5374616e64617264496d706c4e616d652f786d6c426c61737465722d504f412f01110c332a141532012a0f0000000000000000010000002c0000000000000001000000010000001c00000000000100010000000105010001000101090000000105010001\n") +
211              string("  </serverRef>\n") +
212              string("  <clientProperty name='intKey' type='int'>123</clientProperty>\n") +
213              string("  <clientProperty name='StringKey' type='String' encoding='") + Constants::ENCODING_BASE64 + string("'>QmxhQmxhQmxh</clientProperty>\n") +
214              string("  <persistent>")+defaultBoolStr[jj]+string("</persistent>\n") +
215              string(" </qos>\n");
216 
217          ConnectQosFactory factory(global_);
218          for (int i=0; i < 2; i++) {
219             ConnectQosRef connQos = factory.readObject(qos);
220             string qos2 = connQos->toXml();
221             connQos = factory.readObject(qos2); // round trip: parse -> dump -> parse again -> check
222             assertEquals(log_, me, "/xmlBlaster/node/heron/client/Tim/session/-3/instanceId/123445", connQos->getInstanceId(), "check 'instanceId' flag");
223             assertEquals(log_, me, defaultBool, connQos->getPtp(), "check 'ptp' flag");
224             assertEquals(log_, me, defaultBool, connQos->isClusterNode(), "check 'clusterNode' flag");
225             assertEquals(log_, me, defaultBool, connQos->isRefreshSession(), "check 'refreshSession' flag");
226             assertEquals(log_, me, defaultBool, connQos->isDuplicateUpdates(), "check 'duplicateUpdates' flag");
227             assertEquals(log_, me, defaultBool, connQos->isPersistent(), "check 'persistent' flag");
228             assertEquals(log_, me, 123, connQos->getClientProperty("intKey", 0), "check 'intKey' flag");
229             // Base64: QmxhQmxhQmxh -> BlaBlaBla
230             assertEquals(log_, me, string("BlaBlaBla"), connQos->getClientProperty("StringKey", string("wrong")), "check 'StringKey' flag");
231             assertEquals(log_, me, "IIOP:01110C332A141532012A0F", connQos->getSecretSessionId(), "check 'secretSessionId' flag");
232             assertEquals(log_, me, 20, connQos->getCbAddress()->getBurstModeMaxEntries(), "getBurstModeMaxEntries check");
233             // TODO: other checks!
234 
235             log_.info(me, string("connect qos: ") + connQos->toXml());
236          }
237       }
238    }
239 };
240 
241 }}} // namespace 
242 
243 
244 using namespace org::xmlBlaster::test;
245 
246 /**
247  * Try
248  * <pre>
249  *   java TestConnectQos -help
250  * </pre>
251  * for usage help
252  */
253 int main(int args, char ** argv)
254 {
255    try {
256       org::xmlBlaster::util::Object_Lifetime_Manager::init();
257       Global& glob = Global::getInstance();
258       glob.initialize(args, argv);
259 
260       TestConnectQos testConnectQos(glob);
261 
262       testConnectQos.setUp();
263       testConnectQos.testSessionQos();
264       testConnectQos.tearDown();
265       testConnectQos.setUp();
266       testConnectQos.testQueueProperty();
267       testConnectQos.tearDown();
268       testConnectQos.setUp();
269       testConnectQos.testConnectQos();
270       testConnectQos.tearDown();
271       org::xmlBlaster::util::Object_Lifetime_Manager::fini();
272    }
273    catch (XmlBlasterException& ex) {
274       std::cout << ex.toXml() << std::endl;
275    }
276    catch (bad_exception& ex) {
277       cout << "bad_exception: " << ex.what() << endl;
278    }
279    catch (exception& ex) {
280       cout << " exception: " << ex.what() << endl;
281    }
282    catch (string& ex) {
283       cout << "string: " << ex << endl;
284    }
285    catch (char* ex) {
286       cout << "char* :  " << ex << endl;
287    }
288 
289    catch (...)
290    {
291       cout << "unknown exception occured" << endl;
292       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
293       cout << e.toXml() << endl;
294    }
295    return 0;
296 }


syntax highlighted by Code2HTML, v. 0.9.1