1 /*------------------------------------------------------------------------------
2 Name: TestSubNotify.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.test.qos;
7
8 import java.util.logging.Logger;
9 import java.util.logging.Level;
10
11 import org.xmlBlaster.test.MsgInterceptor;
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.util.XmlBlasterException;
14 import org.xmlBlaster.client.qos.ConnectQos;
15 import org.xmlBlaster.util.Timestamp;
16 import org.xmlBlaster.client.I_XmlBlasterAccess;
17 import org.xmlBlaster.client.I_Callback;
18 import org.xmlBlaster.client.key.EraseKey;
19 import org.xmlBlaster.client.key.SubscribeKey;
20 import org.xmlBlaster.client.key.UpdateKey;
21 import org.xmlBlaster.client.qos.EraseQos;
22 import org.xmlBlaster.client.qos.SubscribeQos;
23 import org.xmlBlaster.client.qos.UpdateQos;
24 import org.xmlBlaster.client.qos.PublishReturnQos;
25 import org.xmlBlaster.client.qos.SubscribeReturnQos;
26 import org.xmlBlaster.client.qos.EraseReturnQos;
27 import org.xmlBlaster.util.MsgUnit;
28 import org.xmlBlaster.util.def.Constants;
29 import org.xmlBlaster.util.qos.address.CallbackAddress;
30
31 import junit.framework.*;
32
33
34 /**
35 * This client tests the method subscribe() with a later publish() with XPath query.
36 * <br />
37 * The subscribe() should be recognized for this later arriving publish()
38 * <p>
39 * This client may be invoked multiple time on the same xmlBlaster server,
40 * as it cleans up everything after his tests are done.
41 * <p>
42 * Invoke examples:<br />
43 * <pre>
44 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubNotify
45 * java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestSubNotify
46 * </pre>
47 */
48 public class TestSubNotify extends TestCase
49 {
50 private final Global glob;
51 private static Logger log = Logger.getLogger(TestSubNotify.class.getName());
52 private String publishOid = "dummyTestSubNotify";
53 private I_XmlBlasterAccess senderConnection;
54 private String senderName;
55 private MsgInterceptor msgInterceptor;
56
57 /**
58 * @param testName The name used in the test suite
59 * @param loginName The name to login to the xmlBlaster
60 */
61 public TestSubNotify(Global glob, String testName) {
62 super(testName);
63 this.glob = glob;
64 this.senderName = testName;
65 }
66
67 /**
68 * Connect to xmlBlaster and login
69 */
70 protected void setUp() {
71 try {
72 this.senderConnection = glob.getXmlBlasterAccess();
73 String passwd = "secret";
74 this.msgInterceptor = new MsgInterceptor(this.glob, log, null);
75 this.msgInterceptor.countErased(true); // count erased() notifications as well
76 this.senderConnection.connect(new ConnectQos(glob, senderName, passwd), this.msgInterceptor); // Login to xmlBlaster
77 }
78 catch (Exception e) {
79 e.printStackTrace();
80 fail("Login failed: " + e.toString());
81 }
82 }
83
84 protected void tearDown() {
85 this.senderConnection.disconnect(null);
86 }
87
88 private void erase() {
89 try {
90 EraseKey ek = new EraseKey(this.glob, this.publishOid);
91 EraseQos eq = new EraseQos(this.glob);
92 EraseReturnQos[] arr = this.senderConnection.erase(ek, eq);
93 assertEquals("Erase", 1, arr.length);
94 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
95 }
96
97 private void subscribe(boolean wantNotify) {
98 try {
99 SubscribeKey sk = new SubscribeKey(this.glob, this.publishOid);
100 SubscribeQos sq = new SubscribeQos(this.glob);
101 sq.setWantNotify(wantNotify);
102 this.senderConnection.subscribe(sk, sq);
103 } catch(XmlBlasterException e) { fail("Subscribe XmlBlasterException: " + e.getMessage()); }
104 }
105
106 private void publish() {
107 try {
108 MsgUnit msgUnit = new MsgUnit("<key oid='"+this.publishOid+"'/>", "Hi".getBytes(), "<qos/>");
109 PublishReturnQos tmp = senderConnection.publish(msgUnit);
110 assertEquals("Wrong publishOid", publishOid, tmp.getKeyOid());
111 } catch(XmlBlasterException e) { fail("publish - XmlBlasterException: " + e.getMessage()); }
112 }
113
114 public void testNotify() {
115 subscribe(true);
116
117 publish();
118 this.msgInterceptor.waitOnUpdate(1000, this.publishOid, Constants.STATE_OK, 1);
119 assertEquals("Missing update", 1, this.msgInterceptor.count());
120 this.msgInterceptor.clear(); this.msgInterceptor.countErased(true);
121
122 erase();
123 this.msgInterceptor.waitOnUpdate(1000, this.publishOid, Constants.STATE_ERASED, 1);
124 assertEquals("Missing ERASED event", 1, this.msgInterceptor.count());
125 log.info("Success, we received the erase notification");
126 }
127
128 public void testNoNotify() {
129 subscribe(false); // NO ERASE NOTIFICATION WANTED
130
131 publish();
132 this.msgInterceptor.waitOnUpdate(1000, this.publishOid, Constants.STATE_OK, 1);
133 assertEquals("Missing update", 1, this.msgInterceptor.count());
134 this.msgInterceptor.clear(); this.msgInterceptor.countErased(true);
135
136 erase();
137 this.msgInterceptor.waitOnUpdate(1000, this.publishOid, Constants.STATE_ERASED, 0);
138 assertEquals("Wrong ERASED event", 0, this.msgInterceptor.count());
139
140 log.info("Success, we didn't receive the erase notification");
141 }
142
143 public static Test suite()
144 {
145 TestSuite suite= new TestSuite();
146 String loginName = "TestSubNotify";
147 suite.addTest(new TestSubNotify(new Global(), "testNotify"));
148 suite.addTest(new TestSubNotify(new Global(), "testNoNotify"));
149 return suite;
150 }
151
152 /**
153 * Invoke: java org.xmlBlaster.test.qos.TestSubNotify
154 */
155 public static void main(String args[]) {
156 //junit.swingui.TestRunner.run(Bla.class);
157 Global glob = new Global();
158 if (glob.init(args) != 0) {
159 System.err.println("Init failed");
160 System.exit(1);
161 }
162 TestSubNotify TestSubNotify = new TestSubNotify(glob, "TestSubNotify");
163
164 TestSubNotify.setUp();
165 TestSubNotify.testNotify();
166 TestSubNotify.tearDown();
167
168 TestSubNotify.setUp();
169 TestSubNotify.testNoNotify();
170 TestSubNotify.tearDown();
171 }
172 }
syntax highlighted by Code2HTML, v. 0.9.1