1
0
Fork 0
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:
rcollier 2014-02-01 22:22:30 +00:00
parent 782448b3ec
commit 5bbf6cf224
5 changed files with 478 additions and 232 deletions

View file

@ -170,6 +170,18 @@ 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 @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {

View file

@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
@ -33,38 +34,58 @@ import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.ThreadFilter; import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.collections.ReferenceMap; import org.jivesoftware.smack.util.collections.ReferenceMap;
/** /**
* The chat manager keeps track of references to all current chats. It will not hold any references * 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)}. * made aware of new chats, register a listener by calling {@link #addChatListener(ChatManagerListener)}.
* *
* @author Alexander Wenckus * @author Alexander Wenckus
*/ */
public class ChatManager { public class ChatManager {
/*
* 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 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;
/** /**
* Returns the next unique id. Each id made up of a short alphanumeric * Defines the different modes under which a match will be attempted with an existing chat when
* prefix along with a unique numeric value. * the incoming message does not have a thread id.
*
* @return the next id.
*/ */
private static synchronized String nextID() { public enum MatchMode {
return prefix + Long.toString(id++); /**
* 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); private boolean normalIncluded = defaultIsNormalInclude;
/** /*
* Keeps track of the current increment, which is appended to the prefix to * Determines how incoming message with no thread will be matched to existing chats.
* forum a unique ID.
*/ */
private static long id = 0; private MatchMode matchMode = defaultMatchMode;
/** /**
* Maps thread ID to chat. * Maps thread ID to chat.
@ -101,11 +122,11 @@ public class ChatManager {
return false; return false;
} }
Message.Type messageType = ((Message) packet).getType(); Message.Type messageType = ((Message) packet).getType();
return messageType != Message.Type.groupchat && return (messageType == Type.chat) || (normalIncluded ? messageType == Type.normal : false);
messageType != Message.Type.headline;
} }
}; };
// 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. // messages to the best Chat instance available.
connection.addPacketListener(new PacketListener() { connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
@ -116,10 +137,6 @@ public class ChatManager {
} }
else { else {
chat = getThreadChat(message.getThread()); 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) { if(chat == null) {
@ -130,6 +147,44 @@ public class ChatManager {
}, filter); }, 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. * Creates a new chat and returns it.
* *
@ -138,12 +193,7 @@ public class ChatManager {
* @return the created chat. * @return the created chat.
*/ */
public Chat createChat(String userJID, MessageListener listener) { public Chat createChat(String userJID, MessageListener listener) {
String threadID; return createChat(userJID, null, listener);
do {
threadID = nextID();
} while (threadChats.get(threadID) != null);
return createChat(userJID, threadID, listener);
} }
/** /**
@ -191,17 +241,22 @@ public class ChatManager {
} }
/** /**
* Try to get a matching chat for the given user JID. Try the full * Try to get a matching chat for the given user JID, based on the {@link MatchMode}.
* JID map first, the try to match on the base JID if no match is * <li>NONE - return null
* found. * <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 * @param userJID jid in the from field of message.
* @return * @return Matching chat, or null if no match found.
*/ */
private Chat getUserChat(String userJID) { private Chat getUserChat(String userJID) {
if (matchMode == MatchMode.NONE) {
return null;
}
Chat match = jidChats.get(userJID); Chat match = jidChats.get(userJID);
if (match == null) { if (match == null && (matchMode == MatchMode.BARE_JID)) {
match = baseJidChats.get(StringUtils.parseBareAddress(userJID)); match = baseJidChats.get(StringUtils.parseBareAddress(userJID));
} }
return match; return match;
@ -278,4 +333,21 @@ public class ChatManager {
interceptors.put(packetInterceptor, filter); 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;
}
} }

View file

@ -80,8 +80,11 @@ public class Message extends Packet {
*/ */
public Message(String to, Type type) { public Message(String to, Type type) {
setTo(to); setTo(to);
if (type != null) {
this.type = type; this.type = type;
} }
}
/** /**
* Returns the type of the message. If no type has been set this method will return {@link * Returns the type of the message. If no type has been set this method will return {@link

View file

@ -452,6 +452,7 @@ public abstract class Packet {
return DEFAULT_LANGUAGE; return DEFAULT_LANGUAGE;
} }
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; 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); return !(xmlns != null ? !xmlns.equals(packet.xmlns) : packet.xmlns != null);
} }
@Override
public int hashCode() { public int hashCode() {
int result; int result;
result = (xmlns != null ? xmlns.hashCode() : 0); result = (xmlns != null ? xmlns.hashCode() : 0);
@ -483,4 +485,9 @@ public abstract class Packet {
result = 31 * result + (error != null ? error.hashCode() : 0); result = 31 * result + (error != null ? error.hashCode() : 0);
return result; return result;
} }
@Override
public String toString() {
return toXML();
}
} }

View file

@ -22,36 +22,27 @@ package org.jivesoftware.smack;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.jivesoftware.smack.ChatManager.MatchMode;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; 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 { public class ChatConnectionTest {
private DummyConnection connection; private DummyConnection connection;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Uncomment this to enable debug output connection = getConnection();
//Connection.DEBUG_ENABLED = true;
connection = new DummyConnection();
connection.connect();
connection.login("me", "secret");
} }
@After @After
@ -60,47 +51,176 @@ public class ChatConnectionTest {
connection.disconnect(); connection.disconnect();
} }
/**
* 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.
*/
@Test @Test
public void chatCreatedWithIncomingChatNoThreadBaseJid() public void validateDefaultSetNormalIncluded() {
{ ChatManager.setDefaultIsNormalIncluded(false);
TestChatManagerListener listener = new TestChatManagerListener(); assertFalse(getConnection().getChatManager().isNormalIncluded());
connection.getChatManager().addChatListener(listener);
Packet incomingChat = createChatPacket(null, false); ChatManager.setDefaultIsNormalIncluded(true);
processServerMessage(incomingChat); assertTrue(getConnection().getChatManager().isNormalIncluded());
Chat newChat = listener.getNewChat();
assertNotNull(newChat);
} }
/**
* 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.
*/
@Test @Test
public void chatCreatedWhenIncomingChatNoThreadFullJid() public void validateDefaultSetMatchMode() {
{ ChatManager.setDefaultMatchMode(MatchMode.NONE);
TestChatManagerListener listener = new TestChatManagerListener(); assertEquals(MatchMode.NONE, getConnection().getChatManager().getMatchMode());
connection.getChatManager().addChatListener(listener);
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); Packet incomingChat = createChatPacket(null, true);
processServerMessage(incomingChat); processServerMessage(incomingChat, con);
Chat newChat = listener.getNewChat(); Chat newChat = listener.getNewChat();
assertNotNull(newChat); 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 an existing chat created with a base jid is matched to an * Confirm that an existing chat created with a base jid is matched to an incoming chat message that has no thread
* incoming chat message that has no thread id and the user is a full jid. * id and the user is a full jid.
*/ */
@Test @Test
public void chatFoundWhenNoThreadFullJid() public void chatFoundWhenNoThreadFullJid() {
{
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); connection.getChatManager().addChatListener(listener);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
@ -114,12 +234,11 @@ public class ChatConnectionTest {
} }
/** /**
* Confirm that an existing chat created with a base jid is matched to an * Confirm that an existing chat created with a base jid is matched to an incoming chat message that has no thread
* incoming chat message that has no thread id and the user is a base jid. * id and the user is a base jid.
*/ */
@Test @Test
public void chatFoundWhenNoThreadBaseJid() public void chatFoundWhenNoThreadBaseJid() {
{
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); connection.getChatManager().addChatListener(listener);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
@ -133,12 +252,11 @@ public class ChatConnectionTest {
} }
/** /**
* Confirm that an existing chat created with a base jid is matched to an * Confirm that an existing chat created with a base jid is matched to an incoming chat message that has the same id
* incoming chat message that has the same id and the user is a full jid. * and the user is a full jid.
*/ */
@Test @Test
public void chatFoundWithSameThreadFullJid() public void chatFoundWithSameThreadFullJid() {
{
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); connection.getChatManager().addChatListener(listener);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
@ -152,12 +270,11 @@ public class ChatConnectionTest {
} }
/** /**
* Confirm that an existing chat created with a base jid is matched to an * Confirm that an existing chat created with a base jid is matched to an incoming chat message that has the same id
* incoming chat message that has the same id and the user is a base jid. * and the user is a base jid.
*/ */
@Test @Test
public void chatFoundWithSameThreadBaseJid() public void chatFoundWithSameThreadBaseJid() {
{
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); connection.getChatManager().addChatListener(listener);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
@ -171,14 +288,11 @@ public class ChatConnectionTest {
} }
/** /**
* Confirm that an existing chat created with a base jid is not matched to * Confirm that an existing chat created with a base jid is not matched to an incoming chat message that has a
* an incoming chat message that has a different id and the same user as a * different id and the same user as a base jid.
* base jid.
*/ */
@Ignore
@Test @Test
public void chatNotFoundWithDiffThreadBaseJid() public void chatNotFoundWithDiffThreadBaseJid() {
{
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); connection.getChatManager().addChatListener(listener);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
@ -192,13 +306,11 @@ public class ChatConnectionTest {
} }
/** /**
* Confirm that an existing chat created with a base jid is not matched to * Confirm that an existing chat created with a base jid is not matched to an incoming chat message that has a
* an incoming chat message that has a different id and the same base jid. * different id and the same base jid.
*/ */
@Ignore
@Test @Test
public void chatNotFoundWithDiffThreadFullJid() public void chatNotFoundWithDiffThreadFullJid() {
{
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); connection.getChatManager().addChatListener(listener);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); Chat outgoing = connection.getChatManager().createChat("you@testserver", null);
@ -211,78 +323,118 @@ public class ChatConnectionTest {
assertFalse(newChat == outgoing); assertFalse(newChat == outgoing);
} }
private Packet createChatPacket(final String threadId, final boolean isFullJid) @Test
{ public void chatNotMatchedWithTypeNormal() {
TestChatManagerListener listener = new TestChatManagerListener();
DummyConnection con = getConnection();
ChatManager cm = con.getChatManager();
cm.setNormalIncluded(false);
cm.addChatListener(listener);
Message incomingChat = createChatPacket(null, false);
incomingChat.setType(Type.normal);
processServerMessage(incomingChat);
assertNull(listener.getNewChat());
}
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); Message chatMsg = new Message("me@testserver", Message.Type.chat);
chatMsg.setBody("the body message"); chatMsg.setBody("the body message - " + System.currentTimeMillis());
chatMsg.setFrom("you@testserver" + (isFullJid ? "/resource" : "")); chatMsg.setFrom("you@testserver" + (isFullJid ? "/resource" : ""));
if (threadId != null) if (threadId != null)
chatMsg.addExtension(new PacketExtension() chatMsg.setThread(threadId);
{
@Override
public String toXML()
{
return "<thread>" + threadId + "</thread>";
}
@Override
public String getNamespace()
{
return null;
}
@Override
public String getElementName()
{
return "thread";
}
});
return chatMsg; return chatMsg;
} }
private void processServerMessage(Packet incomingChat) private void processServerMessage(Packet incomingChat) {
{ processServerMessage(incomingChat, connection);
TestChatServer chatServer = new TestChatServer(incomingChat); }
private void processServerMessage(Packet incomingChat, DummyConnection con) {
TestChatServer chatServer = new TestChatServer(incomingChat, con);
chatServer.start(); chatServer.start();
try try {
{
chatServer.join(); chatServer.join();
} catch (InterruptedException e) } catch (InterruptedException e) {
{
fail(); fail();
} }
} }
class TestChatManagerListener implements ChatManagerListener class TestChatManagerListener implements ChatManagerListener {
{
private Chat newChat; private Chat newChat;
private MessageListener listener;
@Override public TestChatManagerListener(TestMessageListener msgListener) {
public void chatCreated(Chat chat, boolean createdLocally) listener = msgListener;
{
newChat = chat;
} }
public Chat getNewChat() public TestChatManagerListener() {
{ }
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
newChat = chat;
if (listener != null)
newChat.addMessageListener(listener);
}
public Chat getNewChat() {
return newChat; return newChat;
} }
} }
private class TestChatServer extends Thread private class TestChatServer extends Thread {
{
private Packet chatPacket; private Packet chatPacket;
private DummyConnection con;
TestChatServer(Packet chatMsg) TestChatServer(Packet chatMsg, DummyConnection conect) {
{
chatPacket = chatMsg; chatPacket = chatMsg;
con = conect;
} }
@Override @Override
public void run() public void run() {
{ con.processPacket(chatPacket);
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;
}
};
} }