@Test(timeout = 5000) public void testSameUserName() throws IOException, InterruptedException { TestClient client2 = createTestClient(); client2.send(new LoginMessage(client1.getName())); WelcomeMessage welcomeMessage = client2.await(WelcomeMessage.class); assertFalse(welcomeMessage.isOK()); }
@Test(timeout = 20000) public void testUserOnlineOffline() throws InterruptedException, UnknownHostException, IOException { TestClient client2 = createTestClient(); client2.send(new LoginMessage("Test2")); client2.await(WelcomeMessage.class); client2.await(ChatMessage.class); UserStatusMessage statusMessage = client1.await(UserStatusMessage.class); ChatMessage chat = client1.await(ChatMessage.class); String message = chat.getMessage(); assertTrue( "Unexpected message: " + message, message.contains(client2.getName()) && message.contains("joined")); int client2id = statusMessage.getUserId(); assertEquals(Status.ONLINE, statusMessage.getStatus()); assertEquals(server.getClients().size() + 1, client2id); assertEquals(client2.getName(), statusMessage.getName()); client2.send(new ServerQueryMessage(Request.USERS)); client2.await(AvailableModsMessage.class); List<UserStatusMessage> users = client2.awaitMany(6, UserStatusMessage.class); System.out.println("Online users: " + users); // There is no determined order in which the UserStatusMessages are received, so it is harder to // make any assertions. assertUserFound(users, client1.getName()); assertUserFound(users, client2.getName()); assertUserFound(users, "AI Fighter"); assertUserFound(users, "AI Loser"); assertUserFound(users, "AI Medium"); assertUserFound(users, "AI Idiot"); client2.disconnect(); System.out.println(chat); statusMessage = client1.await(UserStatusMessage.class); assertEquals(Status.OFFLINE, statusMessage.getStatus()); assertEquals(client2id, statusMessage.getUserId()); assertEquals(client2.getName(), statusMessage.getName()); }
@Test(timeout = 5000) public void testUsersQueryNotLoggedIn() throws IOException, InterruptedException { TestClient client2 = createTestClient(); client1.send(new ServerQueryMessage(Request.USERS, "")); List<UserStatusMessage> users = client1.awaitMany(5, UserStatusMessage.class); assertUserFound(users, client1.getName()); assertUserFound(users, "AI Fighter"); assertUserFound(users, "AI Loser"); assertUserFound(users, "AI Medium"); assertUserFound(users, "AI Idiot"); // There shouldn't be a UserStatusMessage for client2 assertNoMessage(client1); }