1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 05:52:06 +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:
Florian Schmaus 2019-03-16 21:12:05 +01:00
parent 8e52e80399
commit c31e93a00f

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2015-2018 Florian Schmaus * Copyright 2015-2019 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.AbstractPresenceEventListener; import org.jivesoftware.smack.roster.AbstractPresenceEventListener;
import org.jivesoftware.smack.roster.PresenceEventListener;
import org.jivesoftware.smack.roster.Roster; import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry; import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.SubscribeListener; import org.jivesoftware.smack.roster.SubscribeListener;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
public class IntegrationTestRosterUtil { public class IntegrationTestRosterUtil {
@ -39,41 +41,47 @@ public class IntegrationTestRosterUtil {
ensureSubscribedTo(conTwo, conOne, timeout); ensureSubscribedTo(conTwo, conOne, timeout);
} }
public static void ensureSubscribedTo(final XMPPConnection conOne, final XMPPConnection conTwo, long timeout) throws TimeoutException, Exception { public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception {
Roster rosterOne = Roster.getInstanceFor(conOne); final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection);
Roster rosterTwo = Roster.getInstanceFor(conTwo); 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; return;
} }
final SubscribeListener subscribeListener = new SubscribeListener() { final SubscribeListener subscribeListener = new SubscribeListener() {
@Override @Override
public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) { public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
if (from.equals(conTwo.getUser().asBareJid())) { if (from.equals(presenceRequestingConnection.getUser().asBareJid())) {
return SubscribeAnswer.Approve; return SubscribeAnswer.Approve;
} }
return SubscribeAnswer.Deny; return SubscribeAnswer.Deny;
} }
}; };
rosterOne.addSubscribeListener(subscribeListener); presenceRequestReceiverRoster.addSubscribeListener(subscribeListener);
final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint(); final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
rosterTwo.addPresenceEventListener(new AbstractPresenceEventListener() { final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {
@Override @Override
public void presenceSubscribed(BareJid address, Presence subscribedPresence) { public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
if (!address.equals(conOne.getUser().asBareJid())) { if (!address.equals(presenceRequestReceiverAddress.asBareJid())) {
return; return;
} }
syncPoint.signal(); syncPoint.signal();
} }
}); };
rosterTwo.sendSubscriptionRequest(conOne.getUser().asBareJid()); presenceRequestingRoster.addPresenceEventListener(presenceEventListener);
try { try {
presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid());
syncPoint.waitForResult(timeout); syncPoint.waitForResult(timeout);
} finally { } finally {
rosterOne.removeSubscribeListener(subscribeListener); presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener);
presenceRequestingRoster.removePresenceEventListener(presenceEventListener);
} }
} }