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