Fix UOE being thrown in BlockingCommandManager

the manager must use a copy of the BlockListIQ's JID list, since it may
be the empty list which is not modifiable.

Also rename the getter methods from getJids() to getBlockedJids().
This commit is contained in:
Florian Schmaus 2016-11-23 17:52:04 +01:00
parent da58b20b53
commit 1b1e88c4c3
3 changed files with 21 additions and 10 deletions

View File

@ -169,7 +169,7 @@ public final class BlockingCommandManager extends Manager {
BlockListIQ blockListIQ = new BlockListIQ();
BlockListIQ blockListIQResult = connection().createPacketCollectorAndSend(blockListIQ).nextResultOrThrow();
blockListCached = blockListIQResult.getJids();
blockListCached = blockListIQResult.getBlockedJidsCopy();
return Collections.unmodifiableList(blockListCached);
}

View File

@ -16,6 +16,7 @@
*/
package org.jivesoftware.smackx.blocking.element;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -52,9 +53,10 @@ public class BlockListIQ extends IQ {
public BlockListIQ(List<Jid> jids) {
super(ELEMENT, NAMESPACE);
if (jids == null) {
jids = Collections.emptyList();
this.jids = Collections.emptyList();
} else {
this.jids = Collections.unmodifiableList(jids);
}
this.jids = jids;
}
/**
@ -65,14 +67,23 @@ public class BlockListIQ extends IQ {
}
/**
* Get the JIDs.
* Get the JIDs as unmodifiable list.
*
* @return the JIDs
* @return the blocked JIDs
*/
public List<Jid> getJids() {
public List<Jid> getBlockedJids() {
return jids;
}
/**
* Get a copy of the blocked list JIDs. This copy is modifiable.
*
* @return the blocked JIDs
*/
public List<Jid> getBlockedJidsCopy() {
return new ArrayList<>(getBlockedJids());
}
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
if (jids.isEmpty()) {

View File

@ -47,13 +47,13 @@ public class GetBlockingListTest {
public void checkBlockListIQ() throws Exception {
IQ iq = (IQ) PacketParserUtils.parseStanza(blockListIQExample);
BlockListIQ blockListIQ = (BlockListIQ) iq;
Assert.assertEquals(2, blockListIQ.getJids().size());
Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockListIQ.getJids().get(0));
Assert.assertEquals(JidCreate.from("iago@shakespeare.lit"), blockListIQ.getJids().get(1));
Assert.assertEquals(2, blockListIQ.getBlockedJids().size());
Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockListIQ.getBlockedJids().get(0));
Assert.assertEquals(JidCreate.from("iago@shakespeare.lit"), blockListIQ.getBlockedJids().get(1));
IQ iq2 = (IQ) PacketParserUtils.parseStanza(emptyBlockListIQExample);
BlockListIQ emptyBlockListIQ = (BlockListIQ) iq2;
Assert.assertEquals(0, emptyBlockListIQ.getJids().size());
Assert.assertEquals(0, emptyBlockListIQ.getBlockedJids().size());
}
}