From e129ac0990fd6aefe1d67b5f6ae9d34c40c66c73 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 5 Nov 2017 19:38:11 +0200 Subject: [PATCH] Fixed Tests --- .../main/java/com/djrapitops/plan/Plan.java | 2 ++ .../java/com/djrapitops/plan/PlanBungee.java | 2 ++ .../djrapitops/plan/WorldAliasSettings.java | 23 +++++++------- .../plan/command/PlanBungeeCommand.java | 19 +++++++----- .../djrapitops/plan/command/PlanCommand.java | 30 +++++++++++-------- .../plan/command/commands/ManageCommand.java | 24 ++++++++------- .../plan/command/commands/WebUserCommand.java | 15 ++++++---- .../plan/utilities/analysis/Analysis.java | 2 +- .../plan/utilities/file/FileUtil.java | 1 + .../plan/database/DatabaseTest.java | 2 +- .../djrapitops/plan/ui/graphs/GraphTest.java | 13 ++++++-- .../plan/utilities/FormatUtilsTest.java | 2 +- Plan/test/test/java/utils/TestInit.java | 27 +++++++++++++---- 13 files changed, 103 insertions(+), 59 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 1fdd8e1c1..bcf5a33bd 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -152,6 +152,8 @@ public class Plan extends BukkitPlugin implements IPlan { Log.infoColor("§a----------------------------------------"); Log.infoColor("§aNew version is available at https://www.spigotmc.org/resources/plan-player-analytics.32536/"); Log.infoColor("§a----------------------------------------"); + } else { + Log.info("You're using the latest version."); } } catch (IOException e) { Log.error("Failed to check newest version number"); diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java index 830de248c..33e70dd8b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -74,6 +74,8 @@ public class PlanBungee extends BungeePlugin implements IPlan { Log.infoColor("§a----------------------------------------"); Log.infoColor("§aNew version is available at https://www.spigotmc.org/resources/plan-player-analytics.32536/"); Log.infoColor("§a----------------------------------------"); + } else { + Log.info("You're using the latest version."); } } catch (IOException e) { Log.error("Failed to check newest version number"); diff --git a/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java b/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java index 4bfb9ad9d..40199b24e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java +++ b/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java @@ -4,8 +4,9 @@ */ package main.java.com.djrapitops.plan; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; +import com.djrapitops.plugin.api.config.Config; +import com.djrapitops.plugin.api.config.ConfigNode; +import com.djrapitops.plugin.utilities.Verify; import java.util.HashMap; import java.util.Map; @@ -34,18 +35,18 @@ public class WorldAliasSettings { * @return Map: Original name, Alias */ public Map getAliases() { - ConfigurationSection aliasSect = getAliasSection(); + ConfigNode aliasSect = getAliasSection(); Map aliasMap = new HashMap<>(); - for (String world : aliasSect.getKeys(false)) { - aliasMap.put(world, aliasSect.getString(world)); + for (Map.Entry world : aliasSect.getChildren().entrySet()) { + aliasMap.put(world.getKey(), world.getValue().getString()); } return aliasMap; } - private ConfigurationSection getAliasSection() { - FileConfiguration config = plugin.getConfig(); - return config.getConfigurationSection(Settings.WORLD_ALIASES.getPath()); + private ConfigNode getAliasSection() { + Config config = plugin.getMainConfig(); + return config.getConfigNode(Settings.WORLD_ALIASES.getPath()); } /** @@ -56,10 +57,10 @@ public class WorldAliasSettings { * @param world World name */ public void addWorld(String world) { - ConfigurationSection aliasSect = getAliasSection(); + ConfigNode aliasSect = getAliasSection(); - Object previousValue = aliasSect.get(world); - if (previousValue == null) { + String previousValue = aliasSect.getConfigNode(world).getValue(); + if (Verify.isEmpty(previousValue)) { aliasSect.set(world, world); } plugin.saveConfig(); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/PlanBungeeCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/PlanBungeeCommand.java index 0d344726e..6f03a0454 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/PlanBungeeCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/PlanBungeeCommand.java @@ -29,6 +29,7 @@ public class PlanBungeeCommand extends TreeCommand { public PlanBungeeCommand(PlanBungee plugin) { super(plugin, "planbungee", CommandType.CONSOLE, "", "", "planbungee"); super.setDefaultCommand("help"); + super.setColorScheme(plugin.getColorScheme()); } @Override @@ -38,13 +39,17 @@ public class PlanBungeeCommand extends TreeCommand { @Override public void addCommands() { - commands.add(new ReloadCommand(plugin)); - commands.add(new StatusCommand<>(plugin, Permissions.MANAGE.getPermission(), plugin.getColorScheme())); - commands.add(new ListCommand()); + add( + new ReloadCommand(plugin), + new StatusCommand<>(plugin, Permissions.MANAGE.getPermission(), plugin.getColorScheme()), + new ListCommand() + ); RegisterCommand registerCommand = new RegisterCommand(plugin); - commands.add(registerCommand); - commands.add(new WebUserCommand(plugin, registerCommand)); - commands.add(new NetworkCommand(plugin)); - commands.add(new ListServersCommand(plugin)); + add( + registerCommand, + new WebUserCommand(plugin, registerCommand), + new NetworkCommand(plugin), + new ListServersCommand(plugin) + ); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java index da7e0ed1c..cf4c86e88 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java @@ -30,6 +30,7 @@ public class PlanCommand extends TreeCommand { public PlanCommand(Plan plugin) { super(plugin, "plan", CommandType.CONSOLE, "", "", "plan"); super.setDefaultCommand("inspect"); + super.setColorScheme(plugin.getColorScheme()); } @Override @@ -39,22 +40,25 @@ public class PlanCommand extends TreeCommand { @Override public void addCommands() { - commands.add(new InspectCommand(plugin)); - commands.add(new AnalyzeCommand(plugin)); - commands.add(new SearchCommand(plugin)); - commands.add(new InfoCommand(plugin)); - commands.add(new ReloadCommand(plugin)); - commands.add(new ManageCommand(plugin)); - commands.add(new StatusCommand<>(plugin, Permissions.MANAGE.getPermission(), plugin.getColorScheme())); - commands.add(new ListCommand()); + add( + new InspectCommand(plugin), + new AnalyzeCommand(plugin), + new SearchCommand(plugin), + new InfoCommand(plugin), + new ReloadCommand(plugin), + new ManageCommand(plugin), + new StatusCommand<>(plugin, Permissions.MANAGE.getPermission(), plugin.getColorScheme()), + new ListCommand() + ); RegisterCommand registerCommand = new RegisterCommand(plugin); - commands.add(registerCommand); - commands.add(new WebUserCommand(plugin, registerCommand)); - commands.add(new NetworkCommand(plugin)); - commands.add(new ListServersCommand(plugin)); + add( + registerCommand, + new WebUserCommand(plugin, registerCommand), + new NetworkCommand(plugin), + new ListServersCommand(plugin)); if (Settings.DEV_MODE.isTrue()) { - commands.add(new DevCommand(plugin)); + add(new DevCommand(plugin)); } } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java index 1e4985409..bf4a199ac 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java @@ -25,7 +25,7 @@ public class ManageCommand extends TreeCommand { */ public ManageCommand(Plan plugin) { super(plugin, "manage,m", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Locale.get(Msg.CMD_USG_MANAGE).toString(), "plan m"); - + super.setColorScheme(plugin.getColorScheme()); } @Override @@ -35,15 +35,17 @@ public class ManageCommand extends TreeCommand { @Override public void addCommands() { - commands.add(new ManageMoveCommand(plugin)); - commands.add(new ManageHotswapCommand(plugin)); - commands.add(new ManageBackupCommand(plugin)); - commands.add(new ManageRestoreCommand(plugin)); - commands.add(new ManageImportCommand(plugin)); - commands.add(new ManageRemoveCommand(plugin)); - commands.add(new ManageClearCommand(plugin)); - commands.add(new ManageDumpCommand(plugin)); - commands.add(new ManageSetupCommand(plugin)); - commands.add(new ManageDisableCommand()); + add( + new ManageMoveCommand(plugin), + new ManageHotswapCommand(plugin), + new ManageBackupCommand(plugin), + new ManageRestoreCommand(plugin), + new ManageImportCommand(plugin), + new ManageRemoveCommand(plugin), + new ManageClearCommand(plugin), + new ManageDumpCommand(plugin), + new ManageSetupCommand(plugin), + new ManageDisableCommand() + ); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/WebUserCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/WebUserCommand.java index baba4368f..2006e6be7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/WebUserCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/WebUserCommand.java @@ -27,7 +27,8 @@ public class WebUserCommand extends TreeCommand { Permissions.MANAGE_WEB.getPerm(), Locale.get(Msg.CMD_USG_WEB).toString(), "plan web"); - commands.add(register); + super.setColorScheme(plugin.getColorScheme()); + add(register); } public WebUserCommand(PlanBungee plugin, RegisterCommand register) { @@ -36,7 +37,7 @@ public class WebUserCommand extends TreeCommand { Permissions.MANAGE_WEB.getPerm(), Locale.get(Msg.CMD_USG_WEB).toString(), "planbungee web"); - commands.add(register); + add(register); } @Override @@ -46,9 +47,11 @@ public class WebUserCommand extends TreeCommand { @Override public void addCommands() { - commands.add(new WebLevelCommand(plugin)); - commands.add(new WebListUsersCommand(plugin)); - commands.add(new WebCheckCommand(plugin)); - commands.add(new WebDeleteCommand(plugin)); + add( + new WebLevelCommand(plugin), + new WebListUsersCommand(plugin), + new WebCheckCommand(plugin), + new WebDeleteCommand(plugin) + ); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java index 70e608051..af0fb821b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java @@ -208,7 +208,7 @@ public class Analysis { Benchmark.stop("Analysis", "Source " + source.getPlaceholder()); } }); - Benchmark.stop("Analysis", "3rd party"); + Benchmark.stop("Analysis", "3rd party Analysis"); return replaceMap; } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java b/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java index 2ca5d13d7..e212d0f13 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/file/FileUtil.java @@ -49,6 +49,7 @@ public class FileUtil { lines.add(scanner.nextLine()); } } catch (NullPointerException e) { + e.printStackTrace(); throw new FileNotFoundException("File not found inside jar: " + resource); } finally { MiscUtils.close(scanner); diff --git a/Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java index 113fece09..cd1f24352 100644 --- a/Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java @@ -46,7 +46,7 @@ import static org.powermock.api.mockito.PowerMockito.when; * @author Rsl1122 */ @RunWith(PowerMockRunner.class) -@PrepareForTest(JavaPlugin.class) +@PrepareForTest({JavaPlugin.class}) public class DatabaseTest { private final UUID uuid = MockUtils.getPlayerUUID(); diff --git a/Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java b/Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java index ac781f161..1225d1634 100644 --- a/Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java @@ -10,10 +10,15 @@ import main.java.com.djrapitops.plan.data.time.WorldTimes; import main.java.com.djrapitops.plan.utilities.analysis.Point; import main.java.com.djrapitops.plan.utilities.html.graphs.*; import org.apache.commons.lang3.StringUtils; +import org.bukkit.plugin.java.JavaPlugin; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.RandomData; +import test.java.utils.TestInit; import java.util.*; @@ -22,6 +27,8 @@ import static junit.framework.TestCase.assertEquals; /** * @author Fuzzlemann */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({JavaPlugin.class}) public class GraphTest { private final List tpsList = new ArrayList<>(); @@ -32,7 +39,8 @@ public class GraphTest { private List points = new ArrayList<>(); @Before - public void setUp() { + public void setUp() throws Exception { + TestInit t = TestInit.init(); for (int i = 0; i < 10; i++) { tpsList.add(new TPS(i, i, i, i, i, i, i)); sessionList.add(new Session(i, (long) i, (long) i, i, i)); @@ -55,7 +63,8 @@ public class GraphTest { assertEquals(expected, WorldLoadGraphCreator.buildSeriesDataStringChunks(tpsList)); assertEquals(expected, WorldLoadGraphCreator.buildSeriesDataStringEntities(tpsList)); assertEquals("[{'code':'1','value':1},{'code':'2','value':2},{'code':'3','value':3},{'code':'4','value':4},{'code':'5','value':5},{'code':'6','value':6},{'code':'7','value':7},{'code':'8','value':8},{'code':'9','value':9}]", WorldMapCreator.createDataSeries(geoList)); - assertEquals("[[{name:'WORLD',y:0,drilldown: 'WORLD'}], [{name:'WORLD', id:'WORLD',colors: gmPieColors,data: []}]]", Arrays.toString(WorldPieCreator.createSeriesData(worldTimes))); + assertEquals("[[{name:'WORLD',y:0,drilldown: 'WORLD'}], [{name:'WORLD', id:'WORLD',colors: gmPieColors,data: [['SURVIVAL',0],['SPECTATOR',0],['CREATIVE',0],['ADVENTURE',0]]}]]", + Arrays.toString(WorldPieCreator.createSeriesData(worldTimes))); } @Test diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java index 4348069be..1ec4e546d 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java @@ -134,7 +134,7 @@ public class FormatUtilsTest { @Test public void testParseVersionNumber() { String versionString = "2.10.2"; - int expResult = 21002; + long expResult = 21002000000000000L; long result = FormatUtils.parseVersionNumber(versionString); diff --git a/Plan/test/test/java/utils/TestInit.java b/Plan/test/test/java/utils/TestInit.java index f330b9df7..4bfe2e434 100644 --- a/Plan/test/test/java/utils/TestInit.java +++ b/Plan/test/test/java/utils/TestInit.java @@ -1,8 +1,11 @@ package test.java.utils; +import com.djrapitops.plugin.IPlugin; import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.api.config.Config; +import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.settings.ColorScheme; +import com.djrapitops.plugin.task.RunnableFactory; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.ServerVariableHolder; import main.java.com.djrapitops.plan.Settings; @@ -22,6 +25,7 @@ import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.Plugin; import org.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; import java.io.File; import java.io.FileInputStream; @@ -78,21 +82,31 @@ public class TestInit { private void setUp() throws Exception { planMock = PowerMockito.mock(Plan.class); + + StaticHolder.register(Plan.class, planMock); StaticHolder.register(planMock); - StaticHolder.register(planMock); + + // Hacks to make APF find classes + StaticHolder.register(IPlugin.class, planMock); + StaticHolder.saveInstance(this.getClass(), Plan.class); + StaticHolder.saveInstance(PowerMockRunner.class, Plan.class); + + Log.setDebugMode("console"); File testFolder = getTestFolder(); when(planMock.getDataFolder()).thenReturn(testFolder); - Config iConfig = new Config(new File(planMock.getDataFolder(), "config.yml"), FileUtil.lines(planMock, "config.yml")); - when(planMock.getMainConfig()).thenReturn(iConfig); - - // Html Files + // Files + File config = new File(getClass().getResource("/config.yml").getPath()); + when(planMock.getResource("config.yml")).thenReturn(new FileInputStream(config)); File analysis = new File(getClass().getResource("/server.html").getPath()); when(planMock.getResource("server.html")).thenReturn(new FileInputStream(analysis)); File player = new File(getClass().getResource("/player.html").getPath()); when(planMock.getResource("player.html")).thenReturn(new FileInputStream(player)); + Config iConfig = new Config(new File(planMock.getDataFolder(), "config.yml"), FileUtil.lines(planMock, "config.yml")); + when(planMock.getMainConfig()).thenReturn(iConfig); + Server mockServer = mockServer(); when(planMock.getServer()).thenReturn(mockServer); @@ -112,6 +126,8 @@ public class TestInit { ColorScheme cs = new ColorScheme(ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK); when(planMock.getColorScheme()).thenReturn(cs); initLocale(planMock); + + RunnableFactory.activateTestMode(); } private Server mockServer() { @@ -140,7 +156,6 @@ public class TestInit { for (String string : strings) { sendMessage(string); } - } @Override