1+ /*
2+ * socket.io-java-client AbstractTestSocketIO.java
3+ *
4+ * Copyright (c) 2012, Enno Boland
5+ * PROJECT DESCRIPTION
6+ *
7+ * See LICENSE file for more information
8+ */
19package io .socket ;
210
311import static org .junit .Assert .*;
1624import org .junit .Test ;
1725import org .junit .runner .RunWith ;
1826
27+ // TODO: Auto-generated Javadoc
28+ /**
29+ * The Class AbstractTestSocketIO.
30+ */
1931@ RunWith (io .socket .RandomBlockJUnit4ClassRunner .class )
2032public abstract class AbstractTestSocketIO implements IOCallback {
33+
34+ /** The Constant to the node executable */
2135 private final static String NODE = "node" ;
36+
37+ /** The port of this test, randomly choosed */
2238 private int port = -1 ;
39+
40+ /** Timeout for the tests */
2341 private static final int TIMEOUT = 1000 ;
42+
43+ /** Received queues. */
2444 LinkedBlockingQueue <String > events ;
45+
46+ /** stdout of the node executable */
2547 LinkedBlockingQueue <String > outputs ;
48+
49+ /** Received arguments of events */
2650 LinkedBlockingQueue <Object > args ;
2751
52+ /** Thread for processing stdout */
2853 Thread stdoutThread ;
54+
55+ /** Thread for processing stderr */
2956 Thread stderrThread ;
57+
58+ /** The node process. */
3059 private Process node ;
60+
61+ /** The socket to test. */
3162 private SocketIO socket ;
63+
64+ /** The transport of this test */
3265 static protected String transport = null ;
3366
67+ /**
68+ * Tear down after class.
69+ *
70+ * @throws Exception
71+ * the exception
72+ */
3473 @ AfterClass
3574 public static void tearDownAfterClass () throws Exception {
3675 }
3776
77+ /**
78+ * Sets up the test. Starts the node testserver on a randomly choosed port,
79+ * starts backgroundthreads for processing stdin/stdout. Adds shutdown-hook
80+ * for clean kill of the node server.
81+ *
82+ * @throws Exception
83+ * the exception
84+ */
3885 @ Before
3986 public void setUp () throws Exception {
4087 assertNotNull ("Transport is set correctly" , transport );
@@ -101,6 +148,12 @@ public void run() {
101148 assertEquals ("OK" , takeLine ());
102149 }
103150
151+ /**
152+ * Tears down this test. Asures queues are empty.
153+ *
154+ * @throws Exception
155+ * the exception
156+ */
104157 @ After
105158 public void tearDown () throws Exception {
106159
@@ -119,13 +172,25 @@ public void tearDown() throws Exception {
119172 }
120173 }
121174
175+ /**
176+ * Sets up a {@link SocketIO} connection.
177+ *
178+ * @throws Exception
179+ * the exception
180+ */
122181 void doConnect () throws Exception {
123182 // Setting up socket connection
124183 socket = new SocketIO ("http://127.0.0.1:" + getPort (), this );
125184 assertEquals ("onConnect" , takeEvent ());
126185 assertEquals (transport , socket .getTransport ());
127186 }
128187
188+ /**
189+ * Closes a {@link SocketIO} connection.
190+ *
191+ * @throws Exception
192+ * the exception
193+ */
129194 void doClose () throws Exception {
130195 socket .disconnect ();
131196 assertEquals ("onDisconnect" , takeEvent ());
@@ -143,7 +208,13 @@ void doClose() throws Exception {
143208
144209 // BEGIN TESTS
145210
146- @ Test (timeout =TIMEOUT )
211+ /**
212+ * Tests sending of a message to the server. Asures result by stdout.
213+ *
214+ * @throws Exception
215+ * the exception
216+ */
217+ @ Test (timeout = TIMEOUT )
147218 public void send () throws Exception {
148219 doConnect ();
149220 String str = "TESTSTRING" ;
@@ -152,7 +223,13 @@ public void send() throws Exception {
152223 doClose ();
153224 }
154225
155- @ Test (timeout =TIMEOUT )
226+ /**
227+ * Emit and on.
228+ *
229+ * @throws Exception
230+ * the exception
231+ */
232+ @ Test (timeout = TIMEOUT )
156233 public void emitAndOn () throws Exception {
157234 doConnect ();
158235
@@ -169,7 +246,13 @@ public void emitAndOn() throws Exception {
169246 doClose ();
170247 }
171248
172- @ Test (timeout =TIMEOUT )
249+ /**
250+ * Emit and message.
251+ *
252+ * @throws Exception
253+ * the exception
254+ */
255+ @ Test (timeout = TIMEOUT )
173256 public void emitAndMessage () throws Exception {
174257 doConnect ();
175258 String str = "TESTSTRING" ;
@@ -187,9 +270,16 @@ public void emitAndMessage() throws Exception {
187270 doClose ();
188271 }
189272
190- @ Test (timeout =TIMEOUT )
273+ /**
274+ * Namespaces.
275+ *
276+ * @throws Exception
277+ * the exception
278+ */
279+ @ Test (timeout = TIMEOUT )
191280 public void namespaces () throws Exception {
192- SocketIO ns1 = new SocketIO ("http://127.0.0.1:" + getPort () + "/ns1" , this );
281+ SocketIO ns1 = new SocketIO ("http://127.0.0.1:" + getPort () + "/ns1" ,
282+ this );
193283 assertEquals ("onConnect" , takeEvent ());
194284
195285 // In some very rare cases, it is possible to receive data on an socket
@@ -202,13 +292,15 @@ public void namespaces() throws Exception {
202292 ns1 .disconnect ();
203293 assertEquals ("onDisconnect" , takeEvent ());
204294
205- SocketIO ns2 = new SocketIO ("http://127.0.0.1:" + getPort () + "/ns2" , this );
295+ SocketIO ns2 = new SocketIO ("http://127.0.0.1:" + getPort () + "/ns2" ,
296+ this );
206297 assertEquals ("onConnect" , takeEvent ());
207298
208299 assertEquals ("onMessage_string" , takeEvent ());
209300 assertEquals ("ns2" , takeArg ());
210301
211- SocketIO ns2_2 = new SocketIO ("http://127.0.0.1:" + getPort () + "/ns2" , this );
302+ SocketIO ns2_2 = new SocketIO ("http://127.0.0.1:" + getPort () + "/ns2" ,
303+ this );
212304 assertEquals ("onConnect" , takeEvent ());
213305
214306 assertEquals ("onMessage_string" , takeEvent ());
@@ -221,15 +313,27 @@ public void namespaces() throws Exception {
221313 doClose ();
222314 }
223315
224- @ Test (timeout =TIMEOUT )
316+ /**
317+ * Error.
318+ *
319+ * @throws Exception
320+ * the exception
321+ */
322+ @ Test (timeout = TIMEOUT )
225323 public void error () throws Exception {
226324 doConnect ();
227325 new SocketIO ("http://127.0.0.1:1024/" , this );
228326 assertEquals ("onError" , takeEvent ());
229327 doClose ();
230328 }
231329
232- @ Test (timeout =TIMEOUT )
330+ /**
331+ * Acknowledge.
332+ *
333+ * @throws Exception
334+ * the exception
335+ */
336+ @ Test (timeout = TIMEOUT )
233337 public void acknowledge () throws Exception {
234338 doConnect ();
235339 socket .emit ("echoAck" , new IOAcknowledge () {
@@ -246,6 +350,13 @@ public void ack(Object... args) {
246350
247351 // END TESTS
248352
353+ /**
354+ * Take event.
355+ *
356+ * @return the string
357+ * @throws InterruptedException
358+ * the interrupted exception
359+ */
249360 String takeEvent () throws InterruptedException {
250361 String event = events .poll (TIMEOUT , TimeUnit .SECONDS );
251362 if (event == null ) {
@@ -255,6 +366,13 @@ String takeEvent() throws InterruptedException {
255366 return event ;
256367 }
257368
369+ /**
370+ * Take line.
371+ *
372+ * @return the string
373+ * @throws InterruptedException
374+ * the interrupted exception
375+ */
258376 String takeLine () throws InterruptedException {
259377 String line = outputs .poll (TIMEOUT , TimeUnit .SECONDS );
260378 if (line == null ) {
@@ -264,6 +382,13 @@ String takeLine() throws InterruptedException {
264382 return line ;
265383 }
266384
385+ /**
386+ * Take arg.
387+ *
388+ * @return the object
389+ * @throws InterruptedException
390+ * the interrupted exception
391+ */
267392 Object takeArg () throws InterruptedException {
268393 Object arg = args .poll (TIMEOUT , TimeUnit .MILLISECONDS );
269394 if (arg == null ) {
@@ -273,42 +398,81 @@ Object takeArg() throws InterruptedException {
273398 return arg ;
274399 }
275400
401+ /*
402+ * (non-Javadoc)
403+ *
404+ * @see io.socket.IOCallback#onDisconnect()
405+ */
276406 @ Override
277407 public void onDisconnect () {
278408 events .add ("onDisconnect" );
279409 }
280410
411+ /*
412+ * (non-Javadoc)
413+ *
414+ * @see io.socket.IOCallback#onConnect()
415+ */
281416 @ Override
282417 public void onConnect () {
283418 events .add ("onConnect" );
284419 }
285420
421+ /*
422+ * (non-Javadoc)
423+ *
424+ * @see io.socket.IOCallback#onMessage(java.lang.String,
425+ * io.socket.IOAcknowledge)
426+ */
286427 @ Override
287428 public void onMessage (String data , IOAcknowledge ack ) {
288429 events .add ("onMessage_string" );
289430 this .args .add (data );
290431 }
291432
433+ /*
434+ * (non-Javadoc)
435+ *
436+ * @see io.socket.IOCallback#onMessage(org.json.JSONObject,
437+ * io.socket.IOAcknowledge)
438+ */
292439 @ Override
293440 public void onMessage (JSONObject json , IOAcknowledge ack ) {
294441 events .add ("onMessage_json" );
295442 this .args .add (json );
296443 }
297444
445+ /*
446+ * (non-Javadoc)
447+ *
448+ * @see io.socket.IOCallback#on(java.lang.String, io.socket.IOAcknowledge,
449+ * java.lang.Object[])
450+ */
298451 @ Override
299452 public void on (String event , IOAcknowledge ack , Object ... args ) {
300453 events .add ("on" );
301454 this .args .addAll (Arrays .asList (args ));
302455 }
303456
457+ /*
458+ * (non-Javadoc)
459+ *
460+ * @see io.socket.IOCallback#onError(io.socket.SocketIOException)
461+ */
304462 @ Override
305463 public void onError (SocketIOException socketIOException ) {
464+ socketIOException .printStackTrace ();
306465 events .add ("onError" );
307466 }
308467
468+ /**
469+ * Gets the port.
470+ *
471+ * @return the port
472+ */
309473 public int getPort () {
310- if (port == -1 )
311- port = 2048 + (int )(Math .random () * 10000 );
474+ if (port == -1 )
475+ port = 2048 + (int ) (Math .random () * 10000 );
312476 return port ;
313477 }
314478
0 commit comments