From 0e9faec9c8218e57772957ca575950b65cddc34b Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 31 Jul 2017 20:08:27 +0300 Subject: [PATCH] a Couple of tests --- .../comparators/SessionDataComparator.java | 1 - .../handling/info/InfoUuidCorrectionTest.java | 33 +++++ .../utilities/comparators/ComparatorTest.java | 121 ++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 Plan/src/test/java/main/java/com/djrapitops/plan/data/handling/info/InfoUuidCorrectionTest.java create mode 100644 Plan/src/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/SessionDataComparator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/SessionDataComparator.java index ce6223ca9..f4eab60dd 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/SessionDataComparator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/SessionDataComparator.java @@ -9,7 +9,6 @@ import java.util.Comparator; */ public class SessionDataComparator implements Comparator { - // This method should only be used if FactionsHook.isEnabled() returns true. @Override public int compare(SessionData s1, SessionData s2) { return Long.compare(s1.getSessionStart(), s2.getSessionStart()); diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/data/handling/info/InfoUuidCorrectionTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/data/handling/info/InfoUuidCorrectionTest.java new file mode 100644 index 000000000..0662d2dcf --- /dev/null +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/data/handling/info/InfoUuidCorrectionTest.java @@ -0,0 +1,33 @@ +package test.java.main.java.com.djrapitops.plan.data.handling.info; + +import main.java.com.djrapitops.plan.data.UserData; +import main.java.com.djrapitops.plan.data.handling.info.*; +import main.java.com.djrapitops.plan.utilities.MiscUtils; +import org.junit.Test; +import test.java.utils.MockUtils; + +import java.util.UUID; + +import static org.junit.Assert.assertTrue; + +public class InfoUuidCorrectionTest { + private final UserData wrong = MockUtils.mockUser2(); + private final UUID test = MockUtils.getPlayerUUID(); + + @Test + public void testAllInfoBooleanReturn() { + long now = MiscUtils.getTime(); + HandlingInfo[] h = new HandlingInfo[]{ + new ChatInfo(test, ""), + new DeathInfo(test), + new KillInfo(test, now, null, ""), + new LoginInfo(test, now, null, false, "", "", 0, ""), + new LogoutInfo(test, now, false, "", null, ""), + new PlaytimeDependentInfo(test, InfoType.OTHER, now, "", ""), + new ReloadInfo(test, now, null, false, "", "", "") + }; + for (HandlingInfo info : h) { + assertTrue(info.getClass().getSimpleName(), !info.process(wrong)); + } + } +} diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java new file mode 100644 index 000000000..b654d3445 --- /dev/null +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java @@ -0,0 +1,121 @@ +package test.java.main.java.com.djrapitops.plan.utilities.comparators; + +import main.java.com.djrapitops.plan.data.SessionData; +import main.java.com.djrapitops.plan.data.TPS; +import main.java.com.djrapitops.plan.data.UserData; +import main.java.com.djrapitops.plan.data.WebUser; +import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo; +import main.java.com.djrapitops.plan.utilities.analysis.Point; +import main.java.com.djrapitops.plan.utilities.comparators.*; +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; + +public class ComparatorTest { + + private Random r = new Random(); + + @Test + public void testHandlingInfoComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + test.add(new HandlingInfo(null, null, r.nextLong()) { + @Override + public boolean process(UserData uData) { + return false; + } + }); + } + List longValues = test.stream().map(HandlingInfo::getTime).collect(Collectors.toList()); + longValues.sort(Long::compare); + test.sort(new HandlingInfoTimeComparator()); + List afterSort = test.stream().map(HandlingInfo::getTime).collect(Collectors.toList()); + assertEquals(longValues, afterSort); + } + + @Test + public void testPointComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + test.add(new Point(r.nextLong(), r.nextLong())); + } + List longValues = test.stream().map(Point::getX).map(i -> (Long) (long) (double) i).collect(Collectors.toList()); + longValues.sort(Long::compare); + test.sort(new PointComparator()); + List afterSort = test.stream().map(Point::getX).map(i -> (Long) (long) (double) i).collect(Collectors.toList()); + assertEquals(longValues, afterSort); + } + + @Test + public void testSessionDataComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + test.add(new SessionData(r.nextLong(), r.nextLong())); + } + List longValues = test.stream().map(SessionData::getSessionStart).collect(Collectors.toList()); + longValues.sort(Long::compare); + test.sort(new SessionDataComparator()); + List afterSort = test.stream().map(SessionData::getSessionStart).collect(Collectors.toList()); + assertEquals(longValues, afterSort); + } + + @Test + public void testTPSComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + test.add(new TPS(r.nextLong(), 0, 0, 0, 0, 0, 0)); + } + List longValues = test.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()); + assertEquals(longValues, afterSort); + } + + @Test + public void testUserDataLastPlayedComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + UserData uD = new UserData(null, 0, false, "", "", false); + uD.setLastPlayed(r.nextLong()); + test.add(uD); + } + List longValues = test.stream().map(UserData::getLastPlayed).collect(Collectors.toList()); + longValues.sort(Long::compare); + Collections.reverse(longValues); + test.sort(new UserDataLastPlayedComparator()); + List afterSort = test.stream().map(UserData::getLastPlayed).collect(Collectors.toList()); + assertEquals(longValues, afterSort); + } + + @Test + public void testUserDataNameComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + String randomName = UUID.randomUUID().toString().split("-")[0]; + test.add(new UserData(null, 0, false, "", randomName, false)); + } + List stringValues = test.stream().map(UserData::getName).collect(Collectors.toList()); + Collections.sort(stringValues); + test.sort(new UserDataNameComparator()); + List afterSort = test.stream().map(UserData::getName).collect(Collectors.toList()); + assertEquals(stringValues, afterSort); + } + + @Test + public void testWebUserComparator() { + List test = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + test.add(new WebUser("", "", r.nextInt())); + } + List intValues = test.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()); + assertEquals(intValues, afterSort); + } +}