39
39
import java .net .InetSocketAddress ;
40
40
import java .util .concurrent .CountDownLatch ;
41
41
42
- import static org .junit .Assert .assertTrue ;
43
- import static org .junit .Assert .fail ;
42
+ import static org .junit .Assert .*;
44
43
45
44
public class Issue661Test {
46
45
@@ -50,42 +49,30 @@ public class Issue661Test {
50
49
private CountDownLatch countServerDownLatch = new CountDownLatch ( 1 );
51
50
52
51
private boolean wasError = false ;
52
+ private boolean wasBindException = false ;
53
53
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 )
67
55
public void testIssue () throws Exception {
68
- System .setErr ( new TestPrintStream ( System .err ) );
69
56
int port = SocketUtil .getAvailablePort ();
70
- WebSocketServer server0 = new WebSocketServer ( new InetSocketAddress ( port ) ) {
57
+ WebSocketServer server0 = new WebSocketServer (new InetSocketAddress (port ) ) {
71
58
@ 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" );
74
61
}
75
62
76
63
@ 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" );
79
66
}
80
67
81
68
@ 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" );
84
71
}
85
72
86
73
@ 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!" );
89
76
}
90
77
91
78
@ Override
@@ -96,42 +83,45 @@ public void onStart() {
96
83
server0 .start ();
97
84
try {
98
85
countServerDownLatch .await ();
99
- } catch ( InterruptedException e ) {
86
+ } catch (InterruptedException e ) {
100
87
//
101
88
}
102
- WebSocketServer server1 = new WebSocketServer ( new InetSocketAddress ( port ) ) {
89
+ WebSocketServer server1 = new WebSocketServer (new InetSocketAddress (port ) ) {
103
90
@ 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" );
106
93
}
107
94
108
95
@ 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" );
111
98
}
112
99
113
100
@ 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" );
116
103
}
117
104
118
105
@ 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 ;
122
111
}
123
112
}
124
113
125
114
@ Override
126
115
public void onStart () {
127
- fail ( "There should be no onStart!" );
116
+ fail ("There should be no onStart!" );
128
117
}
129
118
};
130
119
server1 .start ();
131
- Thread .sleep ( 1000 );
120
+ Thread .sleep (1000 );
132
121
server1 .stop ();
133
122
server0 .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 );
136
126
}
137
127
}
0 commit comments