Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ChatClient implements Runnable {
private DataOutputStream streamOut = null;
private SocketHandler client = null;

private ChatClient(String serverName, int serverPort) {
protected ChatClient(String serverName, int serverPort) {
System.out.println("Establishing connection. Please wait ...");
try {
socket = new Socket(serverName, serverPort);
Expand All @@ -26,6 +26,10 @@ private ChatClient(String serverName, int serverPort) {
}
}

protected Socket getSocket() {
return socket;
}

public void run() {
while (thread != null) {
try {
Expand All @@ -38,7 +42,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();
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/com/agilefaqs/samples/chatbroadcast/ChatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;

import static spark.Spark.get;

Expand All @@ -16,7 +17,7 @@ class ChatServer implements Runnable {
private int clientCount = 0;
DataStore dataStore = new DataStore();

private ChatServer(int port) {
protected ChatServer(int port) {
try {
JsonTransformer transformer = new JsonTransformer();
get("/hello", "application/json", (req, res) -> "Hello World",
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -101,19 +114,21 @@ 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);
try {
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() {
Expand Down Expand Up @@ -189,5 +204,10 @@ void close() throws IOException {
if (streamIn != null) streamIn.close();
if (streamOut != null) streamOut.close();
}

public long getId() {
return id;
}

}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}



}