summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/isode/stroke/presence/DirectedPresenceSender.java6
-rw-r--r--src/com/isode/stroke/presence/PayloadAddingPresenceSender.java7
-rw-r--r--src/com/isode/stroke/presence/PresenceOracle.java14
-rw-r--r--src/com/isode/stroke/presence/SubscriptionManager.java14
-rw-r--r--test/com/isode/stroke/presence/DirectedPresenceSenderTest.java153
-rw-r--r--test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java144
-rw-r--r--test/com/isode/stroke/presence/PresenceOracleTest.java219
7 files changed, 543 insertions, 14 deletions
diff --git a/src/com/isode/stroke/presence/DirectedPresenceSender.java b/src/com/isode/stroke/presence/DirectedPresenceSender.java
index 5af85ef..1938bec 100644
--- a/src/com/isode/stroke/presence/DirectedPresenceSender.java
+++ b/src/com/isode/stroke/presence/DirectedPresenceSender.java
@@ -47,9 +47,9 @@ public class DirectedPresenceSender implements PresenceSender {
public void addDirectedPresenceReceiver(JID jid, SendPresence sendPresence) {
directedPresenceReceivers_.add(jid);
- if (sendPresence == SendPresence.AndSendPresence && sender_.isAvailable()) {
+ if (SendPresence.AndSendPresence.equals(sendPresence) && sender_.isAvailable()) {
if (lastSentUndirectedPresence_ != null &&
- lastSentUndirectedPresence_.getType() == Presence.Type.Available) {
+ Presence.Type.Available.equals(lastSentUndirectedPresence_.getType())) {
Presence presenceCopy = new Presence(lastSentUndirectedPresence_);
presenceCopy.setTo(jid);
sender_.sendPresence(presenceCopy);
@@ -64,7 +64,7 @@ public class DirectedPresenceSender implements PresenceSender {
*/
public void removeDirectedPresenceReceiver(JID jid, SendPresence sendPresence) {
directedPresenceReceivers_.remove(jid);
- if (sendPresence == SendPresence.AndSendPresence && sender_.isAvailable()) {
+ if (SendPresence.AndSendPresence.equals(sendPresence) && sender_.isAvailable()) {
Presence presence = new Presence();
presence.setType(Presence.Type.Unavailable);
presence.setTo(jid);
diff --git a/src/com/isode/stroke/presence/PayloadAddingPresenceSender.java b/src/com/isode/stroke/presence/PayloadAddingPresenceSender.java
index ed6106b..c634555 100644
--- a/src/com/isode/stroke/presence/PayloadAddingPresenceSender.java
+++ b/src/com/isode/stroke/presence/PayloadAddingPresenceSender.java
@@ -7,6 +7,11 @@ package com.isode.stroke.presence;
import com.isode.stroke.elements.Payload;
import com.isode.stroke.elements.Presence;
+/**
+ * This presence sender adds payloads to outgoing presences.
+ *
+ * This class isn't meant to be used with directed presence.
+ */
public class PayloadAddingPresenceSender implements PresenceSender {
private Presence lastSentPresence;
private final PresenceSender sender;
@@ -25,7 +30,7 @@ public class PayloadAddingPresenceSender implements PresenceSender {
lastSentPresence = null;
}
if (payload != null) {
- Presence sentPresence = presence;
+ Presence sentPresence = new Presence(presence);
sentPresence.updatePayload(payload);
sender.sendPresence(sentPresence);
} else {
diff --git a/src/com/isode/stroke/presence/PresenceOracle.java b/src/com/isode/stroke/presence/PresenceOracle.java
index 8d63f59..e3e68cb 100644
--- a/src/com/isode/stroke/presence/PresenceOracle.java
+++ b/src/com/isode/stroke/presence/PresenceOracle.java
@@ -56,7 +56,7 @@ public class PresenceOracle {
void handleIncomingPresence(Presence presence) {
JID bareJID = presence.getFrom().toBare();
- if (presence.getType() == Presence.Type.Subscribe) {
+ if (Presence.Type.Subscribe.equals(presence.getType())) {
}
else {
Presence passedPresence = presence;
@@ -69,14 +69,14 @@ public class PresenceOracle {
}
Map<JID,Presence> jidMap = entries_.get(bareJID);
if (jidMap == null) jidMap = new HashMap<JID,Presence>();
- if (passedPresence.getFrom().isBare() && presence.getType() == Presence.Type.Unavailable) {
+ if (passedPresence.getFrom().isBare() && Presence.Type.Unavailable.equals(presence.getType())) {
/* Have a bare-JID only presence of offline */
jidMap.clear();
- } else if (passedPresence.getType() == Presence.Type.Available) {
+ } else if (Presence.Type.Available.equals(passedPresence.getType())) {
/* Don't have a bare-JID only offline presence once there are available presences */
jidMap.remove(bareJID);
}
- if (passedPresence.getType() == Presence.Type.Unavailable && jidMap.size() > 1) {
+ if (Presence.Type.Unavailable.equals(passedPresence.getType()) && jidMap.size() > 1) {
jidMap.remove(passedPresence.getFrom());
} else {
jidMap.put(passedPresence.getFrom(), passedPresence);
@@ -88,13 +88,13 @@ public class PresenceOracle {
public Presence getLastPresence(final JID jid) {
Map<JID,Presence> presenceMap = entries_.get(jid.toBare());
- if (presenceMap == null) return new Presence();
+ if (presenceMap == null) return null;
Presence i = presenceMap.get(jid);
if (i != null) {
return i;
} else {
- return new Presence();
+ return null;
}
}
@@ -110,7 +110,7 @@ public class PresenceOracle {
public Presence getHighestPriorityPresence(final JID bareJID) {
Map<JID,Presence> presenceMap = entries_.get(bareJID);
- if (presenceMap == null) return new Presence();
+ if (presenceMap == null) return null;
Presence highest = null;
for (Presence current : presenceMap.values()) {
diff --git a/src/com/isode/stroke/presence/SubscriptionManager.java b/src/com/isode/stroke/presence/SubscriptionManager.java
index 91075fc..24cfd0a 100644
--- a/src/com/isode/stroke/presence/SubscriptionManager.java
+++ b/src/com/isode/stroke/presence/SubscriptionManager.java
@@ -15,7 +15,15 @@ import com.isode.stroke.signals.Slot1;
public class SubscriptionManager {
private StanzaChannel stanzaChannel;
private SignalConnection onPresenceReceivedConnection;
-
+
+ /**
+ * This signal is emitted when a presence subscription request is
+ * received.
+ *
+ * The third parameter of this signal is the original presence stanza
+ * received. This is useful when the subscriber adds extensions to
+ * the request.
+ */
public final Signal3<JID, String, Presence> onPresenceSubscriptionRequest = new Signal3<JID, String, Presence>();
public final Signal2<JID, String> onPresenceSubscriptionRevoked = new Signal2<JID, String>();
@@ -58,10 +66,10 @@ public class SubscriptionManager {
void handleIncomingPresence(Presence presence) {
JID bareJID = presence.getFrom().toBare();
- if (presence.getType() == Presence.Type.Subscribe) {
+ if (Presence.Type.Subscribe.equals(presence.getType())) {
onPresenceSubscriptionRequest.emit(bareJID, presence.getStatus(), presence);
}
- else if (presence.getType() == Presence.Type.Unsubscribe) {
+ else if (Presence.Type.Unsubscribe.equals(presence.getType())) {
onPresenceSubscriptionRevoked.emit(bareJID, presence.getStatus());
}
}
diff --git a/test/com/isode/stroke/presence/DirectedPresenceSenderTest.java b/test/com/isode/stroke/presence/DirectedPresenceSenderTest.java
new file mode 100644
index 0000000..a40a1a9
--- /dev/null
+++ b/test/com/isode/stroke/presence/DirectedPresenceSenderTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.presence;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.elements.Presence;
+import com.isode.stroke.client.DummyStanzaChannel;
+import com.isode.stroke.presence.DirectedPresenceSender;
+import com.isode.stroke.presence.StanzaChannelPresenceSender;
+import com.isode.stroke.jid.JID;
+
+public class DirectedPresenceSenderTest {
+
+ private DummyStanzaChannel channel;
+ private StanzaChannelPresenceSender stanzaChannelPresenceSender;
+ private Presence testPresence;
+ private Presence secondTestPresence;
+
+ private DirectedPresenceSender createPresenceSender() {
+ return new DirectedPresenceSender(stanzaChannelPresenceSender);
+ }
+
+ @Before
+ public void setUp() {
+ channel = new DummyStanzaChannel();
+ testPresence = new Presence();
+ testPresence.setStatus("Foo");
+ secondTestPresence = new Presence();
+ secondTestPresence.setStatus("Bar");
+ stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel);
+ }
+
+ @Test
+ public void testSendPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence, presence);
+ }
+
+ @Test
+ public void testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+
+ testling.sendPresence(testPresence);
+
+ assertEquals(2, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence, presence);
+ presence = (Presence)(channel.sentStanzas.get(1));
+ assertEquals(testPresence.getStatus(), presence.getStatus());
+ assertEquals(new JID("alice@wonderland.lit/teaparty"), presence.getTo());
+ }
+
+ @Test
+ public void testSendPresence_DirectedPresenceWithDirectedPresenceReceivers() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+ channel.sentStanzas.clear();
+
+ testPresence.setTo(new JID("foo@bar.com"));
+ testling.sendPresence(testPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence, presence);
+ }
+
+ @Test
+ public void testAddDirectedPresenceReceiver() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+ channel.sentStanzas.clear();
+
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence.getStatus(), presence.getStatus());
+ assertEquals(new JID("alice@wonderland.lit/teaparty"), presence.getTo());
+ }
+
+ @Test
+ public void testAddDirectedPresenceReceiver_WithoutSendingPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+ channel.sentStanzas.clear();
+
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.DontSendPresence);
+
+ assertEquals(0, (channel.sentStanzas.size()));
+ }
+
+ @Test
+ public void testAddDirectedPresenceReceiver_AfterSendingDirectedPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+ secondTestPresence.setTo(new JID("foo@bar.com"));
+ testling.sendPresence(secondTestPresence);
+ channel.sentStanzas.clear();
+
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence.getStatus(), presence.getStatus());
+ assertEquals(new JID("alice@wonderland.lit/teaparty"), presence.getTo());
+ }
+
+ @Test
+ public void testRemoveDirectedPresenceReceiver() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.DontSendPresence);
+
+ testling.removeDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+ testling.sendPresence(testPresence);
+
+ assertEquals(2, (channel.sentStanzas.size()));
+ assertEquals(((Presence)(channel.sentStanzas.get(0))).getType(), Presence.Type.Unavailable);
+ assertEquals(channel.sentStanzas.get(1), testPresence);
+ }
+
+ @Test
+ public void testRemoveDirectedPresenceReceiver_WithoutSendingPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+ channel.sentStanzas.clear();
+
+ testling.removeDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.DontSendPresence);
+ testling.sendPresence(testPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ assertEquals(channel.sentStanzas.get(0), testPresence);
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java b/test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java
new file mode 100644
index 0000000..fb75a99
--- /dev/null
+++ b/test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.presence;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.elements.Presence;
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.client.DummyStanzaChannel;
+import com.isode.stroke.presence.DirectedPresenceSender;
+import com.isode.stroke.presence.StanzaChannelPresenceSender;
+import com.isode.stroke.presence.PayloadAddingPresenceSender;
+import com.isode.stroke.jid.JID;
+
+public class PayloadAddingPresenceSenderTest {
+
+ private DummyStanzaChannel stanzaChannel;
+ private StanzaChannelPresenceSender presenceSender;
+
+ private PayloadAddingPresenceSender createSender() {
+ PayloadAddingPresenceSender sender = new PayloadAddingPresenceSender(presenceSender);
+ return sender;
+ }
+
+ private class MyPayload extends Payload {
+
+ public MyPayload() {
+
+ }
+
+ public MyPayload(final String body) {
+ this.body = body;
+ }
+
+ public String body = "";
+ };
+
+ @Before
+ public void setUp() {
+ stanzaChannel = new DummyStanzaChannel();
+ presenceSender = new StanzaChannelPresenceSender(stanzaChannel);
+ }
+
+ @Test
+ public void testSetPayloadAddsPayloadOnPresenceSend() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.setPayload(new MyPayload("foo"));
+ testling.sendPresence(new Presence("bar"));
+
+ assertEquals(1, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 0).getStatus());
+ assertNotNull(stanzaChannel.getStanzaAtIndex(new Presence(), 0).getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSetNullPayloadDoesNotAddPayloadOnPresenceSend() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.setPayload(null);
+ testling.sendPresence(new Presence("bar"));
+
+ assertEquals(1, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 0).getStatus());
+ assertNull(stanzaChannel.getStanzaAtIndex(new Presence(), 0).getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSendPresenceDoesNotAlterOriginalPayload() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.setPayload(new MyPayload("foo"));
+ Presence presence = new Presence("bar");
+ testling.sendPresence(presence);
+
+ assertNull(presence.getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSetPayloadAfterInitialPresenceResendsPresence() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.sendPresence(new Presence("bar"));
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(2, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 1).getStatus());
+ assertNotNull(stanzaChannel.getStanzaAtIndex(new Presence(), 1).getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSetPayloadAfterUnavailablePresenceDoesNotResendPresence() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.sendPresence(new Presence("bar"));
+
+ Presence presence = new Presence("bar");
+ presence.setType(Presence.Type.Unavailable);
+ testling.sendPresence(presence);
+
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(2, (stanzaChannel.sentStanzas.size()));
+ }
+
+ @Test
+ public void testSetPayloadAfterResetDoesNotResendPresence() {
+ PayloadAddingPresenceSender testling = createSender();
+ testling.sendPresence(new Presence("bar"));
+
+ testling.reset();
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(1, (stanzaChannel.sentStanzas.size()));
+ }
+
+ @Test
+ public void testSendDirectedPresenceIsNotResent() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.sendPresence(new Presence("bar"));
+ Presence directedPresence = new Presence("baz");
+ directedPresence.setTo(new JID("foo@bar.com"));
+ testling.sendPresence(directedPresence);
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(3, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 2).getStatus());
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/presence/PresenceOracleTest.java b/test/com/isode/stroke/presence/PresenceOracleTest.java
new file mode 100644
index 0000000..03aea04
--- /dev/null
+++ b/test/com/isode/stroke/presence/PresenceOracleTest.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.presence;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.elements.Presence;
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.client.DummyStanzaChannel;
+import com.isode.stroke.presence.DirectedPresenceSender;
+import com.isode.stroke.presence.StanzaChannelPresenceSender;
+import com.isode.stroke.presence.PayloadAddingPresenceSender;
+import com.isode.stroke.presence.PresenceOracle;
+import com.isode.stroke.presence.SubscriptionManager;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.signals.SignalConnection;
+import com.isode.stroke.signals.Slot2;
+import com.isode.stroke.signals.Slot3;
+import com.isode.stroke.signals.Slot1;
+import java.util.Collection;
+import java.util.Vector;
+
+public class PresenceOracleTest {
+
+ private class SubscriptionRequestInfo {
+ public JID jid;
+ public String reason;
+
+ public SubscriptionRequestInfo() {
+ jid = new JID();
+ reason = "";
+ }
+ };
+
+ private PresenceOracle oracle_;
+ private SubscriptionManager subscriptionManager_;
+ private DummyStanzaChannel stanzaChannel_;
+ private Collection<Presence> changes = new Vector<Presence>();
+ private Collection<SubscriptionRequestInfo> subscriptionRequests = new Vector<SubscriptionRequestInfo>();
+ private JID user1;
+ private JID user1alt;
+ private JID user2;
+
+ private Presence makeOnline(final String resource, int priority) {
+ Presence presence = new Presence();
+ presence.setPriority(priority);
+ presence.setFrom(new JID("alice@wonderland.lit/" + resource));
+ return presence;
+ }
+
+ private Presence makeOffline(final String resource) {
+ Presence presence = new Presence();
+ presence.setFrom(new JID("alice@wonderland.lit" + resource));
+ presence.setType(Presence.Type.Unavailable);
+ return presence;
+ }
+
+ private void handlePresenceChange(Presence newPresence) {
+ changes.add(newPresence);
+ }
+
+ private void handlePresenceSubscriptionRequest(final JID jid, final String reason) {
+ SubscriptionRequestInfo subscriptionRequest = new SubscriptionRequestInfo();
+ subscriptionRequest.jid = jid;
+ subscriptionRequest.reason = reason;
+ subscriptionRequests.add(subscriptionRequest);
+ }
+
+ private Presence createPresence(final JID jid) {
+ Presence sentPresence = new Presence("blarb");
+ sentPresence.setFrom(jid);
+ return sentPresence;
+ }
+
+ @Before
+ public void setUp() {
+ stanzaChannel_ = new DummyStanzaChannel();
+ oracle_ = new PresenceOracle(stanzaChannel_);
+ oracle_.onPresenceChange.connect(new Slot1<Presence>() {
+ @Override
+ public void call(Presence p) {
+ handlePresenceChange(p);
+ }
+ });
+ subscriptionManager_ = new SubscriptionManager(stanzaChannel_);
+ subscriptionManager_.onPresenceSubscriptionRequest.connect(new Slot3<JID, String, Presence>() {
+ @Override
+ public void call(JID j, String s, Presence p) {
+ handlePresenceSubscriptionRequest(j, s);
+ }
+ });
+ user1 = new JID("user1@foo.com/Foo");
+ user1alt = new JID("user1@foo.com/Bar");
+ user2 = new JID("user2@bar.com/Bar");
+ }
+
+ @Test
+ public void testHighestPresenceSingle() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ assertEquals(null, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOff);
+ assertEquals(fiveOff, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testHighestPresenceMultiple() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ Presence tenOn = makeOnline("bert", 10);
+ Presence tenOff = makeOffline("/bert");
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOn);
+ assertEquals(tenOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOff);
+ assertEquals(tenOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOff);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testHighestPresenceGlobal() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ Presence tenOn = makeOnline("bert", 10);
+ Presence allOff = makeOffline("");
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOn);
+ stanzaChannel_.onPresenceReceived.emit(allOff);
+ assertEquals(allOff, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testHighestPresenceChangePriority() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ Presence tenOn = makeOnline("bert", 10);
+ Presence tenOnThree = makeOnline("bert", 3);
+ Presence tenOff = makeOffline("/bert");
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOnThree);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOff);
+ assertEquals(tenOnThree, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testReceivePresence() {
+ Presence sentPresence = createPresence(user1);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence);
+
+ assertEquals(1, (changes.size()));
+ assertEquals(0, (subscriptionRequests.size()));
+ assertEquals(sentPresence, changes.toArray()[0]);
+ assertEquals(sentPresence, oracle_.getLastPresence(user1));
+ }
+
+ @Test
+ public void testReceivePresenceFromDifferentResources() {
+ Presence sentPresence1 = createPresence(user1);
+ Presence sentPresence2 = createPresence(user1alt);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence1);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence2);
+
+ assertEquals(sentPresence1, oracle_.getLastPresence(user1));
+ assertEquals(sentPresence2, oracle_.getLastPresence(user1alt));
+ }
+
+ @Test
+ public void testSubscriptionRequest() {
+ String reasonText = "Because I want to";
+ JID sentJID = new JID("me@example.com");
+
+ Presence sentPresence = new Presence();
+ sentPresence.setType(Presence.Type.Subscribe);
+ sentPresence.setFrom(sentJID);
+ sentPresence.setStatus(reasonText);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence);
+
+ assertEquals(0, (changes.size()));
+ assertEquals(1, (subscriptionRequests.size()));
+ assertEquals(sentJID, ((SubscriptionRequestInfo)subscriptionRequests.toArray()[0]).jid);
+ assertEquals(reasonText, ((SubscriptionRequestInfo)subscriptionRequests.toArray()[0]).reason);
+ }
+
+ @Test
+ public void testReconnectResetsPresences() {
+ Presence sentPresence = createPresence(user1);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence);
+ stanzaChannel_.setAvailable(false);
+ stanzaChannel_.setAvailable(true);
+
+ assertNull(oracle_.getLastPresence(user1));
+ }
+} \ No newline at end of file