1 /*------------------------------------------------------------------------------
  2 Name:      TestPtDQueue.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing PtP (point to point) messages
  6 Version:   $Id: TestPtDQueue.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.I_XmlBlasterAccess;
 19 import org.xmlBlaster.util.MsgUnit;
 20 
 21 import junit.framework.*;
 22 
 23 
 24 /**
 25  * This client tests the PtP (or PtD = point to destination) style, William sends to Averell a message.
 26  * <p>
 27  * Note that the two clients (client logins) are simulated in this class.<br />
 28  * William is the 'sender' and Averell the 'receiver'<br />
 29  * Averell is not online when William sends the message, and will receive the message
 30  * from his queue in the xmlBlaster when he logs in.
 31  * <p>
 32  * A second test checks if there is an Exception thrown, if the receiver
 33  * is not logged in and the <forceQueuing> is not set.
 34  * Invoke examples:<br />
 35  * <pre>
 36  *    java junit.textui.TestRunner org.xmlBlaster.test.qos.TestPtDQueue
 37  *
 38  *    java junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestPtDQueue
 39  * </pre>
 40  */
 41 public class TestPtDQueue extends TestCase implements I_Callback
 42 {
 43    private final static String ME = "TestPtDQueue";
 44    private final Global glob;
 45    private static Logger log = Logger.getLogger(TestPtDQueue.class.getName());
 46 
 47    private final String senderName = "William";
 48    private String publishOid = "";
 49    private I_XmlBlasterAccess senderConnection = null;
 50    private String senderContent;
 51 
 52    private final String receiverName = "Averell";
 53    private I_XmlBlasterAccess receiverConnection = null;
 54 
 55    private String passwd = "secret";
 56 
 57    private int numReceived = 0;
 58    private boolean messageArrived = false;
 59 
 60 
 61    /**
 62     * Constructs the TestPtDQueue 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 TestPtDQueue(Global glob, String testName)
 68    {
 69       super(testName);
 70       this.glob = glob;
 71 
 72    }
 73 
 74 
 75    /**
 76     * Sets up the fixture.
 77     * <p />
 78     * Creates a CORBA connection and does a login.<br />
 79     * - One connection for the sender client<br />
 80     */
 81    protected void setUp()
 82    {
 83       try {
 84          senderConnection = glob.getClone(null).getXmlBlasterAccess();
 85          senderConnection.connect(new ConnectQos(senderConnection.getGlobal(), senderName, passwd), this);
 86          log.info("Successful login for " + senderName);
 87       }
 88       catch (XmlBlasterException e) {
 89           log.severe(e.toString());
 90           e.printStackTrace();
 91           assertTrue("login - XmlBlasterException: " + e.getMessage(), false);
 92       }
 93    }
 94 
 95 
 96    /**
 97     * Tears down the fixture.
 98     * <p />
 99     * cleaning up .... logout
100     */
101    protected void tearDown()
102    {
103       try { Thread.sleep(200L); } catch( InterruptedException i) {}   // Wait 200 milli seconds, until all updates are processed ...
104       receiverConnection.disconnect(null);
105       senderConnection.disconnect(null);
106    }
107 
108 
109    /**
110     * TEST: Sending a message to a not logged in client, which logs in later.
111     * <p />
112     * The sent message will be stored in a xmlBlaster queue for this client and than delivered
113     * only if the &lt;destination forceQueuing='true' is set.
114     */
115    public void testPtUnknownDestination()
116    {
117       {
118          log.info("[1] Testing point to a unknown destination with NO forceQueuing set ...");
119 
120          // Construct a message and send it to "Averell"
121          String xmlKey = "<key oid='' contentMime='text/plain'/>";
122          String qos = "<qos>" +
123                       "   <destination queryType='EXACT' forceQueuing='false'>" +
124                               receiverName +
125                       "   </destination>" +
126                       "</qos>";
127 
128          senderContent = "Hi " + receiverName + ", who are you? " + senderName;
129          try {
130             MsgUnit msgUnit = new MsgUnit(senderConnection.getGlobal(), xmlKey, senderContent.getBytes(), qos);
131             publishOid = senderConnection.publish(msgUnit).getKeyOid();
132             log.severe("Publishing to a not logged in client should throw an exception, forceQueuing is not set");
133             assertTrue("Publishing to a not logged in client should throw an exception, forceQueuing is not set", false);
134          } catch(XmlBlasterException e) {
135             log.info("Exception is correct, client is not logged in: " + e.getMessage());
136          }
137 
138          waitOnUpdate(1000L);
139          assertEquals("numReceived after sending to '" + receiverName + "'", 0, numReceived); // no message?
140          numReceived = 0;
141       }
142 
143       {
144          log.info("[2] Testing point to a unknown destination with forceQueuing set ...");
145 
146          // Construct a message and send it to "Martin Unknown"
147          String xmlKey = "<key oid='' contentMime='text/plain'>\n" +
148                          "</key>";
149 
150          String qos = "<qos>" +
151                       "   <destination queryType='EXACT' forceQueuing='true'>" +
152                               receiverName +
153                       "   </destination>" +
154                       "</qos>";
155 
156          senderContent = "Hi " + receiverName + ", who are you? " + senderName;
157          try {
158             MsgUnit msgUnit = new MsgUnit(senderConnection.getGlobal(), xmlKey, senderContent.getBytes(), qos);
159             publishOid = senderConnection.publish(msgUnit).getKeyOid();
160             log.info("Sending done, returned oid=" + publishOid);
161          } catch(XmlBlasterException e) {
162             log.severe("publish() XmlBlasterException: " + e.getMessage());
163             assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
164          }
165 
166          waitOnUpdate(1000L);
167          assertEquals("numReceived after sending to '" + receiverName + "'", 0, numReceived); // no message?
168          numReceived = 0;
169 
170          log.info("[3] Now the receiver '" + receiverName + "' logs in and should get the message '" + publishOid + "' from the xmlBlaster queue ...");
171 
172          // Now the receiver logs in and should get the message from the xmlBlaster queue ...
173          try {
174             receiverConnection = glob.getClone(null).getXmlBlasterAccess();
175             ConnectQos connectQos = new ConnectQos(receiverConnection.getGlobal(), receiverName, passwd);
176             receiverConnection.connect(connectQos, this); // Login to xmlBlaster
177          } catch (XmlBlasterException e) {
178              log.severe(e.toString());
179              e.printStackTrace();
180              assertTrue("login - XmlBlasterException: " + e.getMessage(), false);
181              return;
182          }
183 
184          waitOnUpdate(1000L);
185          assertEquals("numReceived after '" + receiverName + "' logged in", 1, numReceived); // message arrived?
186          numReceived = 0;
187       }
188    }
189 
190    /**
191     * This is the callback method invoked from xmlBlaster
192     * delivering us a new asynchronous message. 
193     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
194     */
195    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
196    {
197       log.info("Receiving update of a message '" + updateKey.getOid() + "' state=" + updateQos.getState() + " ...");
198 
199       numReceived += 1;
200 
201       assertEquals("Wrong sender", senderName, updateQos.getSender().getLoginName());
202       assertEquals("Wrong oid of message returned", publishOid, updateKey.getOid());
203       assertEquals("Message content is corrupted", new String(senderContent), new String(content));
204 
205       messageArrived = true;
206       return "";
207    }
208 
209    /**
210     * Little helper, waits until the variable 'messageArrive' is set
211     * to true, or returns when the given timeout occurs.
212     * @param timeout in milliseconds
213     */
214    private void waitOnUpdate(final long timeout)
215    {
216       long pollingInterval = 50L;  // check every 0.05 seconds
217       if (timeout < 50)  pollingInterval = timeout / 10L;
218       long sum = 0L;
219       while (!messageArrived) {
220          try {
221             Thread.sleep(pollingInterval);
222          }
223          catch( InterruptedException i)
224          {}
225          sum += pollingInterval;
226          if (sum > timeout) {
227             log.warning("Timeout of " + timeout + " occurred");
228             break;
229          }
230       }
231       messageArrived = false;
232    }
233 
234 
235    /**
236     * Method is used by TestRunner to load these tests
237     */
238    public static Test suite()
239    {
240        TestSuite suite= new TestSuite();
241        suite.addTest(new TestPtDQueue(new Global(), "testPtUnknownDestination"));
242        return suite;
243    }
244 
245 
246    /**
247     * Invoke: java org.xmlBlaster.test.qos.TestPtDQueue
248     * @deprecated Use the TestRunner from the testsuite to run it:<p />
249     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestPtDQueue</pre>
250     */
251    public static void main(String args[])
252    {
253       Global glob = new Global();
254       if (glob.init(args) != 0) {
255          System.err.println(ME + ": Init failed");
256          System.exit(1);
257       }
258       TestPtDQueue testSub = new TestPtDQueue(glob, "TestPtDQueue");
259       testSub.setUp();
260       testSub.testPtUnknownDestination();
261       testSub.tearDown();
262    }
263 }


syntax highlighted by Code2HTML, v. 0.9.1