Skip to content

Commit

Permalink
Added test class for TopBlockManager. Cleaned up code and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Feb 26, 2023
1 parent 9774a59 commit 34c11dd
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 42 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ Permissions are given automatically to players as listed below. If your permissi

```
permissions:
'aoneblock.intopten':
description: Player is in the top ten.
default: true
'aoneblock.island.topblock':
description: Player can use TopBlock command
default: true
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
<version>3.0.0-M5</version>
<configuration>
<argLine>
${argLine}
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.math=ALL-UNNAMED
--add-opens java.base/java.io=ALL-UNNAMED
Expand Down
47 changes: 11 additions & 36 deletions src/main/java/world/bentobox/topblock/TopBlockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand All @@ -27,7 +23,6 @@


public class TopBlockManager implements Listener {
private static final String INTOPTEN = "intopten";
private static final TreeMap<BigInteger, String> LEVELS;
private static final BigInteger THOUSAND = BigInteger.valueOf(1000);
static {
Expand All @@ -40,6 +35,13 @@ public class TopBlockManager implements Listener {
}
private final TopBlock addon;

/**
* @param island island
* @param blockNumber the number of blocks mined this time around
* @param lifetime the lifetime number of blocks mined
* @param phaseName the name of the current phase
*
*/
public record TopTenData(Island island, int blockNumber, long lifetime, String phaseName) implements Comparable<TopTenData> {

@Override
Expand All @@ -54,6 +56,10 @@ public int compareTo(TopTenData o) {
private final List<TopTenData> topTen = new ArrayList<>();


/**
* Top Block Manager - provides methods to get data
* @param addon addon
*/
public TopBlockManager(TopBlock addon) {
this.addon = addon;

Expand Down Expand Up @@ -115,35 +121,4 @@ public List<TopTenData> getTopTen(int size) {
.toList();
}

/**
* Get the rank of the player in the rankings
* @param world - world
* @param islandId - unique ID of the island
* @return rank placing - note - placing of 1 means top ranked
*/
public int getRank(@NonNull World world, String islandId) {
Stream<TopTenData> stream = topTen.stream()
.sorted(Collections.reverseOrder());
return stream.takeWhile(x -> !x.equals(islandId)).collect(Collectors.toList()).size() + 1;
}

/**
* Checks if player has the correct top ten perm to have their level saved
* @param world
* @param targetPlayer
* @return true if player has the perm or the player is offline
*/
boolean hasTopTenPerm(@NonNull World world, @NonNull UUID targetPlayer) {
String permPrefix = addon.getPlugin().getIWM().getPermissionPrefix(world);
return Bukkit.getPlayer(targetPlayer) == null || Bukkit.getPlayer(targetPlayer).hasPermission(permPrefix + INTOPTEN);
}

/**
* Removes island from top ten
* @param uniqueId - island UUID
*/
public void deleteIsland(@NonNull String uniqueId) {
topTen.removeIf(en -> en.island().getUniqueId().equals(uniqueId));
}

}
3 changes: 0 additions & 3 deletions src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ authors: tastybento
depend: AOneBlock

permissions:
'aoneblock.intopten':
description: Player is in the top ten.
default: true
'aoneblock.island.topblock':
description: Player can use TopBlock command
default: true
222 changes: 222 additions & 0 deletions src/test/java/world/bentobox/topblock/TopBlockManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package world.bentobox.topblock;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
import world.bentobox.aoneblock.listeners.BlockListener;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.topblock.TopBlockManager.TopTenData;
import world.bentobox.topblock.config.ConfigSettings;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
public class TopBlockManagerTest {

@Mock
private TopBlock addon;
@Mock
private Island island;

private TopBlockManager tbm;
@Mock
private AOneBlock aob;
@Mock
private BlockListener bl;
@Mock
private IslandsManager im;


/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
List<OneBlockIslands> list = new ArrayList<>();
OneBlockIslands i = new OneBlockIslands(UUID.randomUUID().toString());
i.setLifetime(100);
i.setBlockNumber(100);
i.setPhaseName("phasy");
list.add(i);

// Island manager
when(addon.getIslands()).thenReturn(im);
when(im.getIslandById(anyString())).thenReturn(Optional.of(island));
// AOneBlock
when(bl.getAllIslands()).thenReturn(list);
when(aob.getBlockListener()).thenReturn(bl);
when(addon.getaOneBlock()).thenReturn(aob);
tbm = new TopBlockManager(addon);
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager#TopBlockManager(world.bentobox.topblock.TopBlock)}.
*/
@Test
public void testTopBlockManager() {
assertNotNull(tbm);
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataSame() {
TopTenData ttd = new TopTenData(island, 0, 0, "phase one");
TopTenData ttd2 = new TopTenData(island, 0, 0, "phase one");
assertEquals(ttd, ttd2);
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataBlockDifferent() {
TopTenData ttd = new TopTenData(island, 1000, 0, "phase one");
TopTenData ttd2 = new TopTenData(island, 0, 0, "phase one");
assertNotEquals(ttd, ttd2);
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataLifetimeDifferent() {
TopTenData ttd = new TopTenData(island, 0, 0, "phase one");
TopTenData ttd2 = new TopTenData(island, 0, 10000, "phase one");
assertNotEquals(ttd, ttd2);
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataPhaseDifferent() {
TopTenData ttd = new TopTenData(island, 0, 0, "phase one");
TopTenData ttd2 = new TopTenData(island, 0, 0, "phase two");
assertNotEquals(ttd, ttd2);
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataGreater() {
TopTenData ttd = new TopTenData(island, 10000, 0, "phase fifty");
TopTenData ttd2 = new TopTenData(island, 0, 0, "phase two");
List<TopTenData> list = new ArrayList<>();
list.add(ttd);
list.add(ttd2);
list = list.stream().sorted(Collections.reverseOrder()).toList();
assertEquals(ttd, list.get(0));
assertEquals(ttd2, list.get(1));
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataLess() {
TopTenData ttd = new TopTenData(island, 0, 0, "phase one");
TopTenData ttd2 = new TopTenData(island, 10000, 0, "phase fifty");
List<TopTenData> list = new ArrayList<>();
list.add(ttd);
list.add(ttd2);
list = list.stream().sorted(Collections.reverseOrder()).toList();
assertEquals(ttd2, list.get(0));
assertEquals(ttd, list.get(1));
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataGreaterLifetime() {
TopTenData ttd = new TopTenData(island, 100, 10100, "phase fifty");
TopTenData ttd2 = new TopTenData(island, 1000, 0, "phase two");
List<TopTenData> list = new ArrayList<>();
list.add(ttd);
list.add(ttd2);
list = list.stream().sorted(Collections.reverseOrder()).toList();
assertEquals(ttd, list.get(0));
assertEquals(ttd2, list.get(1));
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager.TopTenData}.
*/
@Test
public void testTopTenDataGreaterLifetime2() {
TopTenData ttd = new TopTenData(island, 100, 10100, "phase fifty");
TopTenData ttd2 = new TopTenData(island, 100, 0, "phase two");
List<TopTenData> list = new ArrayList<>();
list.add(ttd2);
list.add(ttd);
list = list.stream().sorted(Collections.reverseOrder()).toList();
assertEquals(ttd, list.get(0));
assertEquals(ttd2, list.get(1));
}


/**
* Test method for {@link world.bentobox.topblock.TopBlockManager#getOneBlockData()}.
*/
@Test
public void testGetOneBlockData() {
this.tbm.getOneBlockData();
@NonNull
List<TopTenData> list = tbm.getTopTen(10);
TopTenData t = list.get(0);
assertEquals(100, t.lifetime());
assertEquals(100, t.blockNumber());
assertEquals("phasy", t.phaseName());

}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager#formatLevel(java.lang.Long)}.
*/
@Test
public void testFormatLevel() {
ConfigSettings settings = new ConfigSettings();
settings.setShorthand(true);
when(addon.getSettings()).thenReturn(settings);
assertEquals("12.3G", tbm.formatLevel(12345678349L));
settings.setShorthand(false);
when(addon.getSettings()).thenReturn(settings);
assertEquals("12345678349", tbm.formatLevel(12345678349L));
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager#getTopTen(int)}.
*/
@Test
public void testGetTopTen() {
List<TopTenData> list = tbm.getTopTen(10);
assertTrue(list.isEmpty());
}

}

0 comments on commit 34c11dd

Please sign in to comment.