1 /*------------------------------------------------------------------------------
2 Name: TestPubForce.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Testing publish()
6 Version: $Id: TestPubForce.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.client.qos.ConnectQos;
14 import org.xmlBlaster.util.XmlBlasterException;
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.client.qos.PublishQos;
20 import org.xmlBlaster.client.I_XmlBlasterAccess;
21 import org.xmlBlaster.util.MsgUnit;
22
23 import junit.framework.*;
24
25
26 /**
27 * This client tests the method publish() with the forceUpdate QOS tag
28 * <br />
29 * <p>
30 * This client may be invoked multiple time on the same xmlBlaster server,
31 * as it cleans up everything after his tests are done.
32 * <p>
33 * Invoke examples:<br />
34 * <pre>
35 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestPubForce
36 * java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestPubForce
37 * </pre>
38 */
39 public class TestPubForce extends TestCase implements I_Callback
40 {
41 private static String ME = "TestPubForce";
42 private final Global glob;
43 private static Logger log = Logger.getLogger(TestPubForce.class.getName());
44
45 private boolean messageArrived = false;
46
47 private String subscribeOid;
48 private String publishOid = "TestMessage";
49 private I_XmlBlasterAccess senderConnection;
50 private String senderName;
51 private String senderContent;
52 private String receiverName; // sender/receiver is here the same client
53
54 private int numReceived = 0; // error checking
55 private final String contentMime = "text/xml";
56 private final String contentMimeExtended = "1.0";
57
58 /**
59 * Constructs the TestPubForce object.
60 * <p />
61 * @param testName The name used in the test suite
62 * @param loginName The name to login to the xmlBlaster
63 */
64 public TestPubForce(Global glob, String testName, String loginName)
65 {
66 super(testName);
67 this.glob = glob;
68
69 this.senderName = loginName;
70 this.receiverName = loginName;
71 }
72
73
74 /**
75 * Sets up the fixture.
76 * <p />
77 * Connect to xmlBlaster and login
78 */
79 protected void setUp()
80 {
81 try {
82 senderConnection = glob.getXmlBlasterAccess(); // Find orb
83 String passwd = "secret";
84 ConnectQos connectQos = new ConnectQos(glob, senderName, passwd);
85 senderConnection.connect(connectQos, this); // Login to xmlBlaster
86 }
87 catch (Exception e) {
88 log.severe(e.toString());
89 e.printStackTrace();
90 }
91 }
92
93
94 /**
95 * Tears down the fixture.
96 * <p />
97 * cleaning up .... erase() the previous message OID and logout
98 */
99 protected void tearDown()
100 {
101 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
102 "<key oid='" + publishOid + "' queryType='EXACT'>\n" +
103 "</key>";
104 String qos = "<qos></qos>";
105 try {
106 EraseReturnQos[] arr = senderConnection.erase(xmlKey, qos);
107 assertEquals("Erase", 1, arr.length);
108 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
109
110 senderConnection.disconnect(null);
111 }
112
113
114 /**
115 * TEST: Subscribe to messages with XPATH.
116 * <p />
117 * The returned subscribeOid is checked
118 */
119 public void testSubscribe()
120 {
121 if (log.isLoggable(Level.FINE)) log.fine("Subscribing using XPath syntax ...");
122
123 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'>\n</key>";
124 String qos = "<qos></qos>";
125 numReceived = 0;
126 subscribeOid = null;
127 try {
128 subscribeOid = senderConnection.subscribe(xmlKey, qos).getSubscriptionId();
129 log.info("Success: Subscribe on " + subscribeOid + " done");
130 } catch(XmlBlasterException e) {
131 log.warning("XmlBlasterException: " + e.getMessage());
132 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
133 }
134 assertTrue("returned null subscribeOid", subscribeOid != null);
135 assertTrue("returned subscribeOid is empty", 0 != subscribeOid.length());
136 }
137
138
139 /**
140 * TEST: Construct a message and publish it.
141 * <p />
142 * The returned publishOid is checked
143 */
144 public void testPublish(boolean forceUpdate)
145 {
146 if (log.isLoggable(Level.FINE)) log.fine("Publishing a message ...");
147
148 numReceived = 0;
149 String xmlKey = "<key oid='" + publishOid + "' contentMime='" + contentMime + "' contentMimeExtended='" + contentMimeExtended + "'>\n" +
150 " <TestPubForce-AGENT id='192.168.124.10' subId='1' type='generic'>" +
151 " </TestPubForce-AGENT>" +
152 "</key>";
153 PublishQos qosWrapper = new PublishQos(glob);
154 qosWrapper.setForceUpdate(forceUpdate);
155 String qos = qosWrapper.toXml(); // == "<qos><forceUpdate/></qos>"
156
157 try {
158 MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), qos);
159 publishOid = senderConnection.publish(msgUnit).getKeyOid();
160 log.info("Success: Publishing done, returned oid=" + publishOid);
161 } catch(XmlBlasterException e) {
162 log.warning("XmlBlasterException: " + e.getMessage());
163 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
164 }
165 assertTrue("returned publishOid == null", publishOid != null);
166 assertTrue("returned publishOid", 0 != publishOid.length());
167 }
168
169
170 /**
171 * TEST: Construct a message and publish it,<br />
172 * Identical messages are not delivered unless ForceUpdate is set
173 */
174 public void testPublishForceUpdate()
175 {
176 testSubscribe();
177 try { Thread.sleep(1000L); } catch( InterruptedException i) {} // Wait some time for callback to arrive ...
178 assertEquals("numReceived after subscribe", 0, numReceived); // there should be no Callback
179
180 senderContent = "I'm the new same content";
181
182 testPublish(true); // true tests ForceUpdate flag
183 waitOnUpdate(4000L);
184 assertEquals("numReceived after publishing", 1, numReceived); // message arrived?
185
186 testPublish(true); // true tests ForceUpdate flag
187 waitOnUpdate(4000L);
188 assertEquals("numReceived after publishing", 1, numReceived); // message arrived?
189
190 testPublish(false);
191 waitOnUpdate(4000L);
192 assertEquals("numReceived after publishing", 0, numReceived); // No message should arrive since they are identical
193
194 testPublish(false);
195 waitOnUpdate(4000L);
196 assertEquals("numReceived after publishing", 0, numReceived); // No message should arrive since they are identical
197
198 testPublish(true); // true tests ForceUpdate flag
199 waitOnUpdate(4000L);
200 assertEquals("numReceived after publishing", 1, numReceived); // message arrived?
201 }
202
203 /**
204 * This is the callback method invoked from xmlBlaster
205 * delivering us a new asynchronous message.
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 {
210 log.info("Receiving update of a message " + updateKey.getOid() + "...");
211
212 numReceived += 1;
213
214 if (updateQos.isErased()) {
215 return "";
216 }
217
218 assertEquals("Wrong sender", senderName, updateQos.getSender().getLoginName());
219 assertEquals("Wrong oid of message returned", publishOid, updateKey.getOid());
220 assertEquals("Message content is corrupted", new String(senderContent), new String(content));
221 assertEquals("Message contentMime is corrupted", contentMime, updateKey.getContentMime());
222 assertEquals("Message contentMimeExtended is corrupted", contentMimeExtended, updateKey.getContentMimeExtended());
223
224 messageArrived = true;
225 return "";
226 }
227
228
229 /**
230 * Little helper, waits until the variable 'messageArrive' is set
231 * to true, or returns when the given timeout occurs.
232 * @param timeout in milliseconds
233 */
234 private void waitOnUpdate(final long timeout)
235 {
236 long pollingInterval = 50L; // check every 0.05 seconds
237 if (timeout < 50) pollingInterval = timeout / 10L;
238 long sum = 0L;
239 while (!messageArrived) {
240 try {
241 Thread.sleep(pollingInterval);
242 }
243 catch( InterruptedException i)
244 {}
245 sum += pollingInterval;
246 if (sum > timeout) {
247 log.warning("Timeout of " + timeout + " occurred");
248 break;
249 }
250 }
251 messageArrived = false;
252 }
253
254
255 /**
256 * Method is used by TestRunner to load these tests
257 */
258 public static Test suite()
259 {
260 TestSuite suite= new TestSuite();
261 String loginName = "Tim";
262 suite.addTest(new TestPubForce(new Global(), "testPublishForceUpdate", loginName));
263 return suite;
264 }
265
266
267 /**
268 * Invoke: java org.xmlBlaster.test.qos.TestPubForce
269 * @deprecated Use the TestRunner from the testsuite to run it:<p />
270 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestPubForce</pre>
271 */
272 public static void main(String args[])
273 {
274 Global glob = new Global();
275 if (glob.init(args) != 0) {
276 System.err.println(ME + ": Init failed");
277 System.exit(1);
278 }
279 TestPubForce testSub = new TestPubForce(glob, "TestPubForce", "Tim");
280 testSub.setUp();
281 testSub.testPublishForceUpdate();
282 testSub.tearDown();
283 }
284 }
syntax highlighted by Code2HTML, v. 0.9.1