From bba53387a450d50c6ba5d39ac3545fdf51f74469 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 10 Oct 2020 19:27:30 -0700 Subject: [PATCH] Added AcidIsland test class --- .../world/bentobox/acidisland/AcidIsland.java | 14 +- .../bentobox/acidisland/AISettingsTest.java | 2 +- .../bentobox/acidisland/AcidIslandTest.java | 269 ++++++++++++++++++ 3 files changed, 275 insertions(+), 10 deletions(-) create mode 100644 src/test/java/world/bentobox/acidisland/AcidIslandTest.java diff --git a/src/main/java/world/bentobox/acidisland/AcidIsland.java b/src/main/java/world/bentobox/acidisland/AcidIsland.java index e00db0e..ccedb2c 100644 --- a/src/main/java/world/bentobox/acidisland/AcidIsland.java +++ b/src/main/java/world/bentobox/acidisland/AcidIsland.java @@ -1,5 +1,6 @@ package world.bentobox.acidisland; +import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.WorldCreator; @@ -100,33 +101,28 @@ public class AcidIsland extends GameModeAddon { return settings; } - @Override - public void log(String string) { - getPlugin().log(string); - } - /* (non-Javadoc) * @see world.bentobox.bentobox.api.addons.GameModeAddon#createWorlds() */ @Override public void createWorlds() { String worldName = settings.getWorldName().toLowerCase(); - if (getServer().getWorld(worldName) == null) { - getLogger().info("Creating AcidIsland..."); + if (Bukkit.getWorld(worldName) == null) { + log("Creating AcidIsland..."); } // Create the world if it does not exist chunkGenerator = new ChunkGeneratorWorld(this); islandWorld = getWorld(worldName, World.Environment.NORMAL, chunkGenerator); // Make the nether if it does not exist if (settings.isNetherGenerate()) { - if (getServer().getWorld(worldName + NETHER) == null) { + if (Bukkit.getWorld(worldName + NETHER) == null) { log("Creating AcidIsland's Nether..."); } netherWorld = settings.isNetherIslands() ? getWorld(worldName, World.Environment.NETHER, chunkGenerator) : getWorld(worldName, World.Environment.NETHER, null); } // Make the end if it does not exist if (settings.isEndGenerate()) { - if (getServer().getWorld(worldName + THE_END) == null) { + if (Bukkit.getWorld(worldName + THE_END) == null) { log("Creating AcidIsland's End World..."); } endWorld = settings.isEndIslands() ? getWorld(worldName, World.Environment.THE_END, chunkGenerator) : getWorld(worldName, World.Environment.THE_END, null); diff --git a/src/test/java/world/bentobox/acidisland/AISettingsTest.java b/src/test/java/world/bentobox/acidisland/AISettingsTest.java index afe106a..0278808 100644 --- a/src/test/java/world/bentobox/acidisland/AISettingsTest.java +++ b/src/test/java/world/bentobox/acidisland/AISettingsTest.java @@ -1740,7 +1740,7 @@ public class AISettingsTest { */ @Test public void testSetAdminCommandAliases() { - s.setAdminCommand("adm"); + s.setAdminCommandAliases("adm"); assertEquals("adm", s.getAdminCommandAliases()); } diff --git a/src/test/java/world/bentobox/acidisland/AcidIslandTest.java b/src/test/java/world/bentobox/acidisland/AcidIslandTest.java new file mode 100644 index 0000000..59dda5e --- /dev/null +++ b/src/test/java/world/bentobox/acidisland/AcidIslandTest.java @@ -0,0 +1,269 @@ +/** + * + */ +package world.bentobox.acidisland; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +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.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +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.entity.Player; +import org.bukkit.plugin.PluginManager; +import org.junit.After; +import org.junit.Before; +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.acidisland.world.ChunkGeneratorWorld; +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.addons.AddonDescription; +import world.bentobox.bentobox.api.configuration.Config; +import world.bentobox.bentobox.api.user.User; +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; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Config.class }) +public class AcidIslandTest { + + /** + * Class under test + */ + private AcidIsland addon; + + @Mock + private User user; + @Mock + private IslandsManager im; + @Mock + private Island island; + @Mock + private BentoBox plugin; + @Mock + private FlagsManager fm; + @Mock + private Settings settings; + + /** + * @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()); + // 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 = 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 + island = mock(Island.class); + 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) invocation -> invocation.getArgument(0, String.class)); + + // Server + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + Server server = mock(Server.class); + when(Bukkit.getServer()).thenReturn(server); + when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); + when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class)); + when(Bukkit.getWorld(anyString())).thenReturn(null); + + // Addon + addon = new AcidIsland(); + File jFile = new File("addon.jar"); + List lines = Arrays.asList("# AcidIsland Configuration", "uniqueId: config"); + Path path = Paths.get("config.yml"); + Files.write(path, lines, Charset.forName("UTF-8")); + 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); + } + } + } + File dataFolder = new File("addons/AcidIsland"); + addon.setDataFolder(dataFolder); + addon.setFile(jFile); + AddonDescription desc = new AddonDescription.Builder("bentobox", "AcidIsland", "1.3").description("test").authors("tasty").build(); + addon.setDescription(desc); + // Addons manager + AddonsManager am = mock(AddonsManager.class); + when(plugin.getAddonsManager()).thenReturn(am); + + // Flags manager + when(plugin.getFlagsManager()).thenReturn(fm); + when(fm.getFlags()).thenReturn(Collections.emptyList()); + + // Settings + when(plugin.getSettings()).thenReturn(settings); + + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + new File("addon.jar").delete(); + new File("config.yml").delete(); + new File("addons/acidisland","config.yml").delete(); + new File("addons/acidisland").delete(); + new File("addons").delete(); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#onLoad()}. + */ + @Test + public void testOnLoad() { + addon.onLoad(); + // Check that config.yml file has been saved + File check = new File("addons/AcidIsland","config.yml"); + assertTrue(check.exists()); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#onEnable()}. + */ + @Test + public void testOnEnable() { + testOnLoad(); + addon.onEnable(); + assertTrue(addon.getPlayerCommand().isPresent()); + assertTrue(addon.getAdminCommand().isPresent()); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#onReload()}. + */ + @Test + public void testOnReload() { + addon.onReload(); + // Check that config.yml file has been saved + File check = new File("addons/AcidIsland","config.yml"); + assertTrue(check.exists()); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#createWorlds()}. + */ + @Test + public void testCreateWorlds() { + addon.onLoad(); + addon.createWorlds(); + Mockito.verify(plugin).log("[AcidIsland] Creating AcidIsland..."); + Mockito.verify(plugin).log("[AcidIsland] Creating AcidIsland's Nether..."); + Mockito.verify(plugin).log("[AcidIsland] Creating AcidIsland's End World..."); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#getSettings()}. + */ + @Test + public void testGetSettings() { + addon.onLoad(); + assertNotNull(addon.getSettings()); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#getWorldSettings()}. + */ + @Test + public void testGetWorldSettings() { + addon.onLoad(); + assertEquals(addon.getSettings(), addon.getWorldSettings()); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#getDefaultWorldGenerator(java.lang.String, java.lang.String)}. + */ + @Test + public void testGetDefaultWorldGeneratorStringString() { + assertNull(addon.getDefaultWorldGenerator("", "")); + addon.onLoad(); + addon.createWorlds(); + assertNotNull(addon.getDefaultWorldGenerator("", "")); + assertTrue(addon.getDefaultWorldGenerator("", "") instanceof ChunkGeneratorWorld); + } + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#allLoaded()}. + */ + @Test + public void testAllLoaded() { + addon.allLoaded(); + } + + + /** + * Test method for {@link world.bentobox.acidisland.AcidIsland#saveWorldSettings()}. + */ + @Test + public void testSaveWorldSettings() { + addon.saveWorldSettings(); + } + +}