diff --git a/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/socketexception/SslServer.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/socketexception/SslServer.java new file mode 100644 index 0000000000..ae783fa8bd --- /dev/null +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/socketexception/SslServer.java @@ -0,0 +1,32 @@ +package com.baeldung.socketexception; + +import java.io.IOException; + +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + +public class SslServer { + + public static void createSSLSocketWithEnabledProtocols(SSLServerSocketFactory socketFactory, int port, String[] enabledProtocols) { + SSLServerSocket serverSocket = null; + + try { + serverSocket = (SSLServerSocket) socketFactory.createServerSocket(port); + // Set the enabled protocols + serverSocket.setEnabledProtocols(enabledProtocols); + System.out.println("Server is running on port " + port); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (serverSocket != null) { + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + +} diff --git a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java index 08b21c6299..197046d273 100644 --- a/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/socketexception/SocketExceptionHandlingUnitTest.java @@ -1,25 +1,33 @@ package com.baeldung.socketexception; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import java.io.IOException; import java.net.SocketException; import java.util.concurrent.Executors; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + import org.junit.BeforeClass; import org.junit.Test; public class SocketExceptionHandlingUnitTest { + private static final int PORT = 6699; @BeforeClass public static void runServer() throws IOException, InterruptedException { Executors.newSingleThreadExecutor() - .submit(() -> new SocketServer().start(6699)); + .submit(() -> new SocketServer().start(PORT)); Thread.sleep(100); } @Test public void givenRunningServer_whenConnectToClosedSocket_thenHandleException() throws IOException { SocketClient client = new SocketClient(); - client.startConnection("127.0.0.1", 6699); + client.startConnection("127.0.0.1", PORT); try { client.sendMessage("hi"); client.sendMessage("hi again"); @@ -28,4 +36,22 @@ public class SocketExceptionHandlingUnitTest { } } + @Test + public void givenRunningServer_whenConnectToSocket_thenHandleConnectionResetException() throws IOException { + // Enable multiple SSL/TLS protocols + String[] enabledProtocols = { "TLSv1.2", "TLSv1.3", "TLSv1.1", "TLSv1", "SSLv3", "SSLv3" }; + SSLServerSocketFactory mockFactory = mock(SSLServerSocketFactory.class); + SSLServerSocket mockServerSocket = mock(SSLServerSocket.class); + + // Set up the mock factory to return the mock server socket + when(mockFactory.createServerSocket(PORT)).thenReturn(mockServerSocket); + + // Call the method being tested + SslServer.createSSLSocketWithEnabledProtocols(mockFactory, PORT, enabledProtocols); + + // Verify that setEnabledProtocols and close were called + verify(mockServerSocket).setEnabledProtocols(enabledProtocols); + verify(mockServerSocket).close(); + } + }