mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-22 18:48:00 +01:00
Improve IntegrationTestRosterUtil.ensureSubscribedto()
Improve the names of the arguments and variables. And ensure that all listeners are removed at the end.
This commit is contained in:
parent
8e52e80399
commit
c31e93a00f
1 changed files with 20 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015-2018 Florian Schmaus
|
||||
* Copyright 2015-2019 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -25,11 +25,13 @@ import org.jivesoftware.smack.XMPPConnection;
|
|||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.roster.AbstractPresenceEventListener;
|
||||
import org.jivesoftware.smack.roster.PresenceEventListener;
|
||||
import org.jivesoftware.smack.roster.Roster;
|
||||
import org.jivesoftware.smack.roster.RosterEntry;
|
||||
import org.jivesoftware.smack.roster.SubscribeListener;
|
||||
|
||||
import org.jxmpp.jid.BareJid;
|
||||
import org.jxmpp.jid.EntityFullJid;
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
public class IntegrationTestRosterUtil {
|
||||
|
@ -39,41 +41,47 @@ public class IntegrationTestRosterUtil {
|
|||
ensureSubscribedTo(conTwo, conOne, timeout);
|
||||
}
|
||||
|
||||
public static void ensureSubscribedTo(final XMPPConnection conOne, final XMPPConnection conTwo, long timeout) throws TimeoutException, Exception {
|
||||
Roster rosterOne = Roster.getInstanceFor(conOne);
|
||||
Roster rosterTwo = Roster.getInstanceFor(conTwo);
|
||||
public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception {
|
||||
final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection);
|
||||
final Roster presenceRequestingRoster = Roster.getInstanceFor(presenceRequestingConnection);
|
||||
|
||||
if (rosterOne.isSubscribedToMyPresence(conTwo.getUser())) {
|
||||
final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser();
|
||||
final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser();
|
||||
|
||||
if (presenceRequestReceiverRoster.isSubscribedToMyPresence(presenceRequestingAddress)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final SubscribeListener subscribeListener = new SubscribeListener() {
|
||||
@Override
|
||||
public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
|
||||
if (from.equals(conTwo.getUser().asBareJid())) {
|
||||
if (from.equals(presenceRequestingConnection.getUser().asBareJid())) {
|
||||
return SubscribeAnswer.Approve;
|
||||
}
|
||||
return SubscribeAnswer.Deny;
|
||||
}
|
||||
};
|
||||
rosterOne.addSubscribeListener(subscribeListener);
|
||||
presenceRequestReceiverRoster.addSubscribeListener(subscribeListener);
|
||||
|
||||
final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
|
||||
rosterTwo.addPresenceEventListener(new AbstractPresenceEventListener() {
|
||||
final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {
|
||||
@Override
|
||||
public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
|
||||
if (!address.equals(conOne.getUser().asBareJid())) {
|
||||
if (!address.equals(presenceRequestReceiverAddress.asBareJid())) {
|
||||
return;
|
||||
}
|
||||
syncPoint.signal();
|
||||
}
|
||||
});
|
||||
rosterTwo.sendSubscriptionRequest(conOne.getUser().asBareJid());
|
||||
};
|
||||
presenceRequestingRoster.addPresenceEventListener(presenceEventListener);
|
||||
|
||||
try {
|
||||
presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid());
|
||||
|
||||
syncPoint.waitForResult(timeout);
|
||||
} finally {
|
||||
rosterOne.removeSubscribeListener(subscribeListener);
|
||||
presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener);
|
||||
presenceRequestingRoster.removePresenceEventListener(presenceEventListener);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue