Skip to content

Commit 27209fd

Browse files
authored
Merge pull request TooTallNate#617 from marci4/master
Extended JUnit test
2 parents 2ff0047 + b375041 commit 27209fd

File tree

7 files changed

+251
-32
lines changed

7 files changed

+251
-32
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.extensions;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.Suite;
29+
30+
31+
@RunWith(Suite.class)
32+
@Suite.SuiteClasses({
33+
org.java_websocket.extensions.DefaultExtensionTest.class
34+
})
35+
/**
36+
* Start all tests for extensuins
37+
*/
38+
public class AllExtensionTests {
39+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.extensions;
27+
28+
import org.java_websocket.framing.BinaryFrame;
29+
import org.java_websocket.framing.TextFrame;
30+
import org.junit.Test;
31+
32+
import java.nio.ByteBuffer;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertTrue;
36+
import static org.junit.Assert.fail;
37+
38+
public class DefaultExtensionTest {
39+
@Test
40+
public void testDecodeFrame() throws Exception {
41+
DefaultExtension defaultExtension = new DefaultExtension();
42+
BinaryFrame binaryFrame = new BinaryFrame();
43+
binaryFrame.setPayload( ByteBuffer.wrap( "test".getBytes() ) );
44+
defaultExtension.decodeFrame( binaryFrame );
45+
assertEquals( ByteBuffer.wrap( "test".getBytes() ), binaryFrame.getPayloadData() );
46+
}
47+
48+
@Test
49+
public void testEncodeFrame() throws Exception {
50+
DefaultExtension defaultExtension = new DefaultExtension();
51+
BinaryFrame binaryFrame = new BinaryFrame();
52+
binaryFrame.setPayload( ByteBuffer.wrap( "test".getBytes() ) );
53+
defaultExtension.encodeFrame( binaryFrame );
54+
assertEquals( ByteBuffer.wrap( "test".getBytes() ), binaryFrame.getPayloadData() );
55+
}
56+
57+
@Test
58+
public void testAcceptProvidedExtensionAsServer() throws Exception {
59+
DefaultExtension defaultExtension = new DefaultExtension();
60+
assertTrue( defaultExtension.acceptProvidedExtensionAsServer( "Test" ) );
61+
assertTrue( defaultExtension.acceptProvidedExtensionAsServer( "" ) );
62+
assertTrue( defaultExtension.acceptProvidedExtensionAsServer( "Test, ASDC, as, ad" ) );
63+
assertTrue( defaultExtension.acceptProvidedExtensionAsServer( "ASDC, as,ad" ) );
64+
assertTrue( defaultExtension.acceptProvidedExtensionAsServer( "permessage-deflate" ) );
65+
}
66+
67+
@Test
68+
public void testAcceptProvidedExtensionAsClient() throws Exception {
69+
DefaultExtension defaultExtension = new DefaultExtension();
70+
assertTrue( defaultExtension.acceptProvidedExtensionAsClient( "Test" ) );
71+
assertTrue( defaultExtension.acceptProvidedExtensionAsClient( "" ) );
72+
assertTrue( defaultExtension.acceptProvidedExtensionAsClient( "Test, ASDC, as, ad" ) );
73+
assertTrue( defaultExtension.acceptProvidedExtensionAsClient( "ASDC, as,ad" ) );
74+
assertTrue( defaultExtension.acceptProvidedExtensionAsClient( "permessage-deflate" ) );
75+
}
76+
77+
@Test
78+
public void testIsFrameValid() throws Exception {
79+
DefaultExtension defaultExtension = new DefaultExtension();
80+
TextFrame textFrame = new TextFrame();
81+
try {
82+
defaultExtension.isFrameValid( textFrame );
83+
} catch ( Exception e ) {
84+
fail( "This frame is valid" );
85+
}
86+
textFrame.setRSV1( true );
87+
try {
88+
defaultExtension.isFrameValid( textFrame );
89+
fail( "This frame is not valid" );
90+
} catch ( Exception e ) {
91+
//
92+
}
93+
textFrame.setRSV1( false );
94+
textFrame.setRSV2( true );
95+
try {
96+
defaultExtension.isFrameValid( textFrame );
97+
fail( "This frame is not valid" );
98+
} catch ( Exception e ) {
99+
//
100+
}
101+
textFrame.setRSV2( false );
102+
textFrame.setRSV3( true );
103+
try {
104+
defaultExtension.isFrameValid( textFrame );
105+
fail( "This frame is not valid" );
106+
} catch ( Exception e ) {
107+
//
108+
}
109+
}
110+
111+
@Test
112+
public void testGetProvidedExtensionAsClient() throws Exception {
113+
DefaultExtension defaultExtension = new DefaultExtension();
114+
assertEquals( "", defaultExtension.getProvidedExtensionAsClient() );
115+
}
116+
117+
@Test
118+
public void testGetProvidedExtensionAsServer() throws Exception {
119+
DefaultExtension defaultExtension = new DefaultExtension();
120+
assertEquals( "", defaultExtension.getProvidedExtensionAsServer() );
121+
}
122+
123+
@Test
124+
public void testCopyInstance() throws Exception {
125+
DefaultExtension defaultExtension = new DefaultExtension();
126+
IExtension extensionCopy = defaultExtension.copyInstance();
127+
assertEquals( defaultExtension, extensionCopy );
128+
}
129+
130+
@Test
131+
public void testToString() throws Exception {
132+
DefaultExtension defaultExtension = new DefaultExtension();
133+
assertEquals( "DefaultExtension", defaultExtension.toString() );
134+
}
135+
136+
@Test
137+
public void testHashCode() throws Exception {
138+
DefaultExtension defaultExtension0 = new DefaultExtension();
139+
DefaultExtension defaultExtension1 = new DefaultExtension();
140+
assertEquals( defaultExtension0.hashCode(), defaultExtension1.hashCode() );
141+
}
142+
143+
@Test
144+
public void testEquals() throws Exception {
145+
DefaultExtension defaultExtension0 = new DefaultExtension();
146+
DefaultExtension defaultExtension1 = new DefaultExtension();
147+
assertEquals( defaultExtension0, defaultExtension1 );
148+
}
149+
150+
}

src/test/java/org/java_websocket/issues/AllIssueTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
@RunWith(Suite.class)
3232
@Suite.SuiteClasses({
33-
org.java_websocket.issues.Issue609.class
33+
org.java_websocket.issues.Issue609Test.class
3434
})
3535
/**
3636
* Start all tests for issues

src/test/java/org/java_websocket/issues/Issue609.java renamed to src/test/java/org/java_websocket/issues/Issue609Test.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.java_websocket.handshake.ClientHandshake;
3131
import org.java_websocket.handshake.ServerHandshake;
3232
import org.java_websocket.server.WebSocketServer;
33+
import org.java_websocket.util.SocketUtil;
3334
import org.junit.Test;
3435

3536
import java.net.InetSocketAddress;
@@ -38,69 +39,73 @@
3839

3940
import static org.junit.Assert.assertTrue;
4041

41-
public class Issue609 {
42+
public class Issue609Test {
4243

4344
CountDownLatch countDownLatch = new CountDownLatch( 1 );
45+
CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
4446

4547
boolean wasOpenClient;
4648
boolean wasOpenServer;
49+
4750
@Test
4851
public void testIssue() throws Exception {
49-
WebSocketServer server = new WebSocketServer( new InetSocketAddress( 8887 ) ) {
52+
int port = SocketUtil.getAvailablePort();
53+
WebSocketClient webSocket = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
5054
@Override
51-
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
52-
}
55+
public void onOpen( ServerHandshake handshakedata ) {
5356

54-
@Override
55-
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
56-
wasOpenServer = conn.isOpen();
5757
}
5858

5959
@Override
60-
public void onMessage( WebSocket conn, String message ) {
60+
public void onMessage( String message ) {
6161

6262
}
6363

6464
@Override
65-
public void onError( WebSocket conn, Exception ex ) {
66-
65+
public void onClose( int code, String reason, boolean remote ) {
66+
wasOpenClient = isOpen();
67+
countDownLatch.countDown();
6768
}
6869

6970
@Override
70-
public void onStart() {
71+
public void onError( Exception ex ) {
7172

7273
}
7374
};
74-
server.start();
75-
WebSocketClient webSocket = new WebSocketClient( new URI( "ws://localhost:8887" ) ) {
75+
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
7676
@Override
77-
public void onOpen( ServerHandshake handshakedata ) {
77+
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
78+
}
7879

80+
@Override
81+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
82+
wasOpenServer = conn.isOpen();
7983
}
8084

8185
@Override
82-
public void onMessage( String message ) {
86+
public void onMessage( WebSocket conn, String message ) {
8387

8488
}
8589

8690
@Override
87-
public void onClose( int code, String reason, boolean remote ) {
88-
wasOpenClient= isOpen();
89-
countDownLatch.countDown();
91+
public void onError( WebSocket conn, Exception ex ) {
92+
9093
}
9194

9295
@Override
93-
public void onError( Exception ex ) {
94-
96+
public void onStart() {
97+
countServerDownLatch.countDown();
9598
}
9699
};
100+
server.start();
101+
countServerDownLatch.await();
97102
webSocket.connectBlocking();
98-
assertTrue( webSocket.isOpen() );
103+
assertTrue( "webSocket.isOpen()", webSocket.isOpen() );
99104
webSocket.getSocket().close();
100105
countDownLatch.await();
101-
assertTrue( !webSocket.isOpen() );
102-
assertTrue( !wasOpenClient );
103-
assertTrue( !wasOpenServer );
106+
assertTrue( "!webSocket.isOpen()", !webSocket.isOpen() );
107+
assertTrue( "!wasOpenClient", !wasOpenClient );
108+
assertTrue( "!wasOpenServer", !wasOpenServer );
104109
server.stop();
105110
}
106111
}

src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.java_websocket.framing.CloseFrame;
3030
import org.java_websocket.handshake.ServerHandshake;
3131
import org.java_websocket.util.Charsetfunctions;
32+
import org.java_websocket.util.SocketUtil;
3233
import org.junit.AfterClass;
3334
import org.junit.BeforeClass;
3435
import org.junit.Test;
@@ -51,13 +52,16 @@ public class OpeningHandshakeRejectionTest {
5152

5253
private static boolean debugPrintouts = false;
5354

55+
private static int port;
56+
5457
@BeforeClass
55-
public static void startServer() {
58+
public static void startServer() throws Exception {
59+
port = SocketUtil.getAvailablePort();
5660
thread = new Thread(
5761
new Runnable() {
5862
public void run() {
5963
try {
60-
serverSocket = new ServerSocket( 8887 );
64+
serverSocket = new ServerSocket( port );
6165
serverSocket.setReuseAddress( true );
6266
while( true ) {
6367
Socket client = null;
@@ -198,7 +202,7 @@ public void testHandshakeRejectionTestCase11() throws Exception {
198202
private void testHandshakeRejection( int i ) throws Exception {
199203
final int finalI = i;
200204
final boolean[] threadReturned = { false };
201-
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ) ) {
205+
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:"+ port + "/" + finalI ) ) {
202206
@Override
203207
public void onOpen( ServerHandshake handshakedata ) {
204208
fail( "There should not be a connection!" );

src/test/java/org/java_websocket/protocols/ProtoclHandshakeRejectionTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.java_websocket.handshake.ServerHandshake;
3333
import org.java_websocket.util.Base64;
3434
import org.java_websocket.util.Charsetfunctions;
35+
import org.java_websocket.util.SocketUtil;
3536
import org.junit.AfterClass;
3637
import org.junit.BeforeClass;
3738
import org.junit.Test;
@@ -58,13 +59,16 @@ public class ProtoclHandshakeRejectionTest {
5859

5960
private static boolean debugPrintouts = false;
6061

62+
private static int port;
63+
6164
@BeforeClass
62-
public static void startServer() {
65+
public static void startServer() throws Exception {
66+
port = SocketUtil.getAvailablePort();
6367
thread = new Thread(
6468
new Runnable() {
6569
public void run() {
6670
try {
67-
serverSocket = new ServerSocket( 8887 );
71+
serverSocket = new ServerSocket( port );
6872
serverSocket.setReuseAddress( true );
6973
int count = 1;
7074
while( true ) {
@@ -168,7 +172,7 @@ public void run() {
168172
}
169173
}
170174
} catch ( Exception e ) {
171-
e.printStackTrace( );
175+
e.printStackTrace();
172176
fail( "There should be no exception" );
173177
}
174178
}
@@ -294,7 +298,7 @@ public void testHandshakeRejectionTestCase17() throws Exception {
294298
private void testProtocolRejection( int i, Draft_6455 draft ) throws Exception {
295299
final int finalI = i;
296300
final boolean[] threadReturned = { false };
297-
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ), draft ) {
301+
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:" + port + "/" + finalI ), draft ) {
298302
@Override
299303
public void onOpen( ServerHandshake handshakedata ) {
300304
switch(finalI) {

0 commit comments

Comments
 (0)