From 1e0d860007026c9b9c5083b6673f2cf8a7f91472 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Wed, 30 Aug 2017 20:21:25 +0200 Subject: [PATCH 1/7] Fix checkKey --- .../com/djrapitops/plan/systems/webserver/WebServer.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java index b298c3666..a8d937aa3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java @@ -326,8 +326,6 @@ public class WebServer { Map variables = readVariables(response); String key = variables.get("key"); - Plan plan = Plan.getInstance(); - if (!checkKey(key)) { String error = "Server Key not given or invalid"; return PageCache.loadPage(error, () -> { @@ -345,7 +343,7 @@ public class WebServer { } try { - return api.onResponse(plan, variables); + return api.onResponse(Plan.getInstance(), variables); } catch (Exception ex) { Log.toLog("WebServer.getAPIResponse", ex); return new InternalErrorResponse(ex, "An error while processing the request happened"); @@ -353,6 +351,10 @@ public class WebServer { } private boolean checkKey(String key) { + if (key == null) { + return false; + } + UUID uuid = Plan.getServerUUID(); UUID keyUUID; try { From 32a17782985ada528f1b2d8f8fea91e524ef7614 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Wed, 30 Aug 2017 23:45:01 +0200 Subject: [PATCH 2/7] Start of Cleanup of the tests --- .../com/djrapitops/plan/PermissionsTest.java | 2 +- .../plan/ServerVariableHolderTest.java | 2 +- .../com/djrapitops/plan/SettingsTest.java | 3 +- .../djrapitops/plan/data/PlayerKillTest.java | 38 ++++++ .../com/djrapitops/plan/data/SessionTest.java | 5 +- .../djrapitops/plan/data/UserInfoTest.java | 11 +- .../data/additional/AnalysisTypeTest.java | 31 +++++ .../importer/ImportBuilderTest.java | 10 +- .../plan/data/cache/DataCacheTest.java | 11 +- .../plan/data/cache/GeolocationCacheTest.java | 14 +- .../plan/data/cache/PageCacheTest.java | 2 +- .../plan/data/cache/SessionCacheTest.java | 8 +- .../plan/data/cache/queue/QueueTest.java | 11 +- .../plan/data/time/GMTimesTest.java | 125 ++++++++++++++++++ .../plan/data/time/WorldTimesTest.java | 95 +++++++------ .../djrapitops/plan/database/DBUtilsTest.java | 4 +- .../plan/database/DatabaseCommitTest.java | 14 +- .../plan/database/DatabaseTest.java | 50 ++++--- .../java/com/djrapitops/plan/ui/HtmlTest.java | 2 +- .../djrapitops/plan/ui/graphs/GraphTest.java | 2 +- .../djrapitops/plan/utilities/CheckTest.java | 3 +- .../plan/utilities/FormatUtilsTest.java | 3 +- .../plan/utilities/HtmlUtilsTest.java | 2 +- .../plan/utilities/MiscUtilsTest.java | 3 +- .../plan/utilities/PassEncryptTest.java | 3 +- .../utilities/analysis/AnalysisUtilsTest.java | 3 +- .../utilities/analysis/MathUtilsTest.java | 36 +---- .../utilities/comparators/ComparatorTest.java | 3 +- .../plan/utilities/dump/DumpLogTest.java | 2 +- .../plan/utilities/dump/HastebinTest.java | 2 +- .../utilities/html/HtmlStructureTest.java | 32 ++--- .../djrapitops/plan/data/PlayerKillTest.java | 59 --------- .../data/additional/AnalysisTypeTest.java | 40 ------ .../plan/data/time/GMTimesTest.java | 103 --------------- Plan/test/test/java/utils/DBTestSuite.java | 4 +- Plan/test/test/java/utils/TestInit.java | 3 - Plan/test/test/java/utils/TestUtils.java | 18 +++ 37 files changed, 354 insertions(+), 405 deletions(-) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/PermissionsTest.java (92%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/ServerVariableHolderTest.java (94%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/SettingsTest.java (95%) create mode 100644 Plan/test/main/java/com/djrapitops/plan/data/PlayerKillTest.java rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/SessionTest.java (76%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/UserInfoTest.java (78%) create mode 100644 Plan/test/main/java/com/djrapitops/plan/data/additional/AnalysisTypeTest.java rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java (94%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java (87%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java (90%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java (96%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java (87%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java (89%) create mode 100644 Plan/test/main/java/com/djrapitops/plan/data/time/GMTimesTest.java rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java (63%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/database/DBUtilsTest.java (92%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java (92%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/database/DatabaseTest.java (94%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/ui/HtmlTest.java (95%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java (98%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/CheckTest.java (92%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java (97%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java (97%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java (96%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java (89%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java (96%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java (89%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java (97%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java (95%) rename Plan/test/{test/java => }/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java (97%) delete mode 100644 Plan/test/test/java/main/java/com/djrapitops/plan/data/PlayerKillTest.java delete mode 100644 Plan/test/test/java/main/java/com/djrapitops/plan/data/additional/AnalysisTypeTest.java delete mode 100644 Plan/test/test/java/main/java/com/djrapitops/plan/data/time/GMTimesTest.java create mode 100644 Plan/test/test/java/utils/TestUtils.java diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/PermissionsTest.java b/Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java similarity index 92% rename from Plan/test/test/java/main/java/com/djrapitops/plan/PermissionsTest.java rename to Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java index 15d338f71..7ec491288 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/PermissionsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan; +package main.java.com.djrapitops.plan; import main.java.com.djrapitops.plan.Permissions; import org.junit.Test; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/ServerVariableHolderTest.java b/Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java similarity index 94% rename from Plan/test/test/java/main/java/com/djrapitops/plan/ServerVariableHolderTest.java rename to Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java index 4bcf1f196..d814c1f65 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/ServerVariableHolderTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan; +package main.java.com.djrapitops.plan; import main.java.com.djrapitops.plan.Plan; import org.bukkit.plugin.java.JavaPlugin; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/SettingsTest.java b/Plan/test/main/java/com/djrapitops/plan/SettingsTest.java similarity index 95% rename from Plan/test/test/java/main/java/com/djrapitops/plan/SettingsTest.java rename to Plan/test/main/java/com/djrapitops/plan/SettingsTest.java index 84a504708..00246a100 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/SettingsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/SettingsTest.java @@ -3,9 +3,8 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan; +package main.java.com.djrapitops.plan; -import main.java.com.djrapitops.plan.Settings; import org.bukkit.plugin.java.JavaPlugin; import org.junit.Before; import org.junit.Test; diff --git a/Plan/test/main/java/com/djrapitops/plan/data/PlayerKillTest.java b/Plan/test/main/java/com/djrapitops/plan/data/PlayerKillTest.java new file mode 100644 index 000000000..a2733d062 --- /dev/null +++ b/Plan/test/main/java/com/djrapitops/plan/data/PlayerKillTest.java @@ -0,0 +1,38 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package main.java.com.djrapitops.plan.data; + +import org.junit.Test; +import test.java.utils.RandomData; + +import java.util.UUID; + +import static org.junit.Assert.assertEquals; + +/** + * @author Rsl1122 + */ +public class PlayerKillTest { + + private String randomString = RandomData.randomString(10); + private UUID testUUID = UUID.randomUUID(); + private PlayerKill playerKill = new PlayerKill(testUUID, randomString, 100L); + + @Test + public void testGetVictim() { + assertEquals(playerKill.getVictim(), testUUID); + } + + @Test + public void testGetDate() { + assertEquals(playerKill.getTime(), 100L); + } + + @Test + public void testGetWeapon() { + assertEquals(playerKill.getWeapon(), randomString); + } +} diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/SessionTest.java b/Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java similarity index 76% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/SessionTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java index 57b212021..4b7188311 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/SessionTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java @@ -3,9 +3,8 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.data; +package main.java.com.djrapitops.plan.data; -import main.java.com.djrapitops.plan.data.Session; import org.junit.Before; /** @@ -13,7 +12,7 @@ import org.junit.Before; */ public class SessionTest { - private Session test; + private Session session; /** * diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/UserInfoTest.java b/Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java similarity index 78% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/UserInfoTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java index 2c5520bf0..b3defe9fe 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/UserInfoTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java @@ -1,7 +1,6 @@ -package test.java.main.java.com.djrapitops.plan.data; +package main.java.com.djrapitops.plan.data; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.data.UserInfo; import org.bukkit.plugin.java.JavaPlugin; import org.junit.Before; import org.junit.Test; @@ -17,18 +16,12 @@ import org.powermock.modules.junit4.PowerMockRunner; public class UserInfoTest { // TODO Rewrite - private UserInfo test; + private UserInfo session; private Plan plan; - /** - * - */ public UserInfoTest() { } - /** - * - */ @Before public void setUp() throws Exception { // TestInit t = TestInit.init(); diff --git a/Plan/test/main/java/com/djrapitops/plan/data/additional/AnalysisTypeTest.java b/Plan/test/main/java/com/djrapitops/plan/data/additional/AnalysisTypeTest.java new file mode 100644 index 000000000..cae285f6d --- /dev/null +++ b/Plan/test/main/java/com/djrapitops/plan/data/additional/AnalysisTypeTest.java @@ -0,0 +1,31 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package main.java.com.djrapitops.plan.data.additional; + +import org.junit.Test; +import test.java.utils.TestUtils; + +import static org.junit.Assert.assertEquals; + +/** + * @author Rsl1122 + */ +public class AnalysisTypeTest { + + @Test + public void testGetModifier() throws NoSuchFieldException, IllegalAccessException { + for (AnalysisType type : AnalysisType.values()) { + assertEquals(TestUtils.getStringFieldValue(type, "modifier"), type.getModifier()); + } + } + + @Test + public void testGetPlaceholderModifier() throws NoSuchFieldException, IllegalAccessException { + for (AnalysisType type : AnalysisType.values()) { + assertEquals(TestUtils.getStringFieldValue(type, "placeholderModifier"), type.getPlaceholderModifier()); + } + } +} diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java b/Plan/test/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java similarity index 94% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java index 93e3a14b4..e9d1184e2 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/additional/importer/ImportBuilderTest.java @@ -2,7 +2,7 @@ * Licence is provided in the jar as license.yml also here: * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml */ -package test.java.main.java.com.djrapitops.plan.data.additional.importer; +package main.java.com.djrapitops.plan.data.additional.importer; import com.google.common.collect.ImmutableMap; import main.java.com.djrapitops.plan.data.PlayerKill; @@ -27,6 +27,9 @@ import static org.junit.Assert.*; */ public class ImportBuilderTest { + private int randomInt = RandomData.randomInt(0, 10); + private String randomString = RandomData.randomString(randomInt); + @Test public void testEmptyServerBuilder() { ServerImportData data = ServerImportData.builder().build(); @@ -65,9 +68,6 @@ public class ImportBuilderTest { public void testServerDataBuilder() { ServerImportData.ServerImportDataBuilder builder = ServerImportData.builder(); - int randomInt = RandomData.randomInt(0, 10); - String randomString = RandomData.randomString(randomInt); - TPS tps = new TPS(randomInt, randomInt, randomInt, randomInt, randomInt, randomInt, randomInt); ServerImportData data = builder.tpsData(tps) @@ -96,9 +96,7 @@ public class ImportBuilderTest { public void testUserDataBuilder() { UserImportData.UserImportDataBuilder builder = UserImportData.builder(); - int randomInt = RandomData.randomInt(0, 10); UUID uuid = UUID.randomUUID(); - String randomString = RandomData.randomString(10); PlayerKill playerKill = new PlayerKill(uuid, randomString, 1); GMTimes gmTimes = new GMTimes(randomString, randomInt); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java b/Plan/test/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java similarity index 87% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java index 593fefc14..69952f927 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/cache/DataCacheTest.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.data.cache; +package main.java.com.djrapitops.plan.data.cache; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.systems.cache.DataCache; @@ -27,15 +27,6 @@ public class DataCacheTest { private int callsToSaveUserData; private int callsToSaveMultiple; - /** - * - */ - public DataCacheTest() { - } - - /** - * - */ @Before public void setUp() { diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java b/Plan/test/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java similarity index 90% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java index 7c218f2c1..95e32b102 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan.data.cache; +package main.java.com.djrapitops.plan.data.cache; import main.java.com.djrapitops.plan.systems.cache.GeolocationCache; import org.bukkit.plugin.java.JavaPlugin; @@ -24,8 +24,10 @@ public class GeolocationCacheTest { private final Map ipsToCountries = new HashMap<>(); @Before - public void setUp() { + public void setUp() throws Exception { + TestInit.init(); GeolocationCache.clearCache(); + ipsToCountries.put("8.8.8.8", "United States"); ipsToCountries.put("8.8.4.4", "United States"); ipsToCountries.put("4.4.2.2", "United States"); @@ -38,9 +40,7 @@ public class GeolocationCacheTest { } @Test - public void testCountryGetting() throws Exception { - TestInit.init(); - + public void testCountryGetting() { for (Map.Entry entry : ipsToCountries.entrySet()) { String ip = entry.getKey(); String expCountry = entry.getValue(); @@ -52,9 +52,7 @@ public class GeolocationCacheTest { } @Test - public void testCaching() throws Exception { - TestInit.init(); - + public void testCaching() { for (Map.Entry entry : ipsToCountries.entrySet()) { String ip = entry.getKey(); String expIp = entry.getValue(); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java b/Plan/test/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java similarity index 96% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java index 123453ce7..573698158 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/cache/PageCacheTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan.data.cache; +package main.java.com.djrapitops.plan.data.cache; import main.java.com.djrapitops.plan.systems.webserver.PageCache; import main.java.com.djrapitops.plan.systems.webserver.PageLoader; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java b/Plan/test/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java similarity index 87% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java index 7d83ff566..32567098f 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.data.cache; +package main.java.com.djrapitops.plan.data.cache; import main.java.com.djrapitops.plan.systems.cache.SessionCache; import org.bukkit.plugin.java.JavaPlugin; @@ -23,12 +23,6 @@ public class SessionCacheTest { private SessionCache test; - /** - * - */ - public SessionCacheTest() { - } - @Before public void setUp() throws Exception { TestInit.init(); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java b/Plan/test/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java similarity index 89% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java index aac1bc7a2..088fb3c22 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/cache/queue/QueueTest.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.data.cache.queue; +package main.java.com.djrapitops.plan.data.cache.queue; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.database.Database; @@ -17,13 +17,11 @@ 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.MockUtils; import test.java.utils.TestInit; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import static org.powermock.api.mockito.PowerMockito.when; @@ -32,21 +30,18 @@ import static org.powermock.api.mockito.PowerMockito.when; @PrepareForTest({JavaPlugin.class}) public class QueueTest { - private final UUID uuid1 = MockUtils.getPlayerUUID(); - private DataCache dataCache; private Database db; - public QueueTest() { - } - @Before public void setUp() throws Exception { TestInit t = TestInit.init(); Plan plan = t.getPlanMock(); + db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()); db.init(); when(plan.getDB()).thenReturn(db); + dataCache = new DataCache(plan); when(plan.getDataCache()).thenReturn(dataCache); } diff --git a/Plan/test/main/java/com/djrapitops/plan/data/time/GMTimesTest.java b/Plan/test/main/java/com/djrapitops/plan/data/time/GMTimesTest.java new file mode 100644 index 000000000..18dc002fd --- /dev/null +++ b/Plan/test/main/java/com/djrapitops/plan/data/time/GMTimesTest.java @@ -0,0 +1,125 @@ +package main.java.com.djrapitops.plan.data.time; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class GMTimesTest { + @Test + public void testSetAllGMTimes() { + GMTimes gmTimes = new GMTimes(); + gmTimes.setAllGMTimes(1L, 2L, 3L, 4L); + + Map times = gmTimes.getTimes(); + + assertEquals(times.size(), 4); + assertTrue(times.get("SURVIVAL") == 1L); + assertTrue(times.get("CREATIVE") == 2L); + assertTrue(times.get("ADVENTURE") == 3L); + assertTrue(times.get("SPECTATOR") == 4L); + } + + @Test + public void testSetAllGMTimesTooFew() { + GMTimes gmTimes = new GMTimes(); + gmTimes.setAllGMTimes(1L, 2L); + + Map times = gmTimes.getTimes(); + + assertEquals(times.size(), 4); + assertTrue(times.get("SURVIVAL") == 1L); + assertTrue(times.get("CREATIVE") == 2L); + assertTrue(times.get("ADVENTURE") == 0L); + assertTrue(times.get("SPECTATOR") == 0L); + } + + @Test + public void testSetAllGMTimesTooMany() { + GMTimes gmTimes = new GMTimes(); + gmTimes.setAllGMTimes(1L, 2L, 3L, 4L, 5L, 6L); + + Map times = gmTimes.getTimes(); + + assertEquals(times.size(), 4); + assertTrue(times.get("SURVIVAL") == 1L); + assertTrue(times.get("CREATIVE") == 2L); + assertTrue(times.get("ADVENTURE") == 3L); + assertTrue(times.get("SPECTATOR") == 4L); + } + + @Test + public void testResetTimes() { + GMTimes gmTimes = new GMTimes(); + gmTimes.setAllGMTimes(4, 3, 2, 1); + gmTimes.resetTimes(10); + + assertTrue(gmTimes.getTotal() == 10L); + assertTrue(gmTimes.getTime("SURVIVAL") == 10L); + assertTrue(gmTimes.getTime("ADVENTURE") == 0L); + } + + @Test + public void testSetTime() { + GMTimes gmTimes = new GMTimes(); + gmTimes.setTime("SURVIVAL", 5L); + + assertTrue(gmTimes.getTime("SURVIVAL") == 5L); + } + + @Test + public void testRenameState() { + GMTimes gmTimes = new GMTimes(); + gmTimes.setAllGMTimes(5L); + gmTimes.renameState("SURVIVAL", "Survival"); + + assertTrue(gmTimes.getTime("SURVIVAL") == 0L); + assertTrue(gmTimes.getTime("Survival") == 5L); + } + + @Test + public void testChangeStateNormal() { + GMTimes gmTimes = new GMTimes(new HashMap<>(), "SURVIVAL", 0); + gmTimes.changeState("CREATIVE", 5L); + + assertTrue(gmTimes.getTime("SURVIVAL") == 5L); + assertTrue(gmTimes.getTime("CREATIVE") == 0L); + + gmTimes.changeState("ADVENTURE", 20L); + + assertTrue(gmTimes.getTime("SURVIVAL") == 5L); + assertTrue(gmTimes.getTime("CREATIVE") == 15L); + assertTrue(gmTimes.getTime("ADVENTURE") == 0L); + } + + @Test + public void testChangeStateMissingStartTime() { + GMTimes gmTimes = new GMTimes("SURVIVAL"); + gmTimes.changeState("CREATIVE", 5L); + + assertTrue(5L == gmTimes.getTime("SURVIVAL")); + assertTrue(0L == gmTimes.getTime("CREATIVE")); + + gmTimes.changeState("ADVENTURE", 20L); + + assertTrue(5L == gmTimes.getTime("SURVIVAL")); + assertTrue(15L == gmTimes.getTime("CREATIVE")); + assertTrue(0L == gmTimes.getTime("ADVENTURE")); + } + + @Test + public void testChangeStateMissingStartState() { + GMTimes test = new GMTimes(); + test.changeState("CREATIVE", 5L); + + assertTrue(5L == test.getTime("CREATIVE")); + + test.changeState("ADVENTURE", 20L); + + assertTrue(20L == test.getTime("CREATIVE")); + assertTrue(0L == test.getTime("ADVENTURE")); + } +} \ No newline at end of file diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java similarity index 63% rename from Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java rename to Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java index 96a975e54..17c466853 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -1,8 +1,5 @@ -package test.java.main.java.com.djrapitops.plan.data.time; +package main.java.com.djrapitops.plan.data.time; -import main.java.com.djrapitops.plan.data.time.GMTimes; -import main.java.com.djrapitops.plan.data.time.WorldTimes; -import org.junit.Before; import org.junit.Test; import test.java.utils.RandomData; @@ -21,42 +18,42 @@ public class WorldTimesTest { private final String worldOne = "ONE"; private final String worldTwo = "TWO"; private final String[] gms = GMTimes.getGMKeyArray(); - private long time; - private WorldTimes test; - - @Before - public void setUp() throws Exception { - test = new WorldTimes(worldOne, gms[0]); - time = test.getGMTimes(worldOne).getLastStateChange(); - } + private WorldTimes worldTimes = new WorldTimes(worldOne, gms[0]); + private long time = worldTimes.getGMTimes(worldOne).getLastStateChange(); @Test public void testWorldChange() { long changeTime = time + 1000L; - test.updateState(worldTwo, gms[0], changeTime); - assertEquals(1000L, test.getWorldPlaytime(worldOne)); - assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); + worldTimes.updateState(worldTwo, gms[0], changeTime); + + assertEquals(1000L, worldTimes.getWorldPlaytime(worldOne)); + assertEquals(1000L, worldTimes.getGMTimes(worldOne).getTime(gms[0])); } @Test public void testGMChange() { long changeTime = time + 1000L; - test.updateState(worldOne, gms[0], changeTime); - assertEquals(1000L, test.getWorldPlaytime(worldOne)); - assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); + worldTimes.updateState(worldOne, gms[0], changeTime); + + assertEquals(1000L, worldTimes.getWorldPlaytime(worldOne)); + assertEquals(1000L, worldTimes.getGMTimes(worldOne).getTime(gms[0])); } @Test public void testBothTwiceChange() { long changeTime = time + 1000L; long changeTime2 = changeTime + 1000L; - test.updateState(worldTwo, gms[2], changeTime); - assertEquals(1000L, test.getWorldPlaytime(worldOne)); - assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); - test.updateState(worldOne, gms[1], changeTime2); - assertEquals(1000L, test.getWorldPlaytime(worldOne)); - assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0])); - assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2])); + + worldTimes.updateState(worldTwo, gms[2], changeTime); + + assertEquals(1000L, worldTimes.getWorldPlaytime(worldOne)); + assertEquals(1000L, worldTimes.getGMTimes(worldOne).getTime(gms[0])); + + worldTimes.updateState(worldOne, gms[1], changeTime2); + + assertEquals(1000L, worldTimes.getWorldPlaytime(worldOne)); + assertEquals(1000L, worldTimes.getGMTimes(worldOne).getTime(gms[0])); + assertEquals(1000L, worldTimes.getGMTimes(worldTwo).getTime(gms[2])); } @Test @@ -82,7 +79,7 @@ public class WorldTimesTest { long time = i * amount + this.time; - test.updateState(world, gm, time); + worldTimes.updateState(world, gm, time); } long worldOneCount = testedW.get(worldOne).size(); @@ -90,8 +87,8 @@ public class WorldTimesTest { long worldTimeOne = worldOneCount * amount; long worldTimeTwo = worldTwoCount * amount; - long time1 = test.getWorldPlaytime(worldOne); - long time2 = test.getWorldPlaytime(worldTwo); + long time1 = worldTimes.getWorldPlaytime(worldOne); + long time2 = worldTimes.getWorldPlaytime(worldTwo); // Tests World time calculation. assertEquals(amount * 50, time1 + time2); @@ -103,11 +100,16 @@ public class WorldTimesTest { public void testGMTrackingSingleWorld() { long changeTime = time + 1000L; long changeTime2 = changeTime + 1000L; - GMTimes gmTimes = test.getGMTimes(worldOne); - test.updateState(worldOne, "CREATIVE", changeTime); + + GMTimes gmTimes = worldTimes.getGMTimes(worldOne); + + worldTimes.updateState(worldOne, "CREATIVE", changeTime); + assertEquals(1000L, gmTimes.getTime("SURVIVAL")); assertEquals(0L, gmTimes.getTime("CREATIVE")); - test.updateState(worldOne, "ADVENTURE", changeTime2); + + worldTimes.updateState(worldOne, "ADVENTURE", changeTime2); + assertEquals(1000L, gmTimes.getTime("SURVIVAL")); assertEquals(1000L, gmTimes.getTime("CREATIVE")); assertEquals(0L, gmTimes.getTime("ADVENTURE")); @@ -117,15 +119,19 @@ public class WorldTimesTest { public void testGMTrackingTwoWorlds() { long changeTime = time + 1000L; long changeTime2 = time + 2000L; - GMTimes worldOneGMTimes = test.getGMTimes(worldOne); - test.updateState(worldOne, "CREATIVE", changeTime); - test.updateState(worldOne, "ADVENTURE", changeTime2); + + GMTimes worldOneGMTimes = worldTimes.getGMTimes(worldOne); + + worldTimes.updateState(worldOne, "CREATIVE", changeTime); + worldTimes.updateState(worldOne, "ADVENTURE", changeTime2); + assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); assertEquals(0L, worldOneGMTimes.getTime("ADVENTURE")); - test.updateState(worldTwo, "SURVIVAL", time + 3000L); - GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo); + worldTimes.updateState(worldTwo, "SURVIVAL", time + 3000L); + GMTimes worldTwoGMTimes = worldTimes.getGMTimes(worldTwo); + assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); @@ -134,7 +140,7 @@ public class WorldTimesTest { assertEquals(0L, worldTwoGMTimes.getTime("CREATIVE")); assertEquals(0L, worldTwoGMTimes.getTime("ADVENTURE")); - test.updateState(worldTwo, "CREATIVE", time + 4000L); + worldTimes.updateState(worldTwo, "CREATIVE", time + 4000L); assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); @@ -143,20 +149,25 @@ public class WorldTimesTest { assertEquals(1000L, worldTwoGMTimes.getTime("SURVIVAL")); assertEquals(0L, worldTwoGMTimes.getTime("CREATIVE")); - test.updateState(worldTwo, "CREATIVE", time + 5000L); + worldTimes.updateState(worldTwo, "CREATIVE", time + 5000L); + assertEquals(1000L, worldTwoGMTimes.getTime("SURVIVAL")); assertEquals(1000L, worldTwoGMTimes.getTime("CREATIVE")); // No change should occur. - test.updateState(worldOne, "ADVENTURE", time + 5000L); + worldTimes.updateState(worldOne, "ADVENTURE", time + 5000L); + assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); assertEquals(1000L, worldTwoGMTimes.getTime("CREATIVE")); - test.updateState(worldTwo, "CREATIVE", time + 5000L); - test.updateState(worldOne, "ADVENTURE", time + 6000L); + + worldTimes.updateState(worldTwo, "CREATIVE", time + 5000L); + worldTimes.updateState(worldOne, "ADVENTURE", time + 6000L); + assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); assertEquals(2000L, worldTwoGMTimes.getTime("CREATIVE")); - test.updateState(worldTwo, "ADVENTURE", time + 7000L); + worldTimes.updateState(worldTwo, "ADVENTURE", time + 7000L); + assertEquals(2000L, worldTwoGMTimes.getTime("CREATIVE")); assertEquals(2000L, worldOneGMTimes.getTime("ADVENTURE")); } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DBUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java similarity index 92% rename from Plan/test/test/java/main/java/com/djrapitops/plan/database/DBUtilsTest.java rename to Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java index d6f230941..4e7a346b8 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DBUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java @@ -3,10 +3,8 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.database; +package main.java.com.djrapitops.plan.database; -import main.java.com.djrapitops.plan.database.Container; -import main.java.com.djrapitops.plan.database.DBUtils; import org.junit.Test; import test.java.utils.RandomData; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java similarity index 92% rename from Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java rename to Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java index 4a9b7a6f5..c66476f23 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan.database; +package main.java.com.djrapitops.plan.database; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.data.TPS; @@ -39,20 +39,18 @@ public class DatabaseCommitTest { public void setUp() throws Exception { TestInit t = TestInit.init(); plan = t.getPlanMock(); - db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()); - File f = new File(plan.getDataFolder(), "Errors.txt"); - rows = FileUtil.lines(f).size(); + db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()); db.init(); + + File file = new File(plan.getDataFolder(), "Errors.txt"); + rows = FileUtil.lines(file).size(); } - /** - * @throws IOException - * @throws SQLException - */ @After public void tearDown() throws IOException, SQLException { db.close(); + File f = new File(plan.getDataFolder(), "Errors.txt"); List lines = FileUtil.lines(f); int rowsAgain = lines.size(); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java similarity index 94% rename from Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java rename to Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java index a530c355a..8ada76aad 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseTest.java @@ -3,14 +3,13 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.database; +package main.java.com.djrapitops.plan.database; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; import main.java.com.djrapitops.plan.data.*; import main.java.com.djrapitops.plan.data.time.GMTimes; import main.java.com.djrapitops.plan.data.time.WorldTimes; -import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.databases.MySQLDB; import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.database.tables.*; @@ -26,6 +25,7 @@ import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.MockUtils; +import test.java.utils.RandomData; import test.java.utils.TestInit; import java.io.File; @@ -52,16 +52,16 @@ public class DatabaseTest { private Database backup; private int rows; - public DatabaseTest() { - } - @Before public void setUp() throws Exception { TestInit t = TestInit.init(); plan = t.getPlanMock(); + db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()); db.init(); + db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", "")); + File f = new File(plan.getDataFolder(), "Errors.txt"); rows = FileUtil.lines(f).size(); } @@ -72,6 +72,7 @@ public class DatabaseTest { if (backup != null) { backup.close(); } + File f = new File(plan.getDataFolder(), "Errors.txt"); List lines = FileUtil.lines(f); @@ -81,6 +82,7 @@ public class DatabaseTest { System.out.println(line); } } + assertTrue("Errors were caught.", rows == rowsAgain); } @@ -90,22 +92,22 @@ public class DatabaseTest { } @Test - public void testSqLiteGetConfigName() { + public void testSQLiteGetConfigName() { assertEquals("sqlite", db.getConfigName()); } @Test - public void testSqLiteGetgName() { + public void testSQLiteGetName() { assertEquals("SQLite", db.getName()); } @Test - public void testMysqlGetConfigName() { + public void testMySQLGetConfigName() { assertEquals("mysql", new MySQLDB(plan).getConfigName()); } @Test - public void testMysqlGetName() { + public void testMySQLGetName() { assertEquals("MySQL", new MySQLDB(plan).getName()); } @@ -120,15 +122,19 @@ public class DatabaseTest { expected.put("help", 21); commandUseTable.commandUsed("plan"); + for (int i = 0; i < 4; i++) { commandUseTable.commandUsed("tp"); } + for (int i = 0; i < 7; i++) { commandUseTable.commandUsed("pla"); } + for (int i = 0; i < 21; i++) { commandUseTable.commandUsed("help"); } + for (int i = 0; i < 3; i++) { commandUseTable.commandUsed("roiergbnougbierubieugbeigubeigubgierbgeugeg"); } @@ -141,9 +147,11 @@ public class DatabaseTest { for (int i = 0; i < 3; i++) { commandUseTable.commandUsed("test"); } + for (int i = 0; i < 2; i++) { commandUseTable.commandUsed("tp"); } + expected.put("test", 3); expected.put("tp", 6); @@ -156,21 +164,29 @@ public class DatabaseTest { public void testCommandUseTableIDSystem() throws SQLException { CommandUseTable commandUseTable = db.getCommandUseTable(); commandUseTable.commandUsed("plan"); + for (int i = 0; i < 4; i++) { commandUseTable.commandUsed("tp"); } + for (int i = 0; i < 7; i++) { commandUseTable.commandUsed("pla"); } + for (int i = 0; i < 21; i++) { commandUseTable.commandUsed("help"); } + for (int i = 0; i < 3; i++) { commandUseTable.commandUsed("roiergbnougbierubieugbeigubeigubgierbgeugeg"); } + Optional id = commandUseTable.getCommandID("plan"); + assertTrue(id.isPresent()); + Optional commandByID = commandUseTable.getCommandByID(id.get()); + assertTrue(commandByID.isPresent()); assertEquals("plan", commandByID.get()); assertFalse(commandUseTable.getCommandID("roiergbnougbierubieugbeigubeigubgierbgeugeg").isPresent()); @@ -181,20 +197,11 @@ public class DatabaseTest { TPSTable tpsTable = db.getTpsTable(); Random r = new Random(); - OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); - int availableProcessors = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors(); - final double averageCPUUsage = MathUtils.round(operatingSystemMXBean.getSystemLoadAverage() / availableProcessors * 100.0); - - final long usedMemory = 51231251254L; - final int entityCount = 6123; - final int chunksLoaded = 2134; - List expected = new ArrayList<>(); - expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded)); - expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded)); - expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded)); - expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded)); + for (int i = 0; i < RandomData.randomInt(1, 5); i++) { + expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), r.nextDouble(), r.nextLong(), r.nextInt(), r.nextInt())); + } for (TPS tps : expected) { tpsTable.insertTPS(tps); @@ -581,6 +588,7 @@ public class DatabaseTest { assertFalse(usersTable.isRegistered(uuid)); assertFalse(usersTable.isRegistered(uuid2)); assertFalse(userInfoTable.isRegistered(uuid)); + assertTrue(nicknamesTable.getNicknames(uuid).isEmpty()); assertTrue(ipsTable.getGeolocations(uuid).isEmpty()); assertTrue(ipsTable.getIps(uuid).isEmpty()); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/ui/HtmlTest.java b/Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java similarity index 95% rename from Plan/test/test/java/main/java/com/djrapitops/plan/ui/HtmlTest.java rename to Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java index b0ca31599..da9289b08 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/ui/HtmlTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.ui; +package main.java.com.djrapitops.plan.ui; import main.java.com.djrapitops.plan.utilities.html.Html; import org.junit.Test; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java b/Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java similarity index 98% rename from Plan/test/test/java/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java rename to Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java index 127aa710f..b913343a4 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/ui/graphs/GraphTest.java @@ -2,7 +2,7 @@ * Licence is provided in the jar as license.yml also here: * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml */ -package test.java.main.java.com.djrapitops.plan.ui.graphs; +package main.java.com.djrapitops.plan.ui.graphs; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.TPS; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/CheckTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/CheckTest.java similarity index 92% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/CheckTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/CheckTest.java index 13cac1e50..ae4bca6f7 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/CheckTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/CheckTest.java @@ -1,7 +1,6 @@ -package test.java.main.java.com.djrapitops.plan.utilities; +package main.java.com.djrapitops.plan.utilities; import com.djrapitops.plugin.command.ISender; -import main.java.com.djrapitops.plan.utilities.Check; import org.bukkit.plugin.java.JavaPlugin; import org.junit.Before; import org.junit.Test; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java similarity index 97% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java index 7be49e322..4edccd157 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java @@ -1,6 +1,5 @@ -package test.java.main.java.com.djrapitops.plan.utilities; +package main.java.com.djrapitops.plan.utilities; -import main.java.com.djrapitops.plan.utilities.FormatUtils; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.plugin.java.JavaPlugin; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java similarity index 97% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java index 9a79f7b28..743573ecf 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.utilities; +package main.java.com.djrapitops.plan.utilities; import com.google.common.collect.ImmutableMap; import main.java.com.djrapitops.plan.utilities.html.HtmlUtils; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java similarity index 96% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java index 08a259b0d..7a50505fc 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java @@ -3,11 +3,10 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.utilities; +package main.java.com.djrapitops.plan.utilities; import com.djrapitops.plugin.command.ISender; import com.djrapitops.plugin.command.bukkit.BukkitCMDSender; -import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.junit.Ignore; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java similarity index 89% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java index 2bc6ae490..b796d062a 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java @@ -1,6 +1,5 @@ -package test.java.main.java.com.djrapitops.plan.utilities; +package main.java.com.djrapitops.plan.utilities; -import main.java.com.djrapitops.plan.utilities.PassEncryptUtil; import org.junit.Before; import org.junit.Test; import test.java.utils.RandomData; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java similarity index 96% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java index 5a3d84eff..9046ef91d 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java @@ -3,11 +3,10 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.utilities.analysis; +package main.java.com.djrapitops.plan.utilities.analysis; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.utilities.MiscUtils; -import main.java.com.djrapitops.plan.utilities.analysis.AnalysisUtils; import org.bukkit.plugin.java.JavaPlugin; import org.junit.Before; import org.junit.Test; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java similarity index 89% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java index c2bccf330..aadebd827 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java @@ -3,9 +3,8 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package test.java.main.java.com.djrapitops.plan.utilities.analysis; +package main.java.com.djrapitops.plan.utilities.analysis; -import main.java.com.djrapitops.plan.utilities.analysis.MathUtils; import org.junit.Test; import java.io.Serializable; @@ -21,15 +20,6 @@ import static org.junit.Assert.assertTrue; */ public class MathUtilsTest { - /** - * - */ - public MathUtilsTest() { - } - - /** - * - */ @Test public void testAverageInt() { List l = new ArrayList<>(); @@ -42,9 +32,6 @@ public class MathUtilsTest { assertTrue(Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testAverageIntEmpty() { List l = Collections.emptyList(); @@ -53,9 +40,6 @@ public class MathUtilsTest { assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testAverageLongCollection() { List l = new ArrayList<>(); @@ -68,9 +52,6 @@ public class MathUtilsTest { assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testAverageDouble() { List l = new ArrayList<>(); @@ -84,9 +65,6 @@ public class MathUtilsTest { } - /** - * - */ @Test public void testAverage() { double exp = 10; @@ -94,9 +72,6 @@ public class MathUtilsTest { assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testCountTrueBoolean() { List l = new ArrayList<>(); @@ -111,9 +86,6 @@ public class MathUtilsTest { assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testSumInt() { List l = new ArrayList<>(); @@ -126,9 +98,6 @@ public class MathUtilsTest { assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testSumLong() { List l = new ArrayList<>(); @@ -141,9 +110,6 @@ public class MathUtilsTest { assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } - /** - * - */ @Test public void testSumDouble() { List l = new ArrayList<>(); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java similarity index 97% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java index a3fff42f7..dda30a6a4 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan.utilities.comparators; +package main.java.com.djrapitops.plan.utilities.comparators; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.TPS; @@ -8,7 +8,6 @@ import main.java.com.djrapitops.plan.locale.Message; import main.java.com.djrapitops.plan.locale.Msg; import main.java.com.djrapitops.plan.utilities.PassEncryptUtil; import main.java.com.djrapitops.plan.utilities.analysis.Point; -import main.java.com.djrapitops.plan.utilities.comparators.*; import org.junit.Test; import test.java.utils.RandomData; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java similarity index 95% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java index d708f7d75..3e0ebfaff 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/dump/DumpLogTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan.utilities.dump; +package main.java.com.djrapitops.plan.utilities.dump; import main.java.com.djrapitops.plan.utilities.file.dump.DumpLog; import org.junit.Test; diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java similarity index 97% rename from Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java rename to Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java index 00ebe53e5..1a00cd616 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java @@ -1,4 +1,4 @@ -package test.java.main.java.com.djrapitops.plan.utilities.dump; +package main.java.com.djrapitops.plan.utilities.dump; import com.google.common.collect.Iterables; import main.java.com.djrapitops.plan.Log; diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java index 3e567cd7d..96c35db00 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java @@ -3,14 +3,17 @@ package main.java.com.djrapitops.plan.utilities.html; import main.java.com.djrapitops.plan.data.Session; import org.apache.commons.lang3.StringUtils; import org.bukkit.plugin.java.JavaPlugin; -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.*; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; @@ -24,37 +27,34 @@ import static org.junit.Assert.assertEquals; @PrepareForTest(JavaPlugin.class) public class HtmlStructureTest { - private Map> sessions; + private Map> sessions = new HashMap<>(); - @Before - public void setUp() throws Exception { - TestInit t = TestInit.init(); - sessions = new HashMap<>(); - sessions.put("World 1", new ArrayList<>()); - sessions.get("World 1").add(new Session(1, 12345L, 23455L, 1, 2)); - sessions.get("World 1").add(new Session(2, 23455L, 23457L, 1, 2)); - sessions.put("World 2", new ArrayList<>()); - sessions.get("World 2").add(new Session(3, 23455L, 23457L, 1, 2)); + public HtmlStructureTest() throws Exception { + TestInit.init(); + + for (int i = 0; i < RandomData.randomInt(0, 5); i++) { + sessions.put(RandomData.randomString(10), RandomData.randomSessions()); + } } @Test public void createServerOverviewColumn() throws Exception { String serverOverviewColumn = HtmlStructure.createServerOverviewColumn(sessions); + int opened = StringUtils.countMatches(serverOverviewColumn, " allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toList()); String sessionsTab = HtmlStructure.createSessionsTabContent(sessions, allSessions); + int opened = StringUtils.countMatches(sessionsTab, " times = test.getTimes(); - assertTrue(times.get("SURVIVAL") == 1L); - assertTrue(times.get("CREATIVE") == 2L); - assertTrue(times.get("ADVENTURE") == 3L); - assertTrue(times.get("SPECTATOR") == 4L); - } - - @Test - public void setAllGMTimesTooFew() throws Exception { - GMTimes test = new GMTimes(); - test.setAllGMTimes(1L, 2L); - Map times = test.getTimes(); - assertTrue(times.get("SURVIVAL") == 1L); - assertTrue(times.get("CREATIVE") == 2L); - assertTrue(times.get("ADVENTURE") == 0L); - assertTrue(times.get("SPECTATOR") == 0L); - } - - @Test - public void setAllGMTimesTooMany() throws Exception { - GMTimes test = new GMTimes(); - test.setAllGMTimes(1L, 2L, 3L, 4L, 5L, 6L); - Map times = test.getTimes(); - assertTrue(times.get("SURVIVAL") == 1L); - assertTrue(times.get("CREATIVE") == 2L); - assertTrue(times.get("ADVENTURE") == 3L); - assertTrue(times.get("SPECTATOR") == 4L); - } - - @Test - public void resetTimes() throws Exception { - GMTimes test = new GMTimes(); - test.setAllGMTimes(4, 3, 2, 1); - test.resetTimes(10); - assertTrue(10L == test.getTime("SURVIVAL")); - assertTrue(0L == test.getTime("ADVENTURE")); - } - - @Test - public void setTime() throws Exception { - GMTimes test = new GMTimes(); - test.setTime("SURVIVAL", 5L); - assertTrue(5L == test.getTime("SURVIVAL")); - } - - @Test - public void renameState() throws Exception { - GMTimes test = new GMTimes(); - test.setAllGMTimes(5L); - test.renameState("SURVIVAL", "Survival"); - assertTrue(0L == test.getTime("SURVIVAL")); - assertTrue(5L == test.getTime("Survival")); - } - - @Test - public void changeStateNormal() throws Exception { - GMTimes test = new GMTimes(new HashMap<>(), "SURVIVAL", 0); - test.changeState("CREATIVE", 5L); - assertTrue(5L == test.getTime("SURVIVAL")); - assertTrue(0L == test.getTime("CREATIVE")); - test.changeState("ADVENTURE", 20L); - assertTrue(5L == test.getTime("SURVIVAL")); - assertTrue(15L == test.getTime("CREATIVE")); - assertTrue(0L == test.getTime("ADVENTURE")); - } - - @Test - public void changeStateMissingStartTime() throws Exception { - GMTimes test = new GMTimes("SURVIVAL"); - test.changeState("CREATIVE", 5L); - assertTrue(5L == test.getTime("SURVIVAL")); - assertTrue(0L == test.getTime("CREATIVE")); - test.changeState("ADVENTURE", 20L); - assertTrue(5L == test.getTime("SURVIVAL")); - assertTrue(15L == test.getTime("CREATIVE")); - assertTrue(0L == test.getTime("ADVENTURE")); - } - - @Test - public void changeStateMissingStartState() throws Exception { - GMTimes test = new GMTimes(); - test.changeState("CREATIVE", 5L); - assertTrue(5L == test.getTime("CREATIVE")); - test.changeState("ADVENTURE", 20L); - assertTrue(20L == test.getTime("CREATIVE")); - assertTrue(0L == test.getTime("ADVENTURE")); - } -} \ No newline at end of file diff --git a/Plan/test/test/java/utils/DBTestSuite.java b/Plan/test/test/java/utils/DBTestSuite.java index 02847b493..2d8d5e35d 100644 --- a/Plan/test/test/java/utils/DBTestSuite.java +++ b/Plan/test/test/java/utils/DBTestSuite.java @@ -8,8 +8,8 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; -import test.java.main.java.com.djrapitops.plan.database.DatabaseCommitTest; -import test.java.main.java.com.djrapitops.plan.database.DatabaseTest; +import main.java.com.djrapitops.plan.database.DatabaseCommitTest; +import main.java.com.djrapitops.plan.database.DatabaseTest; import java.io.File; import java.io.IOException; diff --git a/Plan/test/test/java/utils/TestInit.java b/Plan/test/test/java/utils/TestInit.java index 2ad32bfca..56e3df539 100644 --- a/Plan/test/test/java/utils/TestInit.java +++ b/Plan/test/test/java/utils/TestInit.java @@ -39,9 +39,6 @@ public class TestInit { private static final UUID serverUUID = UUID.fromString("9a27457b-f1a2-4b71-be7f-daf2170a1b66"); private Plan planMock; - public TestInit() { - } - /** * Init locale with empty messages. *

diff --git a/Plan/test/test/java/utils/TestUtils.java b/Plan/test/test/java/utils/TestUtils.java new file mode 100644 index 000000000..09e3be2bf --- /dev/null +++ b/Plan/test/test/java/utils/TestUtils.java @@ -0,0 +1,18 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package test.java.utils; + +import java.lang.reflect.Field; + +/** + * @author Fuzzlemann + */ +public class TestUtils { + public static String getStringFieldValue(Enum enumeration, String modifier) throws NoSuchFieldException, IllegalAccessException { + Field field = enumeration.getClass().getDeclaredField(modifier); + field.setAccessible(true); + return (String) field.get(enumeration); + } +} From 825bb458060628e39781293f2d31081c8fbf9412 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 31 Aug 2017 17:52:08 +0200 Subject: [PATCH 3/7] Replace removeXSS method with Regex --- .../java/com/djrapitops/plan/utilities/html/HtmlUtils.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java index fe369c159..72ceee668 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java @@ -93,10 +93,7 @@ public class HtmlUtils { * @return */ public static String removeXSS(String string) { - return string.replace("", "") - .replace("", ""); + return string.replaceAll("()|()", ""); } /** From dc2b139ac64b5833dcc8abfd1791166c38e67060 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 31 Aug 2017 18:44:24 +0200 Subject: [PATCH 4/7] Further Cleanup of Tests --- .../com/djrapitops/plan/PermissionsTest.java | 20 ++-- .../plan/ServerVariableHolderTest.java | 13 ++- .../com/djrapitops/plan/SettingsTest.java | 11 +- .../djrapitops/plan/database/DBUtilsTest.java | 6 + .../plan/database/DatabaseCommitTest.java | 1 + .../java/com/djrapitops/plan/ui/HtmlTest.java | 27 +---- .../djrapitops/plan/ui/graphs/GraphTest.java | 10 +- .../plan/utilities/FormatUtilsTest.java | 44 ++++++- .../plan/utilities/HtmlUtilsTest.java | 2 +- .../plan/utilities/MiscUtilsTest.java | 35 ++++-- .../plan/utilities/PassEncryptTest.java | 4 +- .../utilities/analysis/AnalysisUtilsTest.java | 61 +++------- .../utilities/analysis/MathUtilsTest.java | 90 +++++++-------- .../utilities/comparators/ComparatorTest.java | 107 ++++++++++-------- .../plan/utilities/dump/HastebinTest.java | 15 +-- .../utilities/html/HtmlStructureTest.java | 4 +- Plan/test/test/java/utils/TestInit.java | 38 +++++-- 17 files changed, 266 insertions(+), 222 deletions(-) diff --git a/Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java b/Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java index 7ec491288..171c9ada3 100644 --- a/Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/PermissionsTest.java @@ -5,8 +5,8 @@ */ package main.java.com.djrapitops.plan; -import main.java.com.djrapitops.plan.Permissions; import org.junit.Test; +import test.java.utils.TestUtils; import static org.junit.Assert.assertEquals; @@ -15,17 +15,13 @@ import static org.junit.Assert.assertEquals; */ public class PermissionsTest { - /** - * - */ - public PermissionsTest() { - } - - /** - * - */ @Test - public void testGetPermission() { - assertEquals("plan.inspect.other", Permissions.INSPECT_OTHER.getPerm()); + public void testGetPermission() throws NoSuchFieldException, IllegalAccessException { + for (Permissions type : Permissions.values()) { + String exp = TestUtils.getStringFieldValue(type, "permission"); + + assertEquals(exp, type.getPermission()); + assertEquals(exp, type.getPerm()); + } } } \ No newline at end of file diff --git a/Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java b/Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java index d814c1f65..d35774bf3 100644 --- a/Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/ServerVariableHolderTest.java @@ -1,7 +1,7 @@ package main.java.com.djrapitops.plan; -import main.java.com.djrapitops.plan.Plan; import org.bukkit.plugin.java.JavaPlugin; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -18,14 +18,17 @@ import static junit.framework.TestCase.assertFalse; @PrepareForTest(JavaPlugin.class) public class ServerVariableHolderTest { - @Test - public void testServerVariable() throws Exception { + @Before + public void setUp() throws Exception { TestInit.init(); + } + @Test + public void testServerVariable() { boolean usingPaper = Plan.getInstance().getVariable().isUsingPaper(); assertFalse(usingPaper); - String ip = Plan.getInstance().getVariable().getIp(); - assertEquals(ip, "0.0.0.0"); + String exp = Plan.getInstance().getVariable().getIp(); + assertEquals(exp, "0.0.0.0"); } } diff --git a/Plan/test/main/java/com/djrapitops/plan/SettingsTest.java b/Plan/test/main/java/com/djrapitops/plan/SettingsTest.java index 00246a100..d0fe6a2ee 100644 --- a/Plan/test/main/java/com/djrapitops/plan/SettingsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/SettingsTest.java @@ -13,7 +13,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.TestInit; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static org.junit.Assert.*; @@ -25,9 +25,6 @@ import static org.junit.Assert.*; @PrepareForTest(JavaPlugin.class) public class SettingsTest { - public SettingsTest() { - } - @Before public void setUp() throws Exception { TestInit.init(); @@ -39,7 +36,7 @@ public class SettingsTest { } @Test - public void testIsTrue2() { + public void testSetValue() { Settings gatherCommands = Settings.LOG_UNKNOWN_COMMANDS; gatherCommands.setValue(false); @@ -61,9 +58,9 @@ public class SettingsTest { @Test public void testGetStringList() { - List exp = new ArrayList<>(); - exp.add("ExampleTown"); + List exp = Collections.singletonList("ExampleTown"); List result = Settings.HIDE_TOWNS.getStringList(); + assertEquals(exp, result); } diff --git a/Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java index 4e7a346b8..9fe3c8645 100644 --- a/Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/database/DBUtilsTest.java @@ -26,7 +26,9 @@ public class DBUtilsTest { for (int i = 0; i < 21336; i++) { list.add(i); } + List> result = DBUtils.splitIntoBatches(list); + assertEquals(3, result.size()); assertEquals(10192, result.get(0).size()); assertEquals(10192, result.get(1).size()); @@ -39,7 +41,9 @@ public class DBUtilsTest { for (int i = 0; i < 10192; i++) { list.add(i); } + List> result = DBUtils.splitIntoBatches(list); + assertEquals(1, result.size()); assertEquals(10192, result.get(0).size()); } @@ -53,7 +57,9 @@ public class DBUtilsTest { map.get(i).add(j); } } + List>> result = DBUtils.splitIntoBatchesId(map); + assertEquals(3, result.size()); assertEquals(10192, result.get(0).size()); assertEquals(10192, result.get(1).size()); diff --git a/Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java index c66476f23..9a4da150c 100644 --- a/Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/database/DatabaseCommitTest.java @@ -59,6 +59,7 @@ public class DatabaseCommitTest { System.out.println(line); } } + assertTrue("Errors were caught.", rows == rowsAgain); } diff --git a/Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java b/Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java index da9289b08..4c37402b7 100644 --- a/Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/ui/HtmlTest.java @@ -9,46 +9,31 @@ import main.java.com.djrapitops.plan.utilities.html.Html; import org.junit.Test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; /** * @author Rsl1122 */ public class HtmlTest { - /** - * - */ - public HtmlTest() { - } - - /** - * - */ @Test public void testParseWithZeroArgs() { - Html instance = Html.SPAN; String expResult = "${0}"; - String result = instance.parse(); + String result = Html.SPAN.parse(); + assertEquals(expResult, result); } - /** - * - */ @Test public void testParseStringArr() { - Html instance = Html.SPAN; String expResult = "Test"; - String result = instance.parse("Test"); + String result = Html.SPAN.parse("Test"); + assertEquals(expResult, result); } - /** - * - */ @Test public void testNoBackSlash() { - assertTrue("Null for some reason", Html.TABLELINE_2.parse("/\\", "0") != null); + assertNotNull(Html.TABLELINE_2.parse("/\\", "0")); } } 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 b913343a4..6b80d8a5e 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 @@ -62,14 +62,8 @@ public class GraphTest { Map expected = new LinkedHashMap<>(); - String key = null; - for (String resultString : splittedResult) { - if (key == null) { - key = resultString; - } else { - expected.put(key, resultString); - key = null; - } + for (int i = 0; i < splittedResult.length; i++) { + expected.put(splittedResult[i++], splittedResult[i]); } int i2 = 0; 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 4edccd157..5f4eeaa0a 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/FormatUtilsTest.java @@ -9,6 +9,7 @@ import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.MockUtils; +import test.java.utils.RandomData; import test.java.utils.TestInit; import java.text.SimpleDateFormat; @@ -32,8 +33,10 @@ public class FormatUtilsTest { @Test public void testFormatTimeAmount() { long ms = 1000L; + String expResult = "1s"; String result = FormatUtils.formatTimeAmount(ms); + assertEquals(expResult, result); } @@ -44,6 +47,7 @@ public class FormatUtilsTest { String expResult = "10s"; String result = FormatUtils.formatTimeAmountDifference(before.getTime(), now.getTime()); + assertEquals(expResult, result); } @@ -58,6 +62,7 @@ public class FormatUtilsTest { long epochZero = 0L; String result = FormatUtils.formatTimeStamp(epochZero); + assertEquals(expResult, result); } @@ -72,6 +77,7 @@ public class FormatUtilsTest { long epochZero = 0L; String result = FormatUtils.formatTimeStampYear(epochZero); + assertEquals(expResult, result); } @@ -86,6 +92,7 @@ public class FormatUtilsTest { long epochZero = 0L; String result = FormatUtils.formatTimeStampSecond(epochZero); + assertEquals(expResult, result); } @@ -93,7 +100,9 @@ public class FormatUtilsTest { public void testRemoveLetters() { String dataPoint = "435729847jirggu.eiwb¤#¤%¤#"; String expResult = "435729847."; + String result = FormatUtils.removeLetters(dataPoint); + assertEquals(expResult, result); } @@ -101,7 +110,9 @@ public class FormatUtilsTest { public void testRemoveNumbers() { String dataPoint = "34532453.5 $"; String expResult = "$"; + String result = FormatUtils.removeNumbers(dataPoint); + assertEquals(expResult, result); } @@ -109,7 +120,9 @@ public class FormatUtilsTest { public void testRemoveNumbers2() { String dataPoint = "l43r4545tl43 4.5"; String expResult = "lrtl"; + String result = FormatUtils.removeNumbers(dataPoint); + assertEquals(expResult, result); } @@ -117,7 +130,9 @@ public class FormatUtilsTest { public void testParseVersionNumber() { String versionString = "2.10.2"; int expResult = 21002; + int result = FormatUtils.parseVersionNumber(versionString); + assertEquals(expResult, result); } @@ -125,41 +140,58 @@ public class FormatUtilsTest { public void testVersionNumber() { String versionString = "2.10.2"; String versionString2 = "2.9.3"; + int result = FormatUtils.parseVersionNumber(versionString); int result2 = FormatUtils.parseVersionNumber(versionString2); + assertTrue("Higher version not higher", result > result2); } @Test public void testMergeArrays() { - String[][] arrays = new String[][]{new String[]{"Test", "One"}, new String[]{"Test", "Two"}}; - String[] expResult = new String[]{"Test", "One", "Test", "Two"}; + String randomString1 = RandomData.randomString(10); + String randomString2 = RandomData.randomString(10); + String randomString3 = RandomData.randomString(10); + String randomString4 = RandomData.randomString(10); + + String[][] arrays = new String[][]{new String[]{randomString1, randomString2}, new String[]{randomString3, randomString4}}; + String[] expResult = new String[]{randomString1, randomString2, randomString3, randomString4}; + String[] result = FormatUtils.mergeArrays(arrays); + assertArrayEquals(expResult, result); } @Test public void testFormatLocation() { + int randomInt = RandomData.randomInt(0, 100); + World mockWorld = MockUtils.mockWorld(); - Location loc = new Location(mockWorld, 0, 0, 0); - String expResult = "x 0 z 0 in World"; + Location loc = new Location(mockWorld, randomInt, randomInt, randomInt); + + String expResult = "x " + randomInt + " z " + randomInt + " in World"; String result = FormatUtils.formatLocation(loc); + assertEquals(expResult, result); } @Test - public void testCutDecimals() throws Exception { + public void testCutDecimalsWhichIsRoundedDown() throws Exception { double d = 0.05234; String expResult = "0,05"; + String result = FormatUtils.cutDecimals(d); + assertEquals(expResult, result); } @Test - public void testCutDecimals2() throws Exception { + public void testCutDecimalsWhichIsRoundedUp() throws Exception { double d = 0.05634; String expResult = "0,06"; + String result = FormatUtils.cutDecimals(d); + assertEquals(expResult, result); } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java index 743573ecf..a211eb907 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java @@ -57,7 +57,7 @@ public class HtmlUtilsTest { public void testRemoveXSS() { String randomString = RandomData.randomString(10); - String xss = ""; String result = HtmlUtils.removeXSS(xss); assertEquals(randomString, result); diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java index 7a50505fc..8f4c9cbb0 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java @@ -29,47 +29,59 @@ import static org.junit.Assert.assertEquals; public class MiscUtilsTest { @Test - public void testGetPlayerDisplaynameArgsPerm() { + public void testGetPlayerDisplayNameArgsPerm() { String[] args = new String[]{"Rsl1122", "Test"}; ISender sender = new BukkitCMDSender(MockUtils.mockPlayer()); + String expResult = "Rsl1122"; String result = MiscUtils.getPlayerName(args, sender); + assertEquals(expResult, result); } @Test - public void testGetPlayerDisplaynameArgsNoPerm() { + public void testGetPlayerDisplayNameArgsNoPerm() throws Exception { + TestInit.init(); + String[] args = new String[]{"Rsl1122", "Test"}; - ISender sender = new BukkitCMDSender(MockUtils.mockPlayer()); + ISender sender = new BukkitCMDSender(MockUtils.mockPlayer2()); + String expResult = "Rsl1122"; String result = MiscUtils.getPlayerName(args, sender); + assertEquals(expResult, result); } @Test - public void testGetPlayerDisplaynameNoArgsPerm() { + public void testGetPlayerDisplayNameNoArgsPerm() { String[] args = new String[]{}; ISender sender = new BukkitCMDSender(MockUtils.mockPlayer()); + String expResult = "TestName"; String result = MiscUtils.getPlayerName(args, sender); + assertEquals(expResult, result); } @Test - public void testGetPlayerDisplaynameNoArgsNoPerm() { + public void testGetPlayerDisplayNameNoArgsNoPerm() { String[] args = new String[]{}; ISender sender = new BukkitCMDSender(MockUtils.mockPlayer2()); + String expResult = "TestName2"; String result = MiscUtils.getPlayerName(args, sender); + assertEquals(expResult, result); } @Test - public void testGetPlayerDisplaynameOwnNameNoPerm() { + public void testGetPlayerDisplayNameOwnNameNoPerm() { String[] args = new String[]{"testname2"}; ISender sender = new BukkitCMDSender(MockUtils.mockPlayer2()); + String expResult = "TestName2"; String result = MiscUtils.getPlayerName(args, sender); + assertEquals(expResult, result); } @@ -77,19 +89,23 @@ public class MiscUtilsTest { public void testGetPlayerDisplaynameConsole() { String[] args = new String[]{"TestConsoleSender"}; ISender sender = new BukkitCMDSender(MockUtils.mockConsoleSender()); + String expResult = "TestConsoleSender"; String result = MiscUtils.getPlayerName(args, sender); + assertEquals(expResult, result); } @Test @Ignore("DB mock") - public void testGetMatchingDisplaynames() throws Exception { + public void testGetMatchingDisplayNames() throws Exception { TestInit.init(); String search = "testname"; + String exp1 = "TestName"; String exp2 = "TestName2"; List result = MiscUtils.getMatchingPlayerNames(search); + assertEquals(2, result.size()); assertEquals(exp1, result.get(0)); assertEquals(exp2, result.get(1)); @@ -97,11 +113,14 @@ public class MiscUtilsTest { @Test @Ignore("DB mock") - public void testGetMatchingDisplaynames2() throws Exception { + public void testGetMatchingDisplayNames2() throws Exception { TestInit.init(); + String search = "2"; String exp2 = "TestName2"; + List result = MiscUtils.getMatchingPlayerNames(search); + assertEquals(1, result.size()); assertEquals(exp2, result.get(0)); } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java index b796d062a..7918a4f04 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/PassEncryptTest.java @@ -19,8 +19,8 @@ public class PassEncryptTest { @Before public void setUp() throws Exception { - for (int i = 0; i < 20; i++) { - String password = RandomData.randomString(RandomData.randomInt(1, 20)); + for (int i = 0; i < RandomData.randomInt(1, 10); i++) { + String password = RandomData.randomString(RandomData.randomInt(5, 16)); PASSWORD_MAP.put(password, PassEncryptUtil.createHash(password)); } } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java index 9046ef91d..a3e90a201 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtilsTest.java @@ -15,9 +15,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.TestInit; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; +import java.util.*; import static org.junit.Assert.assertEquals; @@ -28,106 +26,81 @@ import static org.junit.Assert.assertEquals; @PrepareForTest(JavaPlugin.class) public class AnalysisUtilsTest { - /** - * - */ - public AnalysisUtilsTest() { - } - - /** - * - */ @Before public void setUp() throws Exception { TestInit.init(); } - /** - * - */ @Test public void testIsActive() { long lastPlayed = MiscUtils.getTime(); long playTime = 12638934876L; int loginTimes = 4; + boolean result = AnalysisUtils.isActive(System.currentTimeMillis(), lastPlayed, playTime, loginTimes); assertEquals(true, result); } - /** - * - */ @Test public void testIsNotActive2() { long lastPlayed = MiscUtils.getTime(); long playTime = 0L; int loginTimes = 4; + boolean result = AnalysisUtils.isActive(System.currentTimeMillis(), lastPlayed, playTime, loginTimes); assertEquals(false, result); } - /** - * - */ @Test public void testIsNotActive3() { long lastPlayed = MiscUtils.getTime(); long playTime = 12638934876L; int loginTimes = 0; + boolean result = AnalysisUtils.isActive(System.currentTimeMillis(), lastPlayed, playTime, loginTimes); assertEquals(false, result); } - /** - * - */ @Test public void testIsNotActive() { long lastPlayed = 0L; long playTime = 12638934876L; int loginTimes = 4; + boolean result = AnalysisUtils.isActive(System.currentTimeMillis(), lastPlayed, playTime, loginTimes); assertEquals(false, result); } - /** - * - */ @Test public void testGetNewPlayers() { - List registered = new ArrayList<>(); - registered.add(5L); - registered.add(1L); + List registered = Arrays.asList(5L, 1L); + long scale = 8L; long now = 10L; long result = AnalysisUtils.getNewPlayers(registered, scale, now); + assertEquals(1L, result); } - /** - * - */ @Test public void testGetNewPlayersEmpty() { - List registered = new ArrayList<>(); long scale = 1L; long now = 2L; - long result = AnalysisUtils.getNewPlayers(registered, scale, now); + long result = AnalysisUtils.getNewPlayers(Collections.emptyList(), scale, now); + assertEquals(0L, result); } - /** - * - */ @Test public void testTransformSessionDataToLengths() { - Collection data = new ArrayList<>(); - data.add(new Session(1, 0L, 5L, 0, 0)); - data.add(new Session(1, 0, 20L, 0, 0)); - List expResult = new ArrayList<>(); - expResult.add(5L); - expResult.add(20L); + Collection data = Arrays.asList( + new Session(1, 0L, 5L, 0, 0), + new Session(1, 0L, 20L, 0, 0) + ); + + List expResult = Arrays.asList(5L, 20L); List result = AnalysisUtils.transformSessionDataToLengths(data); + assertEquals(expResult, result); } } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java index aadebd827..f8c66161c 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/analysis/MathUtilsTest.java @@ -6,12 +6,13 @@ package main.java.com.djrapitops.plan.utilities.analysis; import org.junit.Test; +import test.java.utils.RandomData; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Random; import static org.junit.Assert.assertTrue; @@ -22,45 +23,41 @@ public class MathUtilsTest { @Test public void testAverageInt() { - List l = new ArrayList<>(); + List integers = Arrays.asList(0, 20, 5, 15); + double exp = 10; - l.add(0); - l.add(20); - l.add(5); - l.add(15); - double result = MathUtils.averageInt(l.stream()); + double result = MathUtils.averageInt(integers.stream()); + assertTrue(Double.compare(exp, result) == 0); } @Test public void testAverageIntEmpty() { - List l = Collections.emptyList(); + List integers = Collections.emptyList(); + double exp = 0; - double result = MathUtils.averageInt(l.stream()); + double result = MathUtils.averageInt(integers.stream()); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testAverageLongCollection() { - List l = new ArrayList<>(); + List longs = Arrays.asList(0L, 20L, 5L, 15L); + double exp = 10; - l.add(0L); - l.add(20L); - l.add(5L); - l.add(15L); - double result = MathUtils.averageLong(l); + double result = MathUtils.averageLong(longs); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testAverageDouble() { - List l = new ArrayList<>(); + List doubles = Arrays.asList(0.0, 20.5, 4.5, 15.0); + double exp = 10; - l.add(0.0); - l.add(20.5); - l.add(4.5); - l.add(15.0); - double result = MathUtils.averageDouble(l.stream()); + double result = MathUtils.averageDouble(doubles.stream()); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @@ -69,64 +66,63 @@ public class MathUtilsTest { public void testAverage() { double exp = 10; double result = MathUtils.average(40, 4); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testCountTrueBoolean() { - List l = new ArrayList<>(); - int exp = new Random().nextInt(1000); + List booleans = new ArrayList<>(); + + int exp = RandomData.randomInt(0, 1000); for (int i = 0; i < exp; i++) { - l.add(true); + booleans.add(true); } - for (int i = exp; i < 1000; i++) { - l.add(false); + + for (int i = exp; i < RandomData.randomInt(100, 1000); i++) { + booleans.add(false); } - long result = MathUtils.countTrueBoolean(l.stream()); + + long result = MathUtils.countTrueBoolean(booleans.stream()); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testSumInt() { - List l = new ArrayList<>(); + List serializable = Arrays.asList(0, 20, 5, 15); + double exp = 40; - l.add(0); - l.add(20); - l.add(5); - l.add(15); - double result = MathUtils.sumInt(l.stream()); + double result = MathUtils.sumInt(serializable.stream()); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testSumLong() { - List l = new ArrayList<>(); + List serializable = Arrays.asList(0L, 20L, 5L, 15L); + long exp = 40; - l.add(0L); - l.add(20L); - l.add(5L); - l.add(15L); - long result = MathUtils.sumLong(l.stream()); + long result = MathUtils.sumLong(serializable.stream()); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testSumDouble() { - List l = new ArrayList<>(); + List serializable = Arrays.asList(0.0, 50.4, 45.0, 5.0531541); + double exp = 100.4531541; - l.add(0.0); - l.add(50.4); - l.add(45.0); - l.add(5.0531541); - double result = MathUtils.sumDouble(l.stream()); + double result = MathUtils.sumDouble(serializable.stream()); + assertTrue(result + "/" + exp, Double.compare(exp, result) == 0); } @Test public void testRoundDouble() { double exp = 412.5123125123; - double roundedExp = MathUtils.round(exp); + double result = MathUtils.round(exp); - assertTrue("", Double.compare(412.51, roundedExp) == 0); + assertTrue(result + "/" + exp, Double.compare(412.51, result) == 0); } } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java index dda30a6a4..2a7da2006 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java @@ -1,5 +1,6 @@ package main.java.com.djrapitops.plan.utilities.comparators; +import com.google.common.collect.Ordering; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.data.TPS; import main.java.com.djrapitops.plan.data.UserInfo; @@ -20,101 +21,117 @@ public class ComparatorTest { @Test public void testPointComparator() { - List test = RandomData.randomPoints(); + List points = RandomData.randomPoints(); - List longValues = test.stream().map(Point::getX).map(i -> (long) (double) i).collect(Collectors.toList()); + List longValues = points.stream().map(Point::getX).map(i -> (long) (double) i).collect(Collectors.toList()); longValues.sort(Long::compare); - test.sort(new PointComparator()); - List afterSort = test.stream().map(Point::getX).map(i -> (long) (double) i).collect(Collectors.toList()); + + points.sort(new PointComparator()); + + List afterSort = points.stream().map(Point::getX).map(i -> (long) (double) i).collect(Collectors.toList()); assertEquals(longValues, afterSort); } @Test public void testSessionDataComparator() { - List test = RandomData.randomSessions(); - List longValues = test.stream().map(Session::getSessionStart).collect(Collectors.toList()); + List sessions = RandomData.randomSessions(); + + List longValues = sessions.stream().map(Session::getSessionStart).collect(Collectors.toList()); longValues.sort(Long::compare); + Collections.reverse(longValues); - test.sort(new SessionStartComparator()); - List afterSort = test.stream().map(Session::getSessionStart).collect(Collectors.toList()); + sessions.sort(new SessionStartComparator()); + List afterSort = sessions.stream().map(Session::getSessionStart).collect(Collectors.toList()); + assertEquals(longValues, afterSort); } @Test public void testTPSComparator() { - List test = RandomData.randomTPS(); - List longValues = test.stream().map(TPS::getDate).collect(Collectors.toList()); + List tpsList = RandomData.randomTPS(); + + List longValues = tpsList.stream().map(TPS::getDate).collect(Collectors.toList()); longValues.sort(Long::compare); - test.sort(new TPSComparator()); - List afterSort = test.stream().map(TPS::getDate).collect(Collectors.toList()); + + tpsList.sort(new TPSComparator()); + List afterSort = tpsList.stream().map(TPS::getDate).collect(Collectors.toList()); + assertEquals(longValues, afterSort); } @Test public void testUserDataLastPlayedComparator() { - List test = RandomData.randomUserData(); - List longValues = test.stream().map(UserInfo::getLastSeen).collect(Collectors.toList()); + List userInfo = RandomData.randomUserData(); + + List longValues = userInfo.stream().map(UserInfo::getLastSeen).collect(Collectors.toList()); longValues.sort(Long::compare); + Collections.reverse(longValues); - test.sort(new UserInfoLastPlayedComparator()); - List afterSort = test.stream().map(UserInfo::getLastSeen).collect(Collectors.toList()); + userInfo.sort(new UserInfoLastPlayedComparator()); + List afterSort = userInfo.stream().map(UserInfo::getLastSeen).collect(Collectors.toList()); + assertEquals(longValues, afterSort); } @Test public void testUserDataNameComparator() { - List test = RandomData.randomUserData(); - List stringValues = test.stream().map(UserInfo::getName).collect(Collectors.toList()); + List userInfo = RandomData.randomUserData(); + + List stringValues = userInfo.stream().map(UserInfo::getName).collect(Collectors.toList()); Collections.sort(stringValues); - test.sort(new UserDataNameComparator()); - List afterSort = test.stream().map(UserInfo::getName).collect(Collectors.toList()); + + userInfo.sort(new UserDataNameComparator()); + List afterSort = userInfo.stream().map(UserInfo::getName).collect(Collectors.toList()); + assertEquals(stringValues, afterSort); } @Test public void testWebUserComparator() throws PassEncryptUtil.CannotPerformOperationException { - List test = RandomData.randomWebUsers(); - List intValues = test.stream().map(WebUser::getPermLevel).collect(Collectors.toList()); + List webUsers = RandomData.randomWebUsers(); + + List intValues = webUsers.stream().map(WebUser::getPermLevel).collect(Collectors.toList()); intValues.sort(Integer::compare); Collections.reverse(intValues); - test.sort(new WebUserComparator()); - List afterSort = test.stream().map(WebUser::getPermLevel).collect(Collectors.toList()); + + webUsers.sort(new WebUserComparator()); + List afterSort = webUsers.stream().map(WebUser::getPermLevel).collect(Collectors.toList()); + assertEquals(intValues, afterSort); } @Test public void testStringLengthComparator() { - List test = new ArrayList<>(); - test.add(RandomData.randomString(10)); - test.add(RandomData.randomString(3)); - test.add(RandomData.randomString(20)); - test.add(RandomData.randomString(7)); - test.add(RandomData.randomString(4)); - test.add(RandomData.randomString(86)); - test.add(RandomData.randomString(6)); + List strings = Ordering.from(new StringLengthComparator()) + .sortedCopy(Arrays.asList( + RandomData.randomString(10), + RandomData.randomString(3), + RandomData.randomString(20), + RandomData.randomString(7), + RandomData.randomString(4), + RandomData.randomString(86), + RandomData.randomString(6))); - test.sort(new StringLengthComparator()); - - assertEquals(86, test.get(0).length()); - assertEquals(20, test.get(1).length()); - assertEquals(3, test.get(test.size() - 1).length()); + assertEquals(86, strings.get(0).length()); + assertEquals(20, strings.get(1).length()); + assertEquals(3, strings.get(strings.size() - 1).length()); } @Test public void testLocaleEntryComparator() { - Map test = new HashMap<>(); - test.put(Msg.CMD_CONSTANT_FOOTER, new Message("")); - test.put(Msg.ANALYSIS_3RD_PARTY, new Message("")); - test.put(Msg.MANAGE_FAIL_NO_PLAYERS, new Message("")); + Map messageMap = new HashMap<>(); + messageMap.put(Msg.CMD_CONSTANT_FOOTER, new Message(RandomData.randomString(10))); + messageMap.put(Msg.ANALYSIS_3RD_PARTY, new Message(RandomData.randomString(10))); + messageMap.put(Msg.MANAGE_FAIL_NO_PLAYERS, new Message(RandomData.randomString(10))); - List sorted = test.entrySet().stream() + List sorted = messageMap.entrySet().stream() .sorted(new LocaleEntryComparator()) .map(entry -> entry.getKey().name()) .collect(Collectors.toList()); - assertEquals("ANALYSIS_3RD_PARTY", sorted.get(0)); - assertEquals("CMD_CONSTANT_FOOTER", sorted.get(1)); - assertEquals("MANAGE_FAIL_NO_PLAYERS", sorted.get(2)); + assertEquals(Msg.ANALYSIS_3RD_PARTY.name(), sorted.get(0)); + assertEquals(Msg.CMD_CONSTANT_FOOTER.name(), sorted.get(1)); + assertEquals(Msg.MANAGE_FAIL_NO_PLAYERS.name(), sorted.get(2)); } } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java index 1a00cd616..9b3107663 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java @@ -15,7 +15,7 @@ import test.java.utils.RandomData; import test.java.utils.TestInit; import java.io.IOException; -import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.atomic.AtomicBoolean; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertNotNull; @@ -28,25 +28,26 @@ import static junit.framework.TestCase.assertNotNull; @PrepareForTest(JavaPlugin.class) public class HastebinTest { - private final AtomicReference testLink = new AtomicReference<>(null); + private final AtomicBoolean testLink = new AtomicBoolean(false); @Before public void checkAvailability() throws Exception { TestInit.init(); Thread thread = new Thread(() -> { - String link = null; try { - link = Hastebin.upload(RandomData.randomString(10)); + Hastebin.upload(RandomData.randomString(10)); } catch (IOException e) { if (e.getMessage().contains("503")) { return; } + + Log.toLog("checkAvailability()", e); } catch (ParseException e) { /* Ignored */ } - testLink.set(link); + testLink.set(true); }); thread.start(); @@ -57,7 +58,7 @@ public class HastebinTest { Log.info("Hastebin timed out"); } - Log.info("Hastebin Availability Test Link: " + testLink.get()); + Log.info("Hastebin Available: " + testLink.get()); } @Test @@ -72,7 +73,7 @@ public class HastebinTest { @Test public void testUpload() throws Exception { - if (testLink.get() == null) { + if (!testLink.get()) { Log.info("Hastebin not available, skipping testUpload()"); return; } diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java index 96c35db00..bef397335 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/html/HtmlStructureTest.java @@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.utilities.html; import main.java.com.djrapitops.plan.data.Session; import org.apache.commons.lang3.StringUtils; import org.bukkit.plugin.java.JavaPlugin; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -29,7 +30,8 @@ public class HtmlStructureTest { private Map> sessions = new HashMap<>(); - public HtmlStructureTest() throws Exception { + @Before + public void setUp() throws Exception { TestInit.init(); for (int i = 0; i < RandomData.randomInt(0, 5); i++) { diff --git a/Plan/test/test/java/utils/TestInit.java b/Plan/test/test/java/utils/TestInit.java index 56e3df539..fa0cb9e1c 100644 --- a/Plan/test/test/java/utils/TestInit.java +++ b/Plan/test/test/java/utils/TestInit.java @@ -26,6 +26,8 @@ import org.powermock.api.mockito.PowerMockito; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; import java.util.UUID; import java.util.logging.Logger; @@ -129,49 +131,69 @@ public class TestInit { @Override public IRunnable createNew(String name, final AbsRunnable runnable) { return new IRunnable() { + Timer timer = new Timer(); + TimerTask task = new TimerTask() { + @Override + public void run() { + runnable.run(); + } + }; + @Override public String getTaskName() { - return "Test"; + return name; } @Override public void cancel() { + timer.cancel(); + task.cancel(); + runnable.cancel(); } @Override public int getTaskId() { - return 0; + return runnable.getTaskId(); } @Override public ITask runTask() { - new Thread(runnable::run).start(); + task.run(); return null; } @Override public ITask runTaskAsynchronously() { - return runTask(); + new Thread(this::runTask).start(); + return null; } @Override public ITask runTaskLater(long l) { - return runTask(); + timer.schedule(task, convertTicksToMillis(l)); + return null; } @Override public ITask runTaskLaterAsynchronously(long l) { - return runTask(); + new Thread(() -> timer.schedule(task, convertTicksToMillis(l))).start(); + return null; } @Override public ITask runTaskTimer(long l, long l1) { - return runTask(); + timer.scheduleAtFixedRate(task, convertTicksToMillis(l), convertTicksToMillis(l1)); + return null; } @Override public ITask runTaskTimerAsynchronously(long l, long l1) { - return runTask(); + new Thread(() -> timer.scheduleAtFixedRate(task, convertTicksToMillis(l), convertTicksToMillis(l1))); + return null; + } + + private long convertTicksToMillis(long ticks) { + return ticks * 50; } }; } From 3579a84a755a0d1840e23fa48e3e68d7b40d5f87 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 31 Aug 2017 20:39:38 +0200 Subject: [PATCH 5/7] Change equals() & hashCode() to using Java 7 Methods Change toString() to using Apache Lang3 ToStringHelper Further Cleanup of Tests --- .../java/com/djrapitops/plan/data/Action.java | 17 ++++---- .../com/djrapitops/plan/data/PlayerKill.java | 43 ++++++++----------- .../com/djrapitops/plan/data/Session.java | 43 +++++++++---------- .../java/com/djrapitops/plan/data/TPS.java | 24 ++++++----- .../com/djrapitops/plan/data/UserInfo.java | 9 ++-- .../com/djrapitops/plan/data/WebUser.java | 15 +++---- .../djrapitops/plan/data/time/TimeKeeper.java | 25 +++++------ .../djrapitops/plan/data/time/WorldTimes.java | 28 ++++++------ .../plan/systems/info/server/ServerInfo.java | 11 +++-- .../systems/webserver/response/Response.java | 12 +++--- .../plan/utilities/analysis/Point.java | 19 +++++--- .../com/djrapitops/plan/data/SessionTest.java | 9 ---- .../djrapitops/plan/data/UserInfoTest.java | 8 ++-- .../utilities/comparators/ComparatorTest.java | 4 +- 14 files changed, 124 insertions(+), 143 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/Action.java b/Plan/src/main/java/com/djrapitops/plan/data/Action.java index fc6b617d6..ab7e392e0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/Action.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/Action.java @@ -4,11 +4,12 @@ */ package main.java.com.djrapitops.plan.data; -import com.google.common.base.Objects; import main.java.com.djrapitops.plan.database.tables.Actions; import main.java.com.djrapitops.plan.utilities.FormatUtils; import main.java.com.djrapitops.plan.utilities.html.Html; +import java.util.Objects; + /** * Class that represents an action made by a player. * @@ -54,11 +55,6 @@ public class Action { return serverID; } - @Override - public String toString() { - return Html.TABLELINE_3.parse(FormatUtils.formatTimeStampYear(date), doneAction.toString(), additionalInfo); - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -67,11 +63,16 @@ public class Action { return date == action.date && serverID == action.serverID && doneAction == action.doneAction && - Objects.equal(additionalInfo, action.additionalInfo); + Objects.equals(additionalInfo, action.additionalInfo); } @Override public int hashCode() { - return Objects.hashCode(date, doneAction, additionalInfo, serverID); + return Objects.hash(date, doneAction, additionalInfo, serverID); + } + + @Override + public String toString() { + return Html.TABLELINE_3.parse(FormatUtils.formatTimeStampYear(date), doneAction.toString(), additionalInfo); } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/data/PlayerKill.java b/Plan/src/main/java/com/djrapitops/plan/data/PlayerKill.java index 6d83aa9b3..ab5c6453f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/PlayerKill.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/PlayerKill.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.data; import main.java.com.djrapitops.plan.database.tables.Actions; +import org.apache.commons.lang3.builder.ToStringBuilder; import java.util.Objects; import java.util.UUID; @@ -57,39 +58,31 @@ public class PlayerKill { return weapon; } - - @Override - public String toString() { - return "{victim:" + victim + "|time:" + time + "|weapon:" + weapon + '}'; + public Action convertToAction() { + return new Action(time, Actions.KILLED, "name with " + weapon); // TODO Name Cache. } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final PlayerKill other = (PlayerKill) obj; - return this.time == other.time - && Objects.equals(this.weapon, other.weapon) - && Objects.equals(this.victim, other.victim); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PlayerKill that = (PlayerKill) o; + return time == that.time && + Objects.equals(victim, that.victim) && + Objects.equals(weapon, that.weapon); } @Override public int hashCode() { - int hash = 3; - hash = 89 * hash + Objects.hashCode(this.victim); - hash = 89 * hash + (int) (this.time ^ (this.time >>> 32)); - hash = 89 * hash + Objects.hashCode(this.weapon); - return hash; + return Objects.hash(victim, time, weapon); } - public Action convertToAction() { - return new Action(time, Actions.KILLED, "name with " + weapon); // TODO Name Cache. + @Override + public String toString() { + return new ToStringBuilder(this) + .append("victim", victim) + .append("time", time) + .append("weapon", weapon) + .toString(); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/Session.java b/Plan/src/main/java/com/djrapitops/plan/data/Session.java index 07f4597ec..4fc12564e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/Session.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/Session.java @@ -5,6 +5,7 @@ import main.java.com.djrapitops.plan.data.time.WorldTimes; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Objects; /** * Object for storing various information about a player's play session. @@ -162,29 +163,6 @@ public class Session { return deaths; } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Session other = (Session) obj; - return this.sessionStart == other.sessionStart && this.sessionEnd == other.sessionEnd; - } - - @Override - public int hashCode() { - int hash = 3; - hash = 97 * hash + (int) (this.sessionStart ^ (this.sessionStart >>> 32)); - hash = 97 * hash + (int) (this.sessionEnd ^ (this.sessionEnd >>> 32)); - return hash; - } - public boolean isFetchedFromDB() { return sessionID != null; } @@ -202,4 +180,23 @@ public class Session { public void setSessionID(int sessionID) { this.sessionID = sessionID; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Session session = (Session) o; + return sessionStart == session.sessionStart && + sessionEnd == session.sessionEnd && + mobKills == session.mobKills && + deaths == session.deaths && + Objects.equals(sessionID, session.sessionID) && + Objects.equals(worldTimes, session.worldTimes) && + Objects.equals(playerKills, session.playerKills); + } + + @Override + public int hashCode() { + return Objects.hash(sessionStart, sessionID, worldTimes, sessionEnd, playerKills, mobKills, deaths); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/TPS.java b/Plan/src/main/java/com/djrapitops/plan/data/TPS.java index 3adbb3de0..d115d8eb0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/TPS.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/TPS.java @@ -5,7 +5,9 @@ */ package main.java.com.djrapitops.plan.data; -import com.google.common.base.Objects; +import org.apache.commons.lang3.builder.ToStringBuilder; + +import java.util.Objects; /** * Class containing single datapoint of TPS / Players online / CPU Usage / Used Memory / Entity Count / Chunks loaded. @@ -123,19 +125,19 @@ public class TPS { @Override public int hashCode() { - return Objects.hashCode(date, ticksPerSecond, players, cpuUsage, usedMemory, entityCount, chunksLoaded); + return Objects.hash(date, ticksPerSecond, players, cpuUsage, usedMemory, entityCount, chunksLoaded); } @Override public String toString() { - return "TPS{" + - "date=" + date + - ", ticksPerSecond=" + ticksPerSecond + - ", players=" + players + - ", cpuUsage=" + cpuUsage + - ", usedMemory=" + usedMemory + - ", entityCount=" + entityCount + - ", chunksLoaded=" + chunksLoaded + - '}'; + return new ToStringBuilder(this) + .append("date", date) + .append("ticksPerSecond", ticksPerSecond) + .append("players", players) + .append("cpuUsage", cpuUsage) + .append("usedMemory", usedMemory) + .append("entityCount", entityCount) + .append("chunksLoaded", chunksLoaded) + .toString(); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/UserInfo.java b/Plan/src/main/java/com/djrapitops/plan/data/UserInfo.java index 7671361d3..5a1c349bc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/UserInfo.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/UserInfo.java @@ -1,7 +1,6 @@ package main.java.com.djrapitops.plan.data; -import com.google.common.base.Objects; - +import java.util.Objects; import java.util.UUID; /** @@ -79,12 +78,12 @@ public class UserInfo { lastSeen == userInfo.lastSeen && banned == userInfo.banned && opped == userInfo.opped && - Objects.equal(uuid, userInfo.uuid) && - Objects.equal(name, userInfo.name); + Objects.equals(uuid, userInfo.uuid) && + Objects.equals(name, userInfo.name); } @Override public int hashCode() { - return Objects.hashCode(uuid, name, registered, lastSeen, banned, opped); + return Objects.hash(uuid, name, registered, lastSeen, banned, opped); } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/data/WebUser.java b/Plan/src/main/java/com/djrapitops/plan/data/WebUser.java index 6b85d2f79..eea832f2a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/WebUser.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/WebUser.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.data; +import java.util.Objects; + /** * Object containing webserver security user information. * @@ -34,19 +36,14 @@ public class WebUser { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - WebUser webUser = (WebUser) o; - - return permLevel == webUser.permLevel - && user.equals(webUser.user) - && saltedPassHash.equals(webUser.saltedPassHash); + return permLevel == webUser.permLevel && + Objects.equals(user, webUser.user) && + Objects.equals(saltedPassHash, webUser.saltedPassHash); } @Override public int hashCode() { - int result = user.hashCode(); - result = 31 * result + saltedPassHash.hashCode(); - result = 31 * result + permLevel; - return result; + return Objects.hash(user, saltedPassHash, permLevel); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java b/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java index 43384c6f8..08da3a54f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java @@ -1,9 +1,11 @@ package main.java.com.djrapitops.plan.data.time; import com.djrapitops.plugin.utilities.Verify; +import org.apache.commons.lang3.builder.ToStringBuilder; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * Abstract class for keeping track of time spent in each state. @@ -140,28 +142,23 @@ public abstract class TimeKeeper { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - TimeKeeper that = (TimeKeeper) o; - - return lastStateChange == that.lastStateChange - && (times != null ? times.equals(that.times) : that.times == null) - && (state != null ? state.equals(that.state) : that.state == null); + return lastStateChange == that.lastStateChange && + Objects.equals(times, that.times) && + Objects.equals(state, that.state); } @Override public int hashCode() { - int result = times != null ? times.hashCode() : 0; - result = 31 * result + (state != null ? state.hashCode() : 0); - result = 31 * result + (int) (lastStateChange ^ (lastStateChange >>> 32)); - return result; + return Objects.hash(times, state, lastStateChange); } @Override public String toString() { - return getClass().getSimpleName() + "{" + - "times=" + times + - ", state='" + state + '\'' + - ", lastStateChange=" + lastStateChange + - '}'; + return new ToStringBuilder(this) + .append("times", times) + .append("state", state) + .append("lastStateChange", lastStateChange) + .toString(); } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java b/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java index 67eaf7ce1..f02eff89e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java @@ -111,19 +111,6 @@ public class WorldTimes { return worldTimes.getOrDefault(world, new GMTimes()); } - @Override - public String toString() { - StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n"); - for (Map.Entry entry : worldTimes.entrySet()) { - b.append("World '").append(entry.getKey()).append("':\n"); - GMTimes value = entry.getValue(); - b.append(" Total: ").append(value.getTotal()).append("\n"); - b.append(" ").append(value.toString()).append("\n"); - } - b.append("}"); - return b.toString(); - } - /** * Used to get the Map for saving. * @@ -136,4 +123,19 @@ public class WorldTimes { public void setGMTimesForWorld(String world, GMTimes gmTimes) { worldTimes.put(world, gmTimes); } + + @Override + public String toString() { + StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n"); + + for (Map.Entry entry : worldTimes.entrySet()) { + GMTimes value = entry.getValue(); + b.append("World '").append(entry.getKey()).append("':\n") + .append(" Total: ").append(value.getTotal()).append("\n") + .append(" ").append(value.toString()).append("\n"); + } + + b.append("}"); + return b.toString(); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/server/ServerInfo.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/server/ServerInfo.java index ece7909fe..b6ee25221 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/server/ServerInfo.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/server/ServerInfo.java @@ -4,8 +4,7 @@ */ package main.java.com.djrapitops.plan.systems.info.server; -import com.google.common.base.Objects; - +import java.util.Objects; import java.util.UUID; /** @@ -60,13 +59,13 @@ public class ServerInfo { if (o == null || getClass() != o.getClass()) return false; ServerInfo that = (ServerInfo) o; return id == that.id && - Objects.equal(uuid, that.uuid) && - Objects.equal(name, that.name) && - Objects.equal(webAddress, that.webAddress); + Objects.equals(uuid, that.uuid) && + Objects.equals(name, that.name) && + Objects.equals(webAddress, that.webAddress); } @Override public int hashCode() { - return Objects.hashCode(id, uuid, name, webAddress); + return Objects.hash(uuid, id, name, webAddress); } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java index 0dcfa46e5..57a1266af 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.systems.webserver.response; +import java.util.Objects; + /** * @author Rsl1122 * @since 3.5.2 @@ -43,17 +45,13 @@ public abstract class Response { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Response response = (Response) o; - - return (header != null ? header.equals(response.header) : response.header == null) - && (content != null ? content.equals(response.content) : response.content == null); + return Objects.equals(header, response.header) && + Objects.equals(content, response.content); } @Override public int hashCode() { - int result = header != null ? header.hashCode() : 0; - result = 31 * result + (content != null ? content.hashCode() : 0); - return result; + return Objects.hash(header, content); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Point.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Point.java index 4fbb08937..3199d1230 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Point.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Point.java @@ -1,6 +1,8 @@ package main.java.com.djrapitops.plan.utilities.analysis; -import com.google.common.base.Objects; +import org.apache.commons.lang3.builder.ToStringBuilder; + +import java.util.Objects; /** * @author Rsl1122 @@ -23,11 +25,6 @@ public class Point { return y; } - @Override - public String toString() { - return "{x:" + x + " y:" + y + '}'; - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -39,6 +36,14 @@ public class Point { @Override public int hashCode() { - return Objects.hashCode(x, y); + return Objects.hash(x, y); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("x", x) + .append("y", y) + .toString(); } } diff --git a/Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java b/Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java index 4b7188311..805961817 100644 --- a/Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/SessionTest.java @@ -14,15 +14,6 @@ public class SessionTest { private Session session; - /** - * - */ - public SessionTest() { - } - - /** - * - */ @Before public void setUp() { } diff --git a/Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java b/Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java index b3defe9fe..4e7a2f808 100644 --- a/Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/UserInfoTest.java @@ -3,28 +3,28 @@ package main.java.com.djrapitops.plan.data; import main.java.com.djrapitops.plan.Plan; import org.bukkit.plugin.java.JavaPlugin; import org.junit.Before; +import org.junit.Ignore; 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.TestInit; /** * @author Rsl1122 */ @RunWith(PowerMockRunner.class) @PrepareForTest(JavaPlugin.class) +@Ignore public class UserInfoTest { // TODO Rewrite private UserInfo session; private Plan plan; - public UserInfoTest() { - } - @Before public void setUp() throws Exception { -// TestInit t = TestInit.init(); + TestInit.init(); } @Test diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java index 2a7da2006..159fbbfc2 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java @@ -73,7 +73,6 @@ public class ComparatorTest { assertEquals(longValues, afterSort); } - @Test public void testUserDataNameComparator() { List userInfo = RandomData.randomUserData(); @@ -111,7 +110,8 @@ public class ComparatorTest { RandomData.randomString(7), RandomData.randomString(4), RandomData.randomString(86), - RandomData.randomString(6))); + RandomData.randomString(6)) + ); assertEquals(86, strings.get(0).length()); assertEquals(20, strings.get(1).length()); From 5a063ace49c28cc245aa54c175634cc15967e055 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 31 Aug 2017 21:02:00 +0200 Subject: [PATCH 6/7] Replace STRING#replace(REMOVED, "") with StringUtils.remove(STRING, REMOVED) (better performance & more readable / understandable) --- .../com/djrapitops/plan/data/additional/HookHandler.java | 3 ++- .../main/java/com/djrapitops/plan/database/Database.java | 3 ++- .../java/com/djrapitops/plan/utilities/FormatUtils.java | 3 ++- .../com/djrapitops/plan/utilities/analysis/Analysis.java | 7 ++++--- .../djrapitops/plan/utilities/analysis/AnalysisUtils.java | 7 +++++-- .../java/com/djrapitops/plan/utilities/html/HtmlUtils.java | 5 +++-- .../main/java/com/djrapitops/plan/ui/graphs/GraphTest.java | 3 ++- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/additional/HookHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/additional/HookHandler.java index d2259b13f..784785c1d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/additional/HookHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/additional/HookHandler.java @@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.data.additional; import com.djrapitops.pluginbridge.plan.Bridge; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Plan; +import org.apache.commons.lang3.StringUtils; import java.io.Serializable; import java.util.*; @@ -52,7 +53,7 @@ public class HookHandler { configHandler.createSection(dataSource); } if (configHandler.isEnabled(dataSource)) { - Log.debug("Registered a new datasource: " + dataSource.getPlaceholder("").replace("%", "")); + Log.debug("Registered a new datasource: " + StringUtils.remove(dataSource.getPlaceholder(""), '%')); additionalDataSources.add(dataSource); } } catch (Exception e) { diff --git a/Plan/src/main/java/com/djrapitops/plan/database/Database.java b/Plan/src/main/java/com/djrapitops/plan/database/Database.java index 47b9d2e32..3d192a132 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/Database.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/Database.java @@ -4,6 +4,7 @@ import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; import main.java.com.djrapitops.plan.database.tables.*; import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.StringUtils; import java.sql.Connection; import java.sql.SQLException; @@ -150,7 +151,7 @@ public abstract class Database { * @return sqlite/mysql */ public String getConfigName() { - return getName().toLowerCase().replace(" ", ""); + return StringUtils.remove(getName().toLowerCase(), ' '); } public abstract boolean isNewDatabase() throws SQLException; diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java index b313d517f..14465fdf4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java @@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.utilities; import com.djrapitops.plugin.utilities.Format; import com.djrapitops.plugin.utilities.FormattingUtils; import main.java.com.djrapitops.plan.Settings; +import org.apache.commons.lang3.StringUtils; import org.bukkit.Location; import java.text.DecimalFormat; @@ -136,7 +137,7 @@ public class FormatUtils { } builder.append(s); } - String formattedTime = builder.toString().replace("%zero%", ""); + String formattedTime = StringUtils.remove(builder.toString(), "%zero%"); if (formattedTime.isEmpty()) { return Settings.FORMAT_SECONDS.toString().replace("%seconds%", "0"); } 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 fe8f10e65..2b038f537 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 @@ -28,6 +28,7 @@ import main.java.com.djrapitops.plan.utilities.comparators.UserInfoLastPlayedCom import main.java.com.djrapitops.plan.utilities.html.HtmlStructure; import main.java.com.djrapitops.plan.utilities.html.HtmlUtils; import main.java.com.djrapitops.plan.utilities.html.tables.PlayersTableCreator; +import org.apache.commons.lang3.StringUtils; import java.io.Serializable; import java.sql.SQLException; @@ -191,7 +192,7 @@ public class Analysis { Log.debug("Analysis", "Additional Sources: " + sources.size()); sources.parallelStream().filter(Verify::notNull).forEach(source -> { try { - Benchmark.start("Source " + source.getPlaceholder("").replace("%", "")); + Benchmark.start("Source " + StringUtils.remove(source.getPlaceholder(""), '%')); final List analysisTypes = source.getAnalysisTypes(); if (analysisTypes.isEmpty()) { return; @@ -217,11 +218,11 @@ public class Analysis { replaceMap.put(source.getPlaceholder(boolTot.getPlaceholderModifier()), AnalysisUtils.getBooleanTotal(boolTot, source, uuids)); } } catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) { - Log.error("A PluginData-source caused an exception: " + source.getPlaceholder("").replace("%", "")); + Log.error("A PluginData-source caused an exception: " + StringUtils.remove(source.getPlaceholder(""), '%')); Log.toLog(this.getClass().getName(), e); } finally { - Benchmark.stop("Analysis", "Source " + source.getPlaceholder("").replace("%", "")); + Benchmark.stop("Analysis", "Source " + StringUtils.remove(source.getPlaceholder(""), '%')); } }); Benchmark.stop("Analysis", "3rd party"); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java index 286565dd9..1d5888451 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java @@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.data.additional.PluginData; import main.java.com.djrapitops.plan.utilities.FormatUtils; import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.comparators.SessionLengthComparator; +import org.apache.commons.lang3.StringUtils; import java.io.Serializable; import java.util.*; @@ -194,8 +195,10 @@ public class AnalysisUtils { } private static String logPluginDataCausedError(PluginData source, Throwable e) { - Log.error("A PluginData-source caused an exception: " + source.getPlaceholder("").replace("%", "")); - Log.toLog("PluginData-source caused an exception: " + source.getPlaceholder("").replace("%", ""), e); + String placeholder = StringUtils.remove(source.getPlaceholder(""), '%'); + + Log.error("A PluginData-source caused an exception: " + placeholder); + Log.toLog("PluginData-source caused an exception: " + placeholder, e); return source.parseContainer("", "Exception during calculation."); } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java index 72ceee668..cd69563dc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlUtils.java @@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.utilities.html; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.systems.webserver.WebServer; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.text.StrSubstitutor; import java.io.Serializable; @@ -93,7 +94,7 @@ public class HtmlUtils { * @return */ public static String removeXSS(String string) { - return string.replaceAll("()|()", ""); + return StringUtils.removeAll(string,"()|()"); } /** @@ -114,7 +115,7 @@ public class HtmlUtils { string = Html.SPAN.parse(string); } - return string.replace("§r", ""); + return StringUtils.remove(string, "§r"); } public static String separateWithQuotes(String... strings) { 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 6b80d8a5e..fb5366427 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 @@ -9,6 +9,7 @@ import main.java.com.djrapitops.plan.data.TPS; 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.junit.Before; import org.junit.Test; import test.java.utils.RandomData; @@ -57,7 +58,7 @@ public class GraphTest { @Test public void testSeriesCreator() { - String result = SeriesCreator.seriesGraph(points, false, false).replaceAll("[\\[\\]]", ""); + String result = StringUtils.removeAll(SeriesCreator.seriesGraph(points, false, false), "[\\[\\]]"); String[] splittedResult = result.split(","); Map expected = new LinkedHashMap<>(); From 61f118b8275205e9f02ba9d7a21eaa48e2c49473 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 31 Aug 2017 22:08:55 +0200 Subject: [PATCH 7/7] Add toString() in Session Fix some tests --- .../java/com/djrapitops/plan/data/Session.java | 14 ++++++++++++++ .../djrapitops/plan/data/time/WorldTimesTest.java | 6 ++---- .../djrapitops/plan/utilities/MiscUtilsTest.java | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/Session.java b/Plan/src/main/java/com/djrapitops/plan/data/Session.java index 4fc12564e..812fd1c9b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/Session.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/Session.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.data; import main.java.com.djrapitops.plan.data.time.WorldTimes; +import org.apache.commons.lang3.builder.ToStringBuilder; import java.util.ArrayList; import java.util.HashMap; @@ -199,4 +200,17 @@ public class Session { public int hashCode() { return Objects.hash(sessionStart, sessionID, worldTimes, sessionEnd, playerKills, mobKills, deaths); } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("sessionStart", sessionStart) + .append("sessionID", sessionID) + .append("worldTimes", worldTimes) + .append("sessionEnd", sessionEnd) + .append("playerKills", playerKills) + .append("mobKills", mobKills) + .append("deaths", deaths) + .toString(); + } } diff --git a/Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java index 17c466853..aea8e910b 100644 --- a/Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -1,10 +1,10 @@ package main.java.com.djrapitops.plan.data.time; +import com.google.common.collect.ImmutableMap; import org.junit.Test; import test.java.utils.RandomData; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -61,9 +61,7 @@ public class WorldTimesTest { long amount = 1000L; String[] worlds = new String[]{worldOne, worldTwo}; - Map> testedW = new HashMap<>(); - testedW.put(worldOne, new ArrayList<>()); - testedW.put(worldTwo, new ArrayList<>()); + Map> testedW = ImmutableMap.of(worldOne, new ArrayList<>(), worldTwo, new ArrayList<>()); String lastWorld = worldOne; String lastGM = gms[0]; diff --git a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java index 8f4c9cbb0..e6ef0968a 100644 --- a/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java +++ b/Plan/test/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java @@ -46,7 +46,7 @@ public class MiscUtilsTest { String[] args = new String[]{"Rsl1122", "Test"}; ISender sender = new BukkitCMDSender(MockUtils.mockPlayer2()); - String expResult = "Rsl1122"; + String expResult = ""; String result = MiscUtils.getPlayerName(args, sender); assertEquals(expResult, result);