1 /*------------------------------------------------------------------------------
  2 Name:      TestClientProperty.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo code for a client using xmlBlaster
  6 Version:   $Id: TestClientProperty.java 14813 2006-03-04 23:02:48Z laghi $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.test.qos;
  9 
 10 import java.util.Map;
 11 
 12 import java.util.logging.Logger;
 13 import java.util.logging.Level;
 14 import org.xmlBlaster.util.Global;
 15 import org.xmlBlaster.util.XmlBlasterException;
 16 import org.xmlBlaster.client.qos.ConnectQos;
 17 import org.xmlBlaster.client.I_XmlBlasterAccess;
 18 import org.xmlBlaster.client.I_Callback;
 19 import org.xmlBlaster.client.key.EraseKey;
 20 import org.xmlBlaster.client.key.PublishKey;
 21 import org.xmlBlaster.client.key.SubscribeKey;
 22 import org.xmlBlaster.client.key.UpdateKey;
 23 import org.xmlBlaster.client.qos.DisconnectQos;
 24 import org.xmlBlaster.client.qos.EraseQos;
 25 import org.xmlBlaster.client.qos.PublishQos;
 26 import org.xmlBlaster.client.qos.SubscribeQos;
 27 import org.xmlBlaster.client.qos.UnSubscribeQos;
 28 import org.xmlBlaster.client.qos.UpdateQos;
 29 import org.xmlBlaster.util.MsgUnit;
 30 import org.xmlBlaster.util.qos.ConnectQosData;
 31 import org.xmlBlaster.util.qos.ConnectQosSaxFactory;
 32 import org.xmlBlaster.util.qos.DisconnectQosData;
 33 import org.xmlBlaster.util.qos.DisconnectQosSaxFactory;
 34 import org.xmlBlaster.util.qos.MsgQosData;
 35 import org.xmlBlaster.util.qos.MsgQosSaxFactory;
 36 import org.xmlBlaster.util.qos.QueryQosData;
 37 import org.xmlBlaster.util.qos.QueryQosSaxFactory;
 38 import org.xmlBlaster.util.qos.ClientProperty;
 39 
 40 import junit.framework.*;
 41 
 42 
 43 /**
 44  *
 45  *  * Invoke examples:<br />
 46  * <pre>
 47  *    java junit.textui.TestRunner org.xmlBlaster.test.qos.TestClientProperty
 48  *    java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestClientProperty
 49  * </pre>
 50  */
 51 public class TestClientProperty extends TestCase implements I_Callback
 52 {
 53    private static String ME = "TestClientProperty";
 54    private final Global glob;
 55    private static Logger log = Logger.getLogger(TestClientProperty.class.getName());
 56 
 57    private boolean messageArrived = false;
 58 
 59    private I_XmlBlasterAccess senderConnection;
 60 
 61    /**
 62     * Constructs the TestClientProperty object.
 63     * <p />
 64     * @param testName  The name used in the test suite
 65     * @param loginName The name to login to the xmlBlaster
 66     */
 67    public TestClientProperty(Global glob, String name) {
 68       super(name);
 69       this.glob = glob;
 70 
 71    }
 72 
 73 
 74    /**
 75     * Sets up the fixture.
 76     * <p />
 77     * Connect to xmlBlaster and login
 78     */
 79    protected void setUp() {
 80    }
 81 
 82 
 83    /**
 84     * Tears down the fixture.
 85     * <p />
 86     * cleaning up .... erase() the previous message OID and logout
 87     */
 88    protected void tearDown() {
 89    }
 90 
 91    private void checkValues(Map map) {
 92       assertEquals("", 3, map.size());
 93       assertEquals("", "oneValue", ((ClientProperty)map.get("oneKey")).getStringValue());
 94       assertEquals("", "twoValue", ((ClientProperty)map.get("twoKey")).getStringValue());
 95       assertEquals("", 55, ((ClientProperty)map.get("threeKey")).getIntValue());
 96    }
 97 
 98 
 99    public void testConnectQos()
100    {
101       if (log.isLoggable(Level.FINE)) log.fine("TestConnectQos");
102       try {
103          ConnectQos qos = new ConnectQos(this.glob);
104          qos.addClientProperty("oneKey", "oneValue");
105          qos.addClientProperty("twoKey", "twoValue");
106          qos.addClientProperty("threeKey", new Integer(55));
107          String literal = qos.toXml();
108          
109          ConnectQosSaxFactory factory = new ConnectQosSaxFactory(this.glob);
110          ConnectQosData data = factory.readObject(literal);
111          checkValues(data.getClientProperties());
112       }
113       catch (XmlBlasterException ex) {
114          assertTrue("Exeption occured : " + ex.getMessage(), false);
115       }
116    }
117 
118    public void testDisconnectQos()
119    {
120       if (log.isLoggable(Level.FINE)) log.fine("TestDisconnectQos");
121       DisconnectQos qos = new DisconnectQos(this.glob);
122       qos.addClientProperty("oneKey", "oneValue");
123       qos.addClientProperty("twoKey", "twoValue");
124       qos.addClientProperty("threeKey", new Integer(55));
125       String literal = qos.toXml();
126       
127       DisconnectQosSaxFactory factory = new DisconnectQosSaxFactory(this.glob);
128       try {
129          DisconnectQosData data = factory.readObject(literal);
130          checkValues(data.getClientProperties());
131       }
132       catch (XmlBlasterException ex) {
133          assertTrue("Exeption occured : " + ex.getMessage(), false);
134       }
135    }
136 
137 
138    public void testPublishQos()
139    {
140       if (log.isLoggable(Level.FINE)) log.fine("TestPublishQos");
141       PublishQos qos = new PublishQos(this.glob);
142       qos.addClientProperty("oneKey", "oneValue");
143       qos.addClientProperty("twoKey", "twoValue");
144       qos.addClientProperty("threeKey", new Integer(55));
145       String literal = qos.toXml();
146       
147       MsgQosSaxFactory factory = new MsgQosSaxFactory(this.glob);
148       try {
149          MsgQosData data = factory.readObject(literal);
150          checkValues(data.getClientProperties());
151       }
152       catch (XmlBlasterException ex) {
153          assertTrue("Exeption occured : " + ex.getMessage(), false);
154       }
155    }
156 
157 
158    public void testSubscribeQos()
159    {
160       if (log.isLoggable(Level.FINE)) log.fine("TestSubscribeQos");
161       SubscribeQos qos = new SubscribeQos(this.glob);
162       qos.addClientProperty("oneKey", "oneValue");
163       qos.addClientProperty("twoKey", "twoValue");
164       qos.addClientProperty("threeKey", new Integer(55));
165       String literal = qos.toXml();
166       
167       QueryQosSaxFactory factory = new QueryQosSaxFactory(this.glob);
168       try {
169          QueryQosData data = factory.readObject(literal);
170          checkValues(data.getClientProperties());
171       }
172       catch (XmlBlasterException ex) {
173          assertTrue("Exeption occured : " + ex.getMessage(), false);
174       }
175    }
176 
177 
178    public void testUnSubscribeQos()
179    {
180       if (log.isLoggable(Level.FINE)) log.fine("TestUnSubscribeQos");
181       UnSubscribeQos qos = new UnSubscribeQos(this.glob);
182       qos.addClientProperty("oneKey", "oneValue");
183       qos.addClientProperty("twoKey", "twoValue");
184       qos.addClientProperty("threeKey", new Integer(55));
185       String literal = qos.toXml();
186       
187       ConnectQosSaxFactory factory = new ConnectQosSaxFactory(this.glob);
188       try {
189          ConnectQosData data = factory.readObject(literal);
190          checkValues(data.getClientProperties());
191       }
192       catch (XmlBlasterException ex) {
193          assertTrue("Exeption occured : " + ex.getMessage(), false);
194       }
195    }
196 
197 
198    public void testGetQos()
199    {
200       if (log.isLoggable(Level.FINE)) log.fine("TestGetQos");
201       try {
202          ConnectQos qos = new ConnectQos(this.glob);
203          qos.addClientProperty("oneKey", "oneValue");
204          qos.addClientProperty("twoKey", "twoValue");
205          qos.addClientProperty("threeKey", new Integer(55));
206          String literal = qos.toXml();
207          
208          QueryQosSaxFactory factory = new QueryQosSaxFactory(this.glob);
209          QueryQosData data = factory.readObject(literal);
210          checkValues(data.getClientProperties());
211       }
212       catch (XmlBlasterException ex) {
213          assertTrue("Exeption occured : " + ex.getMessage(), false);
214       }
215    }
216 
217    public void testEraseQos()
218    {
219       if (log.isLoggable(Level.FINE)) log.fine("TestEraseQos");
220       EraseQos qos = new EraseQos(this.glob);
221       qos.addClientProperty("oneKey", "oneValue");
222       qos.addClientProperty("twoKey", "twoValue");
223       qos.addClientProperty("threeKey", new Integer(55));
224       String literal = qos.toXml();
225       
226       QueryQosSaxFactory factory = new QueryQosSaxFactory(this.glob);
227       try {
228          QueryQosData data = factory.readObject(literal);
229          checkValues(data.getClientProperties());
230       }
231       catch (XmlBlasterException ex) {
232          assertTrue("Exeption occured : " + ex.getMessage(), false);
233       }
234    }
235 
236    /**
237     * TEST: Construct a message and publish it.
238     * <p />
239     * The returned publishOid is checked
240     */
241    public void testUpdateQos()
242    {
243       if (log.isLoggable(Level.FINE)) log.fine("Testing the update qos ...");
244 
245       try {
246          senderConnection = glob.getXmlBlasterAccess(); // Find orb
247          String passwd = "secret";
248          ConnectQos connQos = new ConnectQos(glob, "clientProperty", passwd);
249          if (log.isLoggable(Level.FINE)) log.fine("the connect qos is: " + connQos.toXml());
250          senderConnection.connect(connQos, this); // Login to xmlBlaster
251 
252          // publish 
253          PublishKey key = new PublishKey(this.glob, "clientProp");
254          PublishQos qos = new PublishQos(this.glob);
255          qos.addClientProperty("oneKey", "oneValue");
256          qos.addClientProperty("twoKey", "twoValue");
257          qos.addClientProperty("threeKey", new Integer(55));
258          MsgUnit msg = new MsgUnit(key, "message".getBytes(), qos);
259          senderConnection.publish(msg);
260 
261          // subscribe
262          senderConnection.subscribe(new SubscribeKey(this.glob, "clientProp"), new SubscribeQos(this.glob));
263 
264          waitOnUpdate(10000);
265 
266          senderConnection.erase(new EraseKey(this.glob, "clientProperty"), new EraseQos(this.glob));
267          senderConnection.disconnect(new DisconnectQos(this.glob));
268          
269       }
270       catch (Exception e) {
271           log.severe("Login failed: " + e.toString());
272           e.printStackTrace();
273           assertTrue("Login failed: " + e.toString(), false);
274       }
275    }
276 
277 
278 
279    /**
280     * This is the callback method invoked from xmlBlaster
281     * delivering us a new asynchronous message. 
282     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
283     */
284    public String update(String cbSessionId_, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
285       log.info("Receiving update of message oid=" + updateKey.getOid() + "...");
286 
287       if (updateQos.isErased()) return "";
288 
289       assertEquals("Wrong sender", "clientProperty", updateQos.getSender().getLoginName());
290       assertEquals("Wrong oid of message returned", "clientProp", updateKey.getOid());
291 
292 
293       Map map = updateQos.getData().getClientProperties();
294 
295       this.checkValues(map);
296       this.messageArrived = true;
297       return "";
298    }
299 
300 
301    /**
302     * Little helper, waits until the variable 'messageArrive' is set
303     * to true, or returns when the given timeout occurs.
304     * @param timeout in milliseconds
305     */
306    private void waitOnUpdate(final long timeout)
307    {
308       long pollingInterval = 50L;  // check every 0.05 seconds
309       if (timeout < 50)  pollingInterval = timeout / 10L;
310       long sum = 0L;
311       while (!messageArrived) {
312          try {
313             Thread.sleep(pollingInterval);
314          }
315          catch( InterruptedException i)
316          {}
317          sum += pollingInterval;
318          if (sum > timeout) {
319             log.warning("Timeout of " + timeout + " occurred");
320             break;
321          }
322       }
323       assertTrue("The message never arrived", messageArrived);
324       messageArrived = false;
325    }
326 
327 
328    /**
329     * Method is used by TestRunner to load these tests
330     */
331    public static Test suite()
332    {
333        TestSuite suite= new TestSuite();
334        String loginName = "Tim";
335       suite.addTest(new TestClientProperty(new Global(), "testConnectQos"));
336       suite.addTest(new TestClientProperty(new Global(), "testDisconnectQos"));
337       suite.addTest(new TestClientProperty(new Global(), "testPublishQos"));
338       suite.addTest(new TestClientProperty(new Global(), "testSubscribeQos"));
339       suite.addTest(new TestClientProperty(new Global(), "testUnSubscribeQos"));
340       suite.addTest(new TestClientProperty(new Global(), "testGetQos"));
341       suite.addTest(new TestClientProperty(new Global(), "testEraseQos"));
342       suite.addTest(new TestClientProperty(new Global(), "testUpdateQos"));
343        return suite;
344    }
345 
346 
347    /**
348     * Invoke: java org.xmlBlaster.test.qos.TestClientProperty
349     * @deprecated Use the TestRunner from the testsuite to run it:<p />
350     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestClientProperty</pre>
351     */
352    public static void main(String args[])
353    {
354       Global glob = new Global();
355       if (glob.init(args) != 0) {
356          System.err.println(ME + ": Init failed");
357          System.exit(1);
358       }
359       TestClientProperty test = new TestClientProperty(glob, "testClientProperty");
360       test.setUp();
361       test.testConnectQos();
362       test.tearDown();
363 
364       test.setUp();
365       test.testDisconnectQos();
366       test.tearDown();
367 
368       test.setUp();
369       test.testPublishQos();
370       test.tearDown();
371 
372       test.setUp();
373       test.testSubscribeQos();
374       test.tearDown();
375 
376       test.setUp();
377       test.testUnSubscribeQos();
378       test.tearDown();
379 
380       test.setUp();
381       test.testGetQos();
382       test.tearDown();
383 
384       test.setUp();
385       test.testEraseQos();
386       test.tearDown();
387 
388       test.setUp();
389       test.testUpdateQos();
390       test.tearDown();
391    }
392 }


syntax highlighted by Code2HTML, v. 0.9.1