use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MultiUserChatTest method testParticipantPresence.
public void testParticipantPresence() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
Thread.sleep(400);
// User1 checks the presence of user2 in the room
Presence presence = muc.getOccupantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
assertTrue("Presence mode of user2 is wrong", presence.getMode() == null || presence.getMode() == Presence.Mode.available);
// User2 changes his availability to AWAY
muc2.changeAvailabilityStatus("Gone to have lunch", Presence.Mode.away);
Thread.sleep(200);
// User1 checks the presence of user2 in the room
presence = muc.getOccupantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
assertEquals("Presence mode of user2 is wrong", Presence.Mode.away, presence.getMode());
assertEquals("Presence status of user2 is wrong", "Gone to have lunch", presence.getStatus());
// User2 changes his nickname
muc2.changeNickname("testbotII");
Thread.sleep(200);
// User1 checks the presence of user2 in the room
presence = muc.getOccupantPresence(room + "/testbot2");
assertNull("Presence of participant testbot2 still exists", presence);
presence = muc.getOccupantPresence(room + "/testbotII");
assertNotNull("Presence of participant testbotII does not exist", presence);
assertEquals("Presence of participant testbotII has a wrong from", room + "/testbotII", presence.getFrom());
// User2 leaves the room
muc2.leave();
Thread.sleep(250);
// User1 checks the presence of user2 in the room
presence = muc.getOccupantPresence(room + "/testbotII");
assertNull("Presence of participant testbotII still exists", presence);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MultiUserChatTest method testAnonymousParticipant.
public void testAnonymousParticipant() {
try {
// Anonymous user joins the new room
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
XMPPTCPConnection anonConnection = new XMPPConnection(connectionConfiguration);
anonConnection.connect();
anonConnection.loginAnonymously();
MultiUserChat muc2 = new MultiUserChat(anonConnection, room);
muc2.join("testbot2");
Thread.sleep(400);
// User1 checks the presence of Anonymous user in the room
Presence presence = muc.getOccupantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
assertTrue("Presence mode of user2 is wrong", presence.getMode() == null || presence.getMode() == Presence.Mode.available);
// Anonymous user leaves the room
muc2.leave();
anonConnection.disconnect();
Thread.sleep(250);
// User1 checks the presence of Anonymous user in the room
presence = muc.getOccupantPresence(room + "/testbot2");
assertNull("Presence of participant testbotII still exists", presence);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class OfflineMessageManagerTest method testReadAndDelete.
/**
* While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
* performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
* retriving the headers, then the real messages of the headers and finally removing the
* loaded messages.
*/
public void testReadAndDelete() {
// Make user2 unavailable
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 checks the number of offline messages
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// Check the message headers
Iterator<OfflineMessageHeader> headers = offlineManager.getHeaders();
assertTrue("No message header was found", headers.hasNext());
List<String> stamps = new ArrayList<String>();
while (headers.hasNext()) {
OfflineMessageHeader header = headers.next();
assertEquals("Incorrect sender", getFullJID(0), header.getJid());
assertNotNull("No stamp was found in the message header", header.getStamp());
stamps.add(header.getStamp());
}
assertEquals("Wrong number of headers", 2, stamps.size());
// Get the offline messages
Iterator<Message> messages = offlineManager.getMessages(stamps);
assertTrue("No message was found", messages.hasNext());
stamps = new ArrayList<String>();
while (messages.hasNext()) {
Message message = messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
assertNotNull("No offline information was included in the offline message", info);
assertNotNull("No stamp was found in the message header", info.getNode());
stamps.add(info.getNode());
}
assertEquals("Wrong number of messages", 2, stamps.size());
// Check that the offline messages have not been deleted
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// User2 becomes available again
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user
Message message = (Message) collector.nextResult(2500);
assertNull("An offline message was sent from the server", message);
// Delete the retrieved offline messages
offlineManager.deleteMessages(stamps);
// Check that there are no offline message for this user
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MessageTest method testOfflineMessage.
/**
* Check that when a client becomes unavailable all messages sent to the client are stored offline. So that when
* the client becomes available again the offline messages are received.
*/
public void testOfflineMessage() {
getConnection(0).sendStanza(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Make user2 unavailable
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 becomes available again
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available
Message message = (Message) collector.nextResult(2500);
assertNotNull(message);
message = (Message) collector.nextResult(2000);
assertNotNull(message);
message = (Message) collector.nextResult(1000);
assertNull(message);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MessageTest method testHugeMessage.
/**
* Send messages with invalid XML characters to offline users. Check that offline users
* are receiving them from the server.<p>
*
* Test case commented out since some servers may just close the connection while others
* are more tolerant and accept stanzas with invalid XML characters.
*/
/*public void testOfflineMessageInvalidXML() {
// Make user2 unavailable
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
StanzaCollector collector = getConnection(1).createStanzaCollector(
new MessageTypeFilter(Message.Type.chat));
chat.sendMessage("Test \f 1");
chat.sendMessage("Test \r 1");
Thread.sleep(500);
// User2 becomes available again
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available
Message message = (Message) collector.nextResult(2500);
assertNotNull(message);
message = (Message) collector.nextResult(2000);
assertNotNull(message);
message = (Message) collector.nextResult(1000);
assertNull(message);
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}*/
/**
* Check that two clients are able to send messages with a body of 4K characters and their
* connections are not being closed.
*/
public void testHugeMessage() {
getConnection(0).sendStanza(new Presence(Presence.Type.available));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// User2 becomes available again
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
// Create message with a body of 4K characters
Message msg = new Message(getFullJID(1), Message.Type.chat);
StringBuilder sb = new StringBuilder(5000);
for (int i = 0; i <= 4000; i++) {
sb.append('X');
}
msg.setBody(sb.toString());
// Send the first message
getConnection(0).sendStanza(msg);
// Check that the connection that sent the message is still connected
assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
// Check that the message was received
Message rcv = (Message) collector.nextResult(1000);
assertNotNull("No Message was received", rcv);
// Send the second message
getConnection(0).sendStanza(msg);
// Check that the connection that sent the message is still connected
assertTrue("XMPPConnection was closed", getConnection(0).isConnected());
// Check that the second message was received
rcv = (Message) collector.nextResult(1000);
assertNotNull("No Message was received", rcv);
}
Aggregations