Skip to content

Commit 85a5d3d

Browse files
committed
Fix some sonarqube errors
Fixed some sonarqube errors & added some tests for the websocket server
1 parent fd4d55c commit 85a5d3d

File tree

4 files changed

+238
-15
lines changed

4 files changed

+238
-15
lines changed

src/main/java/org/java_websocket/SocketChannelIOHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
public class SocketChannelIOHelper {
3535

36+
private SocketChannelIOHelper() {
37+
throw new IllegalStateException("Utility class");
38+
}
39+
3640
public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel channel ) throws IOException {
3741
buf.clear();
3842
int read = channel.read( buf );

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ public class WebSocketImpl implements WebSocket {
106106
/**
107107
* Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
108108
**/
109-
private volatile WebSocketWorker workerThread;
109+
private WebSocketWorker workerThread;
110110
/**
111111
* When true no further frames may be submitted to be sent
112112
*/
113-
private volatile boolean flushandclosestate = false;
113+
private boolean flushandclosestate = false;
114114

115115
/**
116116
* The current state of the connection

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i) throws Interr
450450
WebSocketImpl conn = (WebSocketImpl) key.attachment();
451451
ByteBuffer buf = takeBuffer();
452452
if(conn.getChannel() == null){
453-
if( key != null )
454-
key.cancel();
453+
key.cancel();
455454

456455
handleIOException( key, conn, new IOException() );
457456
return false;
@@ -462,10 +461,8 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i) throws Interr
462461
conn.inQueue.put( buf );
463462
queue( conn );
464463
i.remove();
465-
if( conn.getChannel() instanceof WrappedByteChannel ) {
466-
if( ( (WrappedByteChannel) conn.getChannel() ).isNeedRead() ) {
467-
iqueue.add( conn );
468-
}
464+
if( conn.getChannel() instanceof WrappedByteChannel && ( (WrappedByteChannel) conn.getChannel() ).isNeedRead() ) {
465+
iqueue.add( conn );
469466
}
470467
} else {
471468
pushBuffer(buf);
@@ -488,8 +485,9 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i) throws Interr
488485
private void doWrite(SelectionKey key) throws IOException {
489486
WebSocketImpl conn = (WebSocketImpl) key.attachment();
490487
if( SocketChannelIOHelper.batch( conn, conn.getChannel() ) ) {
491-
if( key.isValid() )
492-
key.interestOps( SelectionKey.OP_READ );
488+
if( key.isValid() ) {
489+
key.interestOps(SelectionKey.OP_READ);
490+
}
493491
}
494492
}
495493

@@ -690,15 +688,11 @@ protected boolean removeConnection( WebSocket ws ) {
690688
log.trace("Removing connection which is not in the connections collection! Possible no handshake recieved! {}", ws);
691689
}
692690
}
693-
if( isclosed.get() && connections.size() == 0 ) {
691+
if( isclosed.get() && connections.isEmpty() ) {
694692
selectorthread.interrupt();
695693
}
696694
return removed;
697695
}
698-
@Override
699-
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request ) throws InvalidDataException {
700-
return super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
701-
}
702696

703697
/**
704698
* @see #removeConnection(WebSocket)
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* Copyright (c) 2010-2019 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+
27+
package org.java_websocket.server;
28+
29+
import org.java_websocket.WebSocket;
30+
import org.java_websocket.drafts.Draft;
31+
import org.java_websocket.drafts.Draft_6455;
32+
import org.java_websocket.handshake.ClientHandshake;
33+
import org.java_websocket.util.SocketUtil;
34+
import org.junit.Test;
35+
36+
import java.io.IOException;
37+
import java.net.InetAddress;
38+
import java.net.InetSocketAddress;
39+
import java.nio.ByteBuffer;
40+
import java.util.Collection;
41+
import java.util.Collections;
42+
import java.util.HashSet;
43+
import java.util.List;
44+
import java.util.concurrent.CountDownLatch;
45+
46+
import static org.junit.Assert.*;
47+
48+
public class WebSocketServerTest {
49+
50+
@Test
51+
public void testConstructor() {
52+
List<Draft> draftCollection = Collections.<Draft>singletonList(new Draft_6455());
53+
Collection<WebSocket> webSocketCollection = new HashSet<WebSocket>();
54+
InetSocketAddress inetAddress = new InetSocketAddress(1337);
55+
56+
try {
57+
WebSocketServer server = new MyWebSocketServer(null, 1,draftCollection, webSocketCollection );
58+
fail("Should fail");
59+
} catch (IllegalArgumentException e) {
60+
//OK
61+
}
62+
try {
63+
WebSocketServer server = new MyWebSocketServer(inetAddress, 0,draftCollection, webSocketCollection );
64+
fail("Should fail");
65+
} catch (IllegalArgumentException e) {
66+
//OK
67+
}
68+
try {
69+
WebSocketServer server = new MyWebSocketServer(inetAddress, -1,draftCollection, webSocketCollection );
70+
fail("Should fail");
71+
} catch (IllegalArgumentException e) {
72+
//OK
73+
}
74+
try {
75+
WebSocketServer server = new MyWebSocketServer(inetAddress, Integer.MIN_VALUE, draftCollection, webSocketCollection );
76+
fail("Should fail");
77+
} catch (IllegalArgumentException e) {
78+
//OK
79+
}
80+
try {
81+
WebSocketServer server = new MyWebSocketServer(inetAddress, Integer.MIN_VALUE, draftCollection, webSocketCollection );
82+
fail("Should fail");
83+
} catch (IllegalArgumentException e) {
84+
//OK
85+
}
86+
try {
87+
WebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, null );
88+
fail("Should fail");
89+
} catch (IllegalArgumentException e) {
90+
//OK
91+
}
92+
93+
try {
94+
WebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, webSocketCollection );
95+
// OK
96+
} catch (IllegalArgumentException e) {
97+
fail("Should not fail");
98+
}
99+
try {
100+
WebSocketServer server = new MyWebSocketServer(inetAddress, 1, null, webSocketCollection );
101+
// OK
102+
} catch (IllegalArgumentException e) {
103+
fail("Should not fail");
104+
}
105+
}
106+
107+
108+
@Test
109+
public void testGetAddress() throws IOException {
110+
int port = SocketUtil.getAvailablePort();
111+
InetSocketAddress inetSocketAddress = new InetSocketAddress(port);
112+
MyWebSocketServer server = new MyWebSocketServer(port);
113+
assertEquals(inetSocketAddress, server.getAddress());
114+
}
115+
116+
@Test
117+
public void testGetDrafts() {
118+
List<Draft> draftCollection = Collections.<Draft>singletonList(new Draft_6455());
119+
Collection<WebSocket> webSocketCollection = new HashSet<WebSocket>();
120+
InetSocketAddress inetAddress = new InetSocketAddress(1337);
121+
MyWebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, webSocketCollection);
122+
assertEquals(1, server.getDraft().size());
123+
assertEquals(draftCollection.get(0), server.getDraft().get(0));
124+
}
125+
126+
@Test
127+
public void testGetPort() throws IOException, InterruptedException {
128+
int port = SocketUtil.getAvailablePort();
129+
CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
130+
MyWebSocketServer server = new MyWebSocketServer(port);
131+
assertEquals(port, server.getPort());
132+
server = new MyWebSocketServer(0, countServerDownLatch);
133+
assertEquals(0, server.getPort());
134+
server.start();
135+
countServerDownLatch.await();
136+
assertNotEquals(0, server.getPort());
137+
}
138+
139+
@Test
140+
public void testBroadcast() {
141+
MyWebSocketServer server = new MyWebSocketServer(1337);
142+
try {
143+
server.broadcast((byte[]) null, Collections.<WebSocket>emptyList());
144+
fail("Should fail");
145+
} catch (IllegalArgumentException e) {
146+
// OK
147+
}
148+
try {
149+
server.broadcast((ByteBuffer) null, Collections.<WebSocket>emptyList());
150+
fail("Should fail");
151+
} catch (IllegalArgumentException e) {
152+
// OK
153+
}
154+
try {
155+
server.broadcast((String) null, Collections.<WebSocket>emptyList());
156+
fail("Should fail");
157+
} catch (IllegalArgumentException e) {
158+
// OK
159+
}
160+
try {
161+
server.broadcast(new byte[] {(byte) 0xD0}, null);
162+
fail("Should fail");
163+
} catch (IllegalArgumentException e) {
164+
// OK
165+
}
166+
try {
167+
server.broadcast(ByteBuffer.wrap(new byte[] {(byte) 0xD0}), null);
168+
fail("Should fail");
169+
} catch (IllegalArgumentException e) {
170+
// OK
171+
}
172+
try {
173+
server.broadcast("", null);
174+
fail("Should fail");
175+
} catch (IllegalArgumentException e) {
176+
// OK
177+
}
178+
try {
179+
server.broadcast("", Collections.<WebSocket>emptyList());
180+
// OK
181+
} catch (IllegalArgumentException e) {
182+
fail("Should not fail");
183+
}
184+
}
185+
private static class MyWebSocketServer extends WebSocketServer {
186+
private CountDownLatch serverLatch = null;
187+
188+
public MyWebSocketServer(InetSocketAddress address , int decodercount , List<Draft> drafts , Collection<WebSocket> connectionscontainer) {
189+
super(address, decodercount, drafts, connectionscontainer);
190+
}
191+
public MyWebSocketServer(int port, CountDownLatch serverLatch) {
192+
super(new InetSocketAddress(port));
193+
this.serverLatch = serverLatch;
194+
}
195+
public MyWebSocketServer(int port) {
196+
this(port, null);
197+
}
198+
199+
@Override
200+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
201+
}
202+
203+
@Override
204+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
205+
}
206+
207+
@Override
208+
public void onMessage(WebSocket conn, String message) {
209+
210+
}
211+
212+
@Override
213+
public void onError(WebSocket conn, Exception ex) {
214+
ex.printStackTrace();
215+
}
216+
217+
@Override
218+
public void onStart() {
219+
if (serverLatch != null) {
220+
serverLatch.countDown();
221+
}
222+
}
223+
}
224+
}
225+

0 commit comments

Comments
 (0)