1 /*------------------------------------------------------------------------------
2 Name: TestUnSub.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Demo code for a client using xmlBlaster
6 Version: $Id: TestUnSub.java 14833 2006-03-06 21:38:58Z laghi $
7 ------------------------------------------------------------------------------*/
8 package org.xmlBlaster.test.qos;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.util.XmlBlasterException;
14 import org.xmlBlaster.util.MsgUnit;
15 import org.xmlBlaster.client.I_XmlBlasterAccess;
16 import org.xmlBlaster.client.I_Callback;
17 import org.xmlBlaster.client.qos.ConnectQos;
18 import org.xmlBlaster.client.key.UpdateKey;
19 import org.xmlBlaster.client.qos.UpdateQos;
20 import org.xmlBlaster.client.qos.EraseReturnQos;
21 import org.xmlBlaster.client.qos.PublishReturnQos;
22 import org.xmlBlaster.client.key.SubscribeKey;
23 import org.xmlBlaster.client.key.UnSubscribeKey;
24 import org.xmlBlaster.client.qos.SubscribeQos;
25 import org.xmlBlaster.client.qos.SubscribeReturnQos;
26 import org.xmlBlaster.client.qos.UnSubscribeQos;
27 import org.xmlBlaster.client.qos.UnSubscribeReturnQos;
28
29 import junit.framework.*;
30
31
32 /**
33 * This client tests the method subscribe() with XPath query and with a later unSubscribe().
34 * <br />
35 * Multiple identical subscribes shouldn't harm and all following are ignored.
36 * <p>
37 * This client may be invoked multiple time on the same xmlBlaster server,
38 * as it cleans up everything after his tests are done.
39 * <p>
40 * Invoke examples:<br />
41 * <pre>
42 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestUnSub
43 * java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestUnSub
44 * </pre>
45 */
46 public class TestUnSub extends TestCase implements I_Callback
47 {
48 private static String ME = "TestUnSub";
49 private final Global glob;
50 private static Logger log = Logger.getLogger(TestUnSub.class.getName());
51 private boolean messageArrived = false;
52
53 private String subscribeOid;
54 private String publishOid = "";
55 private I_XmlBlasterAccess senderConnection;
56 private String senderName;
57 private String senderContent;
58 private String receiverName; // sender/receiver is here the same client
59 private String xpath = "//TestUnSub-AGENT";
60
61 private int numReceived = 0; // error checking
62 private final String contentMime = "text/xml";
63 private final String contentMimeExtended = "1.0";
64
65 /**
66 * Constructs the TestUnSub object.
67 * <p />
68 * @param testName The name used in the test suite
69 * @param loginName The name to login to the xmlBlaster
70 */
71 public TestUnSub(Global glob, String testName, String loginName)
72 {
73 super(testName);
74 this.glob = glob;
75
76 this.senderName = loginName;
77 this.receiverName = loginName;
78 }
79
80
81 /**
82 * Sets up the fixture (login).
83 * <p />
84 * Connect to xmlBlaster and login
85 */
86 protected void setUp()
87 {
88 try {
89 senderConnection = glob.getXmlBlasterAccess(); // Find orb
90 String passwd = "secret";
91 ConnectQos connectQos = new ConnectQos(glob, senderName, passwd);
92 senderConnection.connect(connectQos, this); // Login to xmlBlaster
93 }
94 catch (Exception e) {
95 log.severe(e.toString());
96 e.printStackTrace();
97 }
98 }
99
100
101 /**
102 * Tears down the fixture (logout).
103 * <p />
104 * cleaning up .... erase() the previous message OID and logout
105 */
106 protected void tearDown()
107 {
108 if (publishOid.length() > 0) { // not for testSubscribeUnSubscribeEmpty
109 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
110 "<key oid='" + publishOid + "' queryType='EXACT'>\n" +
111 "</key>";
112 try {
113 EraseReturnQos[] arr = senderConnection.erase(xmlKey, "<qos/>");
114 assertEquals("Erase", 1, arr.length);
115 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
116 }
117
118 senderConnection.disconnect(null);
119 }
120
121
122 /**
123 * TEST: Subscribe to messages with XPATH.
124 * <p />
125 * The returned subscribeOid is checked
126 */
127 public void subscribeXPath()
128 {
129 if (log.isLoggable(Level.FINE)) log.fine("Subscribing using XPath syntax ...");
130
131 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
132 "<key oid='' queryType='XPATH'>\n" +
133 " " + xpath +
134 "</key>";
135 String qos = "<qos></qos>";
136 numReceived = 0;
137 subscribeOid = null;
138 try {
139 subscribeOid = senderConnection.subscribe(xmlKey, qos).getSubscriptionId();
140 log.info("Success: Subscribe on " + subscribeOid + " done");
141 } catch(XmlBlasterException e) {
142 log.warning("XmlBlasterException: " + e.getMessage());
143 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
144 }
145 assertTrue("returned null subscribeOid", subscribeOid != null);
146 assertTrue("returned subscribeOid is empty", 0 != subscribeOid.length());
147 }
148
149
150 /**
151 * TEST: UnSubscribe to messages with XPATH.
152 * <p />
153 */
154 public void unSubscribeXPath()
155 {
156 if (log.isLoggable(Level.FINE)) log.fine("UnSubscribing using XPath syntax ...");
157
158 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
159 " " + xpath +
160 "</key>";
161 String qos = "<qos></qos>";
162 numReceived = 0;
163 try {
164 senderConnection.unSubscribe(xmlKey, qos);
165 log.info("Success: UnSubscribe with " + xpath + " done");
166 } catch(XmlBlasterException e) {
167 log.warning("XmlBlasterException: " + e.getMessage());
168 assertTrue("unSubscribe - XmlBlasterException: " + e.getMessage(), false);
169 }
170 }
171
172
173 /**
174 * TEST: UnSubscribe to messages with EXACT oid (which was returned from our XPATH subscription).
175 * <p />
176 */
177 public void unSubscribeExact()
178 {
179 if (log.isLoggable(Level.FINE)) log.fine("UnSubscribing using EXACT syntax ...");
180
181 String xmlKey = "<key oid='" + subscribeOid + "' queryType='EXACT'>\n" +
182 "</key>";
183 String qos = "<qos></qos>";
184 numReceived = 0;
185 try {
186 senderConnection.unSubscribe(xmlKey, qos);
187 log.info("Success: UnSubscribe on " + subscribeOid + " done");
188 } catch(XmlBlasterException e) {
189 log.warning("XmlBlasterException: " + e.getMessage());
190 assertTrue("unSubscribe - XmlBlasterException: " + e.getMessage(), false);
191 }
192 }
193
194
195 /**
196 * TEST: Construct a message and publish it.
197 * <p />
198 * The returned publishOid is checked
199 */
200 public void doPublish()
201 {
202 if (log.isLoggable(Level.FINE)) log.fine("Publishing a message ...");
203
204 numReceived = 0;
205 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
206 "<key oid='' contentMime='" + contentMime + "' contentMimeExtended='" + contentMimeExtended + "'>\n" +
207 " <TestUnSub-AGENT id='192.168.124.10' subId='1' type='generic'>" +
208 " <TestUnSub-DRIVER id='FileProof' pollingFreq='10'>" +
209 " </TestUnSub-DRIVER>"+
210 " </TestUnSub-AGENT>" +
211 "</key>";
212 senderContent = "Yeahh, i'm the new content";
213 PublishReturnQos publishReturnQos = null;
214 try {
215 MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), "<qos></qos>");
216 publishReturnQos = senderConnection.publish(msgUnit);
217 publishOid = publishReturnQos.getKeyOid();
218 log.info("Success: Publishing done, returned oid=" + publishOid);
219 } catch(XmlBlasterException e) {
220 log.warning("XmlBlasterException: " + e.getMessage());
221 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
222 }
223
224 assertTrue("returned publishOid == null", publishOid != null);
225 assertTrue("returned publishOid: " + publishReturnQos.toXml(), 0 != publishOid.length());
226 }
227
228 /**
229 * TEST: subscribe and unSubscribe on an empty topic (without a publish)
230 * The unSubscribe is done with an oid instead of a subId
231 */
232 public void testSubscribeUnSubscribeOid()
233 {
234 log.info("Starting testSubscribeUnSubscribeOid()");
235 publishOid = "";
236
237 String oid = "SomeDummySubscribe";
238 SubscribeReturnQos subRet = null;
239 try {
240 SubscribeKey sk = new SubscribeKey(glob, oid);
241 SubscribeQos sq = new SubscribeQos(glob);
242 subRet = senderConnection.subscribe(sk.toXml(), sq.toXml());
243 log.info("testSubscribeUnSubscribeOid() subscribed to " + subRet.getSubscriptionId());
244 }
245 catch (XmlBlasterException e) {
246 log.severe("testSubscribeUnSubscribeOid() subscribe failed: " + e.getMessage());
247 fail("testSubscribeUnSubscribeOid() subscribe failed: " + e.getMessage());
248 }
249
250 try {
251 // !! Here we unsubscribe with the oid instead of using the subId !!
252 UnSubscribeKey uk = new UnSubscribeKey(glob, oid);
253 UnSubscribeQos uq = new UnSubscribeQos(glob);
254 UnSubscribeReturnQos[] urq = senderConnection.unSubscribe(uk.toXml(), uq.toXml());
255 log.info("testSubscribeUnSubscribeOid() unSubscribed");
256 assertEquals("Return wrong", 1, urq.length);
257 assertEquals("SubId wrong", subRet.getSubscriptionId(), urq[0].getSubscriptionId());
258 }
259 catch (XmlBlasterException e) {
260 log.severe("testSubscribeUnSubscribeOid() unSubscribe failed: " + e.getMessage());
261 fail("testSubscribeUnSubscribeOid() unSubscribe failed: " + e.getMessage());
262 }
263 }
264
265 /**
266 * TEST: subscribe and unSubscribe on an empty topic (without a publish)
267 */
268 public void testSubscribeUnSubscribeEmpty()
269 {
270 log.info("Starting testSubscribeUnSubscribeEmpty()");
271 publishOid = "";
272
273 SubscribeReturnQos subRet = null;
274 try {
275 SubscribeKey sk = new SubscribeKey(glob, "SomeDummySubscribe");
276 SubscribeQos sq = new SubscribeQos(glob);
277 subRet = senderConnection.subscribe(sk.toXml(), sq.toXml());
278 log.info("testSubscribeUnSubscribeEmpty() subscribed to " + subRet.getSubscriptionId());
279 }
280 catch (XmlBlasterException e) {
281 log.severe("testSubscribeUnSubscribeEmpty() subscribe failed: " + e.getMessage());
282 fail("testSubscribeUnSubscribeEmpty() subscribe failed: " + e.getMessage());
283 }
284
285 try {
286 UnSubscribeKey uk = new UnSubscribeKey(glob, subRet.getSubscriptionId());
287 UnSubscribeQos uq = new UnSubscribeQos(glob);
288 UnSubscribeReturnQos[] urq = senderConnection.unSubscribe(uk.toXml(), uq.toXml());
289 log.info("testSubscribeUnSubscribeEmpty() unSubscribed");
290 assertEquals("Return wrong", 1, urq.length);
291 assertEquals("SubId wrong", subRet.getSubscriptionId(), urq[0].getSubscriptionId());
292 }
293 catch (XmlBlasterException e) {
294 log.severe("testSubscribeUnSubscribeEmpty() unSubscribe failed: " + e.getMessage());
295 fail("testSubscribeUnSubscribeEmpty() unSubscribe failed: " + e.getMessage());
296 }
297 }
298
299
300 /**
301 * TEST: Publish a message, subscribe on it with XPATH and
302 * unSubscribe again with the returned oid.
303 */
304 public void testSubscribeUnSubscribeExact()
305 {
306 log.info("Starting testSubscribeUnSubscribeExact()");
307 numReceived = 0;
308 doPublish(); // Feed some data
309 subscribeXPath(); // Subscribe to it
310 waitOnUpdate(2000L);
311 assertEquals("numReceived after publishing", 1, numReceived); // message arrived?
312 unSubscribeExact(); // cancel XPATH subscription with XPATH-subscription-oid
313 }
314
315
316 /**
317 * TEST: Publish a message, subscribe on it with XPATH and
318 * unSubscribe again with the same XPATH query.
319 */
320 public void testSubscribeUnSubscribeXPath()
321 {
322 log.info("Starting testSubscribeUnSubscribeXPath()");
323 numReceived = 0;
324 doPublish(); // Feed some data
325 subscribeXPath(); // Subscribe to it
326 waitOnUpdate(2000L);
327 assertEquals("numReceived after publishing", 1, numReceived); // message arrived?
328 unSubscribeXPath(); // cancel with XPATH syntax
329 }
330
331
332 /**
333 * This is the callback method invoked from xmlBlaster
334 * delivering us a new asynchronous message.
335 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
336 */
337 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
338 {
339 if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
340
341 numReceived += 1;
342
343 //assertEquals("Wrong sender, used="+senderName+" updated="+updateQos.getSender().getRelativeName(), senderName, updateQos.getSender().getRelativeName());
344 assertEquals("Wrong oid of message returned", publishOid, updateKey.getOid());
345 assertEquals("Message content is corrupted", new String(senderContent), new String(content));
346 assertEquals("Message contentMime is corrupted", contentMime, updateKey.getContentMime());
347 assertEquals("Message contentMimeExtended is corrupted", contentMimeExtended, updateKey.getContentMimeExtended());
348
349 messageArrived = true;
350 return "";
351 }
352
353
354 /**
355 * Little helper, waits until the variable 'messageArrive' is set
356 * to true, or returns when the given timeout occurs.
357 * @param timeout in milliseconds
358 */
359 private void waitOnUpdate(final long timeout)
360 {
361 long pollingInterval = 50L; // check every 0.05 seconds
362 if (timeout < 50) pollingInterval = timeout / 10L;
363 long sum = 0L;
364 while (!messageArrived) {
365 try {
366 Thread.sleep(pollingInterval);
367 }
368 catch( InterruptedException i)
369 {}
370 sum += pollingInterval;
371 if (sum > timeout) {
372 log.warning("Timeout of " + timeout + " occurred");
373 break;
374 }
375 }
376 messageArrived = false;
377 }
378
379
380 /**
381 * Method is used by TestRunner to load these tests
382 */
383 public static Test suite()
384 {
385 TestSuite suite= new TestSuite();
386 String loginName = "TestUnSub/5";
387 Global glob = new Global();
388 suite.addTest(new TestUnSub(glob, "testSubscribeUnSubscribeEmpty", loginName));
389 suite.addTest(new TestUnSub(glob, "testSubscribeUnSubscribeOid", loginName));
390 suite.addTest(new TestUnSub(glob, "testSubscribeUnSubscribeExact", loginName));
391 suite.addTest(new TestUnSub(glob, "testSubscribeUnSubscribeXPath", loginName));
392 return suite;
393 }
394
395
396 /**
397 * Invoke: java org.xmlBlaster.test.qos.TestUnSub
398 * @deprecated Use the TestRunner from the testsuite to run it:<p />
399 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestUnSub</pre>
400 */
401 public static void main(String args[])
402 {
403 TestUnSub testSub = new TestUnSub(new Global(args), "TestUnSub", "TestUnSub/5");
404
405 testSub.setUp();
406 testSub.testSubscribeUnSubscribeOid();
407 testSub.tearDown();
408
409 testSub.setUp();
410 testSub.testSubscribeUnSubscribeEmpty();
411 testSub.tearDown();
412
413 testSub.setUp();
414 testSub.testSubscribeUnSubscribeXPath();
415 testSub.tearDown();
416
417 testSub.setUp();
418 testSub.testSubscribeUnSubscribeExact();
419 testSub.tearDown();
420 }
421 }
syntax highlighted by Code2HTML, v. 0.9.1