From dc2b139ac64b5833dcc8abfd1791166c38e67060 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Thu, 31 Aug 2017 18:44:24 +0200 Subject: [PATCH] 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; } }; }