From 4177024acfacce76b86b9888106e909e92a5370c Mon Sep 17 00:00:00 2001 From: Temp Date: Thu, 7 Jul 2016 13:08:15 +0530 Subject: [PATCH 1/2] Refactored a bit and Added test cases --- .../samples/chatbroadcast/ChatClient.java | 7 +- .../samples/chatbroadcast/ChatServer.java | 30 +++++++-- .../samples/chatbroadcast/ChatClientTest.java | 30 +++++++++ .../samples/chatbroadcast/ChatServerTest.java | 65 +++++++++++++++++-- 4 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/agilefaqs/samples/chatbroadcast/ChatClientTest.java diff --git a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java index aee9f40..ed2bf6c 100644 --- a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java +++ b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java @@ -13,7 +13,7 @@ class ChatClient implements Runnable { private DataOutputStream streamOut = null; private SocketHandler client = null; - private ChatClient(String serverName, int serverPort) { + public ChatClient(String serverName, int serverPort) { System.out.println("Establishing connection. Please wait ..."); try { socket = new Socket(serverName, serverPort); @@ -26,6 +26,9 @@ private ChatClient(String serverName, int serverPort) { } } + public Socket getSocket() { + return socket; + } public void run() { while (thread != null) { try { @@ -38,7 +41,7 @@ public void run() { } } - private void handle(String msg) { + protected void handle(String msg) { if (msg.equals(".bye")) { System.out.println("Good bye. Press RETURN to exit ..."); stop(); diff --git a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java index 22ee7d8..4c462a0 100644 --- a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java +++ b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java @@ -6,6 +6,7 @@ import java.io.*; import java.net.ServerSocket; import java.net.Socket; +import java.sql.SQLException; import static spark.Spark.get; @@ -16,7 +17,7 @@ class ChatServer implements Runnable { private int clientCount = 0; DataStore dataStore = new DataStore(); - private ChatServer(int port) { + public ChatServer(int port) { try { JsonTransformer transformer = new JsonTransformer(); get("/hello", "application/json", (req, res) -> "Hello World", @@ -64,7 +65,10 @@ public void run() { } } - private int findClient(int id) { + protected int getClientCount() { + return clientCount; + } + protected int findClient(int id) { for (int i = 0; i < clientCount; i++) if (clients[i].id == id) return i; @@ -79,11 +83,20 @@ private synchronized void handle(Integer id, String input) { clients[findClient(id)].send(".bye"); remove(id); } else { - for (int i = 0; i < clientCount; i++) - clients[i].send(id + ": " + input); + for (int i = 0; i < clientCount; i++) { + clients[i].send(id + ": " + input); + try { + dataStore.add(id + ": " + input); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } + private synchronized void remove(int ID) { int pos = findClient(ID); if (pos >= 0) { @@ -101,7 +114,7 @@ private synchronized void remove(int ID) { } } - private void addThread(Socket socket) { + public synchronized boolean addThread(Socket socket) { if (clientCount < clients.length) { System.out.println("Client accepted: " + socket); clients[clientCount] = new SocketHandler(this, socket); @@ -109,11 +122,13 @@ private void addThread(Socket socket) { clients[clientCount].open(); clients[clientCount].start(); clientCount++; + return true; } catch (IOException ioe) { System.out.println("Error opening thread: " + ioe); } } else System.out.println("Client refused: maximum " + clients.length + " reached."); + return false; } private void start() { @@ -189,5 +204,10 @@ void close() throws IOException { if (streamIn != null) streamIn.close(); if (streamOut != null) streamOut.close(); } + + public long getId() { + return id; + } + } } \ No newline at end of file diff --git a/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatClientTest.java b/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatClientTest.java new file mode 100644 index 0000000..980227d --- /dev/null +++ b/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatClientTest.java @@ -0,0 +1,30 @@ +package com.agilefaqs.samples.chatbroadcast; + +import static org.junit.Assert.fail; + +import org.junit.Test; + +public class ChatClientTest { + + @Test + public void testNullMessageInHandle() { + ChatClient client = new ChatClient("localhost", 9090); + try { + client.handle(null); + } catch (Exception e) { + e.printStackTrace(); + fail("handle(msg) should handle null messages"); + } + } + + @Test + public void testValidMessageInHandle() { + ChatClient client = new ChatClient("localhost", 9090); + try { + client.handle("Hello"); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception should not be caught while handle(msg) is called"); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatServerTest.java b/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatServerTest.java index e61972a..87b8cb2 100644 --- a/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatServerTest.java +++ b/src/test/java/com/agilefaqs/samples/chatbroadcast/ChatServerTest.java @@ -1,12 +1,69 @@ package com.agilefaqs.samples.chatbroadcast; +import java.io.IOException; +import java.net.Socket; +import org.junit.BeforeClass; import org.junit.Test; +import static org.junit.Assert.*; public class ChatServerTest { - @Test - public void sampleTest() throws Exception { -// fail("Add test"); - } + private static ChatServer server; + private static int portNo; + private static String host; + + @BeforeClass + public static void setUp() { + System.out.println(System.getProperty("user.dir")); + portNo = 9090; + server = new ChatServer(portNo); + host = "localhost"; + } + + @Test + public void testAddNullSocket() { + int initialClientCount = server.getClientCount(); + try { + Socket socket = null; + assertFalse("Null socket should not get added", server.addThread(socket)); + } catch (Exception e) { + fail("add thread should handle null sockets"); + } + assertEquals("Client Count should not be incremented on adding null socket", initialClientCount , server.getClientCount()); + } + + @Test + public void testAddValidSocket() { + int initialClientCount = server.getClientCount(); + Socket socket; + try { + socket = new Socket(host, portNo); + assertFalse("socket should get added", server.addThread(socket)); + } catch (IOException e) { + e.printStackTrace(); + } + assertEquals("Client Count should be incremented on adding socket", initialClientCount + 1, server.getClientCount()); + } + + @Test + public void testFindClientForInvalidId() { + assertEquals("-1 should be returned in case of invalid id", -1, server.findClient(-100)); + } + + @Test + public void testFindClientForValidId() { + Socket socket = null; + try { + socket = new Socket(host, portNo); + } catch (IOException e) { + e.printStackTrace(); + fail("failed to create socket"); + } + server.addThread(socket); + assertNotEquals("-1 should be returned in case of invalid id", -1, server.findClient(portNo)); + } + + + } \ No newline at end of file From 3306d0a4d0697ab072446bc8661fc09efc01ca76 Mon Sep 17 00:00:00 2001 From: Temp Date: Thu, 7 Jul 2016 13:12:29 +0530 Subject: [PATCH 2/2] Changing access modifier --- .../java/com/agilefaqs/samples/chatbroadcast/ChatClient.java | 5 +++-- .../java/com/agilefaqs/samples/chatbroadcast/ChatServer.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java index ed2bf6c..fe6a4f5 100644 --- a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java +++ b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatClient.java @@ -13,7 +13,7 @@ class ChatClient implements Runnable { private DataOutputStream streamOut = null; private SocketHandler client = null; - public ChatClient(String serverName, int serverPort) { + protected ChatClient(String serverName, int serverPort) { System.out.println("Establishing connection. Please wait ..."); try { socket = new Socket(serverName, serverPort); @@ -26,9 +26,10 @@ public ChatClient(String serverName, int serverPort) { } } - public Socket getSocket() { + protected Socket getSocket() { return socket; } + public void run() { while (thread != null) { try { diff --git a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java index 4c462a0..19f7575 100644 --- a/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java +++ b/src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java @@ -17,7 +17,7 @@ class ChatServer implements Runnable { private int clientCount = 0; DataStore dataStore = new DataStore(); - public ChatServer(int port) { + protected ChatServer(int port) { try { JsonTransformer transformer = new JsonTransformer(); get("/hello", "application/json", (req, res) -> "Hello World",