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.BufferedInputStream; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.io.InterruptedIOException; 13 14 import java.net.Socket; 15 16 import java.util.Random; 17 18 import junit.framework.TestCase; 19 20 import org.indy.io.IOHandlerSocket; 21 import org.indy.io.PeerDisconnectedException; 22 23 24 /*** 25 * DOCUMENT ME! 26 * 27 * @version $Revision$ 28 * @author $author$ 29 */ 30 public class TestTCPClient extends TestCase { 31 /* 32 * Server Executors 33 */ 34 final TCPServerExecuteListener ServerExecuteBound = new TCPServerExecuteListener() { 35 public void onExecute(TCPServer sender, TCPServerConnection connection) 36 throws IndyException { 37 try { 38 String ip = connection.readLine(); 39 int port = 0; 40 41 try { 42 port = Integer.parseInt(connection.readLine()); 43 } 44 catch (NumberFormatException nfe) { 45 port = 0; 46 } 47 48 String peerIP = ((IOHandlerSocket) connection.getIOHandler()).getPeerIP(); 49 int peerPort = ((IOHandlerSocket) connection.getIOHandler()).getPeerPort(); 50 51 if (ip.equals(peerIP) && (port == peerPort)) { 52 //System.err.println("WRITING DONE"); 53 connection.writeLine("DONE"); 54 } 55 else { 56 System.err.println("WRITING ERROR"); 57 connection.writeLine("ERROR"); 58 } 59 } 60 finally { 61 connection.disconnect(); 62 } 63 } 64 }; 65 66 final TCPServerExecuteListener ServerExecuteConnectGetAll = 67 new TCPServerExecuteListener() { 68 public void onExecute(TCPServer sender, TCPServerConnection connection) 69 throws IndyException { 70 try { 71 for (int i = 0; i < 4; i++) { 72 connection.write( 73 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789_abcdefghijklmnopqrstuvwxyz"); 74 } 75 76 connection.getIOHandler().flush(); 77 } 78 finally { 79 connection.disconnect(); 80 } 81 } 82 }; 83 84 final TCPServerExecuteListener ServerExecuteDisconnect = 85 new TCPServerExecuteListener() { 86 public void onExecute(TCPServer sender, TCPServerConnection connection) 87 throws IndyException { 88 try { 89 Thread.currentThread().sleep(1000); 90 } 91 catch (InterruptedException ie) { 92 } 93 94 connection.disconnect(); 95 } 96 }; 97 98 final TCPServerExecuteListener ServerExecuteNagle = new TCPServerExecuteListener() { 99 public void onExecute(TCPServer sender, TCPServerConnection connection) 100 throws IndyException { 101 try { 102 String line = null; 103 104 while (true) { 105 if (connection.readLine().equals("DONE")) { 106 break; 107 } 108 } 109 110 for (int i = 0; i < 64; i++) { 111 connection.writeLine("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 112 } 113 114 connection.writeLine("DONE"); 115 } 116 finally { 117 connection.disconnect(); 118 } 119 } 120 }; 121 122 final TCPServerExecuteListener ServerExecuteReadAll = 123 new TCPServerExecuteListener() { 124 public void onExecute(TCPServer sender, TCPServerConnection connection) 125 throws IndyException { 126 try { 127 //while (connection.isConnected()) { 128 // connection.readFromStack(false); 129 // } 130 while (connection.isConnected()) { 131 connection.allData(); 132 } 133 } 134 catch (Exception ex) { 135 throw new IndyException(ex); 136 } 137 } 138 }; 139 140 final TCPServerExecuteListener ServerExecuteReuse = new TCPServerExecuteListener() { 141 public void onExecute(TCPServer sender, TCPServerConnection connection) 142 throws IndyException { 143 if (connection.isConnected()) { 144 connection.readLine(); 145 connection.writeLine("DONE"); 146 } 147 } 148 }; 149 150 TCPClient client = null; 151 TCPServer server = new TCPServer(); 152 153 /*** 154 * Creates a new TestTCPClient object. 155 * 156 * @param s DOCUMENT ME! 157 */ 158 public TestTCPClient(String s) { 159 super(s); 160 } 161 162 /*** 163 * The JUnit setup method 164 */ 165 protected void setUp() { 166 client = new TCPClient(); 167 168 //server.setDefaultPort(6666); 169 try { 170 server.setActive(true); 171 } 172 catch (Exception ex) { 173 ex.printStackTrace(); 174 fail(ex.getMessage()); 175 } 176 } 177 178 /*** 179 * Main method for individual runs 180 * 181 *@param arg The command line arguments 182 */ 183 public static void main(String[] arg) { 184 junit.swingui.TestRunner.run(TestTCPClient.class); 185 } 186 187 /*** 188 * The teardown method for JUnit 189 */ 190 public void tearDown() { 191 try { 192 if (client.isConnected()) { 193 client.disconnect(); 194 } 195 196 server.addServerExecuteListener(null); 197 server.setActive(false); 198 } 199 catch (IndyException ie) { 200 ie.printStackTrace(); 201 } 202 finally { 203 client = null; 204 } 205 } 206 207 /*** 208 * A unit test for JUnit 209 */ 210 public void testClientReuse() { 211 server.addServerExecuteListener(ServerExecuteReuse); 212 213 client.setHost("127.0.0.1"); 214 client.setPort(6666); 215 216 for (int i = 0; i < 5; i++) { 217 try { 218 System.err.println(i); 219 client.connect(); 220 client.writeLine("QUIT\n"); 221 222 String data = client.readLine(); 223 224 System.err.println(data); 225 assertEquals("Connection #" + i, "DONE", data); 226 } 227 catch (IndyException ie) { 228 ie.printStackTrace(); 229 fail("Indy Exception thrown: " + ie.getMessage()); 230 } 231 catch (Exception e) { 232 e.printStackTrace(); 233 } 234 finally { 235 try { 236 client.disconnect(); 237 } 238 catch (IndyException ie) { 239 ie.printStackTrace(); 240 } 241 } 242 } 243 } 244 245 /*** 246 * A unit test for JUnit 247 */ 248 public void testClientBound() { 249 server.addServerExecuteListener(ServerExecuteBound); 250 251 252 //while(true){} 253 client.setHost("127.0.0.1"); 254 client.setPort(6666); 255 256 client.setBoundIP("127.0.0.1"); 257 client.setBoundPort(0); 258 259 try { 260 client.connect(); 261 assertEquals("Testing Bound IP", "127.0.0.1", client.getBoundIP()); 262 client.writeLine(client.getBoundIP()); 263 client.writeLine(String.valueOf(client.getBoundPort())); 264 assertEquals("Testing Server Receipt of Bound IP / Port", "DONE", 265 client.readLine()); 266 } 267 catch (IndyException ie) { 268 ie.printStackTrace(); 269 fail("Indy Exception thrown: " + ie.getMessage()); 270 } 271 finally { 272 try { 273 client.disconnect(); 274 } 275 catch (IndyException ie) { 276 ie.printStackTrace(); 277 } 278 } 279 280 client.setBoundIP(null); 281 282 Random r = new Random(); 283 284 int port = 15000 + r.nextInt(10000); 285 client.setBoundPort(port); 286 287 try { 288 client.connect(); 289 assertEquals("Testing Bound Port", port, client.getBoundPort()); 290 client.writeLine(client.getBoundIP()); 291 client.writeLine(String.valueOf(client.getBoundPort())); 292 assertEquals("Testing Server Receipt of Bound IP / Port", "DONE", 293 client.readLine()); 294 } 295 catch (IndyException ie) { 296 ie.printStackTrace(); 297 fail("Indy Exception thrown: " + ie.getMessage()); 298 } 299 finally { 300 try { 301 client.disconnect(); 302 } 303 catch (IndyException ie) { 304 ie.printStackTrace(); 305 } 306 } 307 308 client.setBoundIP("127.0.0.1"); 309 client.setBoundPort(15000 + r.nextInt(10000)); 310 311 try { 312 client.connect(); 313 assertEquals("Testing Bound IP and Port", "127.0.0.1", 314 client.getBoundIP()); 315 client.writeLine(client.getBoundIP()); 316 client.writeLine(String.valueOf(client.getBoundPort())); 317 assertEquals("Testing Server Receipt of Bound IP / Port", "DONE", 318 client.readLine()); 319 } 320 catch (IndyException ie) { 321 ie.printStackTrace(); 322 fail("Indy Exception thrown: " + ie.getMessage()); 323 } 324 finally { 325 try { 326 client.disconnect(); 327 } 328 catch (IndyException ie) { 329 ie.printStackTrace(); 330 } 331 } 332 } 333 334 /*** 335 * A unit test for JUnit 336 */ 337 public void testClientConnectGetAll() { 338 server.addServerExecuteListener(ServerExecuteConnectGetAll); 339 client.setBoundIP(null); 340 client.setBoundPort(0); 341 client.setHost("127.0.0.1"); 342 client.setPort(6666); 343 344 String s = client.connectAndGetAll(); 345 346 347 // System.err.println(s); 348 assertEquals("Connect and get all test", 256, s.length()); 349 } 350 351 // public void testClientNagle(){ 352 // } 353 // public void testClientConnectionTimeout(){} 354 355 /*** 356 * A unit test for JUnit 357 */ 358 359 /* public void testClientForGracefulDisconnectOnly() { 360 server.setExecutor(ServerExecuteDisconnect); 361 362 client.setHost("127.0.0.1"); 363 client.setPort(6666); 364 365 try { 366 client.connect(); 367 368 client.checkForGracefulDisconnect(true); 369 370 } 371 catch (ConnectionClosedGracefullyException e) { 372 e.printStackTrace(); 373 } 374 catch (IndyException ie) { 375 fail(ie.getMessage()); 376 } 377 finally { 378 try { 379 client.disconnect(); 380 } 381 catch (IndyException ide) { 382 ide.printStackTrace(); 383 } 384 } 385 }*/ 386 387 /*** 388 * Description of the Method 389 */ 390 public void ntestSomething() { 391 server.addServerExecuteListener(ServerExecuteReuse); 392 393 try { 394 Socket s = new Socket("127.0.0.1", 6666); 395 396 InputStream inS = new BufferedInputStream(s.getInputStream()); 397 398 s.getOutputStream().write("Hello\n".getBytes()); 399 s.getInputStream().read(new byte[5], 0, 5); 400 401 s.setSoTimeout(250); 402 403 //long start = System.currentTimeMillis(); 404 while (inS.available() == 0) { 405 try { 406 inS.mark(0); 407 408 int read = inS.read(); 409 410 System.err.println(read); 411 412 if (read == -1) { 413 break; 414 } 415 else { 416 inS.reset(); 417 } 418 } 419 catch (InterruptedIOException iioe) { 420 //iioe.printStackTrace(); 421 } 422 } 423 424 //System.err.println(((System.currentTimeMillis()-start) / 1000) + " seconds"); 425 } 426 catch (IOException ioe) { 427 fail(ioe.getMessage()); 428 } 429 } 430 431 /*** 432 * A unit test for JUnit 433 */ 434 public void testClientForGracefulDisconnectRead() { 435 server.addServerExecuteListener(ServerExecuteDisconnect); 436 437 client.setHost("127.0.0.1"); 438 client.setPort(6666); 439 440 try { 441 client.setReadTimeOut(0); 442 client.connect(1000); 443 444 while (true) { 445 client.readLine(); 446 } 447 } 448 catch (PeerDisconnectedException ide) { 449 //ide.printStackTrace(); 450 return; 451 } 452 catch (IndyException ie) { 453 fail(ie.getMessage()); 454 } 455 finally { 456 try { 457 client.disconnect(); 458 } 459 catch (IndyException ide) { 460 ide.printStackTrace(); 461 } 462 } 463 } 464 465 /*** 466 * DOCUMENT ME! 467 */ 468 public void testClientTimeTests() { 469 } 470 }

This page was automatically generated by Maven