Skip to content

Commit 08be869

Browse files
committed
Code Quality
Reduce complexity Fix broken test
1 parent 67ff5f8 commit 08be869

File tree

6 files changed

+77
-46
lines changed

6 files changed

+77
-46
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.net.URI;
3737
import java.net.UnknownHostException;
3838
import java.nio.ByteBuffer;
39+
import java.security.KeyManagementException;
40+
import java.security.NoSuchAlgorithmException;
3941
import java.util.Collection;
4042
import java.util.Collections;
4143
import java.util.Map;
@@ -461,19 +463,7 @@ public void sendPing() {
461463
public void run() {
462464
InputStream istream;
463465
try {
464-
boolean upgradeSocketToSSLSocket = false;
465-
// Prioritise a proxy over a socket factory and apply the socketfactory later
466-
if (proxy != Proxy.NO_PROXY) {
467-
socket = new Socket(proxy);
468-
upgradeSocketToSSLSocket = true;
469-
} else if (socketFactory != null) {
470-
socket = socketFactory.createSocket();
471-
} else if (socket == null) {
472-
socket = new Socket(proxy);
473-
upgradeSocketToSSLSocket = true;
474-
} else if (socket.isClosed()) {
475-
throw new IOException();
476-
}
466+
boolean upgradeSocketToSSLSocket = prepareSocket();
477467

478468
socket.setTcpNoDelay(isTcpNoDelay());
479469
socket.setReuseAddress(isReuseAddr());
@@ -485,17 +475,7 @@ public void run() {
485475

486476
// if the socket is set by others we don't apply any TLS wrapper
487477
if (upgradeSocketToSSLSocket && "wss".equals(uri.getScheme())) {
488-
SSLSocketFactory factory;
489-
// Prioritise the provided socketfactory
490-
// Helps when using web debuggers like Fiddler Classic
491-
if (socketFactory != null && (socketFactory instanceof SSLSocketFactory)) {
492-
factory = (SSLSocketFactory) socketFactory;
493-
} else {
494-
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
495-
sslContext.init(null, null, null);
496-
factory = sslContext.getSocketFactory();
497-
}
498-
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
478+
upgradeSocketToSSL();
499479
}
500480

501481
if (socket instanceof SSLSocket) {
@@ -546,6 +526,38 @@ public void run() {
546526
connectReadThread = null;
547527
}
548528

529+
private void upgradeSocketToSSL()
530+
throws NoSuchAlgorithmException, KeyManagementException, IOException {
531+
SSLSocketFactory factory;
532+
// Prioritise the provided socketfactory
533+
// Helps when using web debuggers like Fiddler Classic
534+
if (socketFactory instanceof SSLSocketFactory) {
535+
factory = (SSLSocketFactory) socketFactory;
536+
} else {
537+
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
538+
sslContext.init(null, null, null);
539+
factory = sslContext.getSocketFactory();
540+
}
541+
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
542+
}
543+
544+
private boolean prepareSocket() throws IOException {
545+
boolean upgradeSocketToSSLSocket = false;
546+
// Prioritise a proxy over a socket factory and apply the socketfactory later
547+
if (proxy != Proxy.NO_PROXY) {
548+
socket = new Socket(proxy);
549+
upgradeSocketToSSLSocket = true;
550+
} else if (socketFactory != null) {
551+
socket = socketFactory.createSocket();
552+
} else if (socket == null) {
553+
socket = new Socket(proxy);
554+
upgradeSocketToSSLSocket = true;
555+
} else if (socket.isClosed()) {
556+
throw new IOException();
557+
}
558+
return upgradeSocketToSSLSocket;
559+
}
560+
549561
/**
550562
* Apply specific SSLParameters If you override this method make sure to always call
551563
* super.onSetSSLParameters() to ensure the hostname validation is active

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void stop(int timeout) throws InterruptedException {
287287
}
288288
}
289289

290-
public void stop() throws IOException, InterruptedException {
290+
public void stop() throws InterruptedException {
291291
stop(0);
292292
}
293293

@@ -538,10 +538,8 @@ private boolean doRead(SelectionKey key, Iterator<SelectionKey> i)
538538
private void doWrite(SelectionKey key) throws WrappedIOException {
539539
WebSocketImpl conn = (WebSocketImpl) key.attachment();
540540
try {
541-
if (SocketChannelIOHelper.batch(conn, conn.getChannel())) {
542-
if (key.isValid()) {
543-
key.interestOps(SelectionKey.OP_READ);
544-
}
541+
if (SocketChannelIOHelper.batch(conn, conn.getChannel()) && key.isValid()) {
542+
key.interestOps(SelectionKey.OP_READ);
545543
}
546544
} catch (IOException e) {
547545
throw new WrappedIOException(conn, e);
@@ -693,9 +691,6 @@ private void handleFatal(WebSocket conn, Exception e) {
693691
}
694692
try {
695693
stop();
696-
} catch (IOException e1) {
697-
log.error("Error during shutdown", e1);
698-
onError(null, e1);
699694
} catch (InterruptedException e1) {
700695
Thread.currentThread().interrupt();
701696
log.error("Interrupt during stop", e);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
3434
org.java_websocket.protocols.ProtocolTest.class,
35-
org.java_websocket.protocols.ProtoclHandshakeRejectionTest.class
35+
ProtocolHandshakeRejectionTest.class
3636
})
3737
/**
3838
* Start all tests for protocols

src/test/java/org/java_websocket/protocols/ProtoclHandshakeRejectionTest.java renamed to src/test/java/org/java_websocket/protocols/ProtocolHandshakeRejectionTest.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import org.junit.BeforeClass;
5252
import org.junit.Test;
5353

54-
public class ProtoclHandshakeRejectionTest {
54+
public class ProtocolHandshakeRejectionTest {
5555

5656
private static final String additionalHandshake = "HTTP/1.1 101 Websocket Connection Upgrade\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n";
5757
private static Thread thread;
@@ -355,31 +355,31 @@ public void testHandshakeRejectionTestCase13() throws Exception {
355355

356356
@Test(timeout = 5000)
357357
public void testHandshakeRejectionTestCase14() throws Exception {
358-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
358+
ArrayList<IProtocol> protocols = new ArrayList<>();
359359
protocols.add(new Protocol("chat"));
360360
protocols.add(new Protocol("chat2"));
361361
testProtocolRejection(14, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
362362
}
363363

364364
@Test(timeout = 5000)
365365
public void testHandshakeRejectionTestCase15() throws Exception {
366-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
366+
ArrayList<IProtocol> protocols = new ArrayList<>();
367367
protocols.add(new Protocol("chat"));
368368
protocols.add(new Protocol("chat2"));
369369
testProtocolRejection(15, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
370370
}
371371

372372
@Test(timeout = 5000)
373373
public void testHandshakeRejectionTestCase16() throws Exception {
374-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
374+
ArrayList<IProtocol> protocols = new ArrayList<>();
375375
protocols.add(new Protocol("chat"));
376376
protocols.add(new Protocol("chat2"));
377377
testProtocolRejection(16, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
378378
}
379379

380380
@Test(timeout = 5000)
381381
public void testHandshakeRejectionTestCase17() throws Exception {
382-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
382+
ArrayList<IProtocol> protocols = new ArrayList<>();
383383
protocols.add(new Protocol("chat"));
384384
protocols.add(new Protocol(""));
385385
testProtocolRejection(17, new Draft_6455(Collections.<IExtension>emptyList(), protocols));
@@ -402,7 +402,7 @@ public void testHandshakeRejectionTestCase20() throws Exception {
402402

403403
@Test(timeout = 5000)
404404
public void testHandshakeRejectionTestCase21() throws Exception {
405-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
405+
ArrayList<IProtocol> protocols = new ArrayList<>();
406406
protocols.add(new Protocol("chat1"));
407407
protocols.add(new Protocol("chat2"));
408408
protocols.add(new Protocol("chat3"));
@@ -411,7 +411,7 @@ public void testHandshakeRejectionTestCase21() throws Exception {
411411

412412
@Test(timeout = 5000)
413413
public void testHandshakeRejectionTestCase22() throws Exception {
414-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
414+
ArrayList<IProtocol> protocols = new ArrayList<>();
415415
protocols.add(new Protocol("chat2"));
416416
protocols.add(new Protocol("chat3"));
417417
protocols.add(new Protocol("chat1"));
@@ -420,7 +420,7 @@ public void testHandshakeRejectionTestCase22() throws Exception {
420420

421421
@Test(timeout = 5000)
422422
public void testHandshakeRejectionTestCase23() throws Exception {
423-
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
423+
ArrayList<IProtocol> protocols = new ArrayList<>();
424424
protocols.add(new Protocol("chat3"));
425425
protocols.add(new Protocol("chat2"));
426426
protocols.add(new Protocol("chat1"));
@@ -463,7 +463,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
463463
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
464464
final int finalI = i;
465465
final boolean[] threadReturned = {false};
466-
WebSocketClient webSocketClient = new WebSocketClient(
466+
final WebSocketClient webSocketClient = new WebSocketClient(
467467
new URI("ws://localhost:" + port + "/" + finalI), draft) {
468468
@Override
469469
public void onOpen(ServerHandshake handshakedata) {
@@ -509,6 +509,7 @@ public void onClose(int code, String reason, boolean remote) {
509509
case 2:
510510
case 3:
511511
case 4:
512+
case 17:
512513
case 20:
513514
case 24:
514515
case 25:
@@ -533,10 +534,9 @@ public void onClose(int code, String reason, boolean remote) {
533534
case 6:
534535
case 7:
535536
case 8:
536-
case 17:
537+
case 14:
537538
assertEquals("chat", getProtocol().getProvidedProtocol());
538539
break;
539-
case 14:
540540
case 22:
541541
assertEquals("chat2", getProtocol().getProvidedProtocol());
542542
break;
@@ -588,9 +588,24 @@ public void onError(Exception ex) {
588588
fail("There should not be an exception");
589589
}
590590
};
591-
Thread finalThread = new Thread(webSocketClient);
591+
final AssertionError[] exc = new AssertionError[1];
592+
exc[0] = null;
593+
Thread finalThread = new Thread(new Runnable() {
594+
@Override
595+
public void run() {
596+
try {
597+
webSocketClient.run();
598+
}catch(AssertionError e){
599+
exc[0] = e;
600+
}
601+
}
602+
603+
});
592604
finalThread.start();
593605
finalThread.join();
606+
if (exc[0] != null) {
607+
throw exc[0];
608+
}
594609

595610
if (!threadReturned[0]) {
596611
fail("Error");

src/test/java/org/java_websocket/server/AllServerTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525

2626
package org.java_websocket.server;
2727

28+
import org.java_websocket.protocols.ProtocolHandshakeRejectionTest;
2829
import org.junit.runner.RunWith;
2930
import org.junit.runners.Suite;
3031

3132

3233
@RunWith(Suite.class)
3334
@Suite.SuiteClasses({
3435
org.java_websocket.server.DefaultWebSocketServerFactoryTest.class,
35-
org.java_websocket.protocols.ProtoclHandshakeRejectionTest.class
36+
ProtocolHandshakeRejectionTest.class
3637
})
3738
/**
3839
* Start all tests for the server

src/test/java/org/java_websocket/server/WebSocketServerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ public void testGetPort() throws IOException, InterruptedException {
142142
assertNotEquals(0, server.getPort());
143143
}
144144

145+
@Test
146+
public void testMaxPendingConnections() {
147+
MyWebSocketServer server = new MyWebSocketServer(1337);
148+
assertEquals(server.getMaxPendingConnections(), -1);
149+
server.setMaxPendingConnections(10);
150+
assertEquals(server.getMaxPendingConnections(), 10);
151+
}
152+
145153
@Test
146154
public void testBroadcast() {
147155
MyWebSocketServer server = new MyWebSocketServer(1337);

0 commit comments

Comments
 (0)