Browse Source

test_lastMessage Test

remotes/origin/server
Paul Kattenborn 11 months ago
parent
commit
962bc1688a
  1. 5
      src/main/java/ClientHandler.java
  2. 18
      src/test/java/ChatServerTest.java

5
src/main/java/ClientHandler.java

@ -9,6 +9,7 @@ public class ClientHandler implements Runnable {
private String name; private String name;
private BufferedReader fromClientReader; private BufferedReader fromClientReader;
private PrintWriter toClientWriter; private PrintWriter toClientWriter;
private String lastMessage;
// Constructor for ClientHandler // Constructor for ClientHandler
public ClientHandler(ChatServer chatServer, Socket connectionToClient) { public ClientHandler(ChatServer chatServer, Socket connectionToClient) {
@ -33,6 +34,7 @@ public class ClientHandler implements Runnable {
while (message!=null) { while (message!=null) {
// Broadcast the client's message to all connected clients // Broadcast the client's message to all connected clients
chatServer.broadcastMessage(name + ": " + message); chatServer.broadcastMessage(name + ": " + message);
lastMessage = message;
message = fromClientReader.readLine(); message = fromClientReader.readLine();
} }
} }
@ -64,4 +66,7 @@ public class ClientHandler implements Runnable {
toClientWriter.println(message); // Send the message to the client toClientWriter.println(message); // Send the message to the client
toClientWriter.flush(); // Flush the stream toClientWriter.flush(); // Flush the stream
} }
public String getLastMessage() {
return lastMessage;
}
} }

18
src/test/java/ChatServerTest.java

@ -21,6 +21,24 @@ class ChatServerTest {
// Assert // Assert
assertNotNull(chatServer); assertNotNull(chatServer);
} }
@Test
public void testBroadcastMessage() {
// Arrange
ChatServer chatServer = new ChatServer(1234);
ClientHandler client1 = new ClientHandler(chatServer, null);
ClientHandler client2 = new ClientHandler(chatServer, null);
chatServer.getClients().add(client1);
chatServer.getClients().add(client2);
// Act
chatServer.broadcastMessage("Test message");
// Assert
List<ClientHandler> clients = chatServer.getClients();
for (ClientHandler client : clients) {
assertEquals("Test message", client.getLastMessage());
}
}
} }
Loading…
Cancel
Save