From 6bd14e03fec6f240c007ae18b727102de737c44e Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 21 Aug 2017 18:29:40 +0300 Subject: [PATCH 1/2] Replace the big test with a more specific test. --- .../plan/data/time/WorldTimesTest.java | 118 +++++++++--------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java index 52cff0aff..2332b7677 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -9,7 +9,7 @@ import test.java.utils.RandomData; import java.util.*; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; /** * @author Rsl1122 @@ -67,7 +67,7 @@ public class WorldTimesTest { } @Test - public void testLotOfChanges() { + public void testLotOfChangesWorldTime() { long amount = 1000L; String[] worlds = new String[]{worldOne, worldTwo}; @@ -89,7 +89,6 @@ public class WorldTimesTest { long time = i * amount + this.time; - printCurrentState(testedW, i, gm, world); test.updateState(world, gm, time); } @@ -114,66 +113,69 @@ public class WorldTimesTest { assertEquals(amount * 50, time1 + time2); assertEquals(worldTimeOne, time1); assertEquals(worldTimeTwo, time2); - - // Tests GM Time calculation - for (Map.Entry> entry : testedW.entrySet()) { - String world = entry.getKey(); - List gmList = entry.getValue(); - for (int i = 0; i < gms.length; i++) { - final String lookFor = gms[i]; - long gmCount = gmList.stream().filter(gmNum -> gmNum.equals(lookFor)).count(); - - Optional gmTimes = test.getGMTimes(world); - - if (gmTimes.isPresent()) { - long expected = gmCount * amount; - long actual = gmTimes.get().getTime(lookFor); - assertEquals(world + ": " + lookFor + ": " + expected + " Actual: " + actual, expected, actual); - } else { - fail("GM Times can't not be present."); - } - } - } } - private void printCurrentState(Map> testedW, int i, String gm, String world) { - int sizeW1 = testedW.get(worldOne).size(); - int sizeW2 = testedW.get(worldTwo).size(); + @Test + public void testGMTrackingSingleWorld() { + long changeTime = time + 1000L; + long changeTime2 = changeTime + 1000L; + GMTimes gmTimes = test.getGMTimes(worldOne).get(); + test.updateState(worldOne, "CREATIVE", changeTime); + assertTrue(1000L == gmTimes.getTime("SURVIVAL")); + assertTrue(0L == gmTimes.getTime("CREATIVE")); + test.updateState(worldOne, "ADVENTURE", changeTime2); + assertTrue(1000L == gmTimes.getTime("SURVIVAL")); + assertTrue(1000L == gmTimes.getTime("CREATIVE")); + assertTrue(0L == gmTimes.getTime("ADVENTURE")); + } - StringBuilder b = new StringBuilder(""+i); - while (b.length() < 3) { - b.append(" "); - } - b.append(world).append(":").append(gm).append(": "); - while (b.length() < 18) { - b.append(" "); - } - b.append(sizeW1); + @Test + public void testGMTrackingTwoWorlds() { + long changeTime = time + 1000L; + long changeTime2 = time + 2000L; + GMTimes worldOneGMTimes = test.getGMTimes(worldOne).get(); + test.updateState(worldOne, "CREATIVE", changeTime); + test.updateState(worldOne, "ADVENTURE", changeTime2); + // Same state as above. - while (b.length() < 21) { - b.append(" "); - } + test.updateState(worldTwo, "SURVIVAL", time + 3000L); + assertTrue(1000L == worldOneGMTimes.getTime("SURVIVAL")); + assertTrue(1000L == worldOneGMTimes.getTime("CREATIVE")); + assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); - for (final String lookFor : gms) { - long count = testedW.get(worldOne).stream().filter(gmNum -> gmNum.equals(lookFor)).count(); - b.append(" ").append(count); - } - while (b.length() < 29) { - b.append(" "); - } - b.append(" |"); - for (final String lookFor : gms) { - long count = testedW.get(worldTwo).stream().filter(gmNum -> gmNum.equals(lookFor)).count(); - b.append(" ").append(count); - } - while (b.length() < 40) { - b.append(" "); - } - b.append(" ") - .append(sizeW2) - .append(" = ") - .append(sizeW1 + sizeW2); - System.out.println(b.toString()); + GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo).get(); + + assertTrue(0L == worldTwoGMTimes.getTime("SURVIVAL")); + assertTrue(0L == worldTwoGMTimes.getTime("CREATIVE")); + assertTrue(0L == worldTwoGMTimes.getTime("ADVENTURE")); + + test.updateState(worldTwo, "CREATIVE", time + 4000L); + + assertTrue(1000L == worldOneGMTimes.getTime("SURVIVAL")); + assertTrue(1000L == worldOneGMTimes.getTime("CREATIVE")); + assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); + + assertTrue(1000L == worldTwoGMTimes.getTime("SURVIVAL")); + assertTrue(0L == worldTwoGMTimes.getTime("CREATIVE")); + + test.updateState(worldTwo, "CREATIVE", time + 5000L); + assertTrue(1000L == worldTwoGMTimes.getTime("SURVIVAL")); + assertTrue(1000L == worldTwoGMTimes.getTime("CREATIVE")); + + // No change should occur. + test.updateState(worldOne, "ADVENTURE", time + 5000L); + assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); + assertTrue(1000L == worldTwoGMTimes.getTime("CREATIVE")); + test.updateState(worldTwo, "CREATIVE", time + 5000L); + System.out.println(test); + test.updateState(worldOne, "ADVENTURE", time + 6000L); + System.out.println(test); + assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); + assertTrue(2000L == worldTwoGMTimes.getTime("CREATIVE")); + + test.updateState(worldTwo, "ADVENTURE", time + 7000L); + assertTrue(2000L == worldTwoGMTimes.getTime("CREATIVE")); + assertTrue(2000L == worldOneGMTimes.getTime("ADVENTURE")); } // TODO Test where SessionData is ended, check if worldTimes & session length add up. From 75ccecce02a2bfc85f7fb5234fc834ea4a75fbe2 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 21 Aug 2017 18:55:07 +0300 Subject: [PATCH 2/2] Fixed WorldTimes, now works as intended. --- .../djrapitops/plan/data/time/WorldTimes.java | 14 +++-- .../plan/data/time/WorldTimesTest.java | 55 ++++++++++--------- 2 files changed, 36 insertions(+), 33 deletions(-) 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 bdeb603eb..ff9dc61a8 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 @@ -43,16 +43,18 @@ public class WorldTimes { public void updateState(String worldName, String gameMode, long changeTime) { GMTimes currentGMTimes = worldTimes.get(currentWorld); - - GMTimes newGMTimes = worldTimes.get(worldName); - if (newGMTimes == null) { - addWorld(worldName, gameMode, currentGMTimes.getLastStateChange()); + if (worldName.equals(currentWorld)) { + currentGMTimes.changeState(gameMode, changeTime); + } else { + GMTimes newGMTimes = worldTimes.get(worldName); + if (newGMTimes == null) { + addWorld(worldName, gameMode, currentGMTimes.getLastStateChange()); + } + currentGMTimes.changeState(currentGamemode, changeTime); } - currentGMTimes.changeState(gameMode, changeTime); for (GMTimes gmTimes : worldTimes.values()) { gmTimes.setLastStateChange(changeTime); } - worldTimes.put(currentWorld, currentGMTimes); currentWorld = worldName; currentGamemode = gameMode; } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java index 2332b7677..d3a7667bd 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/time/WorldTimesTest.java @@ -9,7 +9,6 @@ import test.java.utils.RandomData; import java.util.*; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * @author Rsl1122 @@ -121,12 +120,12 @@ public class WorldTimesTest { long changeTime2 = changeTime + 1000L; GMTimes gmTimes = test.getGMTimes(worldOne).get(); test.updateState(worldOne, "CREATIVE", changeTime); - assertTrue(1000L == gmTimes.getTime("SURVIVAL")); - assertTrue(0L == gmTimes.getTime("CREATIVE")); + assertEquals(1000L, gmTimes.getTime("SURVIVAL")); + assertEquals(0L, gmTimes.getTime("CREATIVE")); test.updateState(worldOne, "ADVENTURE", changeTime2); - assertTrue(1000L == gmTimes.getTime("SURVIVAL")); - assertTrue(1000L == gmTimes.getTime("CREATIVE")); - assertTrue(0L == gmTimes.getTime("ADVENTURE")); + assertEquals(1000L, gmTimes.getTime("SURVIVAL")); + assertEquals(1000L, gmTimes.getTime("CREATIVE")); + assertEquals(0L, gmTimes.getTime("ADVENTURE")); } @Test @@ -136,46 +135,48 @@ public class WorldTimesTest { GMTimes worldOneGMTimes = test.getGMTimes(worldOne).get(); test.updateState(worldOne, "CREATIVE", changeTime); test.updateState(worldOne, "ADVENTURE", changeTime2); - // Same state as above. + assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); + assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); + assertEquals(0L, worldOneGMTimes.getTime("ADVENTURE")); test.updateState(worldTwo, "SURVIVAL", time + 3000L); - assertTrue(1000L == worldOneGMTimes.getTime("SURVIVAL")); - assertTrue(1000L == worldOneGMTimes.getTime("CREATIVE")); - assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); - GMTimes worldTwoGMTimes = test.getGMTimes(worldTwo).get(); + assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); + assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); + assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); - assertTrue(0L == worldTwoGMTimes.getTime("SURVIVAL")); - assertTrue(0L == worldTwoGMTimes.getTime("CREATIVE")); - assertTrue(0L == worldTwoGMTimes.getTime("ADVENTURE")); + assertEquals(0L, worldTwoGMTimes.getTime("SURVIVAL")); + assertEquals(0L, worldTwoGMTimes.getTime("CREATIVE")); + assertEquals(0L, worldTwoGMTimes.getTime("ADVENTURE")); test.updateState(worldTwo, "CREATIVE", time + 4000L); - assertTrue(1000L == worldOneGMTimes.getTime("SURVIVAL")); - assertTrue(1000L == worldOneGMTimes.getTime("CREATIVE")); - assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); + assertEquals(1000L, worldOneGMTimes.getTime("SURVIVAL")); + assertEquals(1000L, worldOneGMTimes.getTime("CREATIVE")); + assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); - assertTrue(1000L == worldTwoGMTimes.getTime("SURVIVAL")); - assertTrue(0L == worldTwoGMTimes.getTime("CREATIVE")); + assertEquals(1000L, worldTwoGMTimes.getTime("SURVIVAL")); + assertEquals(0L, worldTwoGMTimes.getTime("CREATIVE")); test.updateState(worldTwo, "CREATIVE", time + 5000L); - assertTrue(1000L == worldTwoGMTimes.getTime("SURVIVAL")); - assertTrue(1000L == worldTwoGMTimes.getTime("CREATIVE")); + assertEquals(1000L, worldTwoGMTimes.getTime("SURVIVAL")); + assertEquals(1000L, worldTwoGMTimes.getTime("CREATIVE")); // No change should occur. test.updateState(worldOne, "ADVENTURE", time + 5000L); - assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); - assertTrue(1000L == worldTwoGMTimes.getTime("CREATIVE")); + System.out.println(test); + assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); + assertEquals(1000L, worldTwoGMTimes.getTime("CREATIVE")); test.updateState(worldTwo, "CREATIVE", time + 5000L); System.out.println(test); test.updateState(worldOne, "ADVENTURE", time + 6000L); System.out.println(test); - assertTrue(1000L == worldOneGMTimes.getTime("ADVENTURE")); - assertTrue(2000L == worldTwoGMTimes.getTime("CREATIVE")); + assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE")); + assertEquals(2000L, worldTwoGMTimes.getTime("CREATIVE")); test.updateState(worldTwo, "ADVENTURE", time + 7000L); - assertTrue(2000L == worldTwoGMTimes.getTime("CREATIVE")); - assertTrue(2000L == worldOneGMTimes.getTime("ADVENTURE")); + assertEquals(2000L, worldTwoGMTimes.getTime("CREATIVE")); + assertEquals(2000L, worldOneGMTimes.getTime("ADVENTURE")); } // TODO Test where SessionData is ended, check if worldTimes & session length add up.