1 /*------------------------------------------------------------------------------
  2 Name:      TestSubXPathMany.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Test XPath.
  6 ------------------------------------------------------------------------------*/
  7 package org.xmlBlaster.test.qos;
  8 
  9 import java.util.logging.Logger;
 10 import org.xmlBlaster.util.Global;
 11 import org.xmlBlaster.client.qos.ConnectQos;
 12 import org.xmlBlaster.util.XmlBlasterException;
 13 import org.xmlBlaster.client.I_XmlBlasterAccess;
 14 import org.xmlBlaster.client.I_Callback;
 15 import org.xmlBlaster.client.key.PublishKey;
 16 import org.xmlBlaster.client.key.EraseKey;
 17 import org.xmlBlaster.client.key.SubscribeKey;
 18 import org.xmlBlaster.client.key.UpdateKey;
 19 import org.xmlBlaster.client.qos.PublishQos;
 20 import org.xmlBlaster.client.qos.UpdateQos;
 21 import org.xmlBlaster.client.qos.SubscribeQos;
 22 import org.xmlBlaster.client.qos.EraseQos;
 23 import org.xmlBlaster.util.MsgUnit;
 24 import org.xmlBlaster.util.def.Constants;
 25 
 26 import junit.framework.*;
 27 
 28 
 29 /**
 30  * This client tests the method subscribe() with a later publish() with XPath query. 
 31  * <p>
 32  * This client may be invoked multiple time on the same xmlBlaster server,
 33  * as it cleans up everything after his tests are done.
 34  * <p>
 35  * Invoke examples:<br />
 36  * <pre>
 37  *    java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubXPathMany
 38  *    java -Djava.compiler= junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestSubXPathMany
 39  * </pre>
 40  * @author xmlBlaster@marcelruff.info
 41  */
 42 public class TestSubXPathMany extends TestCase {
 43    private final Global glob;
 44    private static Logger log = Logger.getLogger(TestSubXPathMany.class.getName());
 45 
 46    private I_XmlBlasterAccess con1, con2, con3;
 47 
 48    private int numReceived1 = 0;        // error checking
 49    private StringBuffer messageArrived1 = new StringBuffer();
 50    private int numReceived2 = 0;        // error checking
 51    private StringBuffer messageArrived2 = new StringBuffer();
 52    private int numReceived3 = 0;        // error checking
 53    private StringBuffer messageArrived3 = new StringBuffer();
 54 
 55    /**
 56     * Constructs the TestSubXPathMany object, used by junit. 
 57     */
 58    public TestSubXPathMany(String name) {
 59        super(name);
 60        this.glob = new Global();
 61 
 62    }
 63 
 64    /**
 65     * Constructs the TestSubXPathMany object, used by main.
 66     */
 67    public TestSubXPathMany(Global glob) {
 68        super("TestSubXPathMany");
 69        this.glob = glob;
 70 
 71    }
 72 
 73    /**
 74     * Sets up the fixture, connect to xmlBlaster 3 times. 
 75     */
 76    protected void setUp() {
 77       ConnectQos connectQos;
 78       try {
 79          Global glob1 = glob.getClone(null);
 80          con1 = glob1.getXmlBlasterAccess(); // Find orb
 81          connectQos = new ConnectQos(glob1, "con1", "secret");
 82          con1.connect(connectQos,  new I_Callback() {
 83             public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 84                log.info("Reveiving asynchronous message '" + updateKey.getOid() + "' in default handler");
 85                numReceived1++;
 86                assertEquals("Message not expected", "command-navigation", updateKey.getOid());
 87                messageArrived1.append("OK");
 88                return "";
 89             }
 90          });
 91 
 92          Global glob2 = glob.getClone(null);
 93          con2 = glob2.getXmlBlasterAccess(); // Find orb
 94          connectQos = new ConnectQos(glob2, "con2", "secret");
 95          con2.connect(connectQos,  new I_Callback() {
 96             public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 97                log.info("Reveiving asynchronous message '" + updateKey.getOid() + "' in default handler");
 98                numReceived2++;
 99                assertEquals("Message not expected", "command-radar-1", updateKey.getOid());
100                messageArrived2.append("OK");
101                return "";
102             }
103          });
104 
105          Global glob3 = glob.getClone(null);
106          con3 = glob3.getXmlBlasterAccess(); // Find orb
107          connectQos = new ConnectQos(glob3, "con3", "secret");
108          con3.connect(connectQos,  new I_Callback() {
109             public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
110                log.info("Reveiving asynchronous message '" + updateKey.getOid() + "' in default handler");
111                numReceived3++;
112                assertEquals("Message not expected", "command-radar-1", updateKey.getOid());
113                messageArrived3.append("OK");
114                return "";
115             }
116          });
117 
118       }
119       catch (XmlBlasterException e) {
120          log.severe("login failed: " + e.toString());
121          fail("Login failed: " + e.toString());
122       }
123    }
124 
125 
126    /**
127     * Tear down the fixture, erase messages and disconnect from xmlBlaster. 
128     */
129    protected void tearDown() {
130       if (con1 != null) {
131          try {
132             EraseKey ek = new EraseKey(glob, "command-navigation");
133             EraseQos eq = new EraseQos(glob);
134             con1.erase(ek.toXml(), eq.toXml());
135 
136             ek = new EraseKey(glob, "command-radar-1");
137             eq = new EraseQos(glob);
138             con1.erase(ek.toXml(), eq.toXml());
139 
140             ek = new EraseKey(glob, "dummyTestSubXPathMany");
141             eq = new EraseQos(glob);
142             con1.erase(ek.toXml(), eq.toXml());
143          }
144          catch (XmlBlasterException e) {
145             fail("doPublish failed: " + e.toString());
146          }
147          con1.disconnect(null);
148       }
149       if (con2 != null) { con2.disconnect(null); con2=null; }
150       if (con3 != null) { con3.disconnect(null); con3=null; }
151    }
152 
153    /**
154     * Subscribe with XPATH.
155     */
156    public void doSubscribe() {
157       log.info("*****Subscribing using XPath syntax ...");
158 
159       try {
160          SubscribeKey sk = new SubscribeKey(glob, "//key[@oid = 'command-navigation']", Constants.XPATH);
161          SubscribeQos sq = new SubscribeQos(glob);
162          con1.subscribe(sk.toXml(), sq.toXml()).getSubscriptionId();
163 
164          String xpath2 = "//key[starts-with(@oid,'command-radar')]";
165          sk = new SubscribeKey(glob, xpath2, Constants.XPATH);
166          sq = new SubscribeQos(glob);
167          con2.subscribe(sk.toXml(), sq.toXml()).getSubscriptionId();
168 
169          sk = new SubscribeKey(glob, xpath2, Constants.XPATH);
170          sq = new SubscribeQos(glob);
171          con3.subscribe(sk.toXml(), sq.toXml()).getSubscriptionId();
172       }
173       catch (XmlBlasterException e) {
174          fail("doPublish failed: " + e.toString());
175       }
176    }
177 
178    /**
179     * Publish some messages. 
180     */
181    public void doPublish() {
182       log.info("*****Publishing messages ...");
183 
184       try {
185          PublishKey pk = new PublishKey(glob, "command-navigation", "text/plain", "1.0");
186          PublishQos pq = new PublishQos(glob);
187          MsgUnit msgUnit = new MsgUnit(pk.toXml(), "Hi".getBytes(), pq.toXml());
188          con1.publish(msgUnit);
189          log.info("Published message '" + pk.getOid() + "'");
190 
191          pk = new PublishKey(glob, "command-radar-1", "text/plain", "1.0");
192          pq = new PublishQos(glob);
193          msgUnit = new MsgUnit(pk.toXml(), "Hi".getBytes(), pq.toXml());
194          con1.publish(msgUnit);
195          log.info("Published message '" + pk.getOid() + "'");
196 
197          pk = new PublishKey(glob, "dummyTestSubXPathMany", "text/plain", "1.0");
198          pq = new PublishQos(glob);
199          msgUnit = new MsgUnit(pk.toXml(), "Hi".getBytes(), pq.toXml());
200          con1.publish(msgUnit);
201          log.info("Published message '" + pk.getOid() + "'");
202       }
203       catch (XmlBlasterException e) {
204          fail("doPublish failed: " + e.toString());
205       }
206    }
207 
208 
209    /**
210     * TEST: Publish some messages, disconnect client3 subscribe with XPath and
211     * check if everything is OK.
212     */
213    public void testIt()
214    {
215       doSubscribe();
216       try { Thread.sleep(2000L); } catch( InterruptedException i) {}                                             // Wait some time for callback to arrive ...
217       assertEquals("numReceived1 after subscribe", 0, numReceived1); // there should be no Callback
218       assertEquals("numReceived2 after subscribe", 0, numReceived2); // there should be no Callback
219       assertEquals("numReceived3 after subscribe", 0, numReceived3); // there should be no Callback
220 
221       doPublish();
222       waitOnUpdate(2000L, messageArrived1);
223       assertEquals("numReceived1 after publishing", 1, numReceived1); // message arrived?
224       waitOnUpdate(2000L, messageArrived2);
225       assertEquals("numReceived2 after publishing", 1, numReceived2); // message arrived?
226       waitOnUpdate(2000L, messageArrived3);
227       assertEquals("numReceived3 after publishing", 1, numReceived3); // message arrived?
228 
229       numReceived1 = numReceived2 = numReceived3 = 0;
230       messageArrived1.setLength(0);
231       messageArrived2.setLength(0);
232       messageArrived3.setLength(0);
233 
234       if (con3 != null) { con3.disconnect(null); con3 = null; }
235 
236       doPublish();
237       waitOnUpdate(2000L, messageArrived1);
238       assertEquals("numReceived1 after publishing", 1, numReceived1); // message arrived?
239       waitOnUpdate(2000L, messageArrived2);
240       assertEquals("numReceived2 after publishing", 1, numReceived2); // message arrived?
241       waitOnUpdate(2000L, messageArrived3);
242       assertEquals("numReceived3 after publishing", 0, numReceived3); // message arrived?
243       
244       numReceived1 = numReceived2 = numReceived3 = 0;
245       messageArrived1.setLength(0);
246       messageArrived2.setLength(0);
247       messageArrived3.setLength(0);
248    }
249 
250    /**
251     * Little helper, waits until the variable 'messageArrive' is set
252     * or returns when the given timeout occurs.
253     * @param timeout in milliseconds
254     */
255    private void waitOnUpdate(final long timeout, StringBuffer messageArrived) {
256       long pollingInterval = 50L;  // check every 0.05 seconds
257       if (timeout < 50)  pollingInterval = timeout / 10L;
258       long sum = 0L;
259       while (messageArrived.length() < 1) {
260          try {
261             Thread.sleep(pollingInterval);
262          }
263          catch( InterruptedException i)
264          {}
265          sum += pollingInterval;
266          if (sum > timeout) {
267             log.warning("Timeout of " + timeout + " occurred");
268             //fail("Timeout of " + timeout + " occurred");
269             break;
270          }
271       }
272       messageArrived.setLength(0);
273    }
274 
275    /**
276     * <pre>
277     *   java -Dtrace=true -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubXPathMany
278     *   java -Djava.compiler= org.xmlBlaster.test.qos.TestSubXPathMany -logging FINE
279     * </pre>
280     */
281    public static void main(String args[]) {
282       TestSubXPathMany testSub = new TestSubXPathMany(new Global(args));
283       testSub.setUp();
284       testSub.testIt();
285       testSub.tearDown();
286    }
287 }


syntax highlighted by Code2HTML, v. 0.9.1