Fixed spawn islands that could be purged as unowned

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1255
Also added a few more tests to prevent regression
This commit is contained in:
Florian CUNY 2020-03-31 14:30:27 +02:00
parent 592965a50e
commit 9e620cbbac
4 changed files with 164 additions and 15 deletions

View File

@ -118,6 +118,7 @@ public class AdminPurgeCommand extends CompositeCommand implements Listener {
Set<String> getOldIslands(int days) {
return getPlugin().getIslands().getIslands().stream()
.filter(i -> !i.isSpawn())
.filter(i -> !i.getPurgeProtected())
.filter(i -> i.getWorld().equals(this.getWorld()))
.filter(Island::isOwned)

View File

@ -49,6 +49,7 @@ public class AdminPurgeUnownedCommand extends ConfirmableCommand {
Set<String> getUnownedIslands() {
return getPlugin().getIslands().getIslands().stream()
.filter(i -> !i.isSpawn())
.filter(i -> !i.getPurgeProtected())
.filter(i -> i.getWorld().equals(this.getWorld()))
.filter(Island::isUnowned)

View File

@ -99,9 +99,6 @@ public class AdminPurgeCommandTest {
apc = new AdminPurgeCommand(ac);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
Mockito.framework().clearInlineMocks();
@ -133,7 +130,7 @@ public class AdminPurgeCommandTest {
*/
@Test
public void testCanExecuteUserStringListOfStringEmptyArgs() {
assertFalse(apc.canExecute(user, "protect", Collections.emptyList()));
assertFalse(apc.canExecute(user, "", Collections.emptyList()));
verify(user).sendMessage("commands.help.header",
"[label]",
"BSkyBlock");
@ -144,7 +141,7 @@ public class AdminPurgeCommandTest {
*/
@Test
public void testCanExecuteUserStringListOfStringWithArg() {
assertTrue(apc.canExecute(user, "protect", Collections.singletonList("23")));
assertTrue(apc.canExecute(user, "", Collections.singletonList("23")));
}
/**
@ -152,7 +149,7 @@ public class AdminPurgeCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringNotNumber() {
assertFalse(apc.execute(user, "protect", Collections.singletonList("abc")));
assertFalse(apc.execute(user, "", Collections.singletonList("abc")));
verify(user).sendMessage(eq("commands.admin.purge.number-error"));
}
@ -161,7 +158,7 @@ public class AdminPurgeCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringZero() {
assertFalse(apc.execute(user, "protect", Collections.singletonList("0")));
assertFalse(apc.execute(user, "", Collections.singletonList("0")));
verify(user).sendMessage(eq("commands.admin.purge.days-one-or-more"));
}
@ -170,7 +167,7 @@ public class AdminPurgeCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringNoIslands() {
assertTrue(apc.execute(user, "protect", Collections.singletonList("10")));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
@ -181,7 +178,7 @@ public class AdminPurgeCommandTest {
public void testExecuteUserStringListOfStringNoIslandsPurgeProtected() {
when(island.getPurgeProtected()).thenReturn(true);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apc.execute(user, "protect", Collections.singletonList("10")));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
@ -193,7 +190,7 @@ public class AdminPurgeCommandTest {
when(island.getPurgeProtected()).thenReturn(false);
when(island.getWorld()).thenReturn(mock(World.class));
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apc.execute(user, "protect", Collections.singletonList("10")));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
@ -208,7 +205,20 @@ public class AdminPurgeCommandTest {
when(island.isUnowned()).thenReturn(true);
when(island.isOwned()).thenReturn(false);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apc.execute(user, "protect", Collections.singletonList("10")));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
/**
* Makes sure that no spawn islands are deleted
*/
@Test
public void testExecuteUserStringListOfStringOnlyIslandSpawn() {
when(island.getPurgeProtected()).thenReturn(false);
when(island.getWorld()).thenReturn(world);
when(island.isSpawn()).thenReturn(true);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
@ -225,7 +235,7 @@ public class AdminPurgeCommandTest {
team.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
when(island.getMembers()).thenReturn(team);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apc.execute(user, "protect", Collections.singletonList("10")));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
@ -245,7 +255,7 @@ public class AdminPurgeCommandTest {
OfflinePlayer op = mock(OfflinePlayer.class);
when(op.getLastPlayed()).thenReturn(System.currentTimeMillis());
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(op);
assertTrue(apc.execute(user, "protect", Collections.singletonList("10")));
assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
@ -266,7 +276,7 @@ public class AdminPurgeCommandTest {
OfflinePlayer op = mock(OfflinePlayer.class);
when(op.getLastPlayed()).thenReturn(0L);
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(op);
assertFalse(apc.execute(user, "protect", Collections.singletonList("10")));
assertFalse(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("1"));
verify(user).sendMessage(eq("commands.admin.purge.confirm"), eq("[label]"), eq("bsb"));
}
@ -281,7 +291,7 @@ public class AdminPurgeCommandTest {
Optional<Island> opIsland = Optional.of(island);
when(im.getIslandById(any())).thenReturn(opIsland);
testExecuteUserStringListOfStringIslandsFound();
assertTrue(apc.execute(user, "protect", Collections.singletonList("confirm")));
assertTrue(apc.execute(user, "", Collections.singletonList("confirm")));
verify(im).deleteIsland(eq(island), eq(true), eq(null));
verify(plugin).log(eq("1 islands purged"));
verify(user).sendMessage(eq("commands.admin.purge.see-console-for-status"));

View File

@ -0,0 +1,137 @@
package world.bentobox.bentobox.api.commands.admin.purge;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import java.util.Collections;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Poslovitch
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class AdminPurgeUnownedCommandTest {
@Mock
private BentoBox plugin;
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private IslandsManager im;
private AdminPurgeCommand apc;
private AdminPurgeUnownedCommand apuc;
@Mock
private Addon addon;
@Mock
private Island island;
@Mock
private World world;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
when(ac.getWorld()).thenReturn(world);
when(ac.getAddon()).thenReturn(addon);
when(ac.getTopLabel()).thenReturn("bsb");
// Island manager
when(plugin.getIslands()).thenReturn(im);
// No islands by default
when(im.getIslands()).thenReturn(Collections.emptyList());
// IWM
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm);
// Island
when(island.getWorld()).thenReturn(world);
when(island.isSpawn()).thenReturn(false);
when(island.getPurgeProtected()).thenReturn(false);
when(island.isOwned()).thenReturn(true); // Default owned
when(island.isUnowned()).thenReturn(false);
// Command
apc = new AdminPurgeCommand(ac);
apuc = new AdminPurgeUnownedCommand(apc);
}
@After
public void tearDown() {
Mockito.framework().clearInlineMocks();
}
/**
* Makes sure no spawn islands are purged whatsoever
*/
@Test
public void testNoPurgeIfIslandIsSpawn() {
when(island.isSpawn()).thenReturn(true);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apuc.execute(user, "", Collections.emptyList()));
verify(user).sendMessage(eq("commands.admin.purge.unowned.unowned-islands"), eq("[number]"), eq("0"));
}
@Test
public void testNoPurgeIfIslandIsOwned() {
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apuc.execute(user, "", Collections.emptyList()));
verify(user).sendMessage(eq("commands.admin.purge.unowned.unowned-islands"), eq("[number]"), eq("0"));
}
@Ignore("unable to mock CompositeCommand#askConfirmation()")
@Test
public void testPurgeIfIslandIsUnowned() {
when(island.isOwned()).thenReturn(false);
when(island.isUnowned()).thenReturn(true);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apuc.execute(user, "", Collections.emptyList()));
verify(user).sendMessage(eq("commands.admin.purge.unowned.unowned-islands"), eq("[number]"), eq("1"));
}
@Test
public void testNoPurgeIfIslandIsPurgeProtected() {
when(island.getPurgeProtected()).thenReturn(true);
when(im.getIslands()).thenReturn(Collections.singleton(island));
assertTrue(apuc.execute(user, "", Collections.emptyList()));
verify(user).sendMessage(eq("commands.admin.purge.unowned.unowned-islands"), eq("[number]"), eq("0"));
}
}