1 /*------------------------------------------------------------------------------
2 Name: TestErase.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Demo code for a client using xmlBlaster
6 ------------------------------------------------------------------------------*/
7 package org.xmlBlaster.test.qos;
8
9 import java.util.logging.Logger;
10 import java.util.logging.Level;
11 import org.xmlBlaster.util.Global;
12 import org.xmlBlaster.client.qos.ConnectQos;
13 import org.xmlBlaster.util.XmlBlasterException;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.client.I_Callback;
16 import org.xmlBlaster.client.key.UpdateKey;
17 import org.xmlBlaster.client.qos.UpdateQos;
18 import org.xmlBlaster.client.qos.EraseReturnQos;
19 import org.xmlBlaster.util.MsgUnit;
20 import org.xmlBlaster.util.def.Constants;
21
22 import org.xmlBlaster.test.Msg;
23 import org.xmlBlaster.test.MsgInterceptor;
24
25 import junit.framework.*;
26
27
28 /**
29 * This client tests if it receives message erase events.
30 * <p>
31 * Invoke examples:<br />
32 * <pre>
33 * java junit.textui.TestRunner -noloading org.xmlBlaster.test.qos.TestErase
34 * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestErase
35 * </pre>
36 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.update.html" target="others">update requirement</a>
37 */
38 public class TestErase extends TestCase implements I_Callback
39 {
40 private static String ME = "TestErase";
41 private final Global glob;
42 private static Logger log = Logger.getLogger(TestErase.class.getName());
43
44 private String subscribeId;
45 private String oidExact = "HelloMessage";
46 private String oidXpath = "//key[@oid=\""+oidExact+"\"]";
47 private MsgUnit msgUnit;
48 private String publishOid = null;
49 private I_XmlBlasterAccess con;
50
51 private final String contentMime = "text/xml";
52
53 private MsgInterceptor updateInterceptor;
54
55 /**
56 * Constructs the TestErase object.
57 * <p />
58 * @param testName The name used in the test suite
59 */
60 public TestErase(Global glob, String testName) {
61 super(testName);
62 this.glob = glob;
63
64 }
65
66 /**
67 * Sets up the fixture.
68 * <p />
69 * Connect to xmlBlaster and login
70 */
71 protected void setUp() {
72 }
73
74 /**
75 * Tears down the fixture.
76 * <p />
77 * cleaning up .... erase() the previous message OID and logout
78 */
79 protected void tearDown() {
80 if (con != null) {
81 if (publishOid != null) {
82 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'/>";
83 try {
84 EraseReturnQos[] arr = con.erase(xmlKey, "<qos/>");
85 // can be erased already in our tests
86 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
87 }
88
89 con.disconnect(null);
90 con = null;
91 }
92 }
93
94 /**
95 * Subscribe three times to same message.
96 * <p />
97 * The returned subscribeId is checked
98 */
99 private void subscribe(boolean exact) {
100 if (log.isLoggable(Level.FINE)) log.fine("Subscribing ...");
101
102 String xmlKey;
103 if (exact)
104 xmlKey = "<key oid='" + oidExact + "' queryType='EXACT'/>";
105 else
106 xmlKey = "<key oid='' queryType='XPATH'>" + oidXpath + "</key>";
107 String qos = "<qos/>";
108 subscribeId = null;
109 try {
110 subscribeId = con.subscribe(xmlKey, qos).getSubscriptionId();
111 assertTrue("returned null subscribeId", subscribeId != null);
112 assertTrue("returned subscribeId is empty", 0 != subscribeId.length());
113 log.info("Success: Subscribe on " + subscribeId + " done");
114 }
115 catch(XmlBlasterException e) {
116 log.warning("XmlBlasterException: " + e.getMessage());
117 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
118 }
119 }
120
121 /**
122 * TEST: Construct a message and publish it.
123 * <p />
124 * The returned publishOid is checked
125 */
126 private void publish() {
127 if (log.isLoggable(Level.FINE)) log.fine("Publishing a message ...");
128
129 String xmlKey = "<key oid='" + oidExact + "' contentMime='" + contentMime + "'/>";
130 String senderContent = "Yeahh, i'm the new content";
131 try {
132 msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), "<qos/>");
133 publishOid = con.publish(msgUnit).getKeyOid();
134 log.info("Success: Publishing done, returned oid=" + publishOid);
135 } catch(XmlBlasterException e) {
136 log.warning("XmlBlasterException: " + e.getMessage());
137 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
138 }
139
140 assertTrue("returned publishOid == null", publishOid != null);
141 assertTrue("returned publishOid", 0 != publishOid.length());
142 assertEquals("returned publishOid is wrong", oidExact, publishOid);
143 }
144
145 /**
146 * unSubscribe three times to same message.
147 */
148 private void unSubscribe() {
149 if (log.isLoggable(Level.FINE)) log.fine("unSubscribing ...");
150
151 String qos = "<qos/>";
152 try {
153 con.unSubscribe("<key oid='" + subscribeId + "'/>", qos);
154 log.info("Success: unSubscribe on " + subscribeId + " done");
155 } catch(XmlBlasterException e) {
156 log.warning("XmlBlasterException: " + e.getMessage());
157 assertTrue("unSubscribe - XmlBlasterException: " + e.getMessage(), false);
158 }
159 }
160
161 private void erase() {
162 if (publishOid != null) {
163 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'/>";
164 try {
165 EraseReturnQos[] arr = con.erase(xmlKey, "<qos/>");
166 assertEquals("Erase", 1, arr.length);
167 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
168 }
169 }
170
171 private void connect() {
172 try {
173 this.updateInterceptor = new MsgInterceptor(this.glob, log, this);
174 con = glob.getXmlBlasterAccess(); // Find orb
175 ConnectQos qos = new ConnectQos(glob);
176 con.connect(qos, this.updateInterceptor);
177 }
178 catch (Exception e) {
179 log.severe("Login failed: " + e.toString());
180 e.printStackTrace();
181 assertTrue("Login failed: " + e.toString(), false);
182 }
183 this.updateInterceptor.clear();
184 }
185
186 /**
187 * TEST: Subscribe to a message, publish it, erase it and check if we are notified
188 * about the erased message
189 * <br />
190 */
191 public void testEraseEvent() throws Exception {
192 log.info("testEraseEvent ...");
193
194 connect();
195
196 subscribe(true);
197 assertEquals("numReceived after subscribe", 0, this.updateInterceptor.waitOnUpdate(1000L, 0)); // no message arrived?
198
199 {
200 publish();
201 assertEquals("numReceived after sending", 1, this.updateInterceptor.waitOnUpdate(1500L, oidExact, Constants.STATE_OK, 1));
202 Msg msg = this.updateInterceptor.getMsg(oidExact, Constants.STATE_OK);
203 assertTrue("Wrong update state", msg.getUpdateQos().isOk());
204
205 msg.compareMsg(msgUnit);
206 assertEquals("Message contentMime is corrupted", contentMime, msg.getUpdateKey().getContentMime());
207
208 this.updateInterceptor.clear();
209 }
210
211 {
212 this.updateInterceptor.countErased(true);
213 erase();
214 assertEquals("erase event is missing", 1, this.updateInterceptor.waitOnUpdate(2500L, oidExact, Constants.STATE_ERASED, 1));
215 Msg msg = this.updateInterceptor.getMsg(oidExact, Constants.STATE_ERASED);
216 assertEquals("wrong subscriptionId expected=" + subscribeId, subscribeId, msg.getUpdateQos().getSubscriptionId());
217 assertTrue("wrong update state", msg.getUpdateQos().isErased());
218
219 this.updateInterceptor.clear();
220 }
221
222 log.info("testEraseEvent SUCCESS");
223 }
224
225 /**
226 * TEST: Subscribe to a message, publish it, erase it and check if we are notified
227 * about the erased message
228 * <br />
229 */
230 public void testXPathEraseEvent() throws Exception {
231 log.info("testXPathEraseEvent ...");
232
233 connect();
234
235 subscribe(false);
236 assertEquals("numReceived after subscribe", 0, this.updateInterceptor.waitOnUpdate(1000L, 0)); // no message arrived?
237
238 {
239 publish();
240 assertEquals("numReceived after sending", 1, this.updateInterceptor.waitOnUpdate(1500L, oidExact, Constants.STATE_OK, 1));
241 Msg msg = this.updateInterceptor.getMsg(oidExact, Constants.STATE_OK);
242 assertTrue("Wrong update state", msg.getUpdateQos().isOk());
243
244 msg.compareMsg(msgUnit);
245 assertEquals("Message contentMime is corrupted", contentMime, msg.getUpdateKey().getContentMime());
246
247 this.updateInterceptor.clear();
248 }
249
250 {
251 log.info("Erasing now ...");
252 this.updateInterceptor.countErased(true);
253
254 erase();
255 assertEquals("erase event is missing", 1, this.updateInterceptor.waitOnUpdate(2500L, oidExact, Constants.STATE_ERASED, 1));
256 Msg msg = this.updateInterceptor.getMsg(oidExact, Constants.STATE_ERASED);
257 assertEquals("wrong subscriptionId expected=" + subscribeId, subscribeId, msg.getUpdateQos().getSubscriptionId());
258 assertTrue("wrong update state", msg.getUpdateQos().isErased());
259
260 this.updateInterceptor.clear();
261 }
262
263 log.info("testXPathEraseEvent SUCCESS");
264 }
265
266 /**
267 * This is the callback method invoked from xmlBlaster
268 * delivering us a new asynchronous message.
269 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
270 */
271 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
272 if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
273 return "";
274 }
275
276 /**
277 * Method is used by TestRunner to load these tests
278 */
279 public static Test suite() {
280 TestSuite suite= new TestSuite();
281 suite.addTest(new TestErase(new Global(), "testEraseEvent"));
282 return suite;
283 }
284
285 /**
286 * Invoke: java org.xmlBlaster.test.qos.TestErase
287 * @deprecated Use the TestRunner from the testsuite to run it:<p />
288 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestErase</pre>
289 */
290 public static void main(String args[]) {
291 Global glob = new Global();
292 if (glob.init(args) != 0) {
293 System.err.println("Init failed");
294 System.exit(1);
295 }
296 try {
297 TestErase testSub = new TestErase(glob, "TestErase");
298 testSub.setUp();
299 //testSub.testEraseEvent();
300 testSub.testXPathEraseEvent();
301 testSub.tearDown();
302 }
303 catch (Exception e) {
304 e.printStackTrace();
305 System.out.println("EERRRROR: " + e.toString());
306 }
307
308 }
309 }
syntax highlighted by Code2HTML, v. 0.9.1