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 * @return the addon's default config file
*/ */
public FileConfiguration getConfig() { public FileConfiguration getConfig() {
if (config == null) {
config = loadYamlFile(ADDON_CONFIG_FILENAME); config = loadYamlFile(ADDON_CONFIG_FILENAME);
}
return config; 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() { public void saveConfig() {
try { try {
config.save(new File(dataFolder, ADDON_CONFIG_FILENAME)); getConfig().save(new File(dataFolder, ADDON_CONFIG_FILENAME));
} catch (IOException e) { } catch (IOException e) {
Bukkit.getLogger().severe("Could not save config!"); Bukkit.getLogger().severe("Could not save config!");
} }

View File

@ -29,7 +29,7 @@ import us.tastybento.bskyblock.api.events.addon.AddonEvent;
/** /**
* @author Tastybento, ComminQ * @author Tastybento, ComminQ
*/ */
public final class AddonsManager { public class AddonsManager {
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static final String LOCALE_FOLDER = "locales"; 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.assertFalse;
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.mockito.Matchers.any;
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.io.File; import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.Listener; 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.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -25,6 +31,9 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock; 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) @RunWith(PowerMockRunner.class)
@PrepareForTest( { BSkyBlock.class }) @PrepareForTest( { BSkyBlock.class })
@ -53,6 +62,13 @@ public class AddonTest {
plugin = mock(BSkyBlock.class); plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); 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 { class TestClass extends Addon {
@ -141,65 +157,125 @@ public class AddonTest {
test.registerListener(listener); test.registerListener(listener);
} }
/*
@Test @Test
public void testSaveConfig() { 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 @Test
public void testSaveDefaultConfig() { 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 @Test
public void testSaveResourceStringBoolean() { public void testSaveResourceStringBooleanNoFile() throws IOException {
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", true);
} }
@Test @Test
public void testSaveResourceStringFileBooleanBoolean() { 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 @Test
public void testGetResource() { 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 @Test
public void testSetAddonFile() { 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 @Test
public void testSetDataFolder() { 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 @Test
public void testSetDescription() { public void testSetDescription() {
fail("Not yet implemented"); // TODO TestClass test = new TestClass();
AddonDescription desc = new AddonDescription();
test.setDescription(desc);
assertEquals(desc, test.getDescription());
} }
@Test @Test
public void testSetEnabled() { 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 @Test
public void testGetPlayers() { 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 @Test
public void testGetIslands() { 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 @Test
public void testGetAddonByName() { 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"));
} }
*/
} }