1 /*------------------------------------------------------------------------------
  2 Name:      TestSessionCb.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.authentication;
  7 
  8 import java.util.logging.Logger;
  9 import org.xmlBlaster.util.Global;
 10 import org.xmlBlaster.util.XmlBlasterException;
 11 import org.xmlBlaster.client.qos.ConnectQos;
 12 import org.xmlBlaster.client.I_XmlBlasterAccess;
 13 import org.xmlBlaster.client.I_Callback;
 14 import org.xmlBlaster.client.key.UpdateKey;
 15 import org.xmlBlaster.client.qos.UpdateQos;
 16 import org.xmlBlaster.client.qos.PublishReturnQos;
 17 import org.xmlBlaster.client.key.SubscribeKey;
 18 import org.xmlBlaster.client.qos.SubscribeQos;
 19 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 20 import org.xmlBlaster.client.key.PublishKey;
 21 import org.xmlBlaster.client.qos.PublishQos;
 22 import org.xmlBlaster.client.key.EraseKey;
 23 import org.xmlBlaster.client.qos.EraseQos;
 24 import org.xmlBlaster.util.def.Constants;
 25 import org.xmlBlaster.util.MsgUnit;
 26 
 27 import org.xmlBlaster.test.Util;
 28 import junit.framework.*;
 29 
 30 
 31 /**
 32  * This client does test callbacks for two sessions and dead letters. 
 33  * <p />
 34  * Test does not work with SOCKET protocol as here we use the same socket for
 35  * callback and we can't simulate a lost callback
 36  * <p />
 37  * Invoke examples:<br />
 38  * <pre>
 39  *    java junit.textui.TestRunner org.xmlBlaster.test.authentication.TestSessionCb
 40  *    java junit.swingui.TestRunner org.xmlBlaster.test.authentication.TestSessionCb
 41  * </pre>
 42  */
 43 public class TestSessionCb extends TestCase
 44 {
 45    private final Global glob;
 46    private static Logger log = Logger.getLogger(TestSessionCb.class.getName());
 47    private I_XmlBlasterAccess con1 = null;
 48    private I_XmlBlasterAccess con2 = null;
 49    private String assertInUpdate = null;
 50    private String oid = "TestSessionCb-msg";
 51    private int deadMessageCounter = 0;
 52 
 53    /**
 54    * Test does not work with SOCKET protocol as here we use the same socket for
 55    * callback and we can't simulate a lost callback
 56    */
 57    private boolean isSocket = false;
 58 
 59    /**
 60     * Constructs the TestSessionCb object.
 61     */
 62    public TestSessionCb(Global glob, String testName) {
 63       super(testName);
 64       this.glob = glob;
 65 
 66    }
 67 
 68    /**
 69     */
 70    protected void setUp() {
 71       String driverType = glob.getProperty().get("client.protocol", "dummy");
 72       if (driverType.equalsIgnoreCase("SOCKET"))
 73          isSocket = true;
 74 
 75       if (isSocket) {
 76          log.warning("callback test ignored for driverType=" + driverType + " as callback server uses same socket as invoce channel");
 77          return;
 78       }
 79    }
 80 
 81    /**
 82     */
 83    protected void tearDown() {
 84       if (isSocket) return;
 85       if (con2 != null) {
 86          try {
 87             EraseKey ek = new EraseKey(glob, oid);
 88             EraseQos eq = new EraseQos(glob);
 89             con2.erase(ek.toXml(), eq.toXml());
 90          } catch (XmlBlasterException e) {
 91             log.severe(e.toString());
 92          }
 93 
 94          con2.disconnect(null);
 95       }
 96    }
 97 
 98    /**
 99     */
100    public void testSessionCb() {
101       if (isSocket) return;
102       log.info("testSessionCb() ...");
103       final Global glob1 = glob.getClone(null);
104       final Global glob2 = glob.getClone(null);
105       String name1 = "NUMBER_ONE";
106       try {
107          log.info("Connecting ...");
108          con1 = glob1.getXmlBlasterAccess();
109          ConnectQos qos = new ConnectQos(glob1, name1, "secret");
110          assertInUpdate = null;
111          con1.connect(qos, new I_Callback() {  // Login to xmlBlaster, register for updates
112                public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
113                   log.info("****** Con1 update arrived" + updateKey.toXml() + updateQos.toXml());
114                   assertInUpdate = glob1.getId() + ": Did not expect message update in first handler";
115                   fail(assertInUpdate); // This is routed to server, not to junit
116                   return "";
117                }
118             });
119 
120          SubscribeKey sk = new SubscribeKey(glob1, oid);
121          SubscribeQos sq = new SubscribeQos(glob1);
122          SubscribeReturnQos sr1 = con1.subscribe(sk.toXml(), sq.toXml());
123 
124          try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait some time
125          assertTrue(assertInUpdate, assertInUpdate == null);
126          con1.getCbServer().shutdown();
127 
128          log.info("############ Con1 is down");
129 
130          assertInUpdate = null;
131          con2 = glob2.getXmlBlasterAccess();
132          qos = new ConnectQos(glob2);  // force a new session
133          con2.connect(qos, new I_Callback() {  // Login to xmlBlaster, register for updates
134                public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
135                   log.info("****** Con2 update arrived" + updateKey.toXml() + updateQos.toXml());
136                   assertInUpdate = glob2.getId() + "Reveiving asynchronous message '" + updateKey.getOid() + "' in second handler";
137                   log.info(assertInUpdate);
138                   return "";
139                }
140             });
141 
142          sk = new SubscribeKey(glob2, oid);
143          sq = new SubscribeQos(glob2);
144          SubscribeReturnQos sr2 = con2.subscribe(sk.toXml(), sq.toXml());
145 
146          sk = new SubscribeKey(glob2, Constants.OID_DEAD_LETTER);
147          sq = new SubscribeQos(glob2);
148          SubscribeReturnQos srDeadMessage = con2.subscribe(sk.toXml(), sq.toXml(), new I_Callback() {
149             public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
150                deadMessageCounter++;
151                log.info("****** Reveiving asynchronous message '" + updateKey.getOid() + "' in deadMessage handler, content=" + new String(content));
152                assertEquals("No dead letter received", Constants.OID_DEAD_LETTER, updateKey.getOid());
153                return "";
154             }
155          });  // subscribe with our specific update handler
156 
157          log.info("############ Con2 subscribed for msg and for DEAD letter, publishing now msg");
158 
159          PublishKey pk = new PublishKey(glob2, oid, "text/plain", "1.0");
160          PublishQos pq = new PublishQos(glob2);
161          MsgUnit msgUnit = new MsgUnit(pk.toXml(), "Hi".getBytes(), pq.toXml());
162          PublishReturnQos retQos = con2.publish(msgUnit);
163          log.info("Published message oid=" + oid);
164 
165          log.info("############ Con2 is waiting for msg and for DEAD letter ...");
166 
167          try { Thread.sleep(2000); } catch( InterruptedException i) {} // Wait some time
168          assertEquals("DeadMessage is missing", 1, deadMessageCounter);
169          assertTrue("Update is missing", assertInUpdate != null);
170 
171          try {
172             log.info("Check that session has dissapeared ...");
173             MsgUnit[] msgs = Util.adminGet(glob, "__cmd:?clientList");
174             assertEquals("Can't access __cmd:?clientList", 1, msgs.length);
175             log.info("Got userList=" + msgs[0].getContentStr() + " checking for " + name1);
176             assertEquals("Session of " + name1 + " was not destroyed by failing callback",
177                       -1, msgs[0].getContentStr().indexOf(name1));
178          }
179          catch (XmlBlasterException e) {
180             fail("Session was not destroyed: " + e.toString());
181          }
182       }
183       catch (XmlBlasterException e) {
184          fail("SessionCb test failed: " + e.toString());
185       }
186       log.info("Success in testSessionCb()");
187    }
188 
189    /**
190     * Method is used by TestRunner to load these tests
191     */
192    public static Test suite()
193    {
194        TestSuite suite= new TestSuite();
195        Global glob = new Global();
196        suite.addTest(new TestSessionCb(glob, "testSessionCb"));
197        suite.addTest(new TestSessionCb(glob, "testSessionCb")); // Run it twice
198        return suite;
199    }
200 
201    /**
202     * Invoke: 
203     * <pre>
204     *   java org.xmlBlaster.test.authentication.TestSessionCb
205     *   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.authentication.TestSessionCb
206     * <pre>
207     */
208    public static void main(String args[]) {
209       TestSessionCb testSub = new TestSessionCb(new Global(args), "testSessionCb");
210       testSub.setUp();
211       testSub.testSessionCb();
212       testSub.testSessionCb();
213       testSub.tearDown();
214    }
215 }


syntax highlighted by Code2HTML, v. 0.9.1