Added Addon class unit tests.

This commit is contained in:
tastybento 2018-04-27 16:34:17 +09:00
parent 5cc71c2a2f
commit b2e68c22d4
3 changed files with 103 additions and 25 deletions

View File

@ -45,7 +45,9 @@ public abstract class Addon implements AddonInterface {
* @return the addon's default config file
*/
public FileConfiguration getConfig() {
config = loadYamlFile(ADDON_CONFIG_FILENAME);
if (config == null) {
config = loadYamlFile(ADDON_CONFIG_FILENAME);
}
return config;
}
@ -121,11 +123,11 @@ public abstract class Addon implements AddonInterface {
}
/**
* Saves the default config file
* Saves the FileConfiguration retrievable by getConfig().
*/
public void saveConfig() {
try {
config.save(new File(dataFolder, ADDON_CONFIG_FILENAME));
getConfig().save(new File(dataFolder, ADDON_CONFIG_FILENAME));
} catch (IOException e) {
Bukkit.getLogger().severe("Could not save config!");
}
@ -207,7 +209,7 @@ public abstract class Addon implements AddonInterface {
if (jarResource == null || jarResource.equals("")) {
throw new IllegalArgumentException("ResourcePath cannot be null or empty");
}
jarResource = jarResource.replace('\\', '/');
try (JarFile jar = new JarFile(file)) {
JarEntry jarConfig = jar.getJarEntry(jarResource);
@ -257,7 +259,7 @@ public abstract class Addon implements AddonInterface {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Get Players Manager
* @return Players manager
@ -265,7 +267,7 @@ public abstract class Addon implements AddonInterface {
public PlayersManager getPlayers() {
return getBSkyBlock().getPlayers();
}
/**
* Get Islands Manager
* @return Islands manager
@ -273,7 +275,7 @@ public abstract class Addon implements AddonInterface {
public IslandsManager getIslands() {
return getBSkyBlock().getIslands();
}
/**
* Get the Addon By Name
* @return Optional Addon

View File

@ -29,7 +29,7 @@ import us.tastybento.bskyblock.api.events.addon.AddonEvent;
/**
* @author Tastybento, ComminQ
*/
public final class AddonsManager {
public class AddonsManager {
private static final boolean DEBUG = false;
private static final String LOCALE_FOLDER = "locales";

View File

@ -4,16 +4,22 @@ 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.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.BeforeClass;
@ -25,11 +31,14 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.managers.AddonsManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.PlayersManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest( { BSkyBlock.class })
public class AddonTest {
@Mock
static BSkyBlock plugin;
static JavaPlugin javaPlugin;
@ -50,9 +59,16 @@ public class AddonTest {
Bukkit.setServer(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
// Mock item factory (for itemstacks)
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
ItemMeta itemMeta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
}
class TestClass extends Addon {
@ -141,65 +157,125 @@ public class AddonTest {
test.registerListener(listener);
}
/*
@Test
public void testSaveConfig() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
// This will wipe out the config.yml of BSB so I am commenting it out
//test.saveConfig();
}
@Test
public void testSaveDefaultConfig() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
File jarFile = new File("addon.jar");
File dataFolder = new File("dataFolder");
test.setDataFolder(dataFolder);
test.setAddonFile(jarFile);
test.saveDefaultConfig();
}
@Test(expected = IllegalArgumentException.class)
public void testSaveResourceStringBoolean() {
TestClass test = new TestClass();
test.saveResource("", true);
}
@Test(expected = IllegalArgumentException.class)
public void testSaveResourceStringBooleanNull() {
TestClass test = new TestClass();
test.saveResource(null, true);
}
@Test
public void testSaveResourceStringBoolean() {
fail("Not yet implemented"); // TODO
public void testSaveResourceStringBooleanNoFile() throws IOException {
TestClass test = new TestClass();
File jarFile = new File("addon.jar");
File dataFolder = new File("dataFolder");
test.setDataFolder(dataFolder);
test.setAddonFile(jarFile);
test.saveResource("no_such_file", true);
}
@Test
public void testSaveResourceStringFileBooleanBoolean() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
File jarFile = new File("addon.jar");
File dataFolder = new File("dataFolder");
test.setDataFolder(dataFolder);
test.setAddonFile(jarFile);
test.saveResource("no_such_file", jarFile, false, false);
test.saveResource("no_such_file", jarFile, false, true);
test.saveResource("no_such_file", jarFile, true, false);
test.saveResource("no_such_file", jarFile, true, true);
}
@Test
public void testGetResource() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
File jarFile = new File("addon.jar");
File dataFolder = new File("dataFolder");
test.setDataFolder(dataFolder);
test.setAddonFile(jarFile);
assertNull(test.getResource("nothing"));
}
@Test
public void testSetAddonFile() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
File jarFile = new File("addon.jar");
test.setAddonFile(jarFile);
assertEquals(jarFile, test.getFile());
}
@Test
public void testSetDataFolder() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
File dataFolder = new File("dataFolder");
test.setDataFolder(dataFolder);
assertEquals(dataFolder, test.getDataFolder());
}
@Test
public void testSetDescription() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
AddonDescription desc = new AddonDescription();
test.setDescription(desc);
assertEquals(desc, test.getDescription());
}
@Test
public void testSetEnabled() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
test.setEnabled(false);
assertFalse(test.isEnabled());
test.setEnabled(true);
assertTrue(test.isEnabled());
}
@Test
public void testGetPlayers() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
PlayersManager pm = mock(PlayersManager.class);
when(plugin.getPlayers()).thenReturn(pm);
assertEquals(pm, test.getPlayers());
}
@Test
public void testGetIslands() {
fail("Not yet implemented"); // TODO
TestClass test = new TestClass();
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
assertEquals(im, test.getIslands());
}
@Test
public void testGetAddonByName() {
fail("Not yet implemented"); // TODO
AddonsManager am = new AddonsManager(plugin);
when(plugin.getAddonsManager()).thenReturn(am);
TestClass test = new TestClass();
assertEquals(Optional.empty(),test.getAddonByName("addon"));
}
*/
}