1 /*------------------------------------------------------------------------------
  2 Name:      TestPtD.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing PtP (point to point) messages
  6 Version:   $Id: TestPtD.java 14846 2006-03-07 17:14:22Z ruff $
  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.util.StopWatch;
 14 
 15 import org.xmlBlaster.util.XmlBlasterException;
 16 import org.xmlBlaster.client.I_XmlBlasterAccess;
 17 import org.xmlBlaster.client.qos.ConnectQos;
 18 import org.xmlBlaster.client.I_Callback;
 19 import org.xmlBlaster.client.key.UpdateKey;
 20 import org.xmlBlaster.client.qos.UpdateQos;
 21 import org.xmlBlaster.util.MsgUnit;
 22 import org.xmlBlaster.util.property.Args;
 23 
 24 import junit.framework.*;
 25 
 26 
 27 /**
 28  * This client tests the PtP (or PtD = point to destination) style.
 29  * <p>
 30  * Note that the three clients (client logins) are simulated in this class.<br />
 31  * Tests performed:<br />
 32  * <ul>
 33  *    <li>Manuel is the 'sender' and Ulrike the 'receiver' of a love letter</li>
 34  *    <li>Manuel sends a message to two destinations</li>
 35  * </ul>
 36  * <p>
 37  * Invoke examples:<br />
 38  * <pre>
 39  *    java junit.textui.TestRunner org.xmlBlaster.test.qos.TestPtD
 40  *
 41  *    java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestPtD
 42  * </pre>
 43  */
 44 public class TestPtD extends TestCase implements I_Callback
 45 {
 46    private final static String ME = "TestPtD";
 47    private final Global glob;
 48    private static Logger log = Logger.getLogger(TestPtD.class.getName());
 49 
 50    private final String senderName = "Manuel";
 51    private String publishOid = "";
 52    private I_XmlBlasterAccess senderConnection = null;
 53    private String senderContent;
 54 
 55    private final String receiverName = "Ulrike";
 56    private I_XmlBlasterAccess receiverConnection = null;
 57 
 58    private final String receiver2Name = "KGB";
 59    private I_XmlBlasterAccess receiver2Connection = null;
 60 
 61    private int numReceived = 0;
 62 
 63 
 64    /**
 65     * Constructs the TestPtD object.
 66     * <p />
 67     * @param testName  The name used in the test suite
 68     * @param loginName The name to login to the xmlBlaster
 69     */
 70    public TestPtD(Global glob, String testName)
 71    {
 72       super(testName);
 73       this.glob = glob;
 74 
 75    }
 76 
 77 
 78    /**
 79     * Sets up the fixture.
 80     * <p />
 81     * Creates a CORBA connection and does a login.<br />
 82     * - One connection for the sender client<br />
 83     * - One connection for the receiver client
 84     * - One connection for the receiver2 client
 85     */
 86    protected void setUp()
 87    {
 88       try {
 89          String passwd = "secret";
 90 
 91          Global receiverGlob = glob.getClone(null);
 92          receiverConnection = receiverGlob.getXmlBlasterAccess();
 93          receiverConnection.connect(new ConnectQos(receiverGlob, receiverName, passwd), this);
 94 
 95          Global receiver2Glob = glob.getClone(null);
 96          receiver2Connection = receiver2Glob.getXmlBlasterAccess();
 97          receiver2Connection.connect(new ConnectQos(receiver2Glob, receiver2Name, passwd), this);
 98 
 99          Global receiver3Glob = glob.getClone(null);
100          senderConnection = receiver3Glob.getXmlBlasterAccess();
101          senderConnection.connect(new ConnectQos(receiver3Glob, senderName, passwd), this);
102       }
103       catch (Exception e) {
104           log.severe(e.toString());
105           e.printStackTrace();
106       }
107    }
108 
109 
110    /**
111     * Tears down the fixture.
112     * <p />
113     * cleaning up .... logout
114     */
115    protected void tearDown()
116    {
117       try { Thread.sleep(200L); } catch( InterruptedException i) {}   // Wait 200 milli seconds, until all updates are processed ...
118       receiverConnection.disconnect(null);
119       receiver2Connection.disconnect(null);
120       senderConnection.disconnect(null);
121    }
122 
123 
124    /**
125     * TEST: Send a message to one destination
126     * <p />
127     * The returned subscribeOid is checked
128     */
129    public void testPtOneDestination()
130    {
131       if (log.isLoggable(Level.FINE)) log.fine("Testing point to one destination ...");
132 
133       // Construct a love message and send it to Ulrike
134       String xmlKey = "<key oid='' contentMime='text/plain'>\n" +
135                       "</key>";
136 
137       String qos = "<qos>" +
138                    "   <destination queryType='EXACT'>" +
139                            receiverName +
140                    "   </destination>" +
141                    "</qos>";
142 
143       senderContent = "Hi " + receiverName + ", i love you, " + senderName;
144       try {
145          MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), qos);
146          publishOid = senderConnection.publish(msgUnit).getKeyOid();
147          log.info("Sending done, returned oid=" + publishOid);
148       } catch(XmlBlasterException e) {
149          log.severe("publish() XmlBlasterException: " + e.getMessage());
150          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
151       }
152 
153       waitOnUpdate(5000L, 1);
154       assertEquals("numReceived after sending", 1, numReceived); // message arrived?
155       numReceived = 0;
156    }
157 
158 
159    /**
160     * TEST: Send a message to two destinations
161     * <p />
162     */
163    public void testPtManyDestinations()
164    {
165       if (log.isLoggable(Level.FINE)) log.fine("Testing point to many destinations ...");
166 
167       // Construct a love message and send it to Ulrike
168       String xmlKey = "<key oid='' contentMime='text/plain'>\n" +
169                       "</key>";
170 
171       String qos = "<qos>" +
172                    "   <destination queryType='EXACT'>" +
173                            receiverName +
174                    "   </destination>" +
175                    "   <destination queryType='EXACT'>" +
176                            receiver2Name +
177                    "   </destination>" +
178                    "</qos>";
179 
180       senderContent = "Hi " + receiver2Name + ", i know you are listening, " + senderName;
181       try {
182          MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), qos);
183          publishOid = senderConnection.publish(msgUnit).getKeyOid();
184          log.info("Sending done, returned oid=" + publishOid);
185       } catch(XmlBlasterException e) {
186          log.severe("publish() XmlBlasterException: " + e.getMessage());
187          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
188       }
189 
190       waitOnUpdate(5000L, 2);
191       assertEquals("numReceived after sending", 2, numReceived); // message arrived at receiver and receiver2 ?
192       numReceived = 0;
193    }
194 
195    /**
196     * This is the callback method invoked from xmlBlaster
197     * delivering us a new asynchronous message. 
198     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
199     */
200    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
201    {
202       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
203 
204       numReceived += 1;
205 
206       // Wait that publish() returns and set 'publishOid' properly
207       try { Thread.sleep(200); } catch( InterruptedException i) {}
208 
209       assertEquals("Wrong sender", senderName, updateQos.getSender().getLoginName());
210       assertEquals("Wrong oid of message returned", publishOid, updateKey.getOid());
211       assertEquals("Message content is corrupted", new String(senderContent), new String(content));
212       return "";
213    }
214 
215 
216    /**
217     * Little helper, waits until the wanted number of messages are arrived
218     * or returns when the given timeout occurs.
219     * <p />
220     * @param timeout in milliseconds
221     * @param numWait how many messages to wait
222     */
223    private void waitOnUpdate(final long timeout, final int numWait)
224    {
225       long pollingInterval = 50L;  // check every 0.05 seconds
226       if (timeout < 50)  pollingInterval = timeout / 10L;
227       long sum = 0L;
228       while (numReceived < numWait) {
229          try {
230             Thread.sleep(pollingInterval);
231          }
232          catch( InterruptedException i)
233          {}
234          sum += pollingInterval;
235          if (sum > timeout) {
236             log.warning("Timeout of " + timeout + " occurred");
237             break;
238          }
239       }
240    }
241 
242 
243    /**
244     * Method is used by TestRunner to load these tests
245     */
246    public static Test suite()
247    {
248        TestSuite suite= new TestSuite();
249        Global glob = new Global();
250        suite.addTest(new TestPtD(glob, "testPtOneDestination"));
251        suite.addTest(new TestPtD(glob, "testPtManyDestinations"));
252        return suite;
253    }
254 
255 
256    /**
257     * Invoke: java org.xmlBlaster.test.qos.TestPtD
258     * @deprecated Use the TestRunner from the testsuite to run it:<p />
259     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestPtD</pre>
260     */
261    public static void main(String args[])
262    {
263       Global glob = new Global();
264       if (glob.init(args) != 0) {
265          System.err.println(ME + ": Init failed");
266          System.exit(1);
267       }
268       TestPtD testSub = new TestPtD(glob, "TestPtD");
269       testSub.setUp();
270       testSub.testPtOneDestination();
271       testSub.tearDown();
272    }
273 }


syntax highlighted by Code2HTML, v. 0.9.1