From f0deb1ce20b5413f52472b4bc1ad7837c6b4c6c5 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 26 Nov 2018 11:02:18 +0200 Subject: [PATCH] Test clean-up with ComponentMocker class rules --- .../com/djrapitops/plan/BukkitSystemTest.java | 43 +++------ .../system/listeners/AFKListenerTest.java | 23 ++--- .../java/rules/BukkitComponentMocker.java | 57 ++++++++++++ .../utilities/mocks/PlanBukkitMocker.java | 3 +- .../com/djrapitops/plan/BungeeSystemTest.java | 90 +++++++++---------- .../java/rules/BungeeComponentMocker.java | 57 ++++++++++++ .../src/test/java/rules/ComponentMocker.java | 27 ++++++ .../src/test/java/utilities/mocks/Mocker.java | 4 +- .../plan/BungeeBukkitConnectionTest.java | 32 ++----- .../database/databases/CommonDBTest.java | 16 ++-- .../settings/network/NetworkSettingsTest.java | 23 ++--- .../webserver/HTTPSWebServerAuthTest.java | 60 +++---------- .../webserver/JSErrorRegressionTest.java | 16 ++-- .../test/java/utilities/HTTPConnector.java | 74 +++++++++++++++ .../com/djrapitops/plan/SpongeSystemTest.java | 43 +++------ .../java/rules/SpongeComponentMocker.java | 57 ++++++++++++ .../djrapitops/plan/VelocitySystemTest.java | 55 ++++-------- .../java/rules/VelocityComponentMocker.java | 55 ++++++++++++ 18 files changed, 458 insertions(+), 277 deletions(-) create mode 100644 Plan/bukkit/src/test/java/rules/BukkitComponentMocker.java create mode 100644 Plan/bungeecord/src/test/java/rules/BungeeComponentMocker.java create mode 100644 Plan/common/src/test/java/rules/ComponentMocker.java create mode 100644 Plan/plugin/src/test/java/utilities/HTTPConnector.java create mode 100644 Plan/sponge/src/test/java/rules/SpongeComponentMocker.java create mode 100644 Plan/velocity/src/test/java/rules/VelocityComponentMocker.java diff --git a/Plan/bukkit/src/test/java/com/djrapitops/plan/BukkitSystemTest.java b/Plan/bukkit/src/test/java/com/djrapitops/plan/BukkitSystemTest.java index fd2b47a48..846b00490 100644 --- a/Plan/bukkit/src/test/java/com/djrapitops/plan/BukkitSystemTest.java +++ b/Plan/bukkit/src/test/java/com/djrapitops/plan/BukkitSystemTest.java @@ -8,11 +8,13 @@ import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.config.PlanConfig; -import org.junit.*; +import org.junit.ClassRule; +import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import utilities.mocks.PlanBukkitMocker; +import rules.BukkitComponentMocker; +import rules.ComponentMocker; /** * Test for Bukkit PlanSystem. @@ -24,35 +26,18 @@ public class BukkitSystemTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - private PlanSystem bukkitSystem; - private static PlanBukkitComponent COMPONENT; - - @BeforeClass - public static void setUpClass() throws Exception { - PlanBukkitMocker mockUtil = PlanBukkitMocker.setUp() - .withDataFolder(temporaryFolder.newFolder()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withServer(); - COMPONENT = DaggerPlanBukkitComponent.builder().plan(mockUtil.getPlanMock()).build(); - } - - @Before - public void setUp() { - bukkitSystem = COMPONENT.system(); - } - - @After - public void tearDown() { - if (bukkitSystem != null) { - bukkitSystem.disable(); - } - } + @ClassRule + public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder); @Test public void testEnable() throws EnableException { - PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); - config.set(Settings.WEBSERVER_PORT, 9005); - bukkitSystem.enable(); + PlanSystem bukkitSystem = component.getPlanSystem(); + try { + PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); + config.set(Settings.WEBSERVER_PORT, 9005); + bukkitSystem.enable(); + } finally { + bukkitSystem.disable(); + } } } diff --git a/Plan/bukkit/src/test/java/com/djrapitops/plan/system/listeners/AFKListenerTest.java b/Plan/bukkit/src/test/java/com/djrapitops/plan/system/listeners/AFKListenerTest.java index fb12af1a1..77d1b76a1 100644 --- a/Plan/bukkit/src/test/java/com/djrapitops/plan/system/listeners/AFKListenerTest.java +++ b/Plan/bukkit/src/test/java/com/djrapitops/plan/system/listeners/AFKListenerTest.java @@ -12,10 +12,6 @@ import org.junit.Test; import org.mockito.Mockito; import utilities.TestConstants; -import java.util.ArrayList; -import java.util.Collection; - -import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; /** @@ -36,30 +32,25 @@ public class AFKListenerTest { @Test public void afkPermissionIsNotCalledMoreThanOnce() { - Collection calls = new ArrayList<>(); - - Player player = mockPlayer(calls); + Player player = mockPlayer(); PlayerMoveEvent event = mockMoveEvent(player); underTest.onMove(event); - assertEquals(1, calls.size()); underTest.onMove(event); - assertEquals(1, calls.size()); + + verify(player, times(1)).hasPermission(anyString()); } private PlayerMoveEvent mockMoveEvent(Player player) { PlayerMoveEvent event = Mockito.mock(PlayerMoveEvent.class); - doReturn(player).when(event).getPlayer(); + when(event.getPlayer()).thenReturn(player); return event; } - private Player mockPlayer(Collection calls) { + private Player mockPlayer() { Player player = Mockito.mock(Player.class); - Mockito.doReturn(TestConstants.PLAYER_ONE_UUID).when(player).getUniqueId(); - doAnswer(perm -> { - calls.add(true); - return true; - }).when(player).hasPermission(Mockito.anyString()); + when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_ONE_UUID); + when(player.hasPermission(anyString())).thenReturn(true); return player; } diff --git a/Plan/bukkit/src/test/java/rules/BukkitComponentMocker.java b/Plan/bukkit/src/test/java/rules/BukkitComponentMocker.java new file mode 100644 index 000000000..090dbde41 --- /dev/null +++ b/Plan/bukkit/src/test/java/rules/BukkitComponentMocker.java @@ -0,0 +1,57 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package rules; + +import com.djrapitops.plan.DaggerPlanBukkitComponent; +import com.djrapitops.plan.Plan; +import com.djrapitops.plan.PlanBukkitComponent; +import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.system.PlanSystem; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; +import utilities.mocks.PlanBukkitMocker; + +public class BukkitComponentMocker extends ExternalResource implements ComponentMocker { + + private final TemporaryFolder testFolder; + + private Plan planMock; + private PlanBukkitComponent component; + + public BukkitComponentMocker(TemporaryFolder testFolder) { + this.testFolder = testFolder; + } + + @Override + protected void before() throws Throwable { + PlanBukkitMocker mocker = PlanBukkitMocker.setUp() + .withDataFolder(testFolder.newFolder()) + .withPluginDescription() + .withResourceFetchingFromJar() + .withServer(); + planMock = mocker.getPlanMock(); + component = DaggerPlanBukkitComponent.builder().plan(planMock).build(); + } + + public PlanPlugin getPlanMock() { + return planMock; + } + + public PlanSystem getPlanSystem() { + return component.system(); + } +} diff --git a/Plan/bukkit/src/test/java/utilities/mocks/PlanBukkitMocker.java b/Plan/bukkit/src/test/java/utilities/mocks/PlanBukkitMocker.java index ae47c87f3..903db898b 100644 --- a/Plan/bukkit/src/test/java/utilities/mocks/PlanBukkitMocker.java +++ b/Plan/bukkit/src/test/java/utilities/mocks/PlanBukkitMocker.java @@ -28,6 +28,7 @@ import utilities.mocks.objects.TestRunnableFactory; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import static org.mockito.Mockito.doReturn; @@ -92,7 +93,7 @@ public class PlanBukkitMocker extends Mocker { return this; } - public PlanBukkitMocker withResourceFetchingFromJar() throws Exception { + public PlanBukkitMocker withResourceFetchingFromJar() throws IOException { withPluginFiles(); return this; } diff --git a/Plan/bungeecord/src/test/java/com/djrapitops/plan/BungeeSystemTest.java b/Plan/bungeecord/src/test/java/com/djrapitops/plan/BungeeSystemTest.java index 4b83276da..3308ffb4e 100644 --- a/Plan/bungeecord/src/test/java/com/djrapitops/plan/BungeeSystemTest.java +++ b/Plan/bungeecord/src/test/java/com/djrapitops/plan/BungeeSystemTest.java @@ -9,12 +9,16 @@ import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.config.PlanConfig; -import org.junit.*; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import utilities.mocks.PlanBungeeMocker; +import rules.BungeeComponentMocker; +import rules.ComponentMocker; /** * Test for Bungee PlanSystem. @@ -26,47 +30,27 @@ public class BungeeSystemTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static PlanBungee PLUGIN_MOCK; + @ClassRule + public static ComponentMocker component = new BungeeComponentMocker(temporaryFolder); + @Rule public ExpectedException thrown = ExpectedException.none(); - private PlanBungeeComponent component; - private PlanSystem bungeeSystem; - - @BeforeClass - public static void setUpClass() throws Exception { - PlanBungeeMocker mocker = PlanBungeeMocker.setUp() - .withDataFolder(temporaryFolder.newFolder()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withProxy(); - PLUGIN_MOCK = mocker.getPlanMock(); - } - - @Before - public void setUp() { - component = DaggerPlanBungeeComponent.builder().plan(PLUGIN_MOCK).build(); - } - - @After - public void tearDown() { - if (bungeeSystem != null) { - bungeeSystem.disable(); - } - } - @Test public void bungeeEnables() throws Exception { - bungeeSystem = component.system(); + PlanSystem bungeeSystem = component.getPlanSystem(); + try { + PlanConfig config = bungeeSystem.getConfigSystem().getConfig(); + config.set(Settings.WEBSERVER_PORT, 9005); + config.set(Settings.BUNGEE_IP, "8.8.8.8"); - PlanConfig config = bungeeSystem.getConfigSystem().getConfig(); - config.set(Settings.WEBSERVER_PORT, 9005); - config.set(Settings.BUNGEE_IP, "8.8.8.8"); + DBSystem dbSystem = bungeeSystem.getDatabaseSystem(); + dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); - DBSystem dbSystem = bungeeSystem.getDatabaseSystem(); - dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); - - bungeeSystem.enable(); + bungeeSystem.enable(); + } finally { + bungeeSystem.disable(); + } } @Test @@ -75,16 +59,19 @@ public class BungeeSystemTest { thrown.expect(EnableException.class); thrown.expectMessage("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server."); - bungeeSystem = component.system(); + PlanSystem bungeeSystem = component.getPlanSystem(); + try { + PlanConfig config = bungeeSystem.getConfigSystem().getConfig(); + config.set(Settings.WEBSERVER_PORT, 9005); + config.set(Settings.BUNGEE_IP, "0.0.0.0"); - PlanConfig config = bungeeSystem.getConfigSystem().getConfig(); - config.set(Settings.WEBSERVER_PORT, 9005); - config.set(Settings.BUNGEE_IP, "0.0.0.0"); + DBSystem dbSystem = bungeeSystem.getDatabaseSystem(); + dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); - DBSystem dbSystem = bungeeSystem.getDatabaseSystem(); - dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); - - bungeeSystem.enable(); + bungeeSystem.enable(); + } finally { + bungeeSystem.disable(); + } } @Test @@ -93,12 +80,15 @@ public class BungeeSystemTest { thrown.expect(EnableException.class); thrown.expectMessage("Database failed to initialize"); - bungeeSystem = component.system(); + PlanSystem bungeeSystem = component.getPlanSystem(); + try { + PlanConfig config = bungeeSystem.getConfigSystem().getConfig(); + config.set(Settings.WEBSERVER_PORT, 9005); + config.set(Settings.BUNGEE_IP, "8.8.8.8"); - PlanConfig config = bungeeSystem.getConfigSystem().getConfig(); - config.set(Settings.WEBSERVER_PORT, 9005); - config.set(Settings.BUNGEE_IP, "8.8.8.8"); - - bungeeSystem.enable(); + bungeeSystem.enable(); + } finally { + bungeeSystem.disable(); + } } } diff --git a/Plan/bungeecord/src/test/java/rules/BungeeComponentMocker.java b/Plan/bungeecord/src/test/java/rules/BungeeComponentMocker.java new file mode 100644 index 000000000..3b9858a35 --- /dev/null +++ b/Plan/bungeecord/src/test/java/rules/BungeeComponentMocker.java @@ -0,0 +1,57 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package rules; + +import com.djrapitops.plan.DaggerPlanBungeeComponent; +import com.djrapitops.plan.PlanBungee; +import com.djrapitops.plan.PlanBungeeComponent; +import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.system.PlanSystem; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; +import utilities.mocks.PlanBungeeMocker; + +public class BungeeComponentMocker extends ExternalResource implements ComponentMocker { + + private final TemporaryFolder testFolder; + + private PlanBungee planMock; + private PlanBungeeComponent component; + + public BungeeComponentMocker(TemporaryFolder testFolder) { + this.testFolder = testFolder; + } + + @Override + protected void before() throws Throwable { + PlanBungeeMocker mocker = PlanBungeeMocker.setUp() + .withDataFolder(testFolder.newFolder()) + .withPluginDescription() + .withResourceFetchingFromJar() + .withProxy(); + planMock = mocker.getPlanMock(); + component = DaggerPlanBungeeComponent.builder().plan(planMock).build(); + } + + public PlanPlugin getPlanMock() { + return planMock; + } + + public PlanSystem getPlanSystem() { + return component.system(); + } +} diff --git a/Plan/common/src/test/java/rules/ComponentMocker.java b/Plan/common/src/test/java/rules/ComponentMocker.java new file mode 100644 index 000000000..359adca5b --- /dev/null +++ b/Plan/common/src/test/java/rules/ComponentMocker.java @@ -0,0 +1,27 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package rules; + +import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.system.PlanSystem; +import org.junit.rules.TestRule; + +public interface ComponentMocker extends TestRule { + PlanPlugin getPlanMock(); + + PlanSystem getPlanSystem(); +} diff --git a/Plan/common/src/test/java/utilities/mocks/Mocker.java b/Plan/common/src/test/java/utilities/mocks/Mocker.java index 6b93bc509..610ef9cd1 100644 --- a/Plan/common/src/test/java/utilities/mocks/Mocker.java +++ b/Plan/common/src/test/java/utilities/mocks/Mocker.java @@ -47,7 +47,7 @@ abstract class Mocker { return file; } - private void withPluginFile(String fileName) throws Exception { + private void withPluginFile(String fileName) throws FileNotFoundException { if (planMock.getDataFolder() == null) { throw new IllegalStateException("withDataFolder needs to be called before setting files"); } @@ -59,7 +59,7 @@ abstract class Mocker { } } - void withPluginFiles() throws Exception { + void withPluginFiles() throws FileNotFoundException { when(planMock.getResource(Mockito.anyString())).thenCallRealMethod(); for (String fileName : new String[]{ "bungeeconfig.yml", diff --git a/Plan/plugin/src/test/java/com/djrapitops/plan/BungeeBukkitConnectionTest.java b/Plan/plugin/src/test/java/com/djrapitops/plan/BungeeBukkitConnectionTest.java index 993321ecb..5198bf8ae 100644 --- a/Plan/plugin/src/test/java/com/djrapitops/plan/BungeeBukkitConnectionTest.java +++ b/Plan/plugin/src/test/java/com/djrapitops/plan/BungeeBukkitConnectionTest.java @@ -12,8 +12,9 @@ import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import utilities.mocks.PlanBukkitMocker; -import utilities.mocks.PlanBungeeMocker; +import rules.BukkitComponentMocker; +import rules.BungeeComponentMocker; +import rules.ComponentMocker; import java.util.UUID; @@ -25,8 +26,10 @@ public class BungeeBukkitConnectionTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static PlanBukkitComponent BUKKIT_COMPONENT; - private static PlanBungeeComponent BUNGEE_COMPONENT; + @ClassRule + public static ComponentMocker bukkitComponent = new BukkitComponentMocker(temporaryFolder); + @ClassRule + public static ComponentMocker bungeeComponent = new BungeeComponentMocker(temporaryFolder); @Rule public ExpectedException thrown = ExpectedException.none(); @@ -37,23 +40,6 @@ public class BungeeBukkitConnectionTest { private UUID bukkitUUID; private UUID bungeeUUID; - @BeforeClass - public static void setUpClass() throws Exception { - PlanBukkitMocker planBukkitMocker = PlanBukkitMocker.setUp() - .withDataFolder(temporaryFolder.getRoot()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withServer(); - BUKKIT_COMPONENT = DaggerPlanBukkitComponent.builder().plan(planBukkitMocker.getPlanMock()).build(); - - PlanBungeeMocker planBungeeMocker = PlanBungeeMocker.setUp() - .withDataFolder(temporaryFolder.getRoot()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withProxy(); - BUNGEE_COMPONENT = DaggerPlanBungeeComponent.builder().plan(planBungeeMocker.getPlanMock()).build(); - } - @After public void tearDown() { System.out.println("------------------------------"); @@ -68,8 +54,8 @@ public class BungeeBukkitConnectionTest { } public void enable() throws Exception { - bukkitSystem = BUKKIT_COMPONENT.system(); - bungeeSystem = BUNGEE_COMPONENT.system(); + bukkitSystem = bukkitComponent.getPlanSystem(); + bungeeSystem = bungeeComponent.getPlanSystem(); bukkitSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); bungeeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9250); diff --git a/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java b/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java index 15a4d9536..2ba2996b8 100644 --- a/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java +++ b/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java @@ -16,8 +16,6 @@ */ package com.djrapitops.plan.system.database.databases; -import com.djrapitops.plan.DaggerPlanBukkitComponent; -import com.djrapitops.plan.PlanBukkitComponent; import com.djrapitops.plan.api.exceptions.database.DBInitException; import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.container.*; @@ -42,10 +40,11 @@ import com.djrapitops.plan.utilities.SHA256Hash; import org.junit.*; import org.junit.rules.TemporaryFolder; import org.junit.rules.Timeout; +import rules.BukkitComponentMocker; +import rules.ComponentMocker; import utilities.OptionalAssert; import utilities.RandomData; import utilities.TestConstants; -import utilities.mocks.PlanBukkitMocker; import java.io.File; import java.lang.management.ManagementFactory; @@ -68,6 +67,9 @@ public abstract class CommonDBTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @ClassRule + public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder); + public static DBSystem dbSystem; public static SQLDB db; public static PlanSystem system; @@ -82,13 +84,7 @@ public abstract class CommonDBTest { static void handleSetup(String dbName) throws Exception { System.out.println("--- Test Class Setup ---"); - PlanBukkitMocker mockUtil = PlanBukkitMocker.setUp() - .withDataFolder(temporaryFolder.newFolder()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withServer(); - PlanBukkitComponent component = DaggerPlanBukkitComponent.builder().plan(mockUtil.getPlanMock()).build(); - system = component.system(); + system = component.getPlanSystem(); system.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); system.enable(); diff --git a/Plan/plugin/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java b/Plan/plugin/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java index c7b902617..a65c4dcb4 100644 --- a/Plan/plugin/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java +++ b/Plan/plugin/src/test/java/com/djrapitops/plan/system/settings/network/NetworkSettingsTest.java @@ -1,41 +1,30 @@ package com.djrapitops.plan.system.settings.network; -import com.djrapitops.plan.DaggerPlanBukkitComponent; -import com.djrapitops.plan.PlanBukkitComponent; import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.settings.Settings; import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import utilities.mocks.PlanBukkitMocker; +import rules.BukkitComponentMocker; +import rules.ComponentMocker; public class NetworkSettingsTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static PlanBukkitComponent COMPONENT; - - @BeforeClass - public static void setUpClass() throws Exception { - PlanBukkitMocker mocker = PlanBukkitMocker.setUp() - .withDataFolder(temporaryFolder.getRoot()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withServer(); - COMPONENT = DaggerPlanBukkitComponent.builder().plan(mocker.getPlanMock()).build(); - } + @ClassRule + public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder); @AfterClass public static void tearDownClass() { - COMPONENT.system().disable(); + component.getPlanSystem().disable(); } @Test public void transferDoesNotProduceException() throws EnableException { - PlanSystem system = COMPONENT.system(); + PlanSystem system = component.getPlanSystem(); system.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); system.enable(); diff --git a/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java b/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java index c2c74ef60..c22d5196d 100644 --- a/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java +++ b/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java @@ -1,7 +1,5 @@ package com.djrapitops.plan.system.webserver; -import com.djrapitops.plan.DaggerPlanBukkitComponent; -import com.djrapitops.plan.PlanBukkitComponent; import com.djrapitops.plan.api.exceptions.connection.*; import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.system.PlanSystem; @@ -13,9 +11,10 @@ import org.junit.*; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import utilities.mocks.PlanBukkitMocker; +import rules.BukkitComponentMocker; +import rules.ComponentMocker; +import utilities.HTTPConnector; -import javax.net.ssl.*; import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; @@ -28,22 +27,20 @@ public class HTTPSWebServerAuthTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + @ClassRule + public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder); + private static PlanSystem bukkitSystem; + private HTTPConnector connector = new HTTPConnector(); + @BeforeClass public static void setUpClass() throws Exception { - PlanBukkitMocker mocker = PlanBukkitMocker.setUp() - .withDataFolder(temporaryFolder.getRoot()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withServer(); - PlanBukkitComponent component = DaggerPlanBukkitComponent.builder().plan(mocker.getPlanMock()).build(); - URL resource = HTTPSWebServerAuthTest.class.getResource("/Cert.keystore"); String keyStore = resource.getPath(); String absolutePath = new File(keyStore).getAbsolutePath(); - bukkitSystem = component.system(); + bukkitSystem = component.getPlanSystem(); PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); @@ -67,31 +64,6 @@ public class HTTPSWebServerAuthTest { } } - private static final TrustManager[] trustAllCerts = new TrustManager[]{ - new X509TrustManager() { - @Override - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return null; - } - - @Override - public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { - //No need to implement. - } - - @Override - public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { - //No need to implement. - } - } - }; - - private SSLSocketFactory getRelaxedSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { - SSLContext sc = SSLContext.getInstance("SSL"); - sc.init(null, trustAllCerts, new java.security.SecureRandom()); - return sc.getSocketFactory(); - } - /** * Test case against "Perm level 0 required, got 0". */ @@ -100,19 +72,7 @@ public class HTTPSWebServerAuthTest { public void testHTTPSAuthForPages() throws IOException, WebException, KeyManagementException, NoSuchAlgorithmException { String address = "https://localhost:9005"; URL url = new URL(address); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - if (address.startsWith("https")) { - HttpsURLConnection httpsConn = (HttpsURLConnection) connection; - - // Disables unsigned certificate & hostname check, because we're trusting the user given certificate. - // This allows https connections internally to local ports. - httpsConn.setHostnameVerifier((hostname, session) -> true); - httpsConn.setSSLSocketFactory(getRelaxedSocketFactory()); - } - connection.setConnectTimeout(10000); - connection.setInstanceFollowRedirects(false); - connection.setRequestMethod("GET"); - connection.setUseCaches(false); + HttpURLConnection connection = connector.getConnection("HET", address); String user = Base64Util.encode("test:testPass"); connection.setRequestProperty("Authorization", "Basic " + user); diff --git a/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/JSErrorRegressionTest.java b/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/JSErrorRegressionTest.java index e46b1ea2e..fba3adcfb 100644 --- a/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/JSErrorRegressionTest.java +++ b/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/JSErrorRegressionTest.java @@ -1,7 +1,5 @@ package com.djrapitops.plan.system.webserver; -import com.djrapitops.plan.DaggerPlanBukkitComponent; -import com.djrapitops.plan.PlanBukkitComponent; import com.djrapitops.plan.data.container.Session; import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.database.DBSystem; @@ -16,9 +14,10 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; import org.openqa.selenium.WebDriver; +import rules.BukkitComponentMocker; +import rules.ComponentMocker; import rules.SeleniumDriver; import utilities.TestConstants; -import utilities.mocks.PlanBukkitMocker; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -39,20 +38,15 @@ public class JSErrorRegressionTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); @ClassRule + public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder); + @ClassRule public static SeleniumDriver seleniumDriver = new SeleniumDriver(); private static PlanSystem bukkitSystem; @BeforeClass public static void setUpClass() throws Exception { - PlanBukkitMocker mocker = PlanBukkitMocker.setUp() - .withDataFolder(temporaryFolder.getRoot()) - .withPluginDescription() - .withResourceFetchingFromJar() - .withServer(); - PlanBukkitComponent component = DaggerPlanBukkitComponent.builder().plan(mocker.getPlanMock()).build(); - - bukkitSystem = component.system(); + bukkitSystem = component.getPlanSystem(); PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); config.set(Settings.WEBSERVER_PORT, 9005); diff --git a/Plan/plugin/src/test/java/utilities/HTTPConnector.java b/Plan/plugin/src/test/java/utilities/HTTPConnector.java new file mode 100644 index 000000000..8b60b67b3 --- /dev/null +++ b/Plan/plugin/src/test/java/utilities/HTTPConnector.java @@ -0,0 +1,74 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package utilities; + +import org.junit.rules.ExternalResource; + +import javax.net.ssl.*; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +public class HTTPConnector extends ExternalResource { + + private static final TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { + //No need to implement. + } + + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { + //No need to implement. + } + } + }; + + private SSLSocketFactory getRelaxedSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + return sc.getSocketFactory(); + } + + public HttpURLConnection getConnection(String method, String address) throws IOException, KeyManagementException, NoSuchAlgorithmException { + URL url = new URL(address); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + if (address.startsWith("https")) { + HttpsURLConnection httpsConn = (HttpsURLConnection) connection; + + // Disables unsigned certificate & hostname check, because we're trusting the user given certificate. + // This allows https connections internally to local ports. + httpsConn.setHostnameVerifier((hostname, session) -> true); + httpsConn.setSSLSocketFactory(getRelaxedSocketFactory()); + } + connection.setConnectTimeout(10000); + connection.setInstanceFollowRedirects(false); + connection.setRequestMethod(method); + connection.setUseCaches(false); + + return connection; + } + +} diff --git a/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java b/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java index 7e84b618a..160f5f8c8 100644 --- a/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java +++ b/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java @@ -7,11 +7,13 @@ package com.djrapitops.plan; import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.settings.Settings; -import org.junit.*; +import org.junit.ClassRule; +import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import utilities.mocks.PlanSpongeMocker; +import rules.ComponentMocker; +import rules.SpongeComponentMocker; /** * Test for Sponge PlanSystem. @@ -23,36 +25,17 @@ public class SpongeSystemTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static PlanSponge planMock; - private PlanSystem spongeSystem; - private PlanSpongeComponent component; - - @BeforeClass - public static void setUpClass() throws Exception { - PlanSpongeMocker mockUtil = PlanSpongeMocker.setUp() - .withDataFolder(temporaryFolder.getRoot()) - .withResourceFetchingFromJar() - .withGame(); - planMock = mockUtil.getPlanMock(); - } - - @Before - public void setUp() { - component = DaggerPlanSpongeComponent.builder().plan(planMock).build(); - } - - @After - public void tearDown() { - if (spongeSystem != null) { - spongeSystem.disable(); - } - } + @ClassRule + public static ComponentMocker component = new SpongeComponentMocker(temporaryFolder); @Test public void testEnable() throws EnableException { - spongeSystem = component.system(); - spongeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); - - spongeSystem.enable(); + PlanSystem spongeSystem = component.getPlanSystem(); + try { + spongeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); + spongeSystem.enable(); + } finally { + spongeSystem.disable(); + } } } diff --git a/Plan/sponge/src/test/java/rules/SpongeComponentMocker.java b/Plan/sponge/src/test/java/rules/SpongeComponentMocker.java new file mode 100644 index 000000000..0d661cc59 --- /dev/null +++ b/Plan/sponge/src/test/java/rules/SpongeComponentMocker.java @@ -0,0 +1,57 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package rules; + +import com.djrapitops.plan.DaggerPlanSpongeComponent; +import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.PlanSponge; +import com.djrapitops.plan.PlanSpongeComponent; +import com.djrapitops.plan.system.PlanSystem; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; +import utilities.mocks.PlanSpongeMocker; + +public class SpongeComponentMocker extends ExternalResource implements ComponentMocker { + + private final TemporaryFolder testFolder; + + private PlanSponge planMock; + private PlanSpongeComponent component; + + public SpongeComponentMocker(TemporaryFolder testFolder) { + this.testFolder = testFolder; + } + + @Override + protected void before() throws Throwable { + PlanSpongeMocker mocker = PlanSpongeMocker.setUp() + .withDataFolder(testFolder.newFolder()) + .withResourceFetchingFromJar() + .withGame(); + planMock = mocker.getPlanMock(); + component = DaggerPlanSpongeComponent.builder().plan(planMock).build(); + } + + public PlanPlugin getPlanMock() { + return planMock; + } + + public PlanSystem getPlanSystem() { + return component.system(); + } + +} diff --git a/Plan/velocity/src/test/java/com/djrapitops/plan/VelocitySystemTest.java b/Plan/velocity/src/test/java/com/djrapitops/plan/VelocitySystemTest.java index a84ab2d4e..d48854fdf 100644 --- a/Plan/velocity/src/test/java/com/djrapitops/plan/VelocitySystemTest.java +++ b/Plan/velocity/src/test/java/com/djrapitops/plan/VelocitySystemTest.java @@ -8,12 +8,13 @@ import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.config.PlanConfig; -import org.junit.*; -import org.junit.rules.ExpectedException; +import org.junit.ClassRule; +import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import utilities.mocks.PlanVelocityMocker; +import rules.ComponentMocker; +import rules.VelocityComponentMocker; /** * Test for Velocity PlanSystem. @@ -25,45 +26,23 @@ public class VelocitySystemTest { @ClassRule public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static PlanVelocity PLUGIN_MOCK; - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private PlanVelocityComponent component; - private PlanSystem velocitySystem; - - @BeforeClass - public static void setUpClass() throws Exception { - PlanVelocityMocker mocker = PlanVelocityMocker.setUp() - .withDataFolder(temporaryFolder.newFolder()) - .withResourceFetchingFromJar() - .withProxy(); - PLUGIN_MOCK = mocker.getPlanMock(); - } - - @Before - public void setUp() { - component = DaggerPlanVelocityComponent.builder().plan(PLUGIN_MOCK).build(); - } - - @After - public void tearDown() { - if (velocitySystem != null) { - velocitySystem.disable(); - } - } + @ClassRule + public static ComponentMocker component = new VelocityComponentMocker(temporaryFolder); @Test public void velocityEnables() throws Exception { - velocitySystem = component.system(); + PlanSystem velocitySystem = component.getPlanSystem(); + try { + PlanConfig config = velocitySystem.getConfigSystem().getConfig(); + config.set(Settings.WEBSERVER_PORT, 9005); + config.set(Settings.BUNGEE_IP, "8.8.8.8"); - PlanConfig config = velocitySystem.getConfigSystem().getConfig(); - config.set(Settings.WEBSERVER_PORT, 9005); - config.set(Settings.BUNGEE_IP, "8.8.8.8"); + DBSystem dbSystem = velocitySystem.getDatabaseSystem(); + dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); - DBSystem dbSystem = velocitySystem.getDatabaseSystem(); - dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); - - velocitySystem.enable(); + velocitySystem.enable(); + } finally { + velocitySystem.disable(); + } } } diff --git a/Plan/velocity/src/test/java/rules/VelocityComponentMocker.java b/Plan/velocity/src/test/java/rules/VelocityComponentMocker.java new file mode 100644 index 000000000..0db5df95d --- /dev/null +++ b/Plan/velocity/src/test/java/rules/VelocityComponentMocker.java @@ -0,0 +1,55 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package rules; + +import com.djrapitops.plan.DaggerPlanVelocityComponent; +import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.PlanVelocity; +import com.djrapitops.plan.PlanVelocityComponent; +import com.djrapitops.plan.system.PlanSystem; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; +import utilities.mocks.PlanVelocityMocker; + +public class VelocityComponentMocker extends ExternalResource implements ComponentMocker { + private final TemporaryFolder testFolder; + + private PlanVelocity planMock; + private PlanVelocityComponent component; + + public VelocityComponentMocker(TemporaryFolder testFolder) { + this.testFolder = testFolder; + } + + @Override + protected void before() throws Throwable { + PlanVelocityMocker mocker = PlanVelocityMocker.setUp() + .withDataFolder(testFolder.newFolder()) + .withResourceFetchingFromJar() + .withProxy(); + planMock = mocker.getPlanMock(); + component = DaggerPlanVelocityComponent.builder().plan(planMock).build(); + } + + public PlanPlugin getPlanMock() { + return planMock; + } + + public PlanSystem getPlanSystem() { + return component.system(); + } +}