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 17984 2011-09-02 16:19:08Z ruff $
  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 //      for (Object key: map.keySet().toArray(new Object[map.size()])) {
 93 //         log.info("Got Update Key = " + key.toString());
 94 //      }
 95       int count = map.containsKey("__isInitialUpdate") ? 4 : 3;
 96       assertEquals("", count, map.size());
 97       assertEquals("", "oneValue", ((ClientProperty)map.get("oneKey")).getStringValue());
 98       assertEquals("", "twoValue", ((ClientProperty)map.get("twoKey")).getStringValue());
 99       assertEquals("", 55, ((ClientProperty)map.get("threeKey")).getIntValue());
100    }
101 
102 
103    public void testConnectQos()
104    {
105       if (log.isLoggable(Level.FINE)) log.fine("TestConnectQos");
106       try {
107          ConnectQos qos = new ConnectQos(this.glob);
108          qos.addClientProperty("oneKey", "oneValue");
109          qos.addClientProperty("twoKey", "twoValue");
110          qos.addClientProperty("threeKey", new Integer(55));
111          String literal = qos.toXml();
112          
113          ConnectQosSaxFactory factory = new ConnectQosSaxFactory(this.glob);
114          ConnectQosData data = factory.readObject(literal);
115          checkValues(data.getClientProperties());
116       }
117       catch (XmlBlasterException ex) {
118          assertTrue("Exeption occured : " + ex.getMessage(), false);
119       }
120    }
121 
122    public void testDisconnectQos()
123    {
124       if (log.isLoggable(Level.FINE)) log.fine("TestDisconnectQos");
125       DisconnectQos qos = new DisconnectQos(this.glob);
126       qos.addClientProperty("oneKey", "oneValue");
127       qos.addClientProperty("twoKey", "twoValue");
128       qos.addClientProperty("threeKey", new Integer(55));
129       String literal = qos.toXml();
130       
131       DisconnectQosSaxFactory factory = new DisconnectQosSaxFactory(this.glob);
132       try {
133          DisconnectQosData data = factory.readObject(literal);
134          checkValues(data.getClientProperties());
135       }
136       catch (XmlBlasterException ex) {
137          assertTrue("Exeption occured : " + ex.getMessage(), false);
138       }
139    }
140 
141 
142    public void testPublishQos()
143    {
144       if (log.isLoggable(Level.FINE)) log.fine("TestPublishQos");
145       PublishQos qos = new PublishQos(this.glob);
146       qos.addClientProperty("oneKey", "oneValue");
147       qos.addClientProperty("twoKey", "twoValue");
148       qos.addClientProperty("threeKey", new Integer(55));
149       String literal = qos.toXml();
150       
151       MsgQosSaxFactory factory = new MsgQosSaxFactory(this.glob);
152       try {
153          MsgQosData data = factory.readObject(literal);
154          checkValues(data.getClientProperties());
155       }
156       catch (XmlBlasterException ex) {
157          assertTrue("Exeption occured : " + ex.getMessage(), false);
158       }
159    }
160 
161 
162    public void testSubscribeQos()
163    {
164       if (log.isLoggable(Level.FINE)) log.fine("TestSubscribeQos");
165       SubscribeQos qos = new SubscribeQos(this.glob);
166       qos.addClientProperty("oneKey", "oneValue");
167       qos.addClientProperty("twoKey", "twoValue");
168       qos.addClientProperty("threeKey", new Integer(55));
169       String literal = qos.toXml();
170       
171       QueryQosSaxFactory factory = new QueryQosSaxFactory(this.glob);
172       try {
173          QueryQosData data = factory.readObject(literal);
174          checkValues(data.getClientProperties());
175       }
176       catch (XmlBlasterException ex) {
177          assertTrue("Exeption occured : " + ex.getMessage(), false);
178       }
179    }
180 
181 
182    public void testUnSubscribeQos()
183    {
184       if (log.isLoggable(Level.FINE)) log.fine("TestUnSubscribeQos");
185       UnSubscribeQos qos = new UnSubscribeQos(this.glob);
186       qos.addClientProperty("oneKey", "oneValue");
187       qos.addClientProperty("twoKey", "twoValue");
188       qos.addClientProperty("threeKey", new Integer(55));
189       String literal = qos.toXml();
190       
191       ConnectQosSaxFactory factory = new ConnectQosSaxFactory(this.glob);
192       try {
193          ConnectQosData data = factory.readObject(literal);
194          checkValues(data.getClientProperties());
195       }
196       catch (XmlBlasterException ex) {
197          assertTrue("Exeption occured : " + ex.getMessage(), false);
198       }
199    }
200 
201 
202    public void testGetQos()
203    {
204       if (log.isLoggable(Level.FINE)) log.fine("TestGetQos");
205       try {
206          ConnectQos qos = new ConnectQos(this.glob);
207          qos.addClientProperty("oneKey", "oneValue");
208          qos.addClientProperty("twoKey", "twoValue");
209          qos.addClientProperty("threeKey", new Integer(55));
210          String literal = qos.toXml();
211          
212          QueryQosSaxFactory factory = new QueryQosSaxFactory(this.glob);
213          QueryQosData data = factory.readObject(literal);
214          checkValues(data.getClientProperties());
215       }
216       catch (XmlBlasterException ex) {
217          assertTrue("Exeption occured : " + ex.getMessage(), false);
218       }
219    }
220 
221    public void testEraseQos()
222    {
223       if (log.isLoggable(Level.FINE)) log.fine("TestEraseQos");
224       EraseQos qos = new EraseQos(this.glob);
225       qos.addClientProperty("oneKey", "oneValue");
226       qos.addClientProperty("twoKey", "twoValue");
227       qos.addClientProperty("threeKey", new Integer(55));
228       String literal = qos.toXml();
229       
230       QueryQosSaxFactory factory = new QueryQosSaxFactory(this.glob);
231       try {
232          QueryQosData data = factory.readObject(literal);
233          checkValues(data.getClientProperties());
234       }
235       catch (XmlBlasterException ex) {
236          assertTrue("Exeption occured : " + ex.getMessage(), false);
237       }
238    }
239 
240    /**
241     * TEST: Construct a message and publish it.
242     * <p />
243     * The returned publishOid is checked
244     */
245    public void testUpdateQos()
246    {
247       if (log.isLoggable(Level.FINE)) log.fine("Testing the update qos ...");
248 
249       try {
250          senderConnection = glob.getXmlBlasterAccess(); // Find orb
251          String passwd = "secret";
252          ConnectQos connQos = new ConnectQos(glob, "clientProperty", passwd);
253          if (log.isLoggable(Level.FINE)) log.fine("the connect qos is: " + connQos.toXml());
254          senderConnection.connect(connQos, this); // Login to xmlBlaster
255 
256          // publish 
257          PublishKey key = new PublishKey(this.glob, "clientProp");
258          PublishQos qos = new PublishQos(this.glob);
259          qos.addClientProperty("oneKey", "oneValue");
260          qos.addClientProperty("twoKey", "twoValue");
261          qos.addClientProperty("threeKey", new Integer(55));
262          MsgUnit msg = new MsgUnit(key, "message".getBytes(), qos);
263          senderConnection.publish(msg);
264 
265          // subscribe
266          senderConnection.subscribe(new SubscribeKey(this.glob, "clientProp"), new SubscribeQos(this.glob));
267 
268          waitOnUpdate(10000);
269 
270          senderConnection.erase(new EraseKey(this.glob, "clientProperty"), new EraseQos(this.glob));
271          senderConnection.disconnect(new DisconnectQos(this.glob));
272          
273       }
274       catch (Exception e) {
275           log.severe("Login failed: " + e.toString());
276           e.printStackTrace();
277           assertTrue("Login failed: " + e.toString(), false);
278       }
279    }
280 
281 
282 
283    /**
284     * This is the callback method invoked from xmlBlaster
285     * delivering us a new asynchronous message. 
286     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
287     */
288    public String update(String cbSessionId_, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
289       log.info("Receiving update of message oid=" + updateKey.getOid() + "...");
290 
291       if (updateQos.isErased()) return "";
292 
293       assertEquals("Wrong sender", "clientProperty", updateQos.getSender().getLoginName());
294       assertEquals("Wrong oid of message returned", "clientProp", updateKey.getOid());
295 
296 
297       Map map = updateQos.getData().getClientProperties();
298 
299       this.checkValues(map);
300       this.messageArrived = true;
301       return "";
302    }
303 
304 
305    /**
306     * Little helper, waits until the variable 'messageArrive' is set
307     * to true, or returns when the given timeout occurs.
308     * @param timeout in milliseconds
309     */
310    private void waitOnUpdate(final long timeout)
311    {
312       long pollingInterval = 50L;  // check every 0.05 seconds
313       if (timeout < 50)  pollingInterval = timeout / 10L;
314       long sum = 0L;
315       while (!messageArrived) {
316          try {
317             Thread.sleep(pollingInterval);
318          }
319          catch( InterruptedException i)
320          {}
321          sum += pollingInterval;
322          if (sum > timeout) {
323             log.warning("Timeout of " + timeout + " occurred");
324             break;
325          }
326       }
327       assertTrue("The message never arrived", messageArrived);
328       messageArrived = false;
329    }
330 
331 
332    /**
333     * Method is used by TestRunner to load these tests
334     */
335    public static Test suite()
336    {
337        TestSuite suite= new TestSuite();
338        String loginName = "Tim";
339       suite.addTest(new TestClientProperty(new Global(), "testConnectQos"));
340       suite.addTest(new TestClientProperty(new Global(), "testDisconnectQos"));
341       suite.addTest(new TestClientProperty(new Global(), "testPublishQos"));
342       suite.addTest(new TestClientProperty(new Global(), "testSubscribeQos"));
343       suite.addTest(new TestClientProperty(new Global(), "testUnSubscribeQos"));
344       suite.addTest(new TestClientProperty(new Global(), "testGetQos"));
345       suite.addTest(new TestClientProperty(new Global(), "testEraseQos"));
346       suite.addTest(new TestClientProperty(new Global(), "testUpdateQos"));
347        return suite;
348    }
349 
350 
351    /**
352     * Invoke: java org.xmlBlaster.test.qos.TestClientProperty
353     * @deprecated Use the TestRunner from the testsuite to run it:<p />
354     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestClientProperty</pre>
355     */
356    public static void main(String args[])
357    {
358       Global glob = new Global();
359       if (glob.init(args) != 0) {
360          System.err.println(ME + ": Init failed");
361          System.exit(1);
362       }
363       TestClientProperty test = new TestClientProperty(glob, "testClientProperty");
364       test.setUp();
365       test.testConnectQos();
366       test.tearDown();
367 
368       test.setUp();
369       test.testDisconnectQos();
370       test.tearDown();
371 
372       test.setUp();
373       test.testPublishQos();
374       test.tearDown();
375 
376       test.setUp();
377       test.testSubscribeQos();
378       test.tearDown();
379 
380       test.setUp();
381       test.testUnSubscribeQos();
382       test.tearDown();
383 
384       test.setUp();
385       test.testGetQos();
386       test.tearDown();
387 
388       test.setUp();
389       test.testEraseQos();
390       test.tearDown();
391 
392       test.setUp();
393       test.testUpdateQos();
394       test.tearDown();
395    }
396 }


syntax highlighted by Code2HTML, v. 0.9.1