mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2024-11-22 10:36:08 +01:00
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
This commit is contained in:
parent
da83f66f8c
commit
b950177ead
2
pom.xml
2
pom.xml
@ -90,7 +90,7 @@
|
||||
<dependency>
|
||||
<groupId>world.bentobox</groupId>
|
||||
<artifactId>bentobox</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<version>1.6.0-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -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<World,TopTenData> topTenList;
|
||||
private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
|
||||
private final Database<TopTenData> handler;
|
||||
private Database<TopTenData> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
180
src/test/java/world/bentobox/level/TopTenTest.java
Normal file
180
src/test/java/world/bentobox/level/TopTenTest.java
Normal file
@ -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<Object> handler;
|
||||
private List<Object> 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
|
||||
}
|
||||
|
||||
}
|
114
src/test/java/world/bentobox/level/objects/TopTenDataTest.java
Normal file
114
src/test/java/world/bentobox/level/objects/TopTenDataTest.java
Normal file
@ -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<UUID, Long> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user