From 78ff9a2e77a10f7a0a4e464cc33841a6397c711d Mon Sep 17 00:00:00 2001 From: Luce Date: Tue, 20 Jan 2015 09:16:03 +0100 Subject: [PATCH 1/2] Tests aangemaakt --- .../fourcharm/server/ClientHandlerTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java diff --git a/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java b/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java new file mode 100644 index 0000000..a4d3795 --- /dev/null +++ b/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2015. Luce Sandfort and Wouter Timmermans + */ + +package com.lucwo.fourcharm.server; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ClientHandlerTest { + + @Before + public void setUp() throws Exception { + + } + + @Test + public void testGetName() throws Exception { + + } + + @Test + public void testSetName() throws Exception { + + } + + @Test + public void testGetClient() throws Exception { + + } + + @Test + public void testGetClientGroup() throws Exception { + + } + + @Test + public void testSetClientGroup() throws Exception { + + } + + @Test + public void testJoin() throws Exception { + + } + + @Test + public void testReady() throws Exception { + + } + + @Test + public void testDoMove() throws Exception { + + } + + @Test + public void testError() throws Exception { + + } +} \ No newline at end of file From e93c1fb1b8752815652d13adfb9ad428aaf24d4d Mon Sep 17 00:00:00 2001 From: Luce Date: Tue, 20 Jan 2015 11:26:36 +0100 Subject: [PATCH 2/2] Testen aangemaakt en aangepast --- .../lucwo/fourcharm/server/ClientGroup.java | 20 +++++- .../lucwo/fourcharm/server/ClientHandler.java | 11 ++- .../fourcharm/server/FourCharmServer.java | 38 +++++++++-- .../com/lucwo/fourcharm/server/GameGroup.java | 14 +++- .../lucwo/fourcharm/server/LobbyGroup.java | 4 -- .../lucwo/fourcharm/server/PreLobbyGroup.java | 3 - .../fourcharm/server/ClientHandlerTest.java | 68 ++++++++++++++++--- .../fourcharm/server/FourCharmServerTest.java | 51 ++++++++++++++ .../lucwo/fourcharm/server/GameGroupTest.java | 46 +++++++++++++ .../fourcharm/server/LobbyGroupTest.java | 45 ++++++++++++ .../fourcharm/server/PreLobbyGroupTest.java | 54 +++++++++++++++ 11 files changed, 331 insertions(+), 23 deletions(-) create mode 100644 src/test/java/com/lucwo/fourcharm/server/FourCharmServerTest.java create mode 100644 src/test/java/com/lucwo/fourcharm/server/GameGroupTest.java create mode 100644 src/test/java/com/lucwo/fourcharm/server/LobbyGroupTest.java create mode 100644 src/test/java/com/lucwo/fourcharm/server/PreLobbyGroupTest.java diff --git a/src/main/java/com/lucwo/fourcharm/server/ClientGroup.java b/src/main/java/com/lucwo/fourcharm/server/ClientGroup.java index 9fe77ba..b636fdb 100644 --- a/src/main/java/com/lucwo/fourcharm/server/ClientGroup.java +++ b/src/main/java/com/lucwo/fourcharm/server/ClientGroup.java @@ -25,6 +25,12 @@ public ClientGroup() { clientCollection = new HashMap<>(); } + /** + * Checks if the name of the client already exists. + * + * @param name the name that will be checked + * @return true if the name already exists, false if not + */ public boolean clientNameExists(String name) { return clientCollection.keySet().contains(name); } @@ -68,7 +74,6 @@ public Collection getClients() { /** * Handles the join command from a client. The implementation is left to Classes * which extend ClientGroup. - * * @param client The client which performed this command. * @param pName Player name * @param gNumber Group number @@ -77,13 +82,24 @@ public Collection getClients() { public abstract void join(ClientHandler client, String pName, int gNumber, Set exts) throws C4Exception; + /** + * Makes a move + * @param client the client that will make the move + * @param col the column the move is about + * @throws C4Exception + */ public abstract void doMove(ClientHandler client, int col) throws C4Exception; + + /** + * If the client is ready to start a game, this method will be called. + * @param client the client that is ready to start a game + * @throws C4Exception + */ public abstract void ready(ClientHandler client) throws C4Exception; /** * Handles the removal of a client from the group. - * * @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group. */ public abstract void removeClientCallback(ClientHandler client); diff --git a/src/main/java/com/lucwo/fourcharm/server/ClientHandler.java b/src/main/java/com/lucwo/fourcharm/server/ClientHandler.java index f997a66..b6e69cb 100644 --- a/src/main/java/com/lucwo/fourcharm/server/ClientHandler.java +++ b/src/main/java/com/lucwo/fourcharm/server/ClientHandler.java @@ -50,7 +50,6 @@ public void setName(String naam) { /** * Gives the client. - * * @return the client */ public CoreClient.Client getClient() { @@ -112,6 +111,13 @@ public void doMove(int col) throws C4Exception { group.doMove(this, col); } + /** + * Gives an error. + * + * @param errorCode the code of the error + * @param message the message about the error + * @throws C4Exception + */ @Override public void error(int errorCode, String message) throws C4Exception { Logger.getGlobal().info(errorCode + " " + message); @@ -133,6 +139,9 @@ public void run() { handleClient(); } + /** + * TODO: Javadoc schrijven voor handleClient() + */ public void handleClient() { final String m_name = "handleClient"; diff --git a/src/main/java/com/lucwo/fourcharm/server/FourCharmServer.java b/src/main/java/com/lucwo/fourcharm/server/FourCharmServer.java index 8f13d46..a8567ff 100644 --- a/src/main/java/com/lucwo/fourcharm/server/FourCharmServer.java +++ b/src/main/java/com/lucwo/fourcharm/server/FourCharmServer.java @@ -18,21 +18,30 @@ public class FourCharmServer { private ClientGroup preLobby; private Collection games; private boolean running; + private int poort; public FourCharmServer(int port) { lobby = new LobbyGroup(this); preLobby = new PreLobbyGroup(lobby, this); games = new ArrayList<>(); running = true; - startServer(port); + poort = port; } public static void main(String[] args) { - new FourCharmServer(8080); + FourCharmServer server = new FourCharmServer(8080); + server.startServer(); } + /** + * Checks if there is already a client with the same name. + * + * @param name the name that will be checked + * @return true if there exists another client with the same name, + * false if there does not exist another client with the same name. + */ public boolean hasClientWithName(String name) { boolean nameExistsinGame = false; for (ClientGroup cG : games) { @@ -44,12 +53,16 @@ public boolean hasClientWithName(String name) { return nameExistsinGame || lobby.clientNameExists(name); } - public void startServer(int port) { + /** + * Starts the server. + */ + public void startServer() { Logger.getGlobal().info("Starting Fourcharm server"); + try { - ServerSocket ss = new ServerSocket(port); + ServerSocket ss = new ServerSocket(poort); while (running) { Socket sock = ss.accept(); ClientHandler client = new ClientHandler(sock); @@ -66,18 +79,35 @@ public void startServer(int port) { } + /** + * Adds a game to the GameGroup + * + * @param game the game that will be added + */ public void addGame(GameGroup game) { games.add(game); } + /** + * Removes a game from the GameGroup whenever a game is finished, + * or when a game stopped. + * @param game the game that will be removed + */ public void removeGame(GameGroup game) { games.remove(game); } + /** + * Makes sure the server will stop. + */ public void stop() { running = false; } + /** + * Gives the specific lobby. + * @return the lobby you've asked for + */ public ClientGroup getLobby() { return lobby; } diff --git a/src/main/java/com/lucwo/fourcharm/server/GameGroup.java b/src/main/java/com/lucwo/fourcharm/server/GameGroup.java index 5ff39c2..9ef300c 100644 --- a/src/main/java/com/lucwo/fourcharm/server/GameGroup.java +++ b/src/main/java/com/lucwo/fourcharm/server/GameGroup.java @@ -71,6 +71,13 @@ public void join(ClientHandler client, String pName, int gNumber, Set throw new InvalidCommandError("You are not allowed to use this command now."); } + /** + * Makes a move for a specific client + * + * @param client the client that is about to make a move + * @param col the column the move will be about + * @throws C4Exception + */ @Override public void doMove(ClientHandler client, int col) throws C4Exception { @@ -105,7 +112,6 @@ public void ready(ClientHandler client) throws C4Exception { /** * Handles the removal of a client from the group. - * * @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group. */ @Override @@ -121,12 +127,18 @@ public void removeClientCallback(ClientHandler client) { endGame(); } + /** + * Starts a new game. + */ public void startGame() { Thread gameThread = new Thread(game); gameThread.start(); } + /** + * Ends a game that started before. + */ private void endGame() { String winnerName = null; diff --git a/src/main/java/com/lucwo/fourcharm/server/LobbyGroup.java b/src/main/java/com/lucwo/fourcharm/server/LobbyGroup.java index 7fb2eac..82e4fdd 100644 --- a/src/main/java/com/lucwo/fourcharm/server/LobbyGroup.java +++ b/src/main/java/com/lucwo/fourcharm/server/LobbyGroup.java @@ -30,7 +30,6 @@ public LobbyGroup(FourCharmServer theServer) { /** * ServerHandler wants to go from the PreLobbyGroup to the LobbyGroup. - * * @param client The client which performed this command. * @param pName Player name * @param gNumber Group number @@ -45,7 +44,6 @@ public void join(ClientHandler client, String pName, int gNumber, Set /** * Makes a move if a client is in a game. This is not allowed in the LobbyGroup. - * * @param client the client that wants to make a move. * @param col the column number the clients wants to use to make a move * @throws C4Exception throws InvalidCommandError because this command is not allowed @@ -59,7 +57,6 @@ public void doMove(ClientHandler client, int col) throws C4Exception { /** * Sets the status of the player to ready. If there is another player ready as well, * a new game will be started. - * * @param client the client that wants to play a game * @throws C4Exception */ @@ -82,7 +79,6 @@ public synchronized void ready(ClientHandler client) throws C4Exception { /** * Handles the removal of a client from the group. - * * @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group. */ @Override diff --git a/src/main/java/com/lucwo/fourcharm/server/PreLobbyGroup.java b/src/main/java/com/lucwo/fourcharm/server/PreLobbyGroup.java index 2834a12..ee1ffb1 100644 --- a/src/main/java/com/lucwo/fourcharm/server/PreLobbyGroup.java +++ b/src/main/java/com/lucwo/fourcharm/server/PreLobbyGroup.java @@ -54,7 +54,6 @@ public void join(ClientHandler client, String pName, int gNumber, /** * Makes a move. - * * @param client the client that makes a move * @param col the column the move is about * @throws C4Exception the client is not playing a game and is not allowed to make a move @@ -68,7 +67,6 @@ public void doMove(ClientHandler client, int col) throws C4Exception { /** * Sets the clients status to 'ready' and starts a game if there is another client * that is ready as well. - * * @param client the client that wants to play a game * @throws C4Exception the client is not allowed to play a game in the PreLobbyGroup */ @@ -80,7 +78,6 @@ public void ready(ClientHandler client) throws C4Exception { /** * Handles the removal of a client from the group. - * * @param client The {@link com.lucwo.fourcharm.server.ClientHandler} which will has been removed from the group. */ @Override diff --git a/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java b/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java index a4d3795..e48590c 100644 --- a/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java +++ b/src/test/java/com/lucwo/fourcharm/server/ClientHandlerTest.java @@ -4,60 +4,112 @@ package com.lucwo.fourcharm.server; +import nl.woutertimmermans.connect4.protocol.exceptions.InvalidCommandError; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.security.cert.Extension; +import java.util.Set; + +import static org.junit.Assert.assertEquals; public class ClientHandlerTest { + ClientHandler clientH; + ClientHandler clientH2; + ClientHandler clientH3; + ClientGroup clientLobbyGroup; + ClientGroup clientPreLobbyGroup; + // ClientGroup clientGameGroup; + FourCharmServer theServer; + Set exts; + + + @Before public void setUp() throws Exception { - + clientH = new ClientHandler(null); + clientH2 = new ClientHandler(null); + clientH3 = new ClientHandler(null); + clientH.setName("Wouter"); + clientPreLobbyGroup = new PreLobbyGroup(clientLobbyGroup, theServer); + clientLobbyGroup = new LobbyGroup(theServer); + clientH2.setClientGroup(clientLobbyGroup); + // clientGameGroup = new GameGroup(theServer, clientH, clientH3); } @Test public void testGetName() throws Exception { - + assertEquals("Test to see if GetName() works", "Wouter", clientH.getName()); } @Test public void testSetName() throws Exception { - + clientH.setName("Luce"); + assertEquals("Test to see if SetName() works", "Luce", clientH.getName()); } @Test public void testGetClient() throws Exception { + //TODO: getClient vergelijken met een client + // assertEquals("Test to see if getClient() works", , clientH3.getClient()); + } @Test public void testGetClientGroup() throws Exception { - + assertEquals("Test to see if getting a GlientGroup works", clientLobbyGroup, clientH2.getClientGroup()); } @Test public void testSetClientGroup() throws Exception { - + clientH2.setClientGroup(clientPreLobbyGroup); + assertEquals("Test to see if setting a ClientGroup to pre lobby group works", clientPreLobbyGroup, clientH2.getClientGroup()); + clientH2.setClientGroup(clientLobbyGroup); + assertEquals("Test to see if setting a ClientGroup to lobby group works", clientLobbyGroup, clientH2.getClientGroup()); + + //TODO: setclientgroup to gamegroup + /* clientH2.setClientGroup(clientGameGroup); + clientH3.setClientGroup(clientGameGroup); + assertEquals("Test to see ifs setting a clientGroup to game group works", clientGameGroup, clientH2.getClientGroup());*/ } - @Test + /* @Test public void testJoin() throws Exception { + // clientPreLobbyGroup.join(clientH3, clientH3.getName(), 23, exts); + } @Test public void testReady() throws Exception { + clientLobbyGroup.ready(clientH3); } @Test public void testDoMove() throws Exception { + //Game group? + }*/ + + @Test(expected = InvalidCommandError.class) + public void testError() throws Exception { + clientPreLobbyGroup.doMove(clientH3, 5); + } @Test - public void testError() throws Exception { + public void run() throws Exception { + + //TODO: run() test schrijven + handleClient(); + } + + @Test + public void handleClient() throws Exception { + //TODO: handleClient() test schrijven } } \ No newline at end of file diff --git a/src/test/java/com/lucwo/fourcharm/server/FourCharmServerTest.java b/src/test/java/com/lucwo/fourcharm/server/FourCharmServerTest.java new file mode 100644 index 0000000..106301d --- /dev/null +++ b/src/test/java/com/lucwo/fourcharm/server/FourCharmServerTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015. Luce Sandfort and Wouter Timmermans + */ + +package com.lucwo.fourcharm.server; + +import org.junit.Before; +import org.junit.Test; + +public class FourCharmServerTest { + + @Before + public void setUp() throws Exception { + + } + + @Test + public void testMain() throws Exception { + + } + + @Test + public void testHasClientWithName() throws Exception { + + } + + @Test + public void testStartServer() throws Exception { + + } + + @Test + public void testAddGame() throws Exception { + + } + + @Test + public void testRemoveGame() throws Exception { + + } + + @Test + public void testStop() throws Exception { + + } + + @Test + public void testGetLobby() throws Exception { + + } +} \ No newline at end of file diff --git a/src/test/java/com/lucwo/fourcharm/server/GameGroupTest.java b/src/test/java/com/lucwo/fourcharm/server/GameGroupTest.java new file mode 100644 index 0000000..9a08bab --- /dev/null +++ b/src/test/java/com/lucwo/fourcharm/server/GameGroupTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015. Luce Sandfort and Wouter Timmermans + */ + +package com.lucwo.fourcharm.server; + +import org.junit.Before; +import org.junit.Test; + +public class GameGroupTest { + + @Before + public void setUp() throws Exception { + + } + + @Test + public void testJoin() throws Exception { + + } + + @Test + public void testDoMove() throws Exception { + + } + + @Test + public void testReady() throws Exception { + + } + + @Test + public void testRemoveClientCallback() throws Exception { + + } + + @Test + public void testStartGame() throws Exception { + + } + + @Test + public void testUpdate() throws Exception { + + } +} \ No newline at end of file diff --git a/src/test/java/com/lucwo/fourcharm/server/LobbyGroupTest.java b/src/test/java/com/lucwo/fourcharm/server/LobbyGroupTest.java new file mode 100644 index 0000000..2bf37d8 --- /dev/null +++ b/src/test/java/com/lucwo/fourcharm/server/LobbyGroupTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015. Luce Sandfort and Wouter Timmermans + */ + +package com.lucwo.fourcharm.server; + +import nl.woutertimmermans.connect4.protocol.exceptions.InvalidCommandError; +import org.junit.Before; +import org.junit.Test; + +import java.net.Socket; + +public class LobbyGroupTest { + + ClientGroup lobbyGroup; + FourCharmServer theServer; + ClientHandler clientje1; + ClientHandler clientje2; + Socket sock; + + @Before + public void setUp() throws Exception { + sock = new Socket(); + lobbyGroup = new LobbyGroup(theServer); + clientje1 = new ClientHandler(sock); + clientje2 = new ClientHandler(sock); + lobbyGroup.ready(clientje2); + + } + + @Test(expected = InvalidCommandError.class) + public void testJoin() throws Exception { + lobbyGroup.join(clientje1, clientje1.getName(), 11, null); + } + + @Test(expected = InvalidCommandError.class) + public void testDoMove() throws Exception { + lobbyGroup.doMove(clientje1, 4); + } + + @Test(expected = InvalidCommandError.class) + public void testSameNameReady() throws Exception { + lobbyGroup.ready(clientje2); + } +} \ No newline at end of file diff --git a/src/test/java/com/lucwo/fourcharm/server/PreLobbyGroupTest.java b/src/test/java/com/lucwo/fourcharm/server/PreLobbyGroupTest.java new file mode 100644 index 0000000..5499084 --- /dev/null +++ b/src/test/java/com/lucwo/fourcharm/server/PreLobbyGroupTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2015. Luce Sandfort and Wouter Timmermans + */ + +package com.lucwo.fourcharm.server; + +import nl.woutertimmermans.connect4.protocol.exceptions.InvalidCommandError; +import org.junit.Before; +import org.junit.Test; + +import java.net.Socket; + +import static org.junit.Assert.assertEquals; + +public class PreLobbyGroupTest { + + ClientGroup preLobbyGroup; + FourCharmServer theServer; + ClientHandler clientje1; + ClientHandler clientje2; + ClientGroup lobbyGroup; + Socket sock; + + @Before + public void setUp() throws Exception { + + //TODO: the server = new Fourcharmserver zorgt ervoor dat nu de server wel start, maar verder niks doet. + theServer = new FourCharmServer(8080); + sock = new Socket(); + preLobbyGroup = new PreLobbyGroup(lobbyGroup, theServer); + lobbyGroup = new LobbyGroup(theServer); + clientje1 = new ClientHandler(sock); + clientje2 = new ClientHandler(null); + //clientje1.setClientGroup(null); + clientje1.setName("Wouter"); + } + + @Test + public void testJoin() throws Exception { + preLobbyGroup.join(clientje1, clientje1.getName(), 11, null); + assertEquals("Test to see if clientje1 joins the LobbyGroup", lobbyGroup, clientje1.getClientGroup()); + } + + @Test(expected = InvalidCommandError.class) + public void testDoMove() throws Exception { + preLobbyGroup.doMove(clientje2, 4); + } + + @Test(expected = InvalidCommandError.class) + public void testReady() throws Exception { + preLobbyGroup.ready(clientje2); + } + +} \ No newline at end of file