1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7
8 /************************************************************
9 * Copyright *
10 * Portions of this software are Copyright (c) 1993 - 2002, *
11 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
12 * - http://www.nevrona.com/Indy/ *
13 ************************************************************/
14 package org.indy.io;
15
16 import java.io.IOException;
17 import java.io.PrintWriter;
18
19 import java.net.ServerSocket;
20 import java.net.Socket;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Random;
25
26 import junit.framework.TestCase;
27
28
29 /***
30 * DOCUMENT ME!
31 *
32 * @version $Revision$
33 * @author $author$
34 */
35 public class TestIOHandlerSocket extends TestCase {
36 private Map connectionData = new HashMap();
37
38 /*** DOCUMENT ME! */
39 public ServerExecute dumbServer = new ServerExecute() {
40 public void execute(Socket s) throws IOException {
41 s.getInputStream().read();
42 }
43 };
44
45 private Runnable serverRunner = new Runnable() {
46 ServerSocket ss;
47 Runnable serverExecute;
48
49 public void run() {
50 if (ss != null) {
51 try {
52 ss.accept();
53 }
54 catch (IOException ex) {
55 ex.printStackTrace();
56 }
57 }
58 }
59 };
60
61 {
62 connectionData.put("host", "127.0.0.1");
63 connectionData.put("port", "6666");
64 }
65
66 /***
67 * Creates a new TestIOHandlerSocket object.
68 *
69 * @param s DOCUMENT ME!
70 */
71 public TestIOHandlerSocket(String s) {
72 super(s);
73 }
74
75 /***
76 * DOCUMENT ME!
77 */
78 protected void setUp() {
79 }
80
81 /***
82 * DOCUMENT ME!
83 */
84 protected void tearDown() {
85 }
86
87 /***
88 * DOCUMENT ME!
89 */
90 public void testClearBuffer() {
91 final String testData = "This is test data";
92 ServerFixture server = new ServerFixture(6666,
93 new ServerExecute() {
94 public void execute(Socket s) throws IOException {
95 PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
96 pw.println(testData);
97 }
98 });
99 server.start();
100
101 IOHandlerSocket ioHandler = new IOHandlerSocket();
102
103 try {
104 ioHandler.connectClient(connectionData, 0);
105
106 while (ioHandler.available() < testData.length())
107 ;
108
109 ioHandler.read(); //this will move data from socket->buffer
110 ioHandler.clearBuffer();
111 assertEquals("No data after clearBuffer()", 0, ioHandler.available());
112 }
113 catch (IndyIOException ex) {
114 fail(ex.getMessage());
115 }
116 }
117
118 /***
119 * DOCUMENT ME!
120 */
121 public void testClose() {
122 ServerFixture server = new ServerFixture(6666, dumbServer);
123 server.start();
124
125 IOHandlerSocket ioHandler = new IOHandlerSocket();
126
127 try {
128 ioHandler.connectClient(connectionData, 0);
129 ioHandler.close();
130 }
131 catch (IndyIOException ex) {
132 fail(ex.getMessage());
133 }
134
135 assertEquals("Test isConnected() false after close()", false,
136 ioHandler.isConnected());
137
138 try {
139 ioHandler.read();
140 }
141 catch (NotConnectedException e) {
142 return;
143 }
144 catch (IndyIOException ex) {
145 fail(ex.getMessage());
146 }
147
148 fail("Failed to throw NotConnectedException calling read() after close()");
149 }
150
151 /***
152 * DOCUMENT ME!
153 */
154 public void testCurrentReadBuffer() {
155 final String testData = "This is test data";
156 ServerFixture server = new ServerFixture(6666,
157 new ServerExecute() {
158 public void execute(Socket s) throws IOException {
159 PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
160 pw.print(testData);
161 pw.flush();
162 }
163 });
164 server.start();
165
166 IOHandlerSocket ioHandler = new IOHandlerSocket();
167
168 try {
169 ioHandler.connectClient(connectionData, 0);
170
171 while (ioHandler.available() < testData.length())
172 ;
173
174 String s = new String(ioHandler.currentReadBuffer());
175 assertEquals("Read buffer matches sent data", testData, s);
176 assertEquals("No data after currentReadBuffer()", 0,
177 ioHandler.available());
178 }
179 catch (IndyIOException ex) {
180 fail(ex.getMessage());
181 }
182 finally {
183 ioHandler.close();
184 }
185 }
186
187 /***
188 * DOCUMENT ME!
189 */
190 public void testGetBoundIP() {
191 ServerFixture server = new ServerFixture(6666, dumbServer);
192 server.start();
193
194 IOHandlerSocket ioHandler = new IOHandlerSocket();
195
196 try {
197 connectionData.put("boundip", "127.0.0.1");
198 ioHandler.connectClient(connectionData, 0);
199
200 String stringRet = ioHandler.getBoundIP();
201 assertEquals("Socket bound to 127.0.0.1", "127.0.0.1", stringRet);
202 }
203 catch (Exception e) {
204 fail(e.getMessage());
205 }
206 finally {
207 ioHandler.close();
208 connectionData.remove("boundip");
209 }
210 }
211
212 /***
213 * DOCUMENT ME!
214 */
215 public void testGetBoundPort() {
216 ServerFixture server = new ServerFixture(6666, dumbServer);
217 server.start();
218
219 IOHandlerSocket ioHandler = new IOHandlerSocket();
220
221 try {
222 Random r = new Random();
223 int port = r.nextInt(5000 - 1024) + 1024;
224 connectionData.put("boundport", String.valueOf(port));
225 ioHandler.connectClient(connectionData, 0);
226
227 int retPort = ioHandler.getBoundPort();
228 assertEquals("Socket bound to 127.0.0.1", port, retPort);
229 }
230 catch (Exception e) {
231 fail(e.getMessage());
232 }
233 finally {
234 ioHandler.close();
235 connectionData.remove("boundport");
236 }
237 }
238
239 /***
240 * DOCUMENT ME!
241 */
242 public void testNagle() {
243 ServerFixture server = new ServerFixture(6666, dumbServer);
244 server.start();
245
246 IOHandlerSocket ioHandler = new IOHandlerSocket();
247
248 try {
249 ioHandler.setUseNagle(true);
250 assertEquals("IO Handler Nagle value set true when not connected", true,
251 ioHandler.getUseNagle());
252 ioHandler.setUseNagle(false);
253 assertEquals("IO Handler Nagle value set false when not connected", false,
254 ioHandler.getUseNagle());
255
256
257 //Now connected
258 ioHandler.connectClient(connectionData, 0);
259 ioHandler.setUseNagle(true);
260 assertEquals("IO Handler Nagle value set true when connected", true,
261 ioHandler.getUseNagle());
262 ioHandler.setUseNagle(false);
263 assertEquals("IO Handler Nagle value set false when connected", false,
264 ioHandler.getUseNagle());
265 }
266 catch (IndyIOException ex) {
267 fail(ex.getMessage());
268 }
269 finally {
270 ioHandler.close();
271 }
272 }
273
274 /***
275 * DOCUMENT ME!
276 */
277 public void testIsConnected() {
278 ServerFixture server = new ServerFixture(6666, dumbServer);
279 server.start();
280
281 IOHandlerSocket ioHandler = new IOHandlerSocket();
282
283 assertEquals("isConnected() false before connection", false,
284 ioHandler.isConnected());
285
286 try {
287 ioHandler.connectClient(connectionData, 0);
288 assertEquals("isConnected() true after connection", true,
289 ioHandler.isConnected());
290 }
291 catch (IndyIOException ex) {
292 fail(ex.getMessage());
293 }
294 finally {
295 ioHandler.close();
296 }
297
298 assertEquals("isConnected() false after disconnection", false,
299 ioHandler.isConnected());
300 server = null;
301 }
302
303 /***
304 * DOCUMENT ME!
305 */
306 public void testRead() {
307 final String testData = "This is Test Data";
308 ServerFixture server = new ServerFixture(6666,
309 new ServerExecute() {
310 public void execute(Socket s) throws IOException {
311 PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
312 pw.print(testData);
313 pw.flush();
314 s.getInputStream().read();
315 s.close();
316 }
317 });
318 server.start();
319
320 IOHandlerSocket ioHandler = new IOHandlerSocket();
321
322 try {
323 //should throw NotConnectionException before connecting
324 boolean notConnectedThrown = false;
325
326 try {
327 ioHandler.read();
328 }
329 catch (NotConnectedException n) {
330 notConnectedThrown = true;
331 }
332
333 assertEquals("read() before connecting throws NotConnectedException",
334 true, notConnectedThrown);
335
336 ioHandler.connectClient(connectionData, 0);
337
338 //try reading, and assembling string from socket
339 byte[] buf = new byte[testData.length()];
340
341 for (int i = 0; i < buf.length; i++) {
342 buf[i] = (byte) ioHandler.read();
343 }
344
345 assertEquals("read() test data correctly", testData, new String(buf));
346
347
348 //write some data, which will cause disconnection,
349 //and try reading - should get PeerDisconnectedException
350 ioHandler.write(0);
351 ioHandler.flush();
352
353 boolean peerDisconnectThrown = false;
354
355 try {
356 ioHandler.read();
357 }
358 catch (PeerDisconnectedException ex) {
359 peerDisconnectThrown = true;
360 }
361
362 assertEquals(
363 "read() after disconnection causes PeerDisconnectedException", true,
364 peerDisconnectThrown);
365 }
366 catch (IndyIOException e) {
367 fail(e.getMessage());
368 }
369 }
370
371 /***
372 * DOCUMENT ME!
373 */
374 public void testReadBuf() {
375 final String testData = "This is Test Data";
376 ServerFixture server = new ServerFixture(6666,
377 new ServerExecute() {
378 public void execute(Socket s) throws IOException {
379 PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
380 pw.print(testData);
381 pw.flush();
382 s.getInputStream().read();
383 s.close();
384 }
385 });
386 server.start();
387
388 IOHandlerSocket ioHandler = new IOHandlerSocket();
389
390 try {
391 byte[] buf = new byte[testData.length()];
392
393 //should throw NotConnectionException before connecting
394 boolean notConnectedThrown = false;
395
396 try {
397 ioHandler.read(buf);
398 }
399 catch (NotConnectedException n) {
400 notConnectedThrown = true;
401 }
402
403 assertEquals(
404 "read(byte[]) before connecting throws NotConnectedException", true,
405 notConnectedThrown);
406
407 ioHandler.connectClient(connectionData, 0);
408
409
410 //try reading, and assembling string from socket
411 ioHandler.read(buf);
412 assertEquals("read(byte[]) test data correctly", testData,
413 new String(buf));
414
415
416 //write some data, which will cause disconnection,
417 //and try reading - should get PeerDisconnectedException
418 ioHandler.write(0);
419 ioHandler.flush();
420
421 boolean peerDisconnectThrown = false;
422
423 try {
424 ioHandler.read(buf);
425 }
426 catch (PeerDisconnectedException ex) {
427 peerDisconnectThrown = true;
428 }
429
430 assertEquals(
431 "read(byte[]) after disconnection causes PeerDisconnectedException",
432 true, peerDisconnectThrown);
433 }
434 catch (IndyIOException e) {
435 fail(e.getMessage());
436 }
437 }
438
439 /***
440 * DOCUMENT ME!
441 */
442 public void testReadLine() {
443 final String testData = "This is Test Data";
444 ServerFixture server = new ServerFixture(6666,
445 new ServerExecute() {
446 public void execute(Socket s) throws IOException {
447 PrintWriter pw = new PrintWriter(s.getOutputStream());
448
449 pw.print(testData + "\n");
450 pw.flush();
451 s.getInputStream().read(); //-->Any input triggers next block
452
453 pw.print(testData + "\r");
454 pw.flush();
455 s.getInputStream().read(); //-->Any input triggers next block
456
457 pw.print(testData + "\r\n");
458 pw.flush();
459 s.getInputStream().read(); //-->Any input triggers next block
460
461 pw.print(testData + "\n\r");
462 pw.flush();
463 s.getInputStream().read(); //-->Any input triggers next block
464
465 s.close();
466 }
467 });
468
469 server.start();
470
471 IOHandlerSocket ioHandler = new IOHandlerSocket();
472
473 try {
474 String line;
475 boolean notConnectedThrown = false;
476
477 try {
478 ioHandler.readLine();
479 }
480 catch (NotConnectedException n) {
481 notConnectedThrown = true;
482 }
483
484 assertEquals("readLine() before connecting throws NotConnectedException",
485 true, notConnectedThrown);
486
487 ioHandler.connectClient(connectionData, 0);
488
489
490 //try reading, and assembling string from socket
491 line = ioHandler.readLine();
492 assertEquals("readLine() test data correctly with single LF", testData,
493 line);
494
495 ioHandler.write(0);
496 ioHandler.flush();
497
498
499 //try reading, and assembling string from socket
500 line = ioHandler.readLine();
501 assertEquals("readLine() test data correctly with single CR", testData,
502 line);
503
504 ioHandler.write(0);
505 ioHandler.flush();
506
507
508 //try reading, and assembling string from socket
509 line = ioHandler.readLine();
510 assertEquals("readLine() test data correctly with CRLF", testData, line);
511
512 ioHandler.write(0);
513 ioHandler.flush();
514
515
516 //next is LFCR, which should be intreprted as two lines, i.e. there should be CR left over in the buffer
517 line = ioHandler.readLine();
518 assertEquals("readLine() test data correctly with LFCR", testData, line);
519
520 int leftOver = 0;
521 leftOver = ioHandler.read(1000); //should be buffered, so should be quick :)
522
523 assertEquals("Leftover CR in buffer", 13, leftOver);
524
525 ioHandler.write(0);
526 ioHandler.flush();
527
528 boolean peerDisconnectThrown = false;
529
530 try {
531 ioHandler.readLine();
532 }
533 catch (PeerDisconnectedException ex) {
534 peerDisconnectThrown = true;
535 }
536
537 assertEquals(
538 "read(byte[]) after disconnection causes PeerDisconnectedException",
539 true, peerDisconnectThrown);
540 }
541 catch (IndyIOException e) {
542 fail(e.getMessage());
543 }
544 }
545
546 private interface ServerExecute {
547 void execute(Socket s) throws IOException;
548 }
549
550 private class ServerFixture extends Thread {
551 ServerExecute executor;
552 int port;
553 private volatile boolean stopped = false;
554
555 public ServerFixture(int port, ServerExecute e) {
556 executor = e;
557 this.port = port;
558 }
559
560 public void stopServer() {
561 stopped = true;
562 }
563
564 public void run() {
565 ServerSocket ss = null;
566
567 try {
568 ss = new ServerSocket(port);
569
570 Socket s = ss.accept();
571 executor.execute(s);
572 }
573 catch (IOException ex) {
574 ex.printStackTrace();
575 }
576 finally {
577 try {
578 ss.close();
579 }
580 catch (IOException ex) {
581 }
582 }
583 }
584 }
585 }
This page was automatically generated by Maven