mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-27 21:27:44 +01:00
Added FlagsManagerTest class to test the flags manager.
Added some code to the Flag, Flags and FlagManager classes to enable passing the tests. Realized that some flags had duplicate icons. This means that the getFlagByIcon() method doesn't work because there could be more than one. If we want to allow duplicate icons then we need to change the manager. Also, do we need to getFlagByIcon method?
This commit is contained in:
parent
97491b1d89
commit
9db2602f78
@ -10,7 +10,7 @@ import us.tastybento.bskyblock.api.commands.User;
|
||||
import us.tastybento.bskyblock.api.panels.PanelItem;
|
||||
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
|
||||
|
||||
public class Flag {
|
||||
public class Flag implements Comparable<Flag> {
|
||||
|
||||
public enum Type {
|
||||
PROTECTION,
|
||||
@ -115,4 +115,17 @@ public class Flag {
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Flag [id=" + id + ", icon=" + icon + ", type=" + type + ", defaultSetting=" + defaultSetting + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Flag o) {
|
||||
return getID().compareTo(o.getID());
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ import us.tastybento.bskyblock.listeners.flags.TeleportationListener;
|
||||
public class Flags {
|
||||
|
||||
public static final Flag BREAK_BLOCKS = new FlagBuilder().id("BREAK_BLOCKS").icon(Material.STONE).listener(new BreakBlocksListener()).build();
|
||||
public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.DIRT).listener(new PlaceBlocksListener()).build();
|
||||
public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.BEDROCK).listener(new PlaceBlocksListener()).build();
|
||||
|
||||
// Block interactions - all use BlockInteractionListener()
|
||||
public static final Flag ANVIL = new FlagBuilder().id("ANVIL").icon(Material.ANVIL).listener(new BlockInteractionListener()).build();
|
||||
@ -105,8 +105,8 @@ public class Flags {
|
||||
public static final Flag SHEARING = new FlagBuilder().id("SHEARING").icon(Material.SHEARS).listener(new ShearingListener()).build();
|
||||
|
||||
// Item pickup or drop
|
||||
public static final Flag ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener()).build();
|
||||
public static final Flag ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.DIRT).build();
|
||||
public static final Flag ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.BEETROOT_SOUP).allowedByDefault(true).listener(new ItemDropPickUpListener()).build();
|
||||
public static final Flag ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.BEETROOT_SEEDS).build();
|
||||
|
||||
/*
|
||||
* Settings flags (not protection flags)
|
||||
|
@ -29,17 +29,21 @@ public class FlagsManager {
|
||||
this.plugin = plugin;
|
||||
|
||||
// Register default flags
|
||||
for (Flag flag : Flags.values()) {
|
||||
registerFlag(flag);
|
||||
}
|
||||
Flags.values().forEach(this::registerFlag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new flag with BSkyBlock
|
||||
* @param flag
|
||||
* @param flag flag to be registered
|
||||
* @return true if successfully registered, false if not, e.g., because one with the same ID already exists
|
||||
*/
|
||||
public void registerFlag(Flag flag) {
|
||||
//TODO throw an exception in case someone registers a flag with an existing id?
|
||||
public boolean registerFlag(Flag flag) {
|
||||
// Check in case the flag id or icon already exists
|
||||
for (Flag fl : flags) {
|
||||
if (fl.getID().equals(flag.getID()) || fl.getIcon().equals(flag.getIcon())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
flags.add(flag);
|
||||
// If there is a listener which is not already registered, register it into Bukkit.
|
||||
flag.getListener().ifPresent(l -> {
|
||||
@ -48,35 +52,32 @@ public class FlagsManager {
|
||||
registeredListeners.add(l);
|
||||
}
|
||||
});
|
||||
|
||||
// Sorts the list
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list of all flags
|
||||
*/
|
||||
public List<Flag> getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get flag by ID
|
||||
* @param id
|
||||
* @param id unique id for this flag
|
||||
* @return Flag or null if not known
|
||||
*/
|
||||
public Flag getFlagByID(String id) {
|
||||
for (Flag flag : flags) {
|
||||
if (flag.getID().equals(id)) {
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return flags.stream().filter(flag -> flag.getID().equals(id)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get flag by icon
|
||||
* @param icon material
|
||||
* @return flag or null if it does not exist
|
||||
*/
|
||||
public Flag getFlagByIcon(Material icon) {
|
||||
for (Flag flag : flags) {
|
||||
if (flag.getIcon().equals(icon)) {
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return flags.stream().filter(flag -> flag.getIcon().equals(icon)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,117 @@
|
||||
package us.tastybento.bskyblock.managers;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.flags.Flag;
|
||||
import us.tastybento.bskyblock.api.flags.FlagBuilder;
|
||||
import us.tastybento.bskyblock.listeners.flags.BreakBlocksListener;
|
||||
import us.tastybento.bskyblock.lists.Flags;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest( { Flags.class} )
|
||||
public class FlagsManagerTest {
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
Server server = mock(Server.class);
|
||||
World world = mock(World.class);
|
||||
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
when(server.getWorld("world")).thenReturn(world);
|
||||
when(server.getVersion()).thenReturn("BSB_Mocking");
|
||||
|
||||
PluginManager pluginManager = mock(PluginManager.class);
|
||||
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
Bukkit.setServer(server);
|
||||
|
||||
SkullMeta skullMeta = mock(SkullMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
PowerMockito.mockStatic(Flags.class);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlagsManager() {
|
||||
BSkyBlock plugin = mock(BSkyBlock.class);
|
||||
assertNotNull(new FlagsManager(plugin));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterFlag() {
|
||||
BSkyBlock plugin = mock(BSkyBlock.class);
|
||||
FlagsManager fm = new FlagsManager(plugin);
|
||||
// Try to register every single flag - it should fail every time
|
||||
Flags.values().forEach(dupe -> assertFalse(fm.registerFlag(dupe)));
|
||||
// Change the ID to something random, but use every icon that is already used
|
||||
Flags.values().forEach(dupe -> {
|
||||
assertFalse(fm.registerFlag(new FlagBuilder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.icon(dupe.getIcon())
|
||||
.listener(new BreakBlocksListener())
|
||||
.build()));
|
||||
});
|
||||
// This should pass
|
||||
Flag originalFlag = new FlagBuilder().id("ORIGINAL").icon(Material.EMERALD_BLOCK).listener(new BreakBlocksListener()).build();
|
||||
assertTrue(fm.registerFlag(originalFlag));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFlags() {
|
||||
BSkyBlock plugin = mock(BSkyBlock.class);
|
||||
FlagsManager fm = new FlagsManager(plugin);
|
||||
assertThat(fm.getFlags(), is(Flags.values()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFlagByID() {
|
||||
BSkyBlock plugin = mock(BSkyBlock.class);
|
||||
FlagsManager fm = new FlagsManager(plugin);
|
||||
// Test in forward and reverse order so that any duplicates are caught
|
||||
Flags.values().stream().sorted().forEach(flag -> assertEquals(flag, fm.getFlagByID(flag.getID())));
|
||||
Flags.values().stream().sorted(Comparator.reverseOrder()).forEach(flag -> assertEquals(flag, fm.getFlagByID(flag.getID())));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFlagByIcon() {
|
||||
BSkyBlock plugin = mock(BSkyBlock.class);
|
||||
FlagsManager fm = new FlagsManager(plugin);
|
||||
// Test in forward and reverse order so that any duplicates are caught
|
||||
Flags.values().stream().sorted().forEach(flag -> assertEquals(flag, fm.getFlagByIcon(flag.getIcon())));
|
||||
Flags.values().stream().sorted(Comparator.reverseOrder()).forEach(flag -> assertEquals(flag, fm.getFlagByIcon(flag.getIcon())));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user