mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-09 18:29:45 +02:00
Add Roster.addSubscribeListener()
Remove unnecessary 'Unsubscribe' case in Roster: It is not required to acknowledge these.
This commit is contained in:
parent
8db0403138
commit
2365c4c208
6 changed files with 296 additions and 33 deletions
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.igniterealtime.smack.inttest.util;
|
||||
|
||||
public class SimpleResultSyncPoint extends ResultSyncPoint<Boolean, Exception> {
|
||||
|
||||
public void signal() {
|
||||
signal(Boolean.TRUE);
|
||||
}
|
||||
|
||||
public void signalFailure() {
|
||||
signalFailure("Unspecified failure");
|
||||
}
|
||||
|
||||
public void signalFailure(String failureMessage) {
|
||||
signal(new Exception(failureMessage));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.roster;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
|
||||
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
|
||||
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
|
||||
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
|
||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.SmackException.NotLoggedInException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.roster.packet.RosterPacket.ItemType;
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
public class RosterIntegrationTest extends AbstractSmackIntegrationTest {
|
||||
|
||||
private final Roster rosterOne;
|
||||
private final Roster rosterTwo;
|
||||
|
||||
public RosterIntegrationTest(SmackIntegrationTestEnvironment environment) {
|
||||
super(environment);
|
||||
rosterOne = Roster.getInstanceFor(conOne);
|
||||
rosterTwo = Roster.getInstanceFor(conTwo);
|
||||
}
|
||||
|
||||
@SmackIntegrationTest
|
||||
public void subscribeRequestListenerTest() throws TimeoutException, Exception {
|
||||
ensureBothAccountsAreNotInEachOthersRoster();
|
||||
|
||||
rosterTwo.setSubscribeListener(new SubscribeListener() {
|
||||
@Override
|
||||
public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
|
||||
if (from.equals(conOne.getUser().asBareJid())) {
|
||||
return SubscribeAnswer.Approve;
|
||||
}
|
||||
return SubscribeAnswer.Deny;
|
||||
}
|
||||
});
|
||||
|
||||
final String conTwosRosterName = "ConTwo " + testRunId;
|
||||
final SimpleResultSyncPoint addedAndSubscribed = new SimpleResultSyncPoint();
|
||||
rosterOne.addRosterListener(new AbstractRosterListener() {
|
||||
@Override
|
||||
public void entriesAdded(Collection<Jid> addresses) {
|
||||
checkIfAddedAndSubscribed(addresses);
|
||||
}
|
||||
@Override
|
||||
public void entriesUpdated(Collection<Jid> addresses) {
|
||||
checkIfAddedAndSubscribed(addresses);
|
||||
}
|
||||
private void checkIfAddedAndSubscribed(Collection<Jid> addresses) {
|
||||
for (Jid jid : addresses) {
|
||||
if (!jid.equals(conTwo.getUser().asBareJidString())) {
|
||||
continue;
|
||||
}
|
||||
RosterEntry rosterEntry = rosterOne.getEntry(conTwo.getUser().asBareJid());
|
||||
if (!rosterEntry.getName().equals(conTwosRosterName)) {
|
||||
addedAndSubscribed.signalFailure("Roster name does not match");
|
||||
return;
|
||||
}
|
||||
if (!rosterEntry.getType().equals(ItemType.to)) {
|
||||
return;
|
||||
}
|
||||
addedAndSubscribed.signal();
|
||||
}
|
||||
}
|
||||
});
|
||||
rosterOne.createEntry(conTwo.getUser().asBareJid(), conTwosRosterName, null);
|
||||
|
||||
assertTrue(addedAndSubscribed.waitForResult(2 * connection.getPacketReplyTimeout()));
|
||||
}
|
||||
|
||||
private void ensureBothAccountsAreNotInEachOthersRoster() throws NotLoggedInException,
|
||||
NoResponseException, XMPPErrorException, NotConnectedException,
|
||||
InterruptedException {
|
||||
notInRoster(conOne, conTwo);
|
||||
notInRoster(conTwo, conOne);
|
||||
}
|
||||
|
||||
private void notInRoster(XMPPConnection c1, XMPPConnection c2) throws NotLoggedInException,
|
||||
NoResponseException, XMPPErrorException, NotConnectedException,
|
||||
InterruptedException {
|
||||
Roster roster = Roster.getInstanceFor(c1);
|
||||
RosterEntry c2Entry = roster.getEntry(c2.getUser().asBareJid());
|
||||
if (c2Entry == null) {
|
||||
return;
|
||||
}
|
||||
roster.removeEntry(c2Entry);
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
../../../../../../../../smack-im/src/main/java/org/jivesoftware/smack/roster/package-info.java
|
Loading…
Add table
Add a link
Reference in a new issue