From b950177ead4e69a8fc18509dc84a84f6bf9b819c Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 31 Jul 2019 23:32:20 -0700 Subject: [PATCH] Fixed permission bug where players were not added to top ten https://github.com/BentoBoxWorld/Level/issues/71 Added test case classes - still a WIP --- pom.xml | 2 +- .../java/world/bentobox/level/TopTen.java | 44 +++-- .../java/world/bentobox/level/TopTenTest.java | 180 ++++++++++++++++++ .../level/objects/TopTenDataTest.java | 114 +++++++++++ 4 files changed, 320 insertions(+), 20 deletions(-) create mode 100644 src/test/java/world/bentobox/level/TopTenTest.java create mode 100644 src/test/java/world/bentobox/level/objects/TopTenDataTest.java diff --git a/pom.xml b/pom.xml index f3361e4..d92b547 100644 --- a/pom.xml +++ b/pom.xml @@ -90,7 +90,7 @@ world.bentobox bentobox - 1.5.0 + 1.6.0-SNAPSHOT provided diff --git a/src/main/java/world/bentobox/level/TopTen.java b/src/main/java/world/bentobox/level/TopTen.java index 01c6b40..0563e6e 100644 --- a/src/main/java/world/bentobox/level/TopTen.java +++ b/src/main/java/world/bentobox/level/TopTen.java @@ -28,11 +28,11 @@ import world.bentobox.level.objects.TopTenData; * */ public class TopTen implements Listener { - private final Level addon; + private Level addon; // Top ten list of players private Map topTenList; private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25}; - private final Database handler; + private Database handler; public TopTen(Level addon) { this.addon = addon; @@ -42,6 +42,21 @@ public class TopTen implements Listener { loadTopTen(); } + /** + * Loads all the top tens from the database + */ + private void loadTopTen() { + topTenList = new HashMap<>(); + handler.loadObjects().forEach(tt -> { + World world = Bukkit.getWorld(tt.getUniqueId()); + if (world != null) { + topTenList.put(world, tt); + } else { + addon.logError("TopTen world " + tt.getUniqueId() + " is not known on server. Skipping..."); + } + }); + } + /** * Adds a player to the top ten, if the level is good enough * @@ -58,8 +73,8 @@ public class TopTen implements Listener { topTenList.get(world).setUniqueId(world.getName()); // Try and see if the player is online - Player player = addon.getServer().getPlayer(ownerUUID); - if (player != null && !player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(world) + ".intopten")) { + Player player = Bukkit.getServer().getPlayer(ownerUUID); + if (player != null && !player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(world) + "intopten")) { topTenList.get(world).remove(ownerUUID); return; } @@ -131,23 +146,14 @@ public class TopTen implements Listener { return builder.build(); } - public TopTenData getTopTenList(World world) { - return topTenList.get(world); - } - /** - * Loads all the top tens from the database + * Get the top ten list for this world + * @param world - world + * @return top ten data object */ - private void loadTopTen() { - topTenList = new HashMap<>(); - handler.loadObjects().forEach(tt -> { - World world = Bukkit.getWorld(tt.getUniqueId()); - if (world != null) { - topTenList.put(world, tt); - } else { - addon.logError("TopTen world " + tt.getUniqueId() + " is not known on server. Skipping..."); - } - }); + public TopTenData getTopTenList(World world) { + topTenList.putIfAbsent(world, new TopTenData()); + return topTenList.get(world); } /** diff --git a/src/test/java/world/bentobox/level/TopTenTest.java b/src/test/java/world/bentobox/level/TopTenTest.java new file mode 100644 index 0000000..4d0065e --- /dev/null +++ b/src/test/java/world/bentobox/level/TopTenTest.java @@ -0,0 +1,180 @@ +package world.bentobox.level; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.entity.Player; +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.powermock.api.mockito.PowerMockito; +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.database.AbstractDatabaseHandler; +import world.bentobox.bentobox.database.DatabaseSetup; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.level.objects.TopTenData; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, DatabaseSetup.class}) +public class TopTenTest { + + @Mock + private Level addon; + @Mock + private World world; + @Mock + private BentoBox plugin; + @Mock + private AbstractDatabaseHandler handler; + private List topTen; + @Mock + private IslandsManager im; + @Mock + private Player player; + @Mock + private IslandWorldManager iwm; + + @Before + public void setUp() throws Exception { + plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + when(addon.getPlugin()).thenReturn(plugin); + + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getWorld(anyString())).thenReturn(world); + Server server = mock(Server.class); + when(server.getPlayer(any(UUID.class))).thenReturn(player); + when(Bukkit.getServer()).thenReturn(server); + // Has perms + when(player.hasPermission(anyString())).thenReturn(true); + // Database + PowerMockito.mockStatic(DatabaseSetup.class); + DatabaseSetup dbSetup = mock(DatabaseSetup.class); + when(DatabaseSetup.getDatabase()).thenReturn(dbSetup); + when(dbSetup.getHandler(any())).thenReturn(handler); + // Fill the top ten + TopTenData ttd = new TopTenData(); + ttd.setUniqueId("world"); + topTen = new ArrayList<>(); + for (long i = -100; i < 100; i ++) { + ttd.addLevel(UUID.randomUUID(), i); + topTen.add(ttd); + } + when(handler.loadObjects()).thenReturn(topTen); + + // Islands + when(addon.getIslands()).thenReturn(im); + // World + when(world.getName()).thenReturn("world"); + // IWM + when(plugin.getIWM()).thenReturn(iwm); + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testTopTen() { + new TopTen(addon); + PowerMockito.verifyStatic(Bukkit.class, times(200)); // 1 + Bukkit.getWorld(eq("world")); + } + + @Test + public void testTopTenNullWorld() { + when(Bukkit.getWorld(anyString())).thenReturn(null); + new TopTen(addon); + verify(addon, times(200)).logError("TopTen world world is not known on server. Skipping..."); + } + + @Test + @Ignore("Runs differently singularly vs in order - bug somewhere") + public void testAddEntryNotOwner() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { + when(handler.loadObjects()).thenReturn(new ArrayList<>()); + TopTen tt = new TopTen(addon); + UUID ownerUUID = UUID.randomUUID(); + tt.addEntry(world, ownerUUID, 200L); + tt.getTopTenList(world).getTopTen().forEach((k,v) -> { + System.out.println(k + " --> " + v); + }); + assertEquals(0, tt.getTopTenList(world).getTopTen().size()); + } + + @Test + public void testAddEntryIsOwner() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { + when(im.isOwner(any(), any())).thenReturn(true); + when(handler.loadObjects()).thenReturn(new ArrayList<>()); + TopTen tt = new TopTen(addon); + UUID ownerUUID = UUID.randomUUID(); + tt.addEntry(world, ownerUUID, 200L); + assertTrue(tt.getTopTenList(world).getTopTen().get(ownerUUID) == 200L); + } + + @Test + public void testAddEntryIsOwnerNoPermission() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { + when(player.hasPermission(anyString())).thenReturn(false); + when(im.isOwner(any(), any())).thenReturn(true); + when(handler.loadObjects()).thenReturn(new ArrayList<>()); + TopTen tt = new TopTen(addon); + UUID ownerUUID = UUID.randomUUID(); + tt.addEntry(world, ownerUUID, 200L); + assertNull(tt + .getTopTenList(world) + .getTopTen() + .get(ownerUUID)); + } + + @Test + @Ignore + public void testGetGUI() { + fail("Not yet implemented"); // TODO + } + + @Test + @Ignore + public void testGetTopTenList() { + fail("Not yet implemented"); // TODO + } + + @Test + @Ignore + public void testRemoveEntry() { + fail("Not yet implemented"); // TODO + } + + @Test + @Ignore + public void testSaveTopTen() { + fail("Not yet implemented"); // TODO + } + +} diff --git a/src/test/java/world/bentobox/level/objects/TopTenDataTest.java b/src/test/java/world/bentobox/level/objects/TopTenDataTest.java new file mode 100644 index 0000000..a0da168 --- /dev/null +++ b/src/test/java/world/bentobox/level/objects/TopTenDataTest.java @@ -0,0 +1,114 @@ +package world.bentobox.level.objects; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tastybento + * + */ +public class TopTenDataTest { + + private Map topTen = new LinkedHashMap<>(); + private TopTenData ttd; + private UUID uuid = UUID.randomUUID(); + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Create a top ten map + for (long i = 0; i < 100; i++) { + topTen.put(UUID.randomUUID(), i); + } + // Add the top player + topTen.put(uuid, 100L); + // Add negative values + for (long i = 0; i < 100; i++) { + topTen.put(UUID.randomUUID(), - i); + } + ttd = new TopTenData(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link world.bentobox.level.objects.TopTenData#getTopTen()}. + */ + @Test + public void testGetTopTen() { + assertTrue(ttd.getTopTen().isEmpty()); + } + + /** + * Test method for {@link world.bentobox.level.objects.TopTenData#setTopTen(java.util.Map)}. + */ + @Test + public void testSetAndGetTopTen() { + ttd.setTopTen(topTen); + // Ten only + assertEquals(10, ttd.getTopTen().size()); + // Check order + long i = 100; + for (long l : ttd.getTopTen().values()) { + + assertEquals(i--, l); + } + } + + /** + * Test method for {@link world.bentobox.level.objects.TopTenData#getUniqueId()}. + */ + @Test + public void testGetUniqueId() { + assertTrue(ttd.getUniqueId().isEmpty()); + } + + /** + * Test method for {@link world.bentobox.level.objects.TopTenData#setUniqueId(java.lang.String)}. + */ + @Test + public void testSetUniqueId() { + ttd.setUniqueId("unique"); + assertEquals("unique", ttd.getUniqueId()); + } + + /** + * Test method for {@link world.bentobox.level.objects.TopTenData#addLevel(java.util.UUID, java.lang.Long)}. + */ + @Test + public void testAddAndGetLevel() { + topTen.forEach(ttd::addLevel); + topTen.keySet().forEach(k -> { + assertTrue(topTen.get(k) == ttd.getLevel(k)); + }); + } + + /** + * Test method for {@link world.bentobox.level.objects.TopTenData#remove(java.util.UUID)}. + */ + @Test + public void testRemove() { + ttd.remove(uuid); + // Check order + long i = 99; + for (long l : ttd.getTopTen().values()) { + assertEquals(i--, l); + } + } + +}