1 /*------------------------------------------------------------------------------
  2 Name:      TestReconnectSameClientOnly.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.qos;
  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.util.Global;
 15 import org.xmlBlaster.util.XmlBlasterException;
 16 import org.xmlBlaster.util.qos.address.CallbackAddress;
 17 import org.xmlBlaster.client.protocol.I_CallbackServer;
 18 import org.xmlBlaster.client.protocol.socket.SocketCallbackImpl;
 19 import org.xmlBlaster.client.protocol.xmlrpc.XmlRpcCallbackImpl;
 20 import org.xmlBlaster.client.qos.ConnectQos;
 21 import org.xmlBlaster.client.I_XmlBlasterAccess;
 22 import org.xmlBlaster.client.qos.UpdateQos;
 23 import org.xmlBlaster.client.key.UpdateKey;
 24 import org.xmlBlaster.client.I_Callback;
 25 import org.xmlBlaster.util.qos.SessionQos;
 26 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 27 import org.xmlBlaster.test.Util;
 28 import org.xmlBlaster.test.util.Client;
 29 
 30 
 31 /**
 32  * This client tests the
 33  * <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.configuration.html">client.configuration requirement</a>
 34  * and especially the <i>-session.reconnectSameClientOnly true</i> setting. 
 35  * <p />
 36  * We start our own xmlBlaster server in a thread.
 37  * This client may be invoked multiple time on the same xmlBlaster server,
 38  * as it cleans up everything after his tests are done.
 39  * <p>
 40  * Invoke examples:<br />
 41  * <pre>
 42  *    java junit.textui.TestRunner -noloading org.xmlBlaster.test.qos.TestReconnectSameClientOnly
 43  *    java junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestReconnectSameClientOnly
 44  * </pre>
 45  * @see org.xmlBlaster.util.qos.SessionQos
 46  */
 47 public class TestReconnectSameClientOnly extends TestCase implements I_Callback
 48 {
 49    private static String ME = "TestReconnectSameClientOnly";
 50    private Global glob;
 51    private static Logger log = Logger.getLogger(TestReconnectSameClientOnly.class.getName());
 52 
 53    private I_XmlBlasterAccess con = null;
 54    private EmbeddedXmlBlaster serverThread;
 55    private int serverPort = 9560;
 56    private boolean startEmbedded = true;
 57 
 58    /**
 59     * Constructs the TestReconnectSameClientOnly object.
 60     * <p />
 61     * @param testName   The name used in the test suite
 62     */
 63    public TestReconnectSameClientOnly(Global glob, String testName) {
 64       super(testName);
 65       this.glob = glob;
 66 
 67    }
 68 
 69    /**
 70     * Sets up the fixture.
 71     * <p />
 72     * We start an own xmlBlaster server in a separate thread,
 73     * it is configured to load our demo qos plugin.
 74     * <p />
 75     * Then we connect as a client
 76     */
 77    protected void setUp() {
 78       //Global embeddedGlobal = glob.getClone(null);  
 79       log.info("#################### setup-testReconnectSameClientOnly ...");
 80       this.startEmbedded = glob.getProperty().get("startEmbedded", this.startEmbedded);
 81       if (this.startEmbedded) {
 82          glob.init(Util.getOtherServerPorts(serverPort));
 83          serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 84          log.info("XmlBlaster is ready for testing");
 85       }
 86    }
 87 
 88    /**
 89     * @param The oid of the status message 
 90     * @param state Choose one of "2M" or "64k"
 91     */
 92    public void testReconnectSameClientOnly() {
 93       log.info("#################### testReconnectSameClientOnly ...");
 94 
 95       try {
 96          log.info("Connecting first ...");
 97          this.con = glob.getXmlBlasterAccess();
 98 
 99          ConnectQos qos = new ConnectQos(glob, "JOE/1", "secret");
100 
101          CallbackAddress callback = new CallbackAddress(glob);
102          callback.setPingInterval(1000);
103          qos.addCallbackAddress(callback);
104 
105          SessionQos sessionQos = qos.getSessionQos();
106          sessionQos.setMaxSessions(1);
107          sessionQos.setReconnectSameClientOnly(true);
108          this.con.connect(qos, this);
109       }
110       catch (Exception e) {
111          Thread.dumpStack();
112          fail(ME+": Can't connect to xmlBlaster: " + e.toString());
113       }
114 
115       try {
116          log.info("Connecting other ...");
117          Global glob2 = glob.getClone(null);
118          I_XmlBlasterAccess con2 = glob2.getXmlBlasterAccess();
119 
120          // Activate plugin for callback only:
121          ConnectQos qos = new ConnectQos(glob2, "JOE/1", "secret");
122          SessionQos sessionQos = qos.getSessionQos();
123          sessionQos.setMaxSessions(1);
124          sessionQos.setReconnectSameClientOnly(true);
125          con2.connect(qos, this);
126          fail(ME+": Reconnect to xmlBlaster should not be possible");
127       }
128       catch (XmlBlasterException e) {
129          log.info("SUCCESS, reconnect is not possible: " + e.getMessage());
130       }
131 
132       // boolean isSocket = Client
133       // boolean isRpc = (this.con.getCbServer() instanceof XmlRpcCallbackImpl);
134 
135       try {
136          Client.shutdownCb(con, Client.Shutdown.LEAVE_SERVER);
137       }
138       catch (XmlBlasterException e) {
139          fail("Can't setup test: " + e.getMessage());
140       }
141       try { Thread.sleep(2000); } catch( InterruptedException i) {} // Wait
142 
143       try {
144          log.info("Connecting other ...");
145          Global glob2 = glob.getClone(null);
146          I_XmlBlasterAccess con2 = glob2.getXmlBlasterAccess();
147 
148          // Activate plugin for callback only:
149          ConnectQos qos = new ConnectQos(glob2, "JOE/1", "secret");
150          SessionQos sessionQos = qos.getSessionQos();
151          sessionQos.setMaxSessions(1);
152          sessionQos.setReconnectSameClientOnly(true);
153          con2.connect(qos, this);
154          log.info("SUCCESS, reconnect is OK after first session died");
155       }
156       catch (XmlBlasterException e) {
157          fail(ME + ": Reconnect should now be possible: " + e.getMessage());
158       }
159 
160       log.info("Success in testReconnectSameClientOnly()");
161    }
162 
163    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
164       log.severe("TEST FAILED: UpdateKey.toString()=" + updateKey.toString() +
165                     "UpdateQos.toString()=" + updateQos.toString());
166       fail("Unexpected UpdateKey.toString()=" + updateKey.toString());
167       return "";
168    }
169 
170    /**
171     * Tears down the fixture.
172     * <p />
173     * cleaning up .... erase() the previous message OID and logout
174     */
175    protected void tearDown() {
176       log.info("#################### tearDown-testReconnectSameClientOnly ...");
177 
178       this.con.disconnect(null);
179       this.con = null;
180 
181       if (this.startEmbedded) {
182          try { Thread.sleep(500L); } catch( InterruptedException i) {} // Wait some time
183          EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
184          this.serverThread = null;
185       }
186 
187       // reset to default server port (necessary if other tests follow in the same JVM).
188       Util.resetPorts(glob);
189       this.glob = null;
190       this.con = null;
191       Global.instance().shutdown();
192    }
193 
194    /**
195     * Method is used by TestRunner to load these tests
196     */
197    public static Test suite() {
198        TestSuite suite= new TestSuite();
199        suite.addTest(new TestReconnectSameClientOnly(Global.instance(), "testReconnectSameClientOnly"));
200        return suite;
201    }
202 
203    /**
204     * Invoke: 
205     * <pre>
206     *  java org.xmlBlaster.test.qos.TestReconnectSameClientOnly -logging/org.xmlBlaster.client.qos FINE -logging/org.xmlBlaster.util.qos FINE -logging/org.xmlBlaster.engine FINEST 
207     *  java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.qos.TestReconnectSameClientOnly
208     * <pre>
209     */
210    public static void main(String args[]) {
211       Global glob = new Global();
212       if (glob.init(args) != 0) {
213          System.exit(0);
214       }
215       TestReconnectSameClientOnly testSub = new TestReconnectSameClientOnly(glob, "TestReconnectSameClientOnly");
216       testSub.setUp();
217       testSub.testReconnectSameClientOnly();
218       testSub.tearDown();
219    }
220 }


syntax highlighted by Code2HTML, v. 0.9.1