Test clean-up with ComponentMocker class rules

This commit is contained in:
Rsl1122 2018-11-26 11:02:18 +02:00
parent b9fa29544d
commit f0deb1ce20
18 changed files with 458 additions and 277 deletions

View File

@ -8,11 +8,13 @@ import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.config.PlanConfig; 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.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.mocks.PlanBukkitMocker; import rules.BukkitComponentMocker;
import rules.ComponentMocker;
/** /**
* Test for Bukkit PlanSystem. * Test for Bukkit PlanSystem.
@ -24,35 +26,18 @@ public class BukkitSystemTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private PlanSystem bukkitSystem; @ClassRule
private static PlanBukkitComponent COMPONENT; public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
@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();
}
}
@Test @Test
public void testEnable() throws EnableException { public void testEnable() throws EnableException {
PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); PlanSystem bukkitSystem = component.getPlanSystem();
config.set(Settings.WEBSERVER_PORT, 9005); try {
bukkitSystem.enable(); PlanConfig config = bukkitSystem.getConfigSystem().getConfig();
config.set(Settings.WEBSERVER_PORT, 9005);
bukkitSystem.enable();
} finally {
bukkitSystem.disable();
}
} }
} }

View File

@ -12,10 +12,6 @@ import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import utilities.TestConstants; import utilities.TestConstants;
import java.util.ArrayList;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
/** /**
@ -36,30 +32,25 @@ public class AFKListenerTest {
@Test @Test
public void afkPermissionIsNotCalledMoreThanOnce() { public void afkPermissionIsNotCalledMoreThanOnce() {
Collection<Boolean> calls = new ArrayList<>(); Player player = mockPlayer();
Player player = mockPlayer(calls);
PlayerMoveEvent event = mockMoveEvent(player); PlayerMoveEvent event = mockMoveEvent(player);
underTest.onMove(event); underTest.onMove(event);
assertEquals(1, calls.size());
underTest.onMove(event); underTest.onMove(event);
assertEquals(1, calls.size());
verify(player, times(1)).hasPermission(anyString());
} }
private PlayerMoveEvent mockMoveEvent(Player player) { private PlayerMoveEvent mockMoveEvent(Player player) {
PlayerMoveEvent event = Mockito.mock(PlayerMoveEvent.class); PlayerMoveEvent event = Mockito.mock(PlayerMoveEvent.class);
doReturn(player).when(event).getPlayer(); when(event.getPlayer()).thenReturn(player);
return event; return event;
} }
private Player mockPlayer(Collection<Boolean> calls) { private Player mockPlayer() {
Player player = Mockito.mock(Player.class); Player player = Mockito.mock(Player.class);
Mockito.doReturn(TestConstants.PLAYER_ONE_UUID).when(player).getUniqueId(); when(player.getUniqueId()).thenReturn(TestConstants.PLAYER_ONE_UUID);
doAnswer(perm -> { when(player.hasPermission(anyString())).thenReturn(true);
calls.add(true);
return true;
}).when(player).hasPermission(Mockito.anyString());
return player; return player;
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}

View File

@ -28,6 +28,7 @@ import utilities.mocks.objects.TestRunnableFactory;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
@ -92,7 +93,7 @@ public class PlanBukkitMocker extends Mocker {
return this; return this;
} }
public PlanBukkitMocker withResourceFetchingFromJar() throws Exception { public PlanBukkitMocker withResourceFetchingFromJar() throws IOException {
withPluginFiles(); withPluginFiles();
return this; return this;
} }

View File

@ -9,12 +9,16 @@ import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.config.PlanConfig; 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.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.mocks.PlanBungeeMocker; import rules.BungeeComponentMocker;
import rules.ComponentMocker;
/** /**
* Test for Bungee PlanSystem. * Test for Bungee PlanSystem.
@ -26,47 +30,27 @@ public class BungeeSystemTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanBungee PLUGIN_MOCK; @ClassRule
public static ComponentMocker component = new BungeeComponentMocker(temporaryFolder);
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); 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 @Test
public void bungeeEnables() throws Exception { 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(); DBSystem dbSystem = bungeeSystem.getDatabaseSystem();
config.set(Settings.WEBSERVER_PORT, 9005); dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
config.set(Settings.BUNGEE_IP, "8.8.8.8");
DBSystem dbSystem = bungeeSystem.getDatabaseSystem(); bungeeSystem.enable();
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); } finally {
bungeeSystem.disable();
bungeeSystem.enable(); }
} }
@Test @Test
@ -75,16 +59,19 @@ public class BungeeSystemTest {
thrown.expect(EnableException.class); thrown.expect(EnableException.class);
thrown.expectMessage("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server."); 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(); DBSystem dbSystem = bungeeSystem.getDatabaseSystem();
config.set(Settings.WEBSERVER_PORT, 9005); dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
config.set(Settings.BUNGEE_IP, "0.0.0.0");
DBSystem dbSystem = bungeeSystem.getDatabaseSystem(); bungeeSystem.enable();
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); } finally {
bungeeSystem.disable();
bungeeSystem.enable(); }
} }
@Test @Test
@ -93,12 +80,15 @@ public class BungeeSystemTest {
thrown.expect(EnableException.class); thrown.expect(EnableException.class);
thrown.expectMessage("Database failed to initialize"); 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(); bungeeSystem.enable();
config.set(Settings.WEBSERVER_PORT, 9005); } finally {
config.set(Settings.BUNGEE_IP, "8.8.8.8"); bungeeSystem.disable();
}
bungeeSystem.enable();
} }
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}

View File

@ -47,7 +47,7 @@ abstract class Mocker {
return file; return file;
} }
private void withPluginFile(String fileName) throws Exception { private void withPluginFile(String fileName) throws FileNotFoundException {
if (planMock.getDataFolder() == null) { if (planMock.getDataFolder() == null) {
throw new IllegalStateException("withDataFolder needs to be called before setting files"); 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(); when(planMock.getResource(Mockito.anyString())).thenCallRealMethod();
for (String fileName : new String[]{ for (String fileName : new String[]{
"bungeeconfig.yml", "bungeeconfig.yml",

View File

@ -12,8 +12,9 @@ import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.mocks.PlanBukkitMocker; import rules.BukkitComponentMocker;
import utilities.mocks.PlanBungeeMocker; import rules.BungeeComponentMocker;
import rules.ComponentMocker;
import java.util.UUID; import java.util.UUID;
@ -25,8 +26,10 @@ public class BungeeBukkitConnectionTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanBukkitComponent BUKKIT_COMPONENT; @ClassRule
private static PlanBungeeComponent BUNGEE_COMPONENT; public static ComponentMocker bukkitComponent = new BukkitComponentMocker(temporaryFolder);
@ClassRule
public static ComponentMocker bungeeComponent = new BungeeComponentMocker(temporaryFolder);
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -37,23 +40,6 @@ public class BungeeBukkitConnectionTest {
private UUID bukkitUUID; private UUID bukkitUUID;
private UUID bungeeUUID; 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 @After
public void tearDown() { public void tearDown() {
System.out.println("------------------------------"); System.out.println("------------------------------");
@ -68,8 +54,8 @@ public class BungeeBukkitConnectionTest {
} }
public void enable() throws Exception { public void enable() throws Exception {
bukkitSystem = BUKKIT_COMPONENT.system(); bukkitSystem = bukkitComponent.getPlanSystem();
bungeeSystem = BUNGEE_COMPONENT.system(); bungeeSystem = bungeeComponent.getPlanSystem();
bukkitSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); bukkitSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005);
bungeeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9250); bungeeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9250);

View File

@ -16,8 +16,6 @@
*/ */
package com.djrapitops.plan.system.database.databases; 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.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.*; import com.djrapitops.plan.data.container.*;
@ -42,10 +40,11 @@ import com.djrapitops.plan.utilities.SHA256Hash;
import org.junit.*; import org.junit.*;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.rules.Timeout; import org.junit.rules.Timeout;
import rules.BukkitComponentMocker;
import rules.ComponentMocker;
import utilities.OptionalAssert; import utilities.OptionalAssert;
import utilities.RandomData; import utilities.RandomData;
import utilities.TestConstants; import utilities.TestConstants;
import utilities.mocks.PlanBukkitMocker;
import java.io.File; import java.io.File;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
@ -68,6 +67,9 @@ public abstract class CommonDBTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@ClassRule
public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
public static DBSystem dbSystem; public static DBSystem dbSystem;
public static SQLDB db; public static SQLDB db;
public static PlanSystem system; public static PlanSystem system;
@ -82,13 +84,7 @@ public abstract class CommonDBTest {
static void handleSetup(String dbName) throws Exception { static void handleSetup(String dbName) throws Exception {
System.out.println("--- Test Class Setup ---"); System.out.println("--- Test Class Setup ---");
PlanBukkitMocker mockUtil = PlanBukkitMocker.setUp() system = component.getPlanSystem();
.withDataFolder(temporaryFolder.newFolder())
.withPluginDescription()
.withResourceFetchingFromJar()
.withServer();
PlanBukkitComponent component = DaggerPlanBukkitComponent.builder().plan(mockUtil.getPlanMock()).build();
system = component.system();
system.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); system.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005);
system.enable(); system.enable();

View File

@ -1,41 +1,30 @@
package com.djrapitops.plan.system.settings.network; 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.api.exceptions.EnableException;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import utilities.mocks.PlanBukkitMocker; import rules.BukkitComponentMocker;
import rules.ComponentMocker;
public class NetworkSettingsTest { public class NetworkSettingsTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanBukkitComponent COMPONENT; @ClassRule
public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
@BeforeClass
public static void setUpClass() throws Exception {
PlanBukkitMocker mocker = PlanBukkitMocker.setUp()
.withDataFolder(temporaryFolder.getRoot())
.withPluginDescription()
.withResourceFetchingFromJar()
.withServer();
COMPONENT = DaggerPlanBukkitComponent.builder().plan(mocker.getPlanMock()).build();
}
@AfterClass @AfterClass
public static void tearDownClass() { public static void tearDownClass() {
COMPONENT.system().disable(); component.getPlanSystem().disable();
} }
@Test @Test
public void transferDoesNotProduceException() throws EnableException { public void transferDoesNotProduceException() throws EnableException {
PlanSystem system = COMPONENT.system(); PlanSystem system = component.getPlanSystem();
system.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); system.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005);
system.enable(); system.enable();

View File

@ -1,7 +1,5 @@
package com.djrapitops.plan.system.webserver; 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.api.exceptions.connection.*;
import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
@ -13,9 +11,10 @@ import org.junit.*;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; 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.File;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -28,22 +27,20 @@ public class HTTPSWebServerAuthTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@ClassRule
public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
private static PlanSystem bukkitSystem; private static PlanSystem bukkitSystem;
private HTTPConnector connector = new HTTPConnector();
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { 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"); URL resource = HTTPSWebServerAuthTest.class.getResource("/Cert.keystore");
String keyStore = resource.getPath(); String keyStore = resource.getPath();
String absolutePath = new File(keyStore).getAbsolutePath(); String absolutePath = new File(keyStore).getAbsolutePath();
bukkitSystem = component.system(); bukkitSystem = component.getPlanSystem();
PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); 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". * Test case against "Perm level 0 required, got 0".
*/ */
@ -100,19 +72,7 @@ public class HTTPSWebServerAuthTest {
public void testHTTPSAuthForPages() throws IOException, WebException, KeyManagementException, NoSuchAlgorithmException { public void testHTTPSAuthForPages() throws IOException, WebException, KeyManagementException, NoSuchAlgorithmException {
String address = "https://localhost:9005"; String address = "https://localhost:9005";
URL url = new URL(address); URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = connector.getConnection("HET", address);
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);
String user = Base64Util.encode("test:testPass"); String user = Base64Util.encode("test:testPass");
connection.setRequestProperty("Authorization", "Basic " + user); connection.setRequestProperty("Authorization", "Basic " + user);

View File

@ -1,7 +1,5 @@
package com.djrapitops.plan.system.webserver; 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.data.container.Session;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
@ -16,9 +14,10 @@ import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import rules.BukkitComponentMocker;
import rules.ComponentMocker;
import rules.SeleniumDriver; import rules.SeleniumDriver;
import utilities.TestConstants; import utilities.TestConstants;
import utilities.mocks.PlanBukkitMocker;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -39,20 +38,15 @@ public class JSErrorRegressionTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@ClassRule @ClassRule
public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
@ClassRule
public static SeleniumDriver seleniumDriver = new SeleniumDriver(); public static SeleniumDriver seleniumDriver = new SeleniumDriver();
private static PlanSystem bukkitSystem; private static PlanSystem bukkitSystem;
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { public static void setUpClass() throws Exception {
PlanBukkitMocker mocker = PlanBukkitMocker.setUp() bukkitSystem = component.getPlanSystem();
.withDataFolder(temporaryFolder.getRoot())
.withPluginDescription()
.withResourceFetchingFromJar()
.withServer();
PlanBukkitComponent component = DaggerPlanBukkitComponent.builder().plan(mocker.getPlanMock()).build();
bukkitSystem = component.system();
PlanConfig config = bukkitSystem.getConfigSystem().getConfig(); PlanConfig config = bukkitSystem.getConfigSystem().getConfig();
config.set(Settings.WEBSERVER_PORT, 9005); config.set(Settings.WEBSERVER_PORT, 9005);

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -7,11 +7,13 @@ package com.djrapitops.plan;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.settings.Settings; 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.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.mocks.PlanSpongeMocker; import rules.ComponentMocker;
import rules.SpongeComponentMocker;
/** /**
* Test for Sponge PlanSystem. * Test for Sponge PlanSystem.
@ -23,36 +25,17 @@ public class SpongeSystemTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanSponge planMock; @ClassRule
private PlanSystem spongeSystem; public static ComponentMocker component = new SpongeComponentMocker(temporaryFolder);
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();
}
}
@Test @Test
public void testEnable() throws EnableException { public void testEnable() throws EnableException {
spongeSystem = component.system(); PlanSystem spongeSystem = component.getPlanSystem();
spongeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005); try {
spongeSystem.getConfigSystem().getConfig().set(Settings.WEBSERVER_PORT, 9005);
spongeSystem.enable(); spongeSystem.enable();
} finally {
spongeSystem.disable();
}
} }
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}

View File

@ -8,12 +8,13 @@ import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import org.junit.*; import org.junit.ClassRule;
import org.junit.rules.ExpectedException; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.mocks.PlanVelocityMocker; import rules.ComponentMocker;
import rules.VelocityComponentMocker;
/** /**
* Test for Velocity PlanSystem. * Test for Velocity PlanSystem.
@ -25,45 +26,23 @@ public class VelocitySystemTest {
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private static PlanVelocity PLUGIN_MOCK; @ClassRule
@Rule public static ComponentMocker component = new VelocityComponentMocker(temporaryFolder);
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();
}
}
@Test @Test
public void velocityEnables() throws Exception { 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(); DBSystem dbSystem = velocitySystem.getDatabaseSystem();
config.set(Settings.WEBSERVER_PORT, 9005); dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
config.set(Settings.BUNGEE_IP, "8.8.8.8");
DBSystem dbSystem = velocitySystem.getDatabaseSystem(); velocitySystem.enable();
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile()); } finally {
velocitySystem.disable();
velocitySystem.enable(); }
} }
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}