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


syntax highlighted by Code2HTML, v. 0.9.1