Update tests

This commit is contained in:
tastybento 2023-11-18 14:44:49 -08:00
parent 3a3c8a320c
commit 1a4077be8c
1 changed files with 198 additions and 201 deletions

View File

@ -72,232 +72,229 @@ import world.bentobox.level.listeners.JoinLeaveListener;
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class}) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class })
public class LevelTest { public class LevelTest {
private static File jFile; private static File jFile;
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im; private IslandsManager im;
@Mock @Mock
private Island island; private Island island;
@Mock @Mock
private BentoBox plugin; private BentoBox plugin;
@Mock @Mock
private FlagsManager fm; private FlagsManager fm;
@Mock @Mock
private GameModeAddon gameMode; private GameModeAddon gameMode;
@Mock @Mock
private AddonsManager am; private AddonsManager am;
@Mock @Mock
private BukkitScheduler scheduler; private BukkitScheduler scheduler;
@Mock @Mock
private Settings pluginSettings; private Settings pluginSettings;
private Level addon; private Level addon;
@Mock @Mock
private Logger logger; private Logger logger;
@Mock @Mock
private PlaceholdersManager phm; private PlaceholdersManager phm;
@Mock @Mock
private CompositeCommand cmd; private CompositeCommand cmd;
@Mock @Mock
private CompositeCommand adminCmd; private CompositeCommand adminCmd;
@Mock @Mock
private World world; private World world;
private UUID uuid; private UUID uuid;
@Mock @Mock
private PluginManager pim; private PluginManager pim;
@Mock @Mock
private BlockConfig blockConfig; private BlockConfig blockConfig;
@BeforeClass @BeforeClass
public static void beforeClass() throws IOException { public static void beforeClass() throws IOException {
// Make the addon jar // Make the addon jar
jFile = new File("addon.jar"); jFile = new File("addon.jar");
// Copy over config file from src folder // Copy over config file from src folder
Path fromPath = Paths.get("src/main/resources/config.yml"); Path fromPath = Paths.get("src/main/resources/config.yml");
Path path = Paths.get("config.yml"); Path path = Paths.get("config.yml");
Files.copy(fromPath, path); Files.copy(fromPath, path);
// Copy over block config file from src folder // Copy over block config file from src folder
fromPath = Paths.get("src/main/resources/blockconfig.yml"); fromPath = Paths.get("src/main/resources/blockconfig.yml");
path = Paths.get("blockconfig.yml"); path = Paths.get("blockconfig.yml");
Files.copy(fromPath, path); Files.copy(fromPath, path);
try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) { try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) {
//Added the new files to the jar. // Added the new files to the jar.
try (FileInputStream fis = new FileInputStream(path.toFile())) { try (FileInputStream fis = new FileInputStream(path.toFile())) {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int bytesRead = 0; int bytesRead = 0;
JarEntry entry = new JarEntry(path.toString()); JarEntry entry = new JarEntry(path.toString());
tempJarOutputStream.putNextEntry(entry); tempJarOutputStream.putNextEntry(entry);
while((bytesRead = fis.read(buffer)) != -1) { while ((bytesRead = fis.read(buffer)) != -1) {
tempJarOutputStream.write(buffer, 0, bytesRead); tempJarOutputStream.write(buffer, 0, bytesRead);
} }
} }
} }
} }
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Set up plugin // Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger()); when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
// The database type has to be created one line before the thenReturn() to work! // The database type has to be created one line before the thenReturn() to work!
DatabaseType value = DatabaseType.JSON; DatabaseType value = DatabaseType.JSON;
when(plugin.getSettings()).thenReturn(pluginSettings); when(plugin.getSettings()).thenReturn(pluginSettings);
when(pluginSettings.getDatabaseType()).thenReturn(value); when(pluginSettings.getDatabaseType()).thenReturn(value);
//when(plugin.isEnabled()).thenReturn(true); // when(plugin.isEnabled()).thenReturn(true);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
// Player // Player
Player p = mock(Player.class); Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging() // Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin); User.setPlugin(plugin);
// Island World Manager // Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class); IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm); 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));
// Player has island to begin with // Server
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); PowerMockito.mockStatic(Bukkit.class);
when(plugin.getIslands()).thenReturn(im); Server server = mock(Server.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
// Locales // Addon
// Return the reference (USE THIS IN THE FUTURE) addon = new Level();
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); 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);
addon.setSettings(new ConfigSettings());
// 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);
// Server // Player command
PowerMockito.mockStatic(Bukkit.class); @NonNull
Server server = mock(Server.class); Optional<CompositeCommand> opCmd = Optional.of(cmd);
when(Bukkit.getServer()).thenReturn(server); when(gameMode.getPlayerCommand()).thenReturn(opCmd);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); // Admin command
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class)); Optional<CompositeCommand> opAdminCmd = Optional.of(adminCmd);
when(gameMode.getAdminCommand()).thenReturn(opAdminCmd);
// Addon // Flags manager
addon = new Level(); when(plugin.getFlagsManager()).thenReturn(fm);
File dataFolder = new File("addons/Level"); when(fm.getFlags()).thenReturn(Collections.emptyList());
addon.setDataFolder(dataFolder);
addon.setFile(jFile);
AddonDescription desc = new AddonDescription.Builder("bentobox", "Level", "1.3").description("test").authors("tastybento").build();
addon.setDescription(desc);
addon.setSettings(new ConfigSettings());
// 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 // Bukkit
@NonNull PowerMockito.mockStatic(Bukkit.class);
Optional<CompositeCommand> opCmd = Optional.of(cmd); when(Bukkit.getScheduler()).thenReturn(scheduler);
when(gameMode.getPlayerCommand()).thenReturn(opCmd); ItemMeta meta = mock(ItemMeta.class);
// Admin command ItemFactory itemFactory = mock(ItemFactory.class);
Optional<CompositeCommand> opAdminCmd = Optional.of(adminCmd); when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(gameMode.getAdminCommand()).thenReturn(opAdminCmd); 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);
// Flags manager // placeholders
when(plugin.getFlagsManager()).thenReturn(fm); when(plugin.getPlaceholdersManager()).thenReturn(phm);
when(fm.getFlags()).thenReturn(Collections.emptyList());
// World
when(world.getName()).thenReturn("bskyblock-world");
// Island
when(island.getWorld()).thenReturn(world);
when(island.getOwner()).thenReturn(uuid);
}
// Bukkit /**
PowerMockito.mockStatic(Bukkit.class); * @throws java.lang.Exception
when(Bukkit.getScheduler()).thenReturn(scheduler); */
ItemMeta meta = mock(ItemMeta.class); @After
ItemFactory itemFactory = mock(ItemFactory.class); public void tearDown() throws Exception {
when(itemFactory.getItemMeta(any())).thenReturn(meta); deleteAll(new File("database"));
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 @AfterClass
when(plugin.getPlaceholdersManager()).thenReturn(phm); public static void cleanUp() throws Exception {
new File("addon.jar").delete();
new File("config.yml").delete();
new File("blockconfig.yml").delete();
deleteAll(new File("addons"));
}
// World private static void deleteAll(File file) throws IOException {
when(world.getName()).thenReturn("bskyblock-world"); if (file.exists()) {
// Island Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
when(island.getWorld()).thenReturn(world); }
when(island.getOwner()).thenReturn(uuid); }
}
/** /**
* @throws java.lang.Exception * Test method for {@link world.bentobox.level.Level#onEnable()}.
*/ */
@After @Test
public void tearDown() throws Exception { public void testOnEnable() {
deleteAll(new File("database")); addon.onEnable();
} verify(plugin).logWarning("[Level] Level Addon: No such world in blockconfig.yml : acidisland_world");
verify(plugin).log("[Level] Level hooking into BSkyBlock");
verify(cmd, times(3)).getAddon(); // 3 commands
verify(adminCmd, times(5)).getAddon(); // Five commands
// Placeholders
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_island_level"), any());
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_visited_island_level"), any());
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_points_to_next_level"), any());
for (int i = 1; i < 11; i++) {
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_top_name_" + i), any());
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_top_value_" + i), any());
}
// Commands
verify(am).registerListener(eq(addon), any(IslandActivitiesListeners.class));
verify(am).registerListener(eq(addon), any(JoinLeaveListener.class));
}
@AfterClass /**
public static void cleanUp() throws Exception { * Test method for {@link world.bentobox.level.Level#getSettings()}.
new File("addon.jar").delete(); */
new File("config.yml").delete(); @Test
new File("blockconfig.yml").delete(); public void testGetSettings() {
deleteAll(new File("addons")); addon.onEnable();
} ConfigSettings s = addon.getSettings();
assertEquals(100, s.getLevelCost());
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.level.Level#onEnable()}.
*/
@Test
public void testOnEnable() {
addon.onEnable();
verify(plugin).logWarning("[Level] Level Addon: No such world in blockconfig.yml : acidisland_world");
verify(plugin).log("[Level] Level hooking into BSkyBlock");
verify(cmd, times(3)).getAddon(); // 3 commands
verify(adminCmd, times(4)).getAddon(); // Four commands
// Placeholders
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_island_level"), any());
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_visited_island_level"), any());
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_points_to_next_level"), any());
for (int i = 1; i < 11; i++) {
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_top_name_" + i), any());
verify(phm).registerPlaceholder(eq(addon), eq("bskyblock_top_value_" + i), any());
}
// Commands
verify(am).registerListener(eq(addon), any(IslandActivitiesListeners.class));
verify(am).registerListener(eq(addon), any(JoinLeaveListener.class));
}
/**
* Test method for {@link world.bentobox.level.Level#getSettings()}.
*/
@Test
public void testGetSettings() {
addon.onEnable();
ConfigSettings s = addon.getSettings();
assertEquals(100, s.getLevelCost());
}
} }