assertTrue(server.postMessage("Sally.Jones", sallyAuthToken, REMOTE_HOST, "Hi, Bob!"));
}
public void testMessageRetrieval()
throws InterruptedException {
Server server = new Server();
Message[] messages;
long lastRetrievedId;
// Retrieve recent messages.
messages = server.getRecentMessages();
assertEquals(0, messages.length);
String bobAuthToken = server.addUser("Bob.Smith", REMOTE_HOST);
// Retrieve authentication message.
messages = server.getRecentMessages();
assertEquals(1, messages.length);
// Post ten messages.
for (int i = 0; i < 10; ++i) {
server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, Integer.toString(i));
}
// Retrieve all messages.
messages = server.getMessages(-1);
assertEquals(11, messages.length);
assertEquals("0", messages[1].getContent());
assertEquals("1", messages[2].getContent());
assertEquals("9", messages[10].getContent());
lastRetrievedId = messages[10].getId();
// Retrieve recent messages.
messages = server.getRecentMessages();
assertEquals(11, messages.length);
assertEquals("0", messages[1].getContent());
assertEquals("1", messages[2].getContent());
assertEquals("9", messages[10].getContent());
// Post ten messages.
for (int i = 10; i < 20; ++i) {
server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, Integer.toString(i));
}
// Retrieve messages.
messages = server.getMessages(lastRetrievedId);
assertEquals(10, messages.length);
assertEquals("10", messages[0].getContent());
assertEquals("11", messages[1].getContent());
assertEquals("19", messages[9].getContent());
lastRetrievedId = messages[9].getId();
// Post one thousand messages.
for (int i = 20; i < 1020; ++i) {
server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, Integer.toString(i));
}
// Retrieve messages.
messages = server.getMessages(lastRetrievedId);
assertEquals(1000, messages.length);
assertEquals("20", messages[0].getContent());
assertEquals("21", messages[1].getContent());
assertEquals("520", messages[500].getContent());
assertEquals("1018", messages[998].getContent());
assertEquals("1019", messages[999].getContent());
// Retrieve messages from a different starting point.
messages = server.getMessages(messages[500].getId());
assertEquals(499, messages.length);
assertEquals("521", messages[0].getContent());
assertEquals("1019", messages[498].getContent());
// Retrieve recent messages.
messages = server.getRecentMessages();
assertEquals(15, messages.length);
assertEquals("1005", messages[0].getContent());
assertEquals("1006", messages[1].getContent());
assertEquals("1019", messages[14].getContent());
lastRetrievedId = messages[14].getId();
// Post message with UTF-8 characters.
server.postMessage("Bob.Smith", bobAuthToken, REMOTE_HOST, "\u0416\u066f");
messages = server.getMessages(lastRetrievedId);
assertEquals("\u0416\u066f", messages[0].getContent());
}