@@ -69,7 +69,15 @@ void testFinishConnect() throws Exception {
6969 sc .register (selector , SelectionKey .OP_CONNECT );
7070
7171 System .err .println ("establishing connection to " + destAddr );
72- boolean connected = sc .connect (destAddr );
72+ boolean connected ;
73+ try {
74+ connected = sc .connect (destAddr );
75+ } catch (ConnectException ce ) {
76+ // Connect failed immediately, which is OK.
77+ System .err .println ("SocketChannel.connect() threw ConnectException - " + ce );
78+ assertExceptionMessage (ce );
79+ return ; // nothing more to test
80+ }
7381 // this test checks the exception message of a ConnectException, so it's
7482 // OK to skip the test if something unexpectedly accepted the connection
7583 assumeFalse (connected , "unexpectedly connected to " + destAddr );
@@ -90,17 +98,22 @@ void testFinishConnect() throws Exception {
9098 // with a return value of false
9199 fail ("ConnectException was not thrown" );
92100 } catch (ConnectException ce ) {
93- System .err .println ("got (expected) ConnectException - " + ce );
101+ System .err .println ("got (expected) ConnectException from " +
102+ "SocketChannel.finishConnect() - " + ce );
94103 // verify exception message
95- if (!"Connection refused" .equals (ce .getMessage ())) {
96- // propagate the original exception
97- fail ("unexpected exception message: " + ce .getMessage (), ce );
98- }
104+ assertExceptionMessage (ce );
99105 }
100106 }
101107 }
102108 }
103109
110+ private static void assertExceptionMessage (final ConnectException ce ) {
111+ if (!"Connection refused" .equals (ce .getMessage ())) {
112+ // propagate the original exception
113+ fail ("unexpected exception message: " + ce .getMessage (), ce );
114+ }
115+ }
116+
104117 // Try to find a suitable port to provoke a "Connection Refused" error.
105118 private static InetSocketAddress findSuitableRefusedAddress () throws IOException {
106119 final InetAddress loopbackAddr = InetAddress .getLoopbackAddress ();
0 commit comments