3939import java .net .InetSocketAddress ;
4040import java .util .concurrent .CountDownLatch ;
4141
42- import static org .junit .Assert .assertTrue ;
43- import static org .junit .Assert .fail ;
42+ import static org .junit .Assert .*;
4443
4544public class Issue661Test {
4645
@@ -50,42 +49,30 @@ public class Issue661Test{
5049private CountDownLatch countServerDownLatch = new CountDownLatch ( 1 );
5150
5251private boolean wasError = false ;
52+ private boolean wasBindException = false ;
5353
54- class TestPrintStream extends PrintStream {
55- public TestPrintStream ( OutputStream out ){
56- super ( out );
57- }
58-
59- @ Override
60- public void println ( Object o ){
61- wasError = true ;
62- super .println ( o );
63- }
64- }
65-
66- //@Test(timeout = 2000)
54+ @ Test (timeout = 2000 )
6755public void testIssue () throws Exception {
68- System .setErr ( new TestPrintStream ( System .err ) );
6956int port = SocketUtil .getAvailablePort ();
70- WebSocketServer server0 = new WebSocketServer (new InetSocketAddress (port ) ){
57+ WebSocketServer server0 = new WebSocketServer (new InetSocketAddress (port ) ){
7158@ Override
72- public void onOpen (WebSocket conn , ClientHandshake handshake ){
73- fail ("There should be no onOpen" );
59+ public void onOpen (WebSocket conn , ClientHandshake handshake ){
60+ fail ("There should be no onOpen" );
7461 }
7562
7663@ Override
77- public void onClose (WebSocket conn , int code , String reason , boolean remote ){
78- fail ("There should be no onClose" );
64+ public void onClose (WebSocket conn , int code , String reason , boolean remote ){
65+ fail ("There should be no onClose" );
7966 }
8067
8168@ Override
82- public void onMessage (WebSocket conn , String message ){
83- fail ("There should be no onMessage" );
69+ public void onMessage (WebSocket conn , String message ){
70+ fail ("There should be no onMessage" );
8471 }
8572
8673@ Override
87- public void onError (WebSocket conn , Exception ex ){
88- fail ("There should be no onError!" );
74+ public void onError (WebSocket conn , Exception ex ){
75+ fail ("There should be no onError!" );
8976 }
9077
9178@ Override
@@ -96,42 +83,45 @@ public void onStart(){
9683server0 .start ();
9784try {
9885countServerDownLatch .await ();
99- } catch (InterruptedException e ){
86+ } catch (InterruptedException e ){
10087//
10188 }
102- WebSocketServer server1 = new WebSocketServer (new InetSocketAddress (port ) ){
89+ WebSocketServer server1 = new WebSocketServer (new InetSocketAddress (port ) ){
10390@ Override
104- public void onOpen (WebSocket conn , ClientHandshake handshake ){
105- fail ("There should be no onOpen" );
91+ public void onOpen (WebSocket conn , ClientHandshake handshake ){
92+ fail ("There should be no onOpen" );
10693 }
10794
10895@ Override
109- public void onClose (WebSocket conn , int code , String reason , boolean remote ){
110- fail ("There should be no onClose" );
96+ public void onClose (WebSocket conn , int code , String reason , boolean remote ){
97+ fail ("There should be no onClose" );
11198 }
11299
113100@ Override
114- public void onMessage (WebSocket conn , String message ){
115- fail ("There should be no onMessage" );
101+ public void onMessage (WebSocket conn , String message ){
102+ fail ("There should be no onMessage" );
116103 }
117104
118105@ Override
119- public void onError ( WebSocket conn , Exception ex ){
120- if ( !( ex instanceof BindException ) ){
121- fail ( "There should be no onError" );
106+ public void onError (WebSocket conn , Exception ex ){
107+ if (ex instanceof BindException ){
108+ wasBindException = true ;
109+ } else {
110+ wasError = true ;
122111 }
123112 }
124113
125114@ Override
126115public void onStart (){
127- fail ("There should be no onStart!" );
116+ fail ("There should be no onStart!" );
128117 }
129118 };
130119server1 .start ();
131- Thread .sleep (1000 );
120+ Thread .sleep (1000 );
132121server1 .stop ();
133122server0 .stop ();
134- Thread .sleep ( 100 );
135- assertTrue ( "There was an error using System.err" , !wasError );
123+ Thread .sleep (100 );
124+ assertFalse ("There was an unexpected exception!" , wasError );
125+ assertTrue ("There was no bind exception!" , wasBindException );
136126 }
137127}
0 commit comments