Skip to content

Commit

Permalink
Merge pull request #833 from marci4/SonarQubeFix
Browse files Browse the repository at this point in the history
Fix some sonarqube errors
  • Loading branch information
marci4 authored Jan 9, 2019
2 parents fd4d55c + 85a5d3d commit 4dbb2a3
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/java_websocket/SocketChannelIOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

public class SocketChannelIOHelper {

private SocketChannelIOHelper() {
throw new IllegalStateException("Utility class");
}

public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel channel ) throws IOException {
buf.clear();
int read = channel.read( buf );
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public class WebSocketImpl implements WebSocket {
/**
* Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
**/
private volatile WebSocketWorker workerThread;
private WebSocketWorker workerThread;
/**
* When true no further frames may be submitted to be sent
*/
private volatile boolean flushandclosestate = false;
private boolean flushandclosestate = false;

/**
* The current state of the connection
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i) throws Interr
WebSocketImpl conn = (WebSocketImpl) key.attachment();
ByteBuffer buf = takeBuffer();
if(conn.getChannel() == null){
if( key != null )
key.cancel();
key.cancel();

handleIOException( key, conn, new IOException() );
return false;
Expand All @@ -462,10 +461,8 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i) throws Interr
conn.inQueue.put( buf );
queue( conn );
i.remove();
if( conn.getChannel() instanceof WrappedByteChannel ) {
if( ( (WrappedByteChannel) conn.getChannel() ).isNeedRead() ) {
iqueue.add( conn );
}
if( conn.getChannel() instanceof WrappedByteChannel && ( (WrappedByteChannel) conn.getChannel() ).isNeedRead() ) {
iqueue.add( conn );
}
} else {
pushBuffer(buf);
Expand All @@ -488,8 +485,9 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i) throws Interr
private void doWrite(SelectionKey key) throws IOException {
WebSocketImpl conn = (WebSocketImpl) key.attachment();
if( SocketChannelIOHelper.batch( conn, conn.getChannel() ) ) {
if( key.isValid() )
key.interestOps( SelectionKey.OP_READ );
if( key.isValid() ) {
key.interestOps(SelectionKey.OP_READ);
}
}
}

Expand Down Expand Up @@ -690,15 +688,11 @@ protected boolean removeConnection( WebSocket ws ) {
log.trace("Removing connection which is not in the connections collection! Possible no handshake recieved! {}", ws);
}
}
if( isclosed.get() && connections.size() == 0 ) {
if( isclosed.get() && connections.isEmpty() ) {
selectorthread.interrupt();
}
return removed;
}
@Override
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request ) throws InvalidDataException {
return super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
}

/**
* @see #removeConnection(WebSocket)
Expand Down
225 changes: 225 additions & 0 deletions src/test/java/org/java_websocket/server/WebSocketServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
* Copyright (c) 2010-2019 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/

package org.java_websocket.server;

import org.java_websocket.WebSocket;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.util.SocketUtil;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.*;

public class WebSocketServerTest {

@Test
public void testConstructor() {
List<Draft> draftCollection = Collections.<Draft>singletonList(new Draft_6455());
Collection<WebSocket> webSocketCollection = new HashSet<WebSocket>();
InetSocketAddress inetAddress = new InetSocketAddress(1337);

try {
WebSocketServer server = new MyWebSocketServer(null, 1,draftCollection, webSocketCollection );
fail("Should fail");
} catch (IllegalArgumentException e) {
//OK
}
try {
WebSocketServer server = new MyWebSocketServer(inetAddress, 0,draftCollection, webSocketCollection );
fail("Should fail");
} catch (IllegalArgumentException e) {
//OK
}
try {
WebSocketServer server = new MyWebSocketServer(inetAddress, -1,draftCollection, webSocketCollection );
fail("Should fail");
} catch (IllegalArgumentException e) {
//OK
}
try {
WebSocketServer server = new MyWebSocketServer(inetAddress, Integer.MIN_VALUE, draftCollection, webSocketCollection );
fail("Should fail");
} catch (IllegalArgumentException e) {
//OK
}
try {
WebSocketServer server = new MyWebSocketServer(inetAddress, Integer.MIN_VALUE, draftCollection, webSocketCollection );
fail("Should fail");
} catch (IllegalArgumentException e) {
//OK
}
try {
WebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, null );
fail("Should fail");
} catch (IllegalArgumentException e) {
//OK
}

try {
WebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, webSocketCollection );
// OK
} catch (IllegalArgumentException e) {
fail("Should not fail");
}
try {
WebSocketServer server = new MyWebSocketServer(inetAddress, 1, null, webSocketCollection );
// OK
} catch (IllegalArgumentException e) {
fail("Should not fail");
}
}


@Test
public void testGetAddress() throws IOException {
int port = SocketUtil.getAvailablePort();
InetSocketAddress inetSocketAddress = new InetSocketAddress(port);
MyWebSocketServer server = new MyWebSocketServer(port);
assertEquals(inetSocketAddress, server.getAddress());
}

@Test
public void testGetDrafts() {
List<Draft> draftCollection = Collections.<Draft>singletonList(new Draft_6455());
Collection<WebSocket> webSocketCollection = new HashSet<WebSocket>();
InetSocketAddress inetAddress = new InetSocketAddress(1337);
MyWebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, webSocketCollection);
assertEquals(1, server.getDraft().size());
assertEquals(draftCollection.get(0), server.getDraft().get(0));
}

@Test
public void testGetPort() throws IOException, InterruptedException {
int port = SocketUtil.getAvailablePort();
CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
MyWebSocketServer server = new MyWebSocketServer(port);
assertEquals(port, server.getPort());
server = new MyWebSocketServer(0, countServerDownLatch);
assertEquals(0, server.getPort());
server.start();
countServerDownLatch.await();
assertNotEquals(0, server.getPort());
}

@Test
public void testBroadcast() {
MyWebSocketServer server = new MyWebSocketServer(1337);
try {
server.broadcast((byte[]) null, Collections.<WebSocket>emptyList());
fail("Should fail");
} catch (IllegalArgumentException e) {
// OK
}
try {
server.broadcast((ByteBuffer) null, Collections.<WebSocket>emptyList());
fail("Should fail");
} catch (IllegalArgumentException e) {
// OK
}
try {
server.broadcast((String) null, Collections.<WebSocket>emptyList());
fail("Should fail");
} catch (IllegalArgumentException e) {
// OK
}
try {
server.broadcast(new byte[] {(byte) 0xD0}, null);
fail("Should fail");
} catch (IllegalArgumentException e) {
// OK
}
try {
server.broadcast(ByteBuffer.wrap(new byte[] {(byte) 0xD0}), null);
fail("Should fail");
} catch (IllegalArgumentException e) {
// OK
}
try {
server.broadcast("", null);
fail("Should fail");
} catch (IllegalArgumentException e) {
// OK
}
try {
server.broadcast("", Collections.<WebSocket>emptyList());
// OK
} catch (IllegalArgumentException e) {
fail("Should not fail");
}
}
private static class MyWebSocketServer extends WebSocketServer {
private CountDownLatch serverLatch = null;

public MyWebSocketServer(InetSocketAddress address , int decodercount , List<Draft> drafts , Collection<WebSocket> connectionscontainer) {
super(address, decodercount, drafts, connectionscontainer);
}
public MyWebSocketServer(int port, CountDownLatch serverLatch) {
super(new InetSocketAddress(port));
this.serverLatch = serverLatch;
}
public MyWebSocketServer(int port) {
this(port, null);
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}

@Override
public void onMessage(WebSocket conn, String message) {

}

@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}

@Override
public void onStart() {
if (serverLatch != null) {
serverLatch.countDown();
}
}
}
}

0 comments on commit 4dbb2a3

Please sign in to comment.