1 /*------------------------------------------------------------------------------
2 Name: TestSubXPath.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.util.XmlBlasterException;
13 import org.xmlBlaster.client.I_XmlBlasterAccess;
14 import org.xmlBlaster.client.key.UpdateKey;
15 import org.xmlBlaster.client.key.PublishKey;
16 import org.xmlBlaster.client.qos.ConnectQos;
17 import org.xmlBlaster.client.qos.PublishQos;
18 import org.xmlBlaster.client.qos.PublishReturnQos;
19 import org.xmlBlaster.client.qos.UpdateQos;
20 import org.xmlBlaster.client.qos.EraseReturnQos;
21 import org.xmlBlaster.util.MsgUnit;
22 import org.xmlBlaster.util.def.Constants;
23 import org.xmlBlaster.test.Msg;
24 import org.xmlBlaster.test.MsgInterceptor;
25
26 import junit.framework.*;
27
28
29 /**
30 * This client tests the method subscribe() with a later publish() with XPath query.
31 * <br />
32 * The subscribe() on message 3 should be recognized for this later arriving publish()
33 * <p>
34 * This client may be invoked multiple time on the same xmlBlaster server,
35 * as it cleans up everything after his tests are done.
36 * <p>
37 * Invoke examples:<br />
38 * <pre>
39 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubXPath
40 * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestSubXPath
41 * </pre>
42 */
43 public class TestSubXPath extends TestCase
44 {
45 private String ME = "TestSubXPath";
46 private final Global glob;
47 private static Logger log = Logger.getLogger(TestSubXPath.class.getName());
48
49 private String publishOid = "";
50 private I_XmlBlasterAccess senderConnection;
51 private String senderName;
52 private String receiverName; // sender/receiver is here the same client
53
54 private int numPublish = 5;
55 private final String contentMime = "text/xml";
56 private String subscribeOid = null;
57
58 private MsgInterceptor updateInterceptor;
59
60 /**
61 * Constructs the TestSubXPath object.
62 * <p />
63 * @param testName The name used in the test suite
64 * @param loginName The name to login to the xmlBlaster
65 */
66 public TestSubXPath(Global glob, String testName, String loginName) {
67 super(testName);
68 this.glob = glob;
69
70 this.senderName = loginName;
71 this.receiverName = loginName;
72 }
73
74 /**
75 * Sets up the fixture.
76 * <p />
77 * Connect to xmlBlaster and login
78 */
79 protected void setUp() {
80 try {
81 senderConnection = glob.getXmlBlasterAccess(); // Find orb
82 ConnectQos qos = new ConnectQos(this.glob, this.senderName, "secret");
83 this.updateInterceptor = new MsgInterceptor(this.glob, log, null);
84 senderConnection.connect(qos, this.updateInterceptor); // Login to xmlBlaster
85 }
86 catch (Exception e) {
87 log.severe("Login failed: " + e.toString());
88 e.printStackTrace();
89 assertTrue("Login failed: " + e.toString(), false);
90 }
91 this.updateInterceptor.clear();
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 senderConnection.disconnect(null);
101 }
102
103 /**
104 * TEST: Subscribe to message number 3 with XPATH.
105 * <p />
106 */
107 private void subscribeXPath(String query) {
108 if (log.isLoggable(Level.FINE)) log.fine("Subscribing using XPath syntax ...");
109
110 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
111 query +
112 "</key>";
113 String qos = "<qos/>";
114 try {
115 subscribeOid = senderConnection.subscribe(xmlKey, qos).getSubscriptionId();
116 log.info("Success: Subscribe on " + subscribeOid + " done:\n" + xmlKey);
117 } catch(XmlBlasterException e) {
118 log.warning("XmlBlasterException: " + e.getMessage());
119 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
120 }
121 assertTrue("returned null subscribeOid", subscribeOid != null);
122 assertTrue("returned subscribeOid is empty", 0 != subscribeOid.length());
123 }
124
125 /**
126 * TEST: Construct 5 messages and publish them.
127 * <p />
128 * The returned publishOid is checked
129 */
130 private void doPublish() {
131 if (log.isLoggable(Level.FINE)) log.fine("Publishing a message ...");
132
133 for (int counter= 1; counter <= numPublish; counter++) {
134 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
135 "<key oid='" + counter + "' contentMime='" + contentMime + "'>\n" +
136 "<AGENT id='message_" + counter + "' subId='1' type='generic'>" +
137 "<DRIVER id='FileProof' pollingFreq='10'>" +
138 "</DRIVER>"+
139 "</AGENT>" +
140 "</key>";
141 String content = "Content: message_" + counter;
142 try {
143 MsgUnit msgUnit = new MsgUnit(glob, xmlKey, content.getBytes(), "<qos></qos>");
144 publishOid = senderConnection.publish(msgUnit).getKeyOid();
145 log.info("Success: Publishing #" + counter + " done, returned oid=" + publishOid);
146 } catch(XmlBlasterException e) {
147 log.warning("XmlBlasterException: " + e.getMessage());
148 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
149 }
150
151 assertTrue("returned publishOid == null", publishOid != null);
152 assertTrue("returned publishOid", 0 != publishOid.length());
153 }
154 }
155
156 /**
157 * TEST: Construct 5 messages and publish them,<br />
158 * the previous XPath subscription should match message #3 and send an update.
159 */
160 public void testInitial() {
161 ME = "TestSubXPath:testInitial()";
162
163 String oid = "INITIAL";
164
165 subscribeXPath("//demoXPath");
166 assertEquals("numReceived after subscribe", 0, this.updateInterceptor.waitOnUpdate(1000L, null, null));
167 this.updateInterceptor.clear();
168
169 try {
170 PublishKey pk = new PublishKey(glob, oid, "text/xml", "1.0");
171 pk.setClientTags("<org.xmlBlaster><demoXPath/></org.xmlBlaster>");
172 PublishQos pq = new PublishQos(glob);
173 MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
174 PublishReturnQos tmp = senderConnection.publish(msgUnit);
175 assertEquals("returned oid", oid, tmp.getKeyOid());
176 assertEquals("numReceived after publishing", 1, this.updateInterceptor.waitOnUpdate(2000L, oid, Constants.STATE_OK));
177 assertEquals("", 1, this.updateInterceptor.getMsgs().length);
178 }
179 catch (XmlBlasterException e) {
180 log.severe(e.getMessage());
181 fail(e.getMessage());
182 }
183
184 try {
185 EraseReturnQos[] arr = senderConnection.erase("<key oid='"+oid+"'/>", "<qos/>");
186 assertEquals("Erase", 1, arr.length);
187 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
188 }
189
190 /**
191 * TEST: Check if XPath finds XML-attributes
192 */
193 public void testAttribute() {
194 ME = "TestSubXPath:testAttribute()";
195
196 String oid = "gunsNroses";
197
198 this.updateInterceptor.clear();
199
200 try {
201 PublishKey pk = new PublishKey(glob, oid, "text/xml", "1.0");
202 pk.setClientTags("<rose><color id='green'></color></rose>");
203 PublishQos pq = new PublishQos(glob);
204 MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
205 PublishReturnQos tmp = senderConnection.publish(msgUnit);
206 assertEquals("returned oid", oid, tmp.getKeyOid());
207 subscribeXPath("//rose/color[@id='green']");
208 assertEquals("numReceived after publishing", 1, this.updateInterceptor.waitOnUpdate(2000L, oid, Constants.STATE_OK));
209 assertEquals("", 1, this.updateInterceptor.getMsgs().length);
210 }
211 catch (XmlBlasterException e) {
212 log.severe(e.getMessage());
213 fail(e.getMessage());
214 }
215
216 try {
217 EraseReturnQos[] arr = senderConnection.erase("<key oid='"+oid+"'/>", "<qos/>");
218 assertEquals("Erase", 1, arr.length);
219 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
220 }
221
222 /**
223 * TEST: Construct 5 messages and publish them,<br />
224 * the previous XPath subscription should match message #3 and send an update.
225 */
226 public void testPublishAfterSubscribeXPath() {
227 ME = "TestSubXPath:testPublishAfterSubscribeXPath()";
228
229 subscribeXPath("/xmlBlaster/key/AGENT[@id='message_3']");
230 // there should be no Callback
231 assertEquals("numReceived after subscribe", 0, this.updateInterceptor.waitOnUpdate(1000L, null, null));
232 this.updateInterceptor.clear();
233
234 int n = 4;
235 for(int i=0; i<n; i++) {
236 log.info("TEST " + (i+1) + " - publishing 5 messages, expecting No.3");
237 doPublish();
238 assertEquals("numReceived after publishing", 1, this.updateInterceptor.waitOnUpdate(2000L, "3", Constants.STATE_OK));
239 assertEquals("", 1, this.updateInterceptor.getMsgs().length);
240 Msg msg = this.updateInterceptor.getMsgs()[0];
241 assertEquals("Corrupt content", senderName, msg.getUpdateQos().getSender().getLoginName());
242 assertEquals("Corrupt content", "Content: message_3", msg.getContentStr());
243 assertEquals("Message contentMime is corrupted", contentMime, msg.getUpdateKey().getContentMime());
244 assertEquals("engine.qos.update.subscriptionId: Wrong subscriptionId", subscribeOid, msg.getUpdateQos().getSubscriptionId());
245 this.updateInterceptor.clear();
246 }
247
248 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
249 " /xmlBlaster/key/AGENT" +
250 "</key>";
251 try {
252 EraseReturnQos[] arr = senderConnection.erase(xmlKey, "<qos/>");
253 assertEquals("Erase", numPublish, arr.length);
254 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
255 }
256
257 /**
258 * Method is used by TestRunner to load these tests
259 */
260 public static Test suite() {
261 TestSuite suite= new TestSuite();
262 String loginName = "Tim";
263 suite.addTest(new TestSubXPath(new Global(), "testInitial", loginName));
264 suite.addTest(new TestSubXPath(new Global(), "testAttribute", loginName));
265 suite.addTest(new TestSubXPath(new Global(), "testPublishAfterSubscribeXPath", loginName));
266 return suite;
267 }
268
269 /**
270 * Invoke: java org.xmlBlaster.test.qos.TestSubXPath
271 */
272 public static void main(String args[]) {
273 TestSubXPath testSub = new TestSubXPath(new Global(args), "TestSubXPath", "Tim");
274 testSub.setUp();
275 testSub.testAttribute();
276 //testSub.testInitial();
277 //testSub.testPublishAfterSubscribeXPath();
278 testSub.tearDown();
279 }
280 }
syntax highlighted by Code2HTML, v. 0.9.1