--- old/test/java/nio/channels/SocketChannel/AdaptSocket.java 2012-10-30 18:40:25.000000000 +0100 +++ new/test/java/nio/channels/SocketChannel/AdaptSocket.java 2012-10-30 18:40:25.000000000 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -35,19 +35,13 @@ static java.io.PrintStream out = System.out; - static final int ECHO_PORT = 7; - static final int DAYTIME_PORT = 13; - static final String REMOTE_HOST = TestUtil.HOST; - static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST; - - static void test(String hn, int timeout, boolean shouldTimeout) + static void test(TestUtil.DayTimeServer dayTimeServer,int timeout, boolean shouldTimeout) throws Exception { out.println(); - InetSocketAddress isa - = new InetSocketAddress(InetAddress.getByName(hn), - DAYTIME_PORT); + InetSocketAddress isa = new InetSocketAddress(dayTimeServer.getAddress(), + dayTimeServer.getPort()); SocketChannel sc = SocketChannel.open(); Socket so = sc.socket(); out.println("opened: " + so); @@ -116,13 +110,13 @@ } } - static void testRead(String hn, int timeout, boolean shouldTimeout) + static void testRead(TestUtil.EchoServer echoServer, int timeout, boolean shouldTimeout) throws Exception { out.println(); - InetSocketAddress isa - = new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT); + InetSocketAddress isa = new InetSocketAddress(echoServer.getAddress(), + echoServer.getPort()); SocketChannel sc = SocketChannel.open(); sc.connect(isa); Socket so = sc.socket(); @@ -134,22 +128,38 @@ out.println("timeout: " + so.getSoTimeout()); testRead(so, shouldTimeout); - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { testRead(so, shouldTimeout); + } sc.close(); } public static void main(String[] args) throws Exception { - test(REMOTE_HOST, 0, false); - test(REMOTE_HOST, 1000, false); - test(VERY_REMOTE_HOST, 10, true); - - testRead(REMOTE_HOST, 0, false); - testRead(REMOTE_HOST, 8000, false); - testRead(VERY_REMOTE_HOST, 10, true); + try (TestUtil.DayTimeServer dayTimeServer = + TestUtil.DayTimeServer.startNewServer()) { + test(dayTimeServer, 0, false); + test(dayTimeServer, 1000, false); + } + + try (TestUtil.DayTimeServer lingerDayTimeServer = + TestUtil.DayTimeServer.startNewServer(100)) { + // this test no longer really test the connection timeout + // since there is no way to prevent the server from eagerly + // accepting connection... + test(lingerDayTimeServer, 10, true); + } + + try (TestUtil.EchoServer echoServer = + TestUtil.EchoServer.startNewServer()) { + testRead(echoServer, 0, false); + testRead(echoServer, 8000, false); + } + try (TestUtil.EchoServer lingerEchoServer = + TestUtil.EchoServer.startNewServer(100)) { + testRead(lingerEchoServer, 10, true); + } } - }