View Javadoc
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 package org.indy; 8 9 import java.io.BufferedReader; 10 import java.io.DataOutputStream; 11 import java.io.File; 12 import java.io.IOException; 13 import java.io.InputStream; 14 import java.io.InputStreamReader; 15 import java.io.PrintWriter; 16 17 import java.net.ServerSocket; 18 import java.net.Socket; 19 20 import java.util.HashMap; 21 import java.util.Map; 22 23 import junit.framework.TestCase; 24 25 import org.indy.io.IOHandler; 26 import org.indy.io.IOHandlerSocket; 27 import org.indy.io.IndyIOException; 28 import org.indy.io.ReadTimedOutException; 29 30 import org.indy.util.StringList; 31 32 33 /*** 34 * DOCUMENT ME! 35 * 36 * @version $Revision$ 37 * @author $author$ 38 */ 39 public class TestConnection extends TestCase { 40 private Map connectionData = new HashMap(); 41 42 /*** DOCUMENT ME! */ 43 public ServerExecute dumbServer = new ServerExecute() { 44 public void execute(Socket s) throws IOException { 45 s.getInputStream().read(); 46 } 47 }; 48 49 { 50 connectionData.put("host", "127.0.0.1"); 51 connectionData.put("port", "6666"); 52 } 53 54 /*** 55 * Creates a new TestConnection object. 56 * 57 * @param s DOCUMENT ME! 58 */ 59 public TestConnection(String s) { 60 super(s); 61 } 62 63 /*** 64 * DOCUMENT ME! 65 */ 66 protected void setUp() { 67 } 68 69 /*** 70 * DOCUMENT ME! 71 */ 72 protected void tearDown() { 73 } 74 75 /*** 76 * DOCUMENT ME! 77 */ 78 public void testReadLineWait() { 79 ServerFixture server = new ServerFixture(6666, dumbServer); 80 server.start(); 81 82 IOHandlerSocket ioHandler = new IOHandlerSocket(); 83 84 Connection connection = new Connection(ioHandler); 85 86 try { 87 ioHandler.connectClient(connectionData, 0); 88 connection.setReadTimeOut(10); 89 90 long start = System.currentTimeMillis(); 91 92 try { 93 connection.readLineWait(20); 94 } 95 catch (ReadTimedOutException e) { 96 } 97 98 long finish = System.currentTimeMillis(); 99 100 assertTrue("At least 20 timeout cycles should have elapsed", 101 (finish - start) >= 200); 102 } 103 catch (IndyIOException ex) { 104 fail(ex.getMessage()); 105 } 106 finally { 107 ioHandler.close(); 108 } 109 } 110 111 /*** 112 * DOCUMENT ME! 113 */ 114 public void testCapture() { 115 final String line1 = "This is Line 1"; 116 final String line2 = "This is Line 2"; 117 final String line3 = "This is Line 3"; 118 119 ServerFixture server = new ServerFixture(6666, 120 new ServerExecute() { 121 public void execute(Socket s) throws IOException { 122 PrintWriter pw = new PrintWriter(s.getOutputStream(), true); 123 pw.println(line1); 124 pw.println(line2); 125 pw.println(line3); 126 pw.println(":"); 127 s.getInputStream().read(); 128 } 129 }); 130 server.start(); 131 132 IOHandlerSocket ioHandler = new IOHandlerSocket(); 133 Connection connection = new Connection(ioHandler); 134 StringList strings = new StringList(); 135 136 try { 137 ioHandler.connectClient(connectionData, 0); 138 139 int intRet = connection.capture(strings, ":", false); 140 assertEquals("String list has 3 entries", 3, strings.size()); 141 assertEquals("1st OK", line1, strings.get(0)); 142 assertEquals("2nd OK", line2, strings.get(1)); 143 assertEquals("3rd OK", line3, strings.get(2)); 144 } 145 catch (Exception e) { 146 fail("Exception thrown: " + e); 147 } 148 finally { 149 ioHandler.close(); 150 } 151 } 152 153 /*** 154 * DOCUMENT ME! 155 */ 156 public void testGetResponse() { 157 final int response = 123; 158 159 ServerFixture server = new ServerFixture(6666, 160 new ServerExecute() { 161 public void execute(Socket s) throws IOException { 162 RFCReply reply = new RFCReply(); 163 reply.setReply(123, "Hello from a unit test"); 164 new PrintWriter(s.getOutputStream(), true).println(reply.toString()); 165 } 166 }); 167 server.start(); 168 169 IOHandler ioHandler = new IOHandlerSocket(); 170 Connection connection = new Connection(ioHandler); 171 172 try { 173 ioHandler.connectClient(connectionData, 0); 174 175 try { 176 connection.getResponse(response); 177 } 178 catch (ProtocolException ex) { 179 fail(ex.getMessage()); 180 } 181 } 182 catch (IndyIOException ex) { 183 fail(ex.getMessage()); 184 } 185 finally { 186 ioHandler.close(); 187 } 188 } 189 190 /*** 191 * DOCUMENT ME! 192 */ 193 public void testReadChar() { 194 ServerFixture server = new ServerFixture(6666, 195 new ServerExecute() { 196 public void execute(Socket s) throws IOException { 197 DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 198 199 for (char c = 'a'; c < 'z'; c++) { 200 dos.writeChar(c); 201 } 202 203 dos.writeChar('a'); 204 dos.flush(); 205 } 206 }); 207 server.start(); 208 209 IOHandler ioHandler = new IOHandlerSocket(); 210 Connection connection = new Connection(ioHandler); 211 212 try { 213 ioHandler.connectClient(connectionData, 0); 214 215 for (char c = 'a'; c < 'z'; c++) { 216 char res = connection.readChar(); 217 assertEquals("Read Chars", c, res); 218 } 219 } 220 catch (Exception e) { 221 fail(e.getMessage()); 222 } 223 finally { 224 ioHandler.close(); 225 } 226 } 227 228 /*** 229 * DOCUMENT ME! 230 */ 231 public void testAllData() { 232 final String data = "This is some data. Not very much. But It can be repeated, oh yeah"; 233 ServerFixture server = new ServerFixture(6666, 234 new ServerExecute() { 235 public void execute(Socket s) throws IOException { 236 PrintWriter pw = new PrintWriter(s.getOutputStream()); 237 238 for (int i = 0; i < 4; i++) { 239 pw.print(data); 240 } 241 242 pw.flush(); 243 s.close(); 244 } 245 }); 246 server.start(); 247 248 IOHandler ioHandler = new IOHandlerSocket(); 249 Connection connection = new Connection(ioHandler); 250 251 try { 252 ioHandler.connectClient(connectionData, 0); 253 254 String res = connection.allData(); 255 assertEquals("Check Data Length", data.length() * 4, res.length()); 256 257 StringBuffer sb = new StringBuffer(); 258 sb.append(data); 259 sb.append(data); 260 sb.append(data); 261 sb.append(data); 262 assertEquals("Check Data", sb.toString(), res); 263 } 264 catch (Exception e) { 265 fail("Exception thrown: " + e); 266 } 267 finally { 268 ioHandler.close(); 269 } 270 } 271 272 /*** 273 * DOCUMENT ME! 274 */ 275 public void testReadLong() { 276 final long data = System.currentTimeMillis(); 277 ServerFixture server = new ServerFixture(6666, 278 new ServerExecute() { 279 public void execute(Socket s) throws IOException { 280 DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 281 dos.writeLong(data); 282 } 283 }); 284 285 IOHandler ioHandler = new IOHandlerSocket(); 286 Connection connection = new Connection(ioHandler); 287 288 try { 289 ioHandler.connectClient(connectionData, 0); 290 291 long longRet = connection.readLong(); 292 assertEquals(data, longRet); 293 } 294 catch (Exception e) { 295 fail("Exception thrown: " + e); 296 } 297 finally { 298 ioHandler.close(); 299 } 300 } 301 302 /*** 303 * DOCUMENT ME! 304 */ 305 public void testReadStream() { 306 final String data = "This is some data to go into a stream"; 307 308 ServerFixture server = new ServerFixture(6666, 309 new ServerExecute() { 310 public void execute(Socket s) throws IOException { 311 new PrintWriter(s.getOutputStream(), true).println(data); 312 } 313 }); 314 315 IOHandler ioHandler = new IOHandlerSocket(); 316 Connection connection = new Connection(ioHandler); 317 318 try { 319 InputStream inputStreamRet = connection.readStream(data.length() + 1, 320 false); 321 assertEquals(data, 322 new BufferedReader(new InputStreamReader(inputStreamRet)).readLine()); 323 } 324 catch (Exception e) { 325 fail("Exception thrown: " + e); 326 } 327 } 328 329 /*** 330 * DOCUMENT ME! 331 */ 332 public void testWriteStrings() { 333 String[] strings = new String[3]; 334 strings[0] = "This is the first string"; 335 strings[1] = "This is the second string"; 336 strings[2] = "This is the third string"; 337 338 final StringList data = new StringList(); 339 data.add(strings[0]); 340 data.add(strings[1]); 341 data.add(strings[2]); 342 343 ServerFixture server = new ServerFixture(6666, 344 new ServerExecute() { 345 public void execute(Socket s) throws IOException { 346 try { 347 IOHandler ioHandler = new IOHandlerSocket(s); 348 Connection c = new Connection(ioHandler); 349 c.writeStrings(data, true); 350 } 351 catch (IndyIOException ex) { 352 ex.printStackTrace(); 353 } 354 } 355 }); 356 server.start(); 357 358 IOHandlerSocket ioHandler = new IOHandlerSocket(); 359 360 try { 361 ioHandler.connectClient(connectionData, 0); 362 363 Connection c = new Connection(ioHandler); 364 365 int lineCount = c.readInt(); 366 assertEquals("Test line count", 3, lineCount); 367 368 for (int i = 0; i < lineCount; i++) { 369 assertEquals("Test lines", strings[i], c.readLine()); 370 } 371 } 372 catch (Exception e) { 373 System.err.println("Exception thrown: " + e); 374 } 375 } 376 377 /*** 378 * DOCUMENT ME! 379 */ 380 public void testWriteStream() { 381 IOHandler val1 = null 382 ; 383 Connection connection = new Connection(val1); 384 InputStream stream1 = null 385 ; 386 int len2 = 0; 387 boolean writeByteCount3 = true; 388 389 try { 390 connection.writeStream(stream1, len2, writeByteCount3); 391 392 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 393 } 394 catch (Exception e) { 395 System.err.println("Exception thrown: " + e); 396 } 397 } 398 399 /*** 400 * DOCUMENT ME! 401 */ 402 public void testWriteShort() { 403 IOHandler val1 = null 404 ; 405 Connection connection = new Connection(val1); 406 short value1 = 0; 407 408 try { 409 connection.writeShort(value1); 410 411 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 412 } 413 catch (Exception e) { 414 System.err.println("Exception thrown: " + e); 415 } 416 } 417 418 /*** 419 * DOCUMENT ME! 420 */ 421 public void testWriteRFCStrings() { 422 IOHandler val1 = null 423 ; 424 Connection connection = new Connection(val1); 425 StringList strings1 = null 426 ; 427 428 try { 429 connection.writeRFCStrings(strings1); 430 431 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 432 } 433 catch (Exception e) { 434 System.err.println("Exception thrown: " + e); 435 } 436 } 437 438 /*** 439 * DOCUMENT ME! 440 */ 441 public void testWriteRFCReply() { 442 IOHandler val1 = null 443 ; 444 Connection connection = new Connection(val1); 445 RFCReply reply1 = null 446 ; 447 448 try { 449 connection.writeRFCReply(reply1); 450 451 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 452 } 453 catch (Exception e) { 454 System.err.println("Exception thrown: " + e); 455 } 456 } 457 458 /*** 459 * DOCUMENT ME! 460 */ 461 public void testWriteLong() { 462 IOHandler val1 = null 463 ; 464 Connection connection = new Connection(val1); 465 long value1 = 0; 466 467 try { 468 connection.writeLong(value1); 469 470 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 471 } 472 catch (Exception e) { 473 System.err.println("Exception thrown: " + e); 474 } 475 } 476 477 /*** 478 * DOCUMENT ME! 479 */ 480 public void testWriteLine() { 481 IOHandler val1 = null 482 ; 483 Connection connection = new Connection(val1); 484 String line1 = "STRING0"; 485 486 try { 487 connection.writeLine(line1); 488 489 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 490 } 491 catch (Exception e) { 492 System.err.println("Exception thrown: " + e); 493 } 494 } 495 496 /*** 497 * DOCUMENT ME! 498 */ 499 public void testWriteInt() { 500 IOHandler val1 = null 501 ; 502 Connection connection = new Connection(val1); 503 int value1 = 0; 504 505 try { 506 connection.writeInt(value1); 507 508 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 509 } 510 catch (Exception e) { 511 System.err.println("Exception thrown: " + e); 512 } 513 } 514 515 /*** 516 * DOCUMENT ME! 517 */ 518 public void testWriteHeader() { 519 IOHandler val1 = null 520 ; 521 Connection connection = new Connection(val1); 522 StringList header1 = null 523 ; 524 525 try { 526 connection.writeHeader(header1); 527 528 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 529 } 530 catch (Exception e) { 531 System.err.println("Exception thrown: " + e); 532 } 533 } 534 535 /*** 536 * DOCUMENT ME! 537 */ 538 public void testWriteFile() { 539 IOHandler val1 = null 540 ; 541 Connection connection = new Connection(val1); 542 File f1 = null 543 ; 544 545 try { 546 connection.writeFile(f1); 547 548 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 549 } 550 catch (Exception e) { 551 System.err.println("Exception thrown: " + e); 552 } 553 } 554 555 /*** 556 * DOCUMENT ME! 557 */ 558 public void testWriteBuffer() { 559 IOHandler val1 = null 560 ; 561 Connection connection = new Connection(val1); 562 byte[] buf1 = null 563 ; 564 565 try { 566 connection.writeBuffer(buf1); 567 568 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 569 } 570 catch (Exception e) { 571 System.err.println("Exception thrown: " + e); 572 } 573 } 574 575 /*** 576 * DOCUMENT ME! 577 */ 578 public void testWrite() { 579 IOHandler val1 = null 580 ; 581 Connection connection = new Connection(val1); 582 String s1 = "STRING0"; 583 584 try { 585 connection.write(s1); 586 587 /*** @todo: Insert test code here. Use assertEquals(), for example. */ 588 } 589 catch (Exception e) { 590 System.err.println("Exception thrown: " + e); 591 } 592 } 593 594 private interface ServerExecute { 595 void execute(Socket s) throws IOException; 596 } 597 598 private class ServerFixture extends Thread { 599 ServerExecute executor; 600 int port; 601 private volatile boolean stopped = false; 602 603 public ServerFixture(int port, ServerExecute e) { 604 executor = e; 605 this.port = port; 606 } 607 608 public void stopServer() { 609 stopped = true; 610 } 611 612 public void run() { 613 ServerSocket ss = null; 614 615 try { 616 ss = new ServerSocket(port); 617 618 Socket s = ss.accept(); 619 executor.execute(s); 620 } 621 catch (IOException ex) { 622 ex.printStackTrace(); 623 } 624 finally { 625 try { 626 ss.close(); 627 } 628 catch (IOException ex) { 629 } 630 } 631 } 632 } 633 }

This page was automatically generated by Maven