mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-26 16:22:06 +01:00
SMACK-387 Added some configuration to the ChatManager to allow for different matching modes on incoming messages with no thread id.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_4_0@13885 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
782448b3ec
commit
5bbf6cf224
5 changed files with 478 additions and 232 deletions
|
@ -170,7 +170,19 @@ public class Chat {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Chat [(participant=" + participant + "), (thread=" + threadID + ")]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 1;
|
||||
hash = hash * 31 + threadID.hashCode();
|
||||
hash = hash * 31 + participant.hashCode();
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof Chat
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.util.Collection;
|
|||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
|
@ -33,39 +34,59 @@ import org.jivesoftware.smack.filter.PacketFilter;
|
|||
import org.jivesoftware.smack.filter.ThreadFilter;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.Message.Type;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.collections.ReferenceMap;
|
||||
|
||||
/**
|
||||
* The chat manager keeps track of references to all current chats. It will not hold any references
|
||||
* in memory on its own so it is neccesary to keep a reference to the chat object itself. To be
|
||||
* in memory on its own so it is necessary to keep a reference to the chat object itself. To be
|
||||
* made aware of new chats, register a listener by calling {@link #addChatListener(ChatManagerListener)}.
|
||||
*
|
||||
* @author Alexander Wenckus
|
||||
*/
|
||||
public class ChatManager {
|
||||
|
||||
/**
|
||||
* Returns the next unique id. Each id made up of a short alphanumeric
|
||||
* prefix along with a unique numeric value.
|
||||
*
|
||||
* @return the next id.
|
||||
/*
|
||||
* Sets the default behaviour for allowing 'normal' messages to be used in chats. As some clients don't set
|
||||
* the message type to chat, the type normal has to be accepted to allow chats with these clients.
|
||||
*/
|
||||
private static synchronized String nextID() {
|
||||
return prefix + Long.toString(id++);
|
||||
private static boolean defaultIsNormalInclude = true;
|
||||
|
||||
/*
|
||||
* Sets the default behaviour for how to match chats when there is NO thread id in the incoming message.
|
||||
*/
|
||||
private static MatchMode defaultMatchMode = MatchMode.BARE_JID;
|
||||
|
||||
/**
|
||||
* Defines the different modes under which a match will be attempted with an existing chat when
|
||||
* the incoming message does not have a thread id.
|
||||
*/
|
||||
public enum MatchMode {
|
||||
/**
|
||||
* Will not attempt to match, always creates a new chat.
|
||||
*/
|
||||
NONE,
|
||||
/**
|
||||
* Will match on the JID in the from field of the message.
|
||||
*/
|
||||
SUPPLIED_JID,
|
||||
/**
|
||||
* Will attempt to match on the JID in the from field, and then attempt the base JID if no match was found.
|
||||
* This is the most lenient matching.
|
||||
*/
|
||||
BARE_JID;
|
||||
}
|
||||
|
||||
/**
|
||||
* A prefix helps to make sure that ID's are unique across mutliple instances.
|
||||
|
||||
/*
|
||||
* Determines whether incoming messages of type normal can create chats.
|
||||
*/
|
||||
private static String prefix = StringUtils.randomString(5);
|
||||
|
||||
/**
|
||||
* Keeps track of the current increment, which is appended to the prefix to
|
||||
* forum a unique ID.
|
||||
private boolean normalIncluded = defaultIsNormalInclude;
|
||||
|
||||
/*
|
||||
* Determines how incoming message with no thread will be matched to existing chats.
|
||||
*/
|
||||
private static long id = 0;
|
||||
|
||||
private MatchMode matchMode = defaultMatchMode;
|
||||
|
||||
/**
|
||||
* Maps thread ID to chat.
|
||||
*/
|
||||
|
@ -101,11 +122,11 @@ public class ChatManager {
|
|||
return false;
|
||||
}
|
||||
Message.Type messageType = ((Message) packet).getType();
|
||||
return messageType != Message.Type.groupchat &&
|
||||
messageType != Message.Type.headline;
|
||||
return (messageType == Type.chat) || (normalIncluded ? messageType == Type.normal : false);
|
||||
}
|
||||
};
|
||||
// Add a listener for all message packets so that we can deliver errant
|
||||
|
||||
// Add a listener for all message packets so that we can deliver
|
||||
// messages to the best Chat instance available.
|
||||
connection.addPacketListener(new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
|
@ -116,10 +137,6 @@ public class ChatManager {
|
|||
}
|
||||
else {
|
||||
chat = getThreadChat(message.getThread());
|
||||
if (chat == null) {
|
||||
// Try to locate the chat based on the sender of the message
|
||||
chat = getUserChat(message.getFrom());
|
||||
}
|
||||
}
|
||||
|
||||
if(chat == null) {
|
||||
|
@ -130,6 +147,44 @@ public class ChatManager {
|
|||
}, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether incoming messages of type <i>normal</i> will be used for creating new chats or matching
|
||||
* a message to existing ones.
|
||||
*
|
||||
* @return true if normal is allowed, false otherwise.
|
||||
*/
|
||||
public boolean isNormalIncluded() {
|
||||
return normalIncluded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to allow incoming messages of type <i>normal</i> to be used for creating new chats or matching
|
||||
* a message to an existing one.
|
||||
*
|
||||
* @param normalIncluded true to allow normal, false otherwise.
|
||||
*/
|
||||
public void setNormalIncluded(boolean normalIncluded) {
|
||||
this.normalIncluded = normalIncluded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current mode for matching messages with <b>NO</b> thread id to existing chats.
|
||||
*
|
||||
* @return The current mode.
|
||||
*/
|
||||
public MatchMode getMatchMode() {
|
||||
return matchMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the mode for matching messages with <b>NO</b> thread id to existing chats.
|
||||
*
|
||||
* @param matchMode The mode to set.
|
||||
*/
|
||||
public void setMatchMode(MatchMode matchMode) {
|
||||
this.matchMode = matchMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new chat and returns it.
|
||||
*
|
||||
|
@ -138,12 +193,7 @@ public class ChatManager {
|
|||
* @return the created chat.
|
||||
*/
|
||||
public Chat createChat(String userJID, MessageListener listener) {
|
||||
String threadID;
|
||||
do {
|
||||
threadID = nextID();
|
||||
} while (threadChats.get(threadID) != null);
|
||||
|
||||
return createChat(userJID, threadID, listener);
|
||||
return createChat(userJID, null, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -155,7 +205,7 @@ public class ChatManager {
|
|||
* @return the created chat.
|
||||
*/
|
||||
public Chat createChat(String userJID, String thread, MessageListener listener) {
|
||||
if(thread == null) {
|
||||
if (thread == null) {
|
||||
thread = nextID();
|
||||
}
|
||||
Chat chat = threadChats.get(thread);
|
||||
|
@ -191,20 +241,25 @@ public class ChatManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Try to get a matching chat for the given user JID. Try the full
|
||||
* JID map first, the try to match on the base JID if no match is
|
||||
* found.
|
||||
* Try to get a matching chat for the given user JID, based on the {@link MatchMode}.
|
||||
* <li>NONE - return null
|
||||
* <li>SUPPLIED_JID - match the jid in the from field of the message exactly.
|
||||
* <li>BARE_JID - if not match for from field, try the bare jid.
|
||||
*
|
||||
* @param userJID
|
||||
* @return
|
||||
* @param userJID jid in the from field of message.
|
||||
* @return Matching chat, or null if no match found.
|
||||
*/
|
||||
private Chat getUserChat(String userJID) {
|
||||
Chat match = jidChats.get(userJID);
|
||||
if (matchMode == MatchMode.NONE) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Chat match = jidChats.get(userJID);
|
||||
|
||||
if (match == null) {
|
||||
match = baseJidChats.get(StringUtils.parseBareAddress(userJID));
|
||||
}
|
||||
return match;
|
||||
if (match == null && (matchMode == MatchMode.BARE_JID)) {
|
||||
match = baseJidChats.get(StringUtils.parseBareAddress(userJID));
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
public Chat getThreadChat(String thread) {
|
||||
|
@ -278,4 +333,21 @@ public class ChatManager {
|
|||
interceptors.put(packetInterceptor, filter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a unique id.
|
||||
*
|
||||
* @return the next id.
|
||||
*/
|
||||
private static String nextID() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public static void setDefaultMatchMode(MatchMode mode) {
|
||||
defaultMatchMode = mode;
|
||||
}
|
||||
|
||||
public static void setDefaultIsNormalIncluded(boolean allowNormal) {
|
||||
defaultIsNormalInclude = allowNormal;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -80,7 +80,10 @@ public class Message extends Packet {
|
|||
*/
|
||||
public Message(String to, Type type) {
|
||||
setTo(to);
|
||||
this.type = type;
|
||||
|
||||
if (type != null) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -452,6 +452,7 @@ public abstract class Packet {
|
|||
return DEFAULT_LANGUAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
@ -472,6 +473,7 @@ public abstract class Packet {
|
|||
return !(xmlns != null ? !xmlns.equals(packet.xmlns) : packet.xmlns != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result;
|
||||
result = (xmlns != null ? xmlns.hashCode() : 0);
|
||||
|
@ -483,4 +485,9 @@ public abstract class Packet {
|
|||
result = 31 * result + (error != null ? error.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toXML();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,267 +22,419 @@ package org.jivesoftware.smack;
|
|||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.jivesoftware.smack.ChatManager.MatchMode;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Message.Type;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests that verifies the correct behavior of the {@see Roster} implementation.
|
||||
*
|
||||
* @see Roster
|
||||
* @see <a href="http://xmpp.org/rfcs/rfc3921.html#roster">Roster Management</a>
|
||||
* @author Guenther Niess
|
||||
*/
|
||||
public class ChatConnectionTest {
|
||||
|
||||
private DummyConnection connection;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Uncomment this to enable debug output
|
||||
//Connection.DEBUG_ENABLED = true;
|
||||
|
||||
connection = new DummyConnection();
|
||||
connection.connect();
|
||||
connection.login("me", "secret");
|
||||
connection = getConnection();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (connection != null)
|
||||
connection.disconnect();
|
||||
if (connection != null)
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateDefaultSetNormalIncluded() {
|
||||
ChatManager.setDefaultIsNormalIncluded(false);
|
||||
assertFalse(getConnection().getChatManager().isNormalIncluded());
|
||||
|
||||
ChatManager.setDefaultIsNormalIncluded(true);
|
||||
assertTrue(getConnection().getChatManager().isNormalIncluded());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateDefaultSetMatchMode() {
|
||||
ChatManager.setDefaultMatchMode(MatchMode.NONE);
|
||||
assertEquals(MatchMode.NONE, getConnection().getChatManager().getMatchMode());
|
||||
|
||||
ChatManager.setDefaultMatchMode(MatchMode.BARE_JID);
|
||||
assertEquals(MatchMode.BARE_JID, getConnection().getChatManager().getMatchMode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateMessageTypeWithDefaults() {
|
||||
DummyConnection dc = getConnection();
|
||||
ChatManager cm = dc.getChatManager();
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
cm.addChatListener(listener);
|
||||
Message incomingChat = createChatPacket("134", true);
|
||||
incomingChat.setType(Type.chat);
|
||||
processServerMessage(incomingChat, dc);
|
||||
assertNotNull(listener.getNewChat());
|
||||
|
||||
dc = getConnection();
|
||||
cm = dc.getChatManager();
|
||||
listener = new TestChatManagerListener();
|
||||
cm.addChatListener(listener);
|
||||
incomingChat = createChatPacket("134", true);
|
||||
incomingChat.setType(Type.normal);
|
||||
processServerMessage(incomingChat, dc);
|
||||
assertNotNull(listener.getNewChat());
|
||||
|
||||
dc = getConnection();
|
||||
cm = dc.getChatManager();
|
||||
listener = new TestChatManagerListener();
|
||||
cm.addChatListener(listener);
|
||||
incomingChat = createChatPacket("134", true);
|
||||
incomingChat.setType(Type.groupchat);
|
||||
processServerMessage(incomingChat, dc);
|
||||
assertNull(listener.getNewChat());
|
||||
|
||||
dc = getConnection();
|
||||
cm = dc.getChatManager();
|
||||
listener = new TestChatManagerListener();
|
||||
cm.addChatListener(listener);
|
||||
incomingChat = createChatPacket("134", true);
|
||||
incomingChat.setType(Type.headline);
|
||||
processServerMessage(incomingChat, dc);
|
||||
assertNull(listener.getNewChat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateMessageTypeWithNoNormal() {
|
||||
ChatManager.setDefaultIsNormalIncluded(false);
|
||||
DummyConnection dc = getConnection();
|
||||
ChatManager cm = dc.getChatManager();
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
cm.addChatListener(listener);
|
||||
Message incomingChat = createChatPacket("134", true);
|
||||
incomingChat.setType(Type.chat);
|
||||
processServerMessage(incomingChat, dc);
|
||||
assertNotNull(listener.getNewChat());
|
||||
|
||||
dc = getConnection();
|
||||
cm = dc.getChatManager();
|
||||
listener = new TestChatManagerListener();
|
||||
cm.addChatListener(listener);
|
||||
incomingChat = createChatPacket("134", true);
|
||||
incomingChat.setType(Type.normal);
|
||||
processServerMessage(incomingChat, dc);
|
||||
assertNull(listener.getNewChat());
|
||||
}
|
||||
|
||||
// No thread behaviour
|
||||
@Test
|
||||
public void chatMatchedOnJIDWhenNoThreadBareMode() {
|
||||
// MatchMode.BARE_JID is the default, so setting required.
|
||||
DummyConnection con = getConnection();
|
||||
TestMessageListener msgListener = new TestMessageListener();
|
||||
TestChatManagerListener listener = new TestChatManagerListener(msgListener);
|
||||
con.getChatManager().addChatListener(listener);
|
||||
Packet incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat, con);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
|
||||
// Should match on chat with full jid
|
||||
incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat, con);
|
||||
assertEquals(2, msgListener.getNumMessages());
|
||||
|
||||
// Should match on chat with bare jid
|
||||
incomingChat = createChatPacket(null, false);
|
||||
processServerMessage(incomingChat, con);
|
||||
assertEquals(3, msgListener.getNumMessages());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chatMatchedOnJIDWhenNoThreadJidMode() {
|
||||
DummyConnection con = getConnection();
|
||||
TestMessageListener msgListener = new TestMessageListener();
|
||||
TestChatManagerListener listener = new TestChatManagerListener(msgListener);
|
||||
ChatManager cm = con.getChatManager();
|
||||
cm.setMatchMode(MatchMode.SUPPLIED_JID);
|
||||
cm.addChatListener(listener);
|
||||
Packet incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat, con);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
cm.removeChatListener(listener);
|
||||
|
||||
// Should match on chat with full jid
|
||||
incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat, con);
|
||||
assertEquals(2, msgListener.getNumMessages());
|
||||
|
||||
// Should not match on chat with bare jid
|
||||
TestChatManagerListener listener2 = new TestChatManagerListener();
|
||||
cm.addChatListener(listener2);
|
||||
incomingChat = createChatPacket(null, false);
|
||||
processServerMessage(incomingChat, con);
|
||||
assertEquals(2, msgListener.getNumMessages());
|
||||
assertNotNull(listener2.getNewChat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chatMatchedOnJIDWhenNoThreadNoneMode() {
|
||||
DummyConnection con = getConnection();
|
||||
TestMessageListener msgListener = new TestMessageListener();
|
||||
TestChatManagerListener listener = new TestChatManagerListener(msgListener);
|
||||
ChatManager cm = con.getChatManager();
|
||||
cm.setMatchMode(MatchMode.NONE);
|
||||
cm.addChatListener(listener);
|
||||
Packet incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat, con);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertEquals(1, msgListener.getNumMessages());
|
||||
cm.removeChatListener(listener);
|
||||
|
||||
// Should not match on chat with full jid
|
||||
TestChatManagerListener listener2 = new TestChatManagerListener();
|
||||
cm.addChatListener(listener2);
|
||||
incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat, con);
|
||||
assertEquals(1, msgListener.getNumMessages());
|
||||
assertNotNull(newChat);
|
||||
cm.removeChatListener(listener2);
|
||||
|
||||
// Should not match on chat with bare jid
|
||||
TestChatManagerListener listener3 = new TestChatManagerListener();
|
||||
cm.addChatListener(listener3);
|
||||
incomingChat = createChatPacket(null, false);
|
||||
processServerMessage(incomingChat, con);
|
||||
assertEquals(1, msgListener.getNumMessages());
|
||||
assertNotNull(listener3.getNewChat());
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that a new chat is created when a chat message is received but
|
||||
* there is no thread id for a user with only a base jid.
|
||||
* Confirm that an existing chat created with a base jid is matched to an incoming chat message that has no thread
|
||||
* id and the user is a full jid.
|
||||
*/
|
||||
@Test
|
||||
public void chatCreatedWithIncomingChatNoThreadBaseJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
public void chatFoundWhenNoThreadFullJid() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
|
||||
Packet incomingChat = createChatPacket(null, false);
|
||||
processServerMessage(incomingChat);
|
||||
Packet incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that a new chat is created when a chat message is received but
|
||||
* there is no thread id for a user with a full jid.
|
||||
* Confirm that an existing chat created with a base jid is matched to an incoming chat message that has no thread
|
||||
* id and the user is a base jid.
|
||||
*/
|
||||
@Test
|
||||
public void chatCreatedWhenIncomingChatNoThreadFullJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
public void chatFoundWhenNoThreadBaseJid() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
|
||||
Packet incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat);
|
||||
Packet incomingChat = createChatPacket(null, false);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that an existing chat created with a base jid is matched to an
|
||||
* incoming chat message that has no thread id and the user is a full jid.
|
||||
* Confirm that an existing chat created with a base jid is matched to an incoming chat message that has the same id
|
||||
* and the user is a full jid.
|
||||
*/
|
||||
@Test
|
||||
public void chatFoundWhenNoThreadFullJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
public void chatFoundWithSameThreadFullJid() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
|
||||
Packet incomingChat = createChatPacket(null, true);
|
||||
processServerMessage(incomingChat);
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID(), true);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that an existing chat created with a base jid is matched to an
|
||||
* incoming chat message that has no thread id and the user is a base jid.
|
||||
* Confirm that an existing chat created with a base jid is matched to an incoming chat message that has the same id
|
||||
* and the user is a base jid.
|
||||
*/
|
||||
@Test
|
||||
public void chatFoundWhenNoThreadBaseJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
public void chatFoundWithSameThreadBaseJid() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
|
||||
Packet incomingChat = createChatPacket(null, false);
|
||||
processServerMessage(incomingChat);
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID(), false);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that an existing chat created with a base jid is matched to an
|
||||
* incoming chat message that has the same id and the user is a full jid.
|
||||
* Confirm that an existing chat created with a base jid is not matched to an incoming chat message that has a
|
||||
* different id and the same user as a base jid.
|
||||
*/
|
||||
@Test
|
||||
public void chatFoundWithSameThreadFullJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
public void chatNotFoundWithDiffThreadBaseJid() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID(), true);
|
||||
processServerMessage(incomingChat);
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", false);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertFalse(newChat == outgoing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that an existing chat created with a base jid is matched to an
|
||||
* incoming chat message that has the same id and the user is a base jid.
|
||||
* Confirm that an existing chat created with a base jid is not matched to an incoming chat message that has a
|
||||
* different id and the same base jid.
|
||||
*/
|
||||
@Test
|
||||
public void chatFoundWithSameThreadBaseJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
public void chatNotFoundWithDiffThreadFullJid() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID(), false);
|
||||
processServerMessage(incomingChat);
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", true);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertTrue(newChat == outgoing);
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertFalse(newChat == outgoing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that an existing chat created with a base jid is not matched to
|
||||
* an incoming chat message that has a different id and the same user as a
|
||||
* base jid.
|
||||
*/
|
||||
@Ignore
|
||||
@Test
|
||||
public void chatNotFoundWithDiffThreadBaseJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
public void chatNotMatchedWithTypeNormal() {
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
DummyConnection con = getConnection();
|
||||
ChatManager cm = con.getChatManager();
|
||||
cm.setNormalIncluded(false);
|
||||
cm.addChatListener(listener);
|
||||
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", false);
|
||||
processServerMessage(incomingChat);
|
||||
Message incomingChat = createChatPacket(null, false);
|
||||
incomingChat.setType(Type.normal);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertFalse(newChat == outgoing);
|
||||
assertNull(listener.getNewChat());
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm that an existing chat created with a base jid is not matched to
|
||||
* an incoming chat message that has a different id and the same base jid.
|
||||
*/
|
||||
@Ignore
|
||||
@Test
|
||||
public void chatNotFoundWithDiffThreadFullJid()
|
||||
{
|
||||
TestChatManagerListener listener = new TestChatManagerListener();
|
||||
connection.getChatManager().addChatListener(listener);
|
||||
Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
|
||||
private ChatManager getChatManager(boolean includeNormal, MatchMode mode) {
|
||||
ChatManager cm = getConnection().getChatManager();
|
||||
cm.setMatchMode(mode);
|
||||
cm.setNormalIncluded(includeNormal);
|
||||
return cm;
|
||||
}
|
||||
|
||||
private DummyConnection getConnection() {
|
||||
DummyConnection con = new DummyConnection();
|
||||
|
||||
try {
|
||||
con.connect();
|
||||
con.login("me", "secret");
|
||||
} catch (XMPPException e) {
|
||||
// No need for handling in a dummy connection.
|
||||
}
|
||||
return con;
|
||||
}
|
||||
private Message createChatPacket(final String threadId, final boolean isFullJid) {
|
||||
Message chatMsg = new Message("me@testserver", Message.Type.chat);
|
||||
chatMsg.setBody("the body message - " + System.currentTimeMillis());
|
||||
chatMsg.setFrom("you@testserver" + (isFullJid ? "/resource" : ""));
|
||||
|
||||
Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", true);
|
||||
processServerMessage(incomingChat);
|
||||
|
||||
Chat newChat = listener.getNewChat();
|
||||
assertNotNull(newChat);
|
||||
assertFalse(newChat == outgoing);
|
||||
if (threadId != null)
|
||||
chatMsg.setThread(threadId);
|
||||
return chatMsg;
|
||||
}
|
||||
|
||||
private Packet createChatPacket(final String threadId, final boolean isFullJid)
|
||||
{
|
||||
Message chatMsg = new Message("me@testserver", Message.Type.chat);
|
||||
chatMsg.setBody("the body message");
|
||||
chatMsg.setFrom("you@testserver" + (isFullJid ? "/resource" : ""));
|
||||
|
||||
if (threadId != null)
|
||||
chatMsg.addExtension(new PacketExtension()
|
||||
{
|
||||
@Override
|
||||
public String toXML()
|
||||
{
|
||||
return "<thread>" + threadId + "</thread>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName()
|
||||
{
|
||||
return "thread";
|
||||
}
|
||||
});
|
||||
return chatMsg;
|
||||
private void processServerMessage(Packet incomingChat) {
|
||||
processServerMessage(incomingChat, connection);
|
||||
}
|
||||
|
||||
private void processServerMessage(Packet incomingChat, DummyConnection con) {
|
||||
TestChatServer chatServer = new TestChatServer(incomingChat, con);
|
||||
chatServer.start();
|
||||
try {
|
||||
chatServer.join();
|
||||
} catch (InterruptedException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
private void processServerMessage(Packet incomingChat)
|
||||
{
|
||||
TestChatServer chatServer = new TestChatServer(incomingChat);
|
||||
chatServer.start();
|
||||
try
|
||||
{
|
||||
chatServer.join();
|
||||
} catch (InterruptedException e)
|
||||
{
|
||||
fail();
|
||||
}
|
||||
class TestChatManagerListener implements ChatManagerListener {
|
||||
private Chat newChat;
|
||||
private MessageListener listener;
|
||||
|
||||
public TestChatManagerListener(TestMessageListener msgListener) {
|
||||
listener = msgListener;
|
||||
}
|
||||
|
||||
public TestChatManagerListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chatCreated(Chat chat, boolean createdLocally) {
|
||||
newChat = chat;
|
||||
|
||||
if (listener != null)
|
||||
newChat.addMessageListener(listener);
|
||||
}
|
||||
|
||||
public Chat getNewChat() {
|
||||
return newChat;
|
||||
}
|
||||
}
|
||||
|
||||
private class TestChatServer extends Thread {
|
||||
private Packet chatPacket;
|
||||
private DummyConnection con;
|
||||
|
||||
TestChatServer(Packet chatMsg, DummyConnection conect) {
|
||||
chatPacket = chatMsg;
|
||||
con = conect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
con.processPacket(chatPacket);
|
||||
}
|
||||
}
|
||||
|
||||
class TestChatManagerListener implements ChatManagerListener
|
||||
{
|
||||
private Chat newChat;
|
||||
|
||||
@Override
|
||||
public void chatCreated(Chat chat, boolean createdLocally)
|
||||
{
|
||||
newChat = chat;
|
||||
}
|
||||
|
||||
public Chat getNewChat()
|
||||
{
|
||||
return newChat;
|
||||
}
|
||||
}
|
||||
|
||||
private class TestChatServer extends Thread
|
||||
{
|
||||
private Packet chatPacket;
|
||||
|
||||
TestChatServer(Packet chatMsg)
|
||||
{
|
||||
chatPacket = chatMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
connection.processPacket(chatPacket);
|
||||
}
|
||||
}
|
||||
private class TestMessageListener implements MessageListener {
|
||||
private Chat msgChat;
|
||||
private int counter = 0;
|
||||
|
||||
@Override
|
||||
public void processMessage(Chat chat, Message message) {
|
||||
msgChat = chat;
|
||||
counter++;
|
||||
}
|
||||
|
||||
public Chat getChat() {
|
||||
return msgChat;
|
||||
}
|
||||
|
||||
public int getNumMessages() {
|
||||
return counter;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue