1 /*------------------------------------------------------------------------------
2 Name: TestSubNoLocal.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 import org.xmlBlaster.util.Global;
11 import org.xmlBlaster.client.qos.ConnectQos;
12 import org.xmlBlaster.util.XmlBlasterException;
13 import org.xmlBlaster.util.def.Constants;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.client.I_Callback;
16 import org.xmlBlaster.client.key.SubscribeKey;
17 import org.xmlBlaster.client.qos.SubscribeQos;
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.SubscribeReturnQos;
22 import org.xmlBlaster.util.MsgUnit;
23
24 import org.xmlBlaster.test.Util;
25 import org.xmlBlaster.test.Msg;
26 import org.xmlBlaster.test.MsgInterceptor;
27
28 import junit.framework.*;
29
30
31 /**
32 * This client tests a subscribe() with local=false to avoid receiving
33 * its own publishes.
34 * <br />
35 * This client may be invoked multiple time on the same xmlBlaster server,
36 * as it cleans up everything after his tests are done.
37 * <p>
38 * Invoke examples:<br />
39 * <pre>
40 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubNoLocal
41 * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestSubNoLocal
42 * </pre>
43 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.subcribe.html" target="others">interface subscribe requirement</a>
44 */
45 public class TestSubNoLocal extends TestCase implements I_Callback
46 {
47 private static String ME = "TestSubNoLocal";
48 private final Global glob;
49 private static Logger log = Logger.getLogger(TestSubNoLocal.class.getName());
50
51 private String subscribeId1;
52 private MsgInterceptor updateInterceptor1;
53
54 private String subscribeId2;
55 private MsgInterceptor updateInterceptor2;
56
57 private String publishOid = "HelloMessageNoLocal";
58 private I_XmlBlasterAccess connection;
59
60 /**
61 * Constructs the TestSubNoLocal object.
62 * <p />
63 * @param testName The name used in the test suite
64 */
65 public TestSubNoLocal(Global glob, String testName) {
66 super(testName);
67 this.glob = glob;
68
69 }
70
71 /**
72 * Sets up the fixture.
73 */
74 protected void setUp() {
75 }
76
77 /**
78 * Tears down the fixture.
79 * <p />
80 * cleaning up .... erase() the previous message OID and logout
81 */
82 protected void tearDown() {
83 if (this.connection != null) {
84 if (this.publishOid != null) {
85 String xmlKey = "<key oid='" + this.publishOid + "' queryType='EXACT'/>";
86 try {
87 EraseReturnQos[] arr = this.connection.erase(xmlKey, "<qos/>");
88 assertEquals("Erase", 1, arr.length);
89 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
90 }
91
92 this.connection.disconnect(null);
93 this.connection = null;
94 }
95 }
96
97 /**
98 * Subscribe twice to the same message, one time with <local>false</local>
99 */
100 public void subscribe() {
101 if (log.isLoggable(Level.FINE)) log.fine("Subscribing ...");
102 try {
103 {
104 SubscribeKey key = new SubscribeKey(glob, publishOid);
105 SubscribeQos qos = new SubscribeQos(glob);
106 qos.setWantLocal(false);
107 this.updateInterceptor1 = new MsgInterceptor(glob, log, null); // Collect received msgs
108 SubscribeReturnQos ret = this.connection.subscribe(key.toXml(), qos.toXml(), this.updateInterceptor1);
109 subscribeId1 = ret.getSubscriptionId();
110 }
111 {
112 SubscribeKey key = new SubscribeKey(glob, publishOid);
113 SubscribeQos qos = new SubscribeQos(glob);
114 qos.setWantLocal(true);
115 this.updateInterceptor2 = new MsgInterceptor(glob, log, null); // Collect received msgs
116 SubscribeReturnQos ret = connection.subscribe(key.toXml(), qos.toXml(), this.updateInterceptor2);
117 subscribeId2 = ret.getSubscriptionId();
118 }
119 } catch(XmlBlasterException e) {
120 log.warning("XmlBlasterException: " + e.getMessage());
121 fail("subscribe - XmlBlasterException: " + e.getMessage());
122 }
123 }
124
125 /**
126 * Construct a message and publish it.
127 */
128 public void publish() {
129 if (log.isLoggable(Level.FINE)) log.fine("Publishing a message ...");
130
131 String xmlKey = "<key oid='" + publishOid + "'/>";
132 String senderContent = "Yeahh, i'm the new content";
133 try {
134 MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), "<qos/>");
135 publishOid = connection.publish(msgUnit).getKeyOid();
136 log.info("Success: Publishing done, returned oid=" + publishOid);
137 } catch(XmlBlasterException e) {
138 log.warning("XmlBlasterException: " + e.getMessage());
139 fail("publish - XmlBlasterException: " + e.getMessage());
140 }
141
142 assertTrue("returned publishOid == null", publishOid != null);
143 assertTrue("returned publishOid", 0 != publishOid.length());
144 }
145
146 /**
147 * unSubscribe twice to same message.
148 */
149 public void unSubscribe() {
150 if (log.isLoggable(Level.FINE)) log.fine("unSubscribing ...");
151
152 String qos = "<qos/>";
153 try {
154 connection.unSubscribe("<key oid='" + subscribeId1 + "'/>", qos);
155 log.info("Success: unSubscribe 1 on " + subscribeId1 + " done");
156
157 connection.unSubscribe("<key oid='" + subscribeId2 + "'/>", qos);
158 log.info("Success: unSubscribe 2 on " + subscribeId2 + " done");
159 } catch(XmlBlasterException e) {
160 log.warning("XmlBlasterException: " + e.getMessage());
161 fail("unSubscribe - XmlBlasterException: " + e.getMessage());
162 }
163 }
164
165 private void connect() {
166 try {
167 connection = glob.getXmlBlasterAccess(); // Find orb
168 ConnectQos qos = new ConnectQos(glob);
169 connection.connect(qos, this);
170 }
171 catch (Exception e) {
172 log.severe("Login failed: " + e.toString());
173 e.printStackTrace();
174 fail("Login failed: " + e.toString());
175 }
176 }
177
178 /**
179 * TEST: Construct a message and publish it,
180 * the first subscription shouldn't receive the message as local==false
181 */
182 public void testLocalUpdates() {
183 log.info("testLocalUpdates ...");
184
185 connect();
186
187 subscribe(); // there should be no Callback
188 assertEquals("", 0, this.updateInterceptor1.waitOnUpdate(1000L, 0));
189 assertEquals("", 0, this.updateInterceptor2.waitOnUpdate(1000L, 0));
190
191 int numPub = 5;
192 for (int i=0; i<numPub; i++)
193 publish(); // We expect sub 2 to receive the updates only
194 assertEquals("", 0, this.updateInterceptor1.waitOnUpdate(1000L, 0));
195 assertEquals("", numPub, this.updateInterceptor2.waitOnUpdate(1000L, publishOid, Constants.STATE_OK));
196 this.updateInterceptor2.clear();
197
198 unSubscribe();
199
200 publish();
201 assertEquals("", 0, this.updateInterceptor1.waitOnUpdate(1000L, 0));
202 assertEquals("", 0, this.updateInterceptor2.waitOnUpdate(1000L, 0));
203 }
204
205 /**
206 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
207 */
208 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
209 log.severe("Receiving unexpected update of a message " + updateKey.getOid() + " state=" + updateQos.getState());
210 return "";
211 }
212
213 /**
214 * Method is used by TestRunner to load these tests
215 */
216 public static Test suite() {
217 TestSuite suite= new TestSuite();
218 suite.addTest(new TestSubNoLocal(new Global(), "testLocalUpdates"));
219 return suite;
220 }
221
222 /**
223 * Invoke: java org.xmlBlaster.test.qos.TestSubNoLocal
224 * @deprecated Use the TestRunner from the testsuite to run it:<p />
225 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubNoLocal</pre>
226 */
227 public static void main(String args[]) {
228 Global glob = new Global();
229 if (glob.init(args) != 0) {
230 System.err.println("Init failed");
231 System.exit(1);
232 }
233 TestSubNoLocal testSub = new TestSubNoLocal(glob, "TestSubNoLocal");
234 testSub.setUp();
235 testSub.testLocalUpdates();
236 testSub.tearDown();
237 }
238 }
syntax highlighted by Code2HTML, v. 0.9.1