Update to BentoBox 2.0.0 API

This commit is contained in:
tastybento 2023-11-12 12:35:38 -08:00
parent 246a4d4185
commit 25fba16138
2 changed files with 202 additions and 162 deletions

View File

@ -59,7 +59,7 @@
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.19.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.23.0</bentobox.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->

View File

@ -4,20 +4,26 @@ 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.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.beans.IntrospectionException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.logging.Logger;
@ -28,6 +34,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@ -43,12 +50,15 @@ 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.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup;
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.RanksManager;
import world.bentobox.bskyblock.generators.ChunkGeneratorWorld;
/**
@ -56,189 +66,219 @@ import world.bentobox.bskyblock.generators.ChunkGeneratorWorld;
*
*/
@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 BSkyBlockTest {
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
private BSkyBlock addon;
@Mock
private BentoBox plugin;
@Mock
private FlagsManager fm;
@Mock
private Settings settings;
private BSkyBlock addon;
@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);
private static AbstractDatabaseHandler<Object> h;
// 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);
@SuppressWarnings("unchecked")
@BeforeClass
public static void beforeClass() throws IllegalAccessException, InvocationTargetException, IntrospectionException {
// This has to be done beforeClass otherwise the tests will interfere with each
// other
h = mock(AbstractDatabaseHandler.class);
// Database
PowerMockito.mockStatic(DatabaseSetup.class);
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
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
@After
public void tearDown() throws IOException {
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
island = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
private void deleteAll(File file) throws IOException {
if (file.exists()) {
Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
// 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));
/**
* @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);
// Addon
addon = new BSkyBlock();
File jFile = new File("addon.jar");
List<String> lines = Arrays.asList("# BSkyBlock 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())) {
// 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);
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/BSkyBlock");
addon.setDataFolder(dataFolder);
addon.setFile(jFile);
AddonDescription desc = new AddonDescription.Builder("bentobox", "bskyblock", "1.3").description("test").authors("tasty").build();
addon.setDescription(desc);
// Addons manager
AddonsManager am = mock(AddonsManager.class);
when(plugin.getAddonsManager()).thenReturn(am);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Flags manager
when(plugin.getFlagsManager()).thenReturn(fm);
when(fm.getFlags()).thenReturn(Collections.emptyList());
// 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);
// Settings
when(plugin.getSettings()).thenReturn(settings);
// 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));
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
new File("addon.jar").delete();
new File("config.yml").delete();
new File("addons/BSkyBlock","config.yml").delete();
new File("addons/BSkyBlock").delete();
new File("addons").delete();
}
// Addon
addon = new BSkyBlock();
File jFile = new File("addon.jar");
List<String> lines = Arrays.asList("# BSkyBlock 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())) {
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#onLoad()}.
*/
@Test
public void testOnLoad() {
addon.onLoad();
// Check that config.yml file has been saved
File check = new File("addons/BSkyBlock","config.yml");
assertTrue(check.exists());
}
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/BSkyBlock");
addon.setDataFolder(dataFolder);
addon.setFile(jFile);
AddonDescription desc = new AddonDescription.Builder("bentobox", "bskyblock", "1.3").description("test")
.authors("tasty").build();
addon.setDescription(desc);
// Addons manager
AddonsManager am = mock(AddonsManager.class);
when(plugin.getAddonsManager()).thenReturn(am);
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#onEnable()}.
*/
@Test
public void testOnEnable() {
testOnLoad();
addon.onEnable();
assertTrue(addon.getPlayerCommand().isPresent());
assertTrue(addon.getAdminCommand().isPresent());
}
// Flags manager
when(plugin.getFlagsManager()).thenReturn(fm);
when(fm.getFlags()).thenReturn(Collections.emptyList());
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#onReload()}.
*/
@Test
public void testOnReload() {
addon.onReload();
// Check that config.yml file has been saved
File check = new File("addons/BSkyBlock","config.yml");
assertTrue(check.exists());
}
// Settings
when(plugin.getSettings()).thenReturn(settings);
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#createWorlds()}.
*/
@Test
public void testCreateWorlds() {
addon.onLoad();
addon.createWorlds();
Mockito.verify(plugin).log("[bskyblock] Creating BSkyBlock world ...");
Mockito.verify(plugin).log("[bskyblock] Creating BSkyBlock's Nether...");
Mockito.verify(plugin).log("[bskyblock] Creating BSkyBlock's End World...");
}
// RanksManager
RanksManager rm = new RanksManager();
when(plugin.getRanksManager()).thenReturn(rm);
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#getSettings()}.
*/
@Test
public void testGetSettings() {
addon.onLoad();
assertNotNull(addon.getSettings());
}
}
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#getWorldSettings()}.
*/
@Test
public void testGetWorldSettings() {
addon.onLoad();
assertEquals(addon.getSettings(), addon.getWorldSettings());
}
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#onLoad()}.
*/
@Test
public void testOnLoad() {
addon.onLoad();
// Check that config.yml file has been saved
File check = new File("addons/BSkyBlock", "config.yml");
assertTrue(check.exists());
}
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#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.bskyblock.BSkyBlock#onEnable()}.
*/
@Test
public void testOnEnable() {
testOnLoad();
addon.onEnable();
assertTrue(addon.getPlayerCommand().isPresent());
assertTrue(addon.getAdminCommand().isPresent());
}
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#onReload()}.
*/
@Test
public void testOnReload() {
addon.onReload();
// Check that config.yml file has been saved
File check = new File("addons/BSkyBlock", "config.yml");
assertTrue(check.exists());
}
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#createWorlds()}.
*/
@Test
public void testCreateWorlds() {
addon.onLoad();
addon.createWorlds();
Mockito.verify(plugin).log("[bskyblock] Creating BSkyBlock world ...");
Mockito.verify(plugin).log("[bskyblock] Creating BSkyBlock's Nether...");
Mockito.verify(plugin).log("[bskyblock] Creating BSkyBlock's End World...");
}
/**
* Test method for {@link world.bentobox.bskyblock.BSkyBlock#getSettings()}.
*/
@Test
public void testGetSettings() {
addon.onLoad();
assertNotNull(addon.getSettings());
}
/**
* Test method for
* {@link world.bentobox.bskyblock.BSkyBlock#getWorldSettings()}.
*/
@Test
public void testGetWorldSettings() {
addon.onLoad();
assertEquals(addon.getSettings(), addon.getWorldSettings());
}
/**
* Test method for
* {@link world.bentobox.bskyblock.BSkyBlock#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);
}
}