Added Limits test class

This commit is contained in:
tastybento 2024-07-17 13:59:45 -07:00
parent 674614eb22
commit 19373fa9b3
2 changed files with 368 additions and 4 deletions

View File

@ -1,14 +1,15 @@
package world.bentobox.limits;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
@ -30,7 +31,7 @@ public class Limits extends Addon {
private static final String LIMIT_NOT_SET = "Limit not set";
private Settings settings;
private List<GameModeAddon> gameModes;
private List<GameModeAddon> gameModes = new ArrayList<>();
private BlockLimitsListener blockLimitListener;
private JoinListener joinListener;
@ -109,9 +110,9 @@ public class Limits extends Addon {
}
/**
* Get the name of the game mode for this world
* Get the permission prefix for this world
* @param world - world
* @return game mode name or empty string if none
* @return permisdsion prefix or empty string if none
*/
public String getGameModePermPrefix(World world) {
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(GameModeAddon::getPermissionPrefix).orElse("");

View File

@ -0,0 +1,363 @@
package world.bentobox.limits;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.UnsafeValues;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
/**
* @author tastybento
*
*/
@SuppressWarnings("deprecation")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class})
public class LimitsTest {
private static File jFile;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private BentoBox plugin;
@Mock
private FlagsManager fm;
@Mock
private GameModeAddon gameMode;
@Mock
private AddonsManager am;
@Mock
private BukkitScheduler scheduler;
@Mock
private Settings pluginSettings;
@Mock
private PlaceholdersManager phm;
@Mock
private CompositeCommand cmd;
@Mock
private CompositeCommand adminCmd;
@Mock
private World world;
private UUID uuid;
@Mock
private PluginManager pim;
private Limits addon;
@BeforeClass
public static void beforeClass() throws Exception {
cleanUp();
// Make the addon jar
jFile = new File("addon.jar");
// Copy over config file from src folder
Path fromPath = Paths.get("src/main/resources/config.yml");
Path path = Paths.get("config.yml");
Files.copy(fromPath, path);
try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) {
//Added the new files to the jar.
try (FileInputStream fis = new FileInputStream(path.toFile())) {
byte[] buffer = new byte[1024];
int bytesRead = 0;
JarEntry entry = new JarEntry(path.toString());
tempJarOutputStream.putNextEntry(entry);
while((bytesRead = fis.read(buffer)) != -1) {
tempJarOutputStream.write(buffer, 0, bytesRead);
}
}
}
}
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
// The database type has to be created one line before the thenReturn() to work!
DatabaseType value = DatabaseType.JSON;
when(plugin.getSettings()).thenReturn(pluginSettings);
when(pluginSettings.getDatabaseType()).thenReturn(value);
//when(plugin.isEnabled()).thenReturn(true);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Locales
// Return the reference (USE THIS IN THE FUTURE)
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Server
PowerMockito.mockStatic(Bukkit.class);
Server server = mock(Server.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
// Addon
addon = new Limits();
File dataFolder = new File("addons/Level");
addon.setDataFolder(dataFolder);
addon.setFile(jFile);
AddonDescription desc = new AddonDescription.Builder("bentobox", "Level", "1.3").description("test").authors("tastybento").build();
addon.setDescription(desc);
// Addons manager
when(plugin.getAddonsManager()).thenReturn(am);
// One game mode
when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode));
AddonDescription desc2 = new AddonDescription.Builder("bentobox", "BSkyBlock", "1.3").description("test").authors("tasty").build();
when(gameMode.getDescription()).thenReturn(desc2);
when(gameMode.getOverWorld()).thenReturn(world);
// Player command
@NonNull
Optional<CompositeCommand> opCmd = Optional.of(cmd);
when(gameMode.getPlayerCommand()).thenReturn(opCmd);
// Admin command
Optional<CompositeCommand> opAdminCmd = Optional.of(adminCmd);
when(gameMode.getAdminCommand()).thenReturn(opAdminCmd);
// Perm prefix
when(gameMode.getPermissionPrefix()).thenReturn("bskyblock.");
// Flags manager
when(plugin.getFlagsManager()).thenReturn(fm);
when(fm.getFlags()).thenReturn(Collections.emptyList());
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(scheduler);
ItemMeta meta = mock(ItemMeta.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
UnsafeValues unsafe = mock(UnsafeValues.class);
when(unsafe.getDataVersion()).thenReturn(777);
when(Bukkit.getUnsafe()).thenReturn(unsafe);
when(Bukkit.getPluginManager()).thenReturn(pim);
// placeholders
when(plugin.getPlaceholdersManager()).thenReturn(phm);
// World
when(world.getName()).thenReturn("bskyblock-world");
// Island
when(island.getWorld()).thenReturn(world);
when(island.getOwner()).thenReturn(uuid);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
deleteAll(new File("database"));
}
@AfterClass
public static void cleanUp() throws Exception {
new File("addon.jar").delete();
new File("config.yml").delete();
deleteAll(new File("addons"));
}
private static void deleteAll(File file) throws IOException {
if (file.exists()) {
Files.walk(file.toPath())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
/**
* Test method for {@link world.bentobox.limits.Limits#onEnable()}.
*/
@Test
public void testOnEnable() {
addon.onEnable();
File f = new File("config.yml");
assertTrue(f.exists());
}
/**
* Test method for {@link world.bentobox.limits.Limits#onDisable()}.
*/
@Test
public void testOnDisable() {
addon.onDisable();
}
/**
* Test method for {@link world.bentobox.limits.Limits#getSettings()}.
*/
@Test
public void testGetSettings() {
assertNull(addon.getSettings());
addon.onEnable();
world.bentobox.limits.Settings set = addon.getSettings();
assertFalse(set.getLimits().isEmpty());
}
/**
* Test method for {@link world.bentobox.limits.Limits#getGameModes()}.
*/
@Test
public void testGetGameModes() {
assertTrue(addon.getGameModes().isEmpty());
addon.onEnable();
assertFalse(addon.getGameModes().isEmpty());
}
/**
* Test method for {@link world.bentobox.limits.Limits#getBlockLimitListener()}.
*/
@Test
public void testGetBlockLimitListener() {
assertNull(addon.getBlockLimitListener());
addon.onEnable();
assertNotNull(addon.getBlockLimitListener());
}
/**
* Test method for {@link world.bentobox.limits.Limits#inGameModeWorld(org.bukkit.World)}.
*/
@Test
public void testInGameModeWorld() {
addon.onEnable();
assertFalse(addon.inGameModeWorld(world));
when(gameMode.inWorld(world)).thenReturn(true);
assertTrue(addon.inGameModeWorld(world));
}
/**
* Test method for {@link world.bentobox.limits.Limits#getGameModeName(org.bukkit.World)}.
*/
@Test
public void testGetGameModeName() {
when(gameMode.inWorld(world)).thenReturn(true);
assertTrue(addon.getGameModeName(world).isEmpty());
addon.onEnable();
assertEquals("BSkyBlock", addon.getGameModeName(world));
}
/**
* Test method for {@link world.bentobox.limits.Limits#getGameModePermPrefix(org.bukkit.World)}.
*/
@Test
public void testGetGameModePermPrefix() {
when(gameMode.inWorld(world)).thenReturn(true);
addon.onEnable();
assertEquals("bskyblock.", addon.getGameModePermPrefix(world));
}
/**
* Test method for {@link world.bentobox.limits.Limits#isCoveredGameMode(java.lang.String)}.
*/
@Test
public void testIsCoveredGameMode() {
assertFalse(addon.isCoveredGameMode("BSkyBlock"));
addon.onEnable();
assertTrue(addon.isCoveredGameMode("BSkyBlock"));
}
/**
* Test method for {@link world.bentobox.limits.Limits#getJoinListener()}.
*/
@Test
public void testGetJoinListener() {
assertNull(addon.getJoinListener());
addon.onEnable();
assertNotNull(addon.getJoinListener());
}
}