Fix test to enable RanksManager database

This commit is contained in:
tastybento 2023-11-12 12:31:33 -08:00
parent ac999a044a
commit 8ee933f2f2

View File

@ -7,21 +7,27 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.beans.IntrospectionException;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -32,6 +38,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -48,222 +55,255 @@ import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.AddonDescription; import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.RanksManager;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Config.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Config.class, DatabaseSetup.class })
public class AcidIslandTest { public class AcidIslandTest {
/** /**
* Class under test * Class under test
*/ */
private AcidIsland addon; private AcidIsland addon;
@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 Settings settings; private Settings settings;
/** private static AbstractDatabaseHandler<Object> h;
* @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 @SuppressWarnings("unchecked")
Player p = mock(Player.class); @BeforeClass
// Sometimes use Mockito.withSettings().verboseLogging() public static void beforeClass() throws IllegalAccessException, InvocationTargetException, IntrospectionException {
when(user.isOp()).thenReturn(false); // This has to be done beforeClass otherwise the tests will interfere with each
UUID uuid = UUID.randomUUID(); // other
when(user.getUniqueId()).thenReturn(uuid); h = mock(AbstractDatabaseHandler.class);
when(user.getPlayer()).thenReturn(p); // Database
when(user.getName()).thenReturn("tastybento"); PowerMockito.mockStatic(DatabaseSetup.class);
User.setPlugin(plugin); DatabaseSetup dbSetup = mock(DatabaseSetup.class);
when(DatabaseSetup.getDatabase()).thenReturn(dbSetup);
when(dbSetup.getHandler(any())).thenReturn(h);
when(h.saveObject(any())).thenReturn(CompletableFuture.completedFuture(true));
}
// Island World Manager @After
IslandWorldManager iwm = mock(IslandWorldManager.class); public void tearDown() throws IOException {
when(plugin.getIWM()).thenReturn(iwm); User.clearUsers();
Mockito.framework().clearInlineMocks();
deleteAll(new File("database"));
deleteAll(new File("database_backup"));
deleteAll(new File("addon.jar"));
deleteAll(new File("config.yml"));
deleteAll(new File("addons"));
}
// Player has island to begin with private void deleteAll(File file) throws IOException {
island = mock(Island.class); if (file.exists()) {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
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, Mockito.RETURNS_MOCKS); * @throws java.lang.Exception
Server server = mock(Server.class); */
when(Bukkit.getServer()).thenReturn(server); @Before
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); public void setUp() throws Exception {
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class)); // Set up plugin
when(Bukkit.getWorld(anyString())).thenReturn(null); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Addon // Player
addon = new AcidIsland(); Player p = mock(Player.class);
File jFile = new File("addon.jar"); // Sometimes use Mockito.withSettings().verboseLogging()
List<String> lines = Arrays.asList("# AcidIsland Configuration", "uniqueId: config"); when(user.isOp()).thenReturn(false);
Path path = Paths.get("config.yml"); UUID uuid = UUID.randomUUID();
Files.write(path, lines, Charset.forName("UTF-8")); when(user.getUniqueId()).thenReturn(uuid);
try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) { when(user.getPlayer()).thenReturn(p);
//Added the new files to the jar. when(user.getName()).thenReturn("tastybento");
try (FileInputStream fis = new FileInputStream(path.toFile())) { User.setPlugin(plugin);
byte[] buffer = new byte[1024]; // Island World Manager
int bytesRead = 0; IslandWorldManager iwm = mock(IslandWorldManager.class);
JarEntry entry = new JarEntry(path.toString()); when(plugin.getIWM()).thenReturn(iwm);
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 // Player has island to begin with
when(plugin.getFlagsManager()).thenReturn(fm); island = mock(Island.class);
when(fm.getFlags()).thenReturn(Collections.emptyList()); when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Settings // Locales
when(plugin.getSettings()).thenReturn(settings); // 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, 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
* @throws java.lang.Exception addon = new AcidIsland();
*/ File jFile = new File("addon.jar");
@After List<String> lines = Arrays.asList("# AcidIsland Configuration", "uniqueId: config");
public void tearDown() throws Exception { Path path = Paths.get("config.yml");
new File("addon.jar").delete(); Files.write(path, lines, Charset.forName("UTF-8"));
new File("config.yml").delete(); try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) {
new File("addons/acidisland","config.yml").delete(); // Added the new files to the jar.
new File("addons/acidisland").delete(); try (FileInputStream fis = new FileInputStream(path.toFile())) {
new File("addons").delete();
}
/** byte[] buffer = new byte[1024];
* Test method for {@link world.bentobox.acidisland.AcidIsland#onLoad()}. int bytesRead = 0;
*/ JarEntry entry = new JarEntry(path.toString());
@Test tempJarOutputStream.putNextEntry(entry);
public void testOnLoad() { while ((bytesRead = fis.read(buffer)) != -1) {
addon.onLoad(); tempJarOutputStream.write(buffer, 0, bytesRead);
// Check that config.yml file has been saved }
File check = new File("addons/AcidIsland","config.yml"); }
assertTrue(check.exists()); }
} 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
* Test method for {@link world.bentobox.acidisland.AcidIsland#onEnable()}. when(plugin.getFlagsManager()).thenReturn(fm);
*/ when(fm.getFlags()).thenReturn(Collections.emptyList());
@Test
public void testOnEnable() {
testOnLoad();
addon.onEnable();
assertTrue(addon.getPlayerCommand().isPresent());
assertTrue(addon.getAdminCommand().isPresent());
}
/** // Settings
* Test method for {@link world.bentobox.acidisland.AcidIsland#onReload()}. when(plugin.getSettings()).thenReturn(settings);
*/
@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());
}
/** // RanksManager
* Test method for {@link world.bentobox.acidisland.AcidIsland#createWorlds()}. RanksManager rm = new RanksManager();
*/ when(plugin.getRanksManager()).thenReturn(rm);
@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 method for {@link world.bentobox.acidisland.AcidIsland#onLoad()}.
*/ */
@Test @Test
public void testGetWorldSettings() { public void testOnLoad() {
addon.onLoad(); addon.onLoad();
assertEquals(addon.getSettings(), addon.getWorldSettings()); // 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#getDefaultWorldGenerator(java.lang.String, java.lang.String)}. * Test method for {@link world.bentobox.acidisland.AcidIsland#onEnable()}.
*/ */
@Test @Test
public void testGetDefaultWorldGeneratorStringString() { public void testOnEnable() {
assertNull(addon.getDefaultWorldGenerator("", "")); testOnLoad();
addon.onLoad(); addon.onEnable();
addon.createWorlds(); assertTrue(addon.getPlayerCommand().isPresent());
assertNotNull(addon.getDefaultWorldGenerator("", "")); assertTrue(addon.getAdminCommand().isPresent());
assertTrue(addon.getDefaultWorldGenerator("", "") instanceof ChunkGeneratorWorld); }
}
/** /**
* Test method for {@link world.bentobox.acidisland.AcidIsland#allLoaded()}. * Test method for {@link world.bentobox.acidisland.AcidIsland#onReload()}.
*/ */
@Test @Test
public void testAllLoaded() { public void testOnReload() {
addon.allLoaded(); 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#saveWorldSettings()}. * Test method for {@link world.bentobox.acidisland.AcidIsland#getSettings()}.
*/ */
@Test @Test
public void testSaveWorldSettings() { public void testGetSettings() {
addon.saveWorldSettings(); 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();
}
} }