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


syntax highlighted by Code2HTML, v. 0.9.1