diff --git a/src/test/java/com/diamonddagger590/mccore/statistic/StatisticEntryTest.java b/src/test/java/com/diamonddagger590/mccore/statistic/StatisticEntryTest.java index bad173a..69bda61 100644 --- a/src/test/java/com/diamonddagger590/mccore/statistic/StatisticEntryTest.java +++ b/src/test/java/com/diamonddagger590/mccore/statistic/StatisticEntryTest.java @@ -9,6 +9,7 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -143,4 +144,42 @@ void equals_returnsTrue_whenSameValues() { assertEquals(a, b); assertEquals(a.hashCode(), b.hashCode()); } + + @Test + @DisplayName("Given a SET_STRING entry with an empty set, when getAsSetString is called, then returns an empty unmodifiable set") + void getAsSetString_returnsEmptyUnmodifiableSet_whenSetIsEmpty() { + Set emptySet = new LinkedHashSet<>(); + StatisticEntry entry = new StatisticEntry(TEST_KEY, StatisticType.SET_STRING, emptySet); + + Set result = entry.getAsSetString(); + assertTrue(result.isEmpty()); + assertNotSame(emptySet, result); + assertThrows(UnsupportedOperationException.class, () -> result.add("x")); + } + + @Test + @DisplayName("Given two entries with different keys, when equals is called, then returns false") + void equals_returnsFalse_whenKeysDiffer() { + @SuppressWarnings("deprecation") + NamespacedKey otherKey = new NamespacedKey("mccore", "other_stat"); + StatisticEntry a = new StatisticEntry(TEST_KEY, StatisticType.INT, 1); + StatisticEntry b = new StatisticEntry(otherKey, StatisticType.INT, 1); + assertNotEquals(a, b); + } + + @Test + @DisplayName("Given two entries with different types, when equals is called, then returns false") + void equals_returnsFalse_whenTypesDiffer() { + StatisticEntry a = new StatisticEntry(TEST_KEY, StatisticType.INT, 1); + StatisticEntry b = new StatisticEntry(TEST_KEY, StatisticType.LONG, 1L); + assertNotEquals(a, b); + } + + @Test + @DisplayName("Given two entries with different values, when equals is called, then returns false") + void equals_returnsFalse_whenValuesDiffer() { + StatisticEntry a = new StatisticEntry(TEST_KEY, StatisticType.INT, 1); + StatisticEntry b = new StatisticEntry(TEST_KEY, StatisticType.INT, 2); + assertNotEquals(a, b); + } } diff --git a/src/test/java/com/diamonddagger590/mccore/statistic/StatisticRegistryTest.java b/src/test/java/com/diamonddagger590/mccore/statistic/StatisticRegistryTest.java index 1b8d64d..d6e6058 100644 --- a/src/test/java/com/diamonddagger590/mccore/statistic/StatisticRegistryTest.java +++ b/src/test/java/com/diamonddagger590/mccore/statistic/StatisticRegistryTest.java @@ -40,27 +40,31 @@ private StatisticRegistry registry() { } @Test - void registerSucceedsForNewKey() { + @DisplayName("Given a new statistic, when registering, then the registry reports it as registered") + void register_succeeds_whenKeyIsNew() { Statistic stat = new SimpleStatistic(key("test", "kills"), StatisticType.INT, 0, "Kills", "Total kills"); registry().register(stat); assertTrue(registry().registered(stat)); } @Test - void registerThrowsForDuplicateKey() { + @DisplayName("Given an already-registered statistic, when registering the same key again, then throws IllegalArgumentException") + void register_throwsIllegalArgument_whenKeyIsDuplicate() { Statistic stat = new SimpleStatistic(key("test", "kills"), StatisticType.INT, 0, "Kills", "Total kills"); registry().register(stat); assertThrows(IllegalArgumentException.class, () -> registry().register(stat)); } @Test - void registeredReturnsFalseForUnregistered() { + @DisplayName("Given a statistic that was never registered, when checking registered, then returns false") + void registered_returnsFalse_whenStatisticNotRegistered() { Statistic stat = new SimpleStatistic(key("test", "unknown"), StatisticType.INT, 0, "Unknown", "Unknown stat"); assertFalse(registry().registered(stat)); } @Test - void getStatisticReturnsRegisteredStatistic() { + @DisplayName("Given a registered statistic, when getting by key, then returns the statistic") + void getStatistic_returnsStatistic_whenKeyIsRegistered() { NamespacedKey key = key("test", "kills"); Statistic stat = new SimpleStatistic(key, StatisticType.INT, 0, "Kills", "Total kills"); registry().register(stat); @@ -69,12 +73,14 @@ void getStatisticReturnsRegisteredStatistic() { } @Test - void getStatisticReturnsEmptyForUnregistered() { + @DisplayName("Given no registered statistic for a key, when getting by that key, then returns empty Optional") + void getStatistic_returnsEmpty_whenKeyIsNotRegistered() { assertFalse(registry().getStatistic(key("test", "none")).isPresent()); } @Test - void getRegisteredStatisticsReturnsAllStats() { + @DisplayName("Given two registered statistics, when getting all registered statistics, then returns both") + void getRegisteredStatistics_returnsAll_whenMultipleRegistered() { Statistic stat1 = new SimpleStatistic(key("test", "a"), StatisticType.INT, 0, "A", "Stat A"); Statistic stat2 = new SimpleStatistic(key("test", "b"), StatisticType.LONG, 0L, "B", "Stat B"); registry().register(stat1); @@ -83,21 +89,22 @@ void getRegisteredStatisticsReturnsAllStats() { } @Test - void registerThrowsForMismatchedDefaultType() { - // INT stat with a String default should fail validation + @DisplayName("Given an INT statistic with a String default, when registering, then throws IllegalArgumentException") + void register_throwsIllegalArgument_whenIntStatHasStringDefault() { assertThrows(IllegalArgumentException.class, () -> registry().register(new SimpleStatistic(key("test", "bad"), StatisticType.INT, "wrong", "Bad", "Mismatched default"))); } @Test - void registerThrowsForLongDefaultOnIntStat() { - // INT stat with a Long default should fail + @DisplayName("Given an INT statistic with a Long default, when registering, then throws IllegalArgumentException") + void register_throwsIllegalArgument_whenIntStatHasLongDefault() { assertThrows(IllegalArgumentException.class, () -> registry().register(new SimpleStatistic(key("test", "bad"), StatisticType.INT, 5L, "Bad", "Long for INT"))); } @Test - void registerSucceedsForSetStringWithValidDefault() { + @DisplayName("Given a SET_STRING statistic with an empty Set default, when registering, then succeeds") + void register_succeeds_whenSetStringStatHasEmptySetDefault() { Set defaultSet = new LinkedHashSet<>(); Statistic stat = new SimpleStatistic(key("test", "set"), StatisticType.SET_STRING, defaultSet, "Set", "A set stat"); registry().register(stat); @@ -105,7 +112,8 @@ void registerSucceedsForSetStringWithValidDefault() { } @Test - void getRegisteredStatisticKeysReturnsAllKeys() { + @DisplayName("Given two registered statistics, when getting all keys, then returns both keys") + void getRegisteredStatisticKeys_returnsAllKeys_whenMultipleRegistered() { Statistic stat1 = new SimpleStatistic(key("test", "a"), StatisticType.INT, 0, "A", "Stat A"); Statistic stat2 = new SimpleStatistic(key("other", "b"), StatisticType.LONG, 0L, "B", "Stat B"); registry().register(stat1); diff --git a/src/test/java/com/diamonddagger590/mccore/statistic/cache/StatisticCacheTest.java b/src/test/java/com/diamonddagger590/mccore/statistic/cache/StatisticCacheTest.java index d6c07b2..b1e2010 100644 --- a/src/test/java/com/diamonddagger590/mccore/statistic/cache/StatisticCacheTest.java +++ b/src/test/java/com/diamonddagger590/mccore/statistic/cache/StatisticCacheTest.java @@ -4,6 +4,7 @@ import com.diamonddagger590.mccore.statistic.StatisticType; import org.bukkit.NamespacedKey; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.UUID; @@ -27,13 +28,15 @@ void setUp() { } @Test - void getReturnsEmptyOnCacheMiss() { + @DisplayName("Given an empty cache, when getting a non-existent entry, then returns empty Optional") + void get_returnsEmpty_whenCacheMiss() { UUID uuid = UUID.randomUUID(); assertFalse(cache.get(uuid, key("test", "stat")).isPresent()); } @Test - void putThenGetReturnsEntry() { + @DisplayName("Given a cached entry, when getting by same UUID and key, then returns the entry") + void get_returnsEntry_afterPut() { UUID uuid = UUID.randomUUID(); NamespacedKey key = key("test", "kills"); StatisticEntry entry = new StatisticEntry(key, StatisticType.INT, 42); @@ -43,7 +46,8 @@ void putThenGetReturnsEntry() { } @Test - void invalidateByUuidRemovesAllEntriesForPlayer() { + @DisplayName("Given multiple entries for one player, when invalidating by UUID, then removes all entries for that player") + void invalidate_byUuid_removesAllEntriesForPlayer() { UUID uuid = UUID.randomUUID(); NamespacedKey key1 = key("test", "kills"); NamespacedKey key2 = key("test", "deaths"); @@ -55,7 +59,8 @@ void invalidateByUuidRemovesAllEntriesForPlayer() { } @Test - void invalidateByKeyRemovesOnlySpecificEntry() { + @DisplayName("Given two entries for one player, when invalidating a specific key, then removes only that entry") + void invalidate_byKey_removesOnlySpecificEntry() { UUID uuid = UUID.randomUUID(); NamespacedKey key1 = key("test", "kills"); NamespacedKey key2 = key("test", "deaths"); @@ -67,19 +72,20 @@ void invalidateByKeyRemovesOnlySpecificEntry() { } @Test - void sizeReflectsCurrentEntryCount() { + @DisplayName("Given entries added to the cache, when calling size, then returns the current entry count") + void size_reflectsCurrentEntryCount() { UUID uuid = UUID.randomUUID(); NamespacedKey key1 = key("test", "a"); NamespacedKey key2 = key("test", "b"); assertEquals(0, cache.size()); cache.put(uuid, key1, new StatisticEntry(key1, StatisticType.INT, 1)); cache.put(uuid, key2, new StatisticEntry(key2, StatisticType.INT, 2)); - // Caffeine may not reflect size immediately; cleanUp for accurate count assertEquals(2, cache.size()); } @Test - void ttlExpiresEntries() throws InterruptedException { + @DisplayName("Given a cache with short TTL, when entry expires, then get returns empty") + void get_returnsEmpty_afterTtlExpires() throws InterruptedException { StatisticCache shortTtlCache = new StatisticCache(100, 1); UUID uuid = UUID.randomUUID(); NamespacedKey key = key("test", "ttl"); @@ -88,4 +94,119 @@ void ttlExpiresEntries() throws InterruptedException { Thread.sleep(1500); assertFalse(shortTtlCache.get(uuid, key).isPresent()); } + + @Test + @DisplayName("Given entries for two players, when invalidating one player by UUID, then the other player's entries remain") + void invalidate_byUuid_doesNotAffectOtherPlayers() { + UUID playerA = UUID.randomUUID(); + UUID playerB = UUID.randomUUID(); + NamespacedKey key1 = key("test", "kills"); + NamespacedKey key2 = key("test", "deaths"); + cache.put(playerA, key1, new StatisticEntry(key1, StatisticType.INT, 10)); + cache.put(playerA, key2, new StatisticEntry(key2, StatisticType.INT, 5)); + cache.put(playerB, key1, new StatisticEntry(key1, StatisticType.INT, 20)); + cache.put(playerB, key2, new StatisticEntry(key2, StatisticType.INT, 15)); + + cache.invalidate(playerA); + + assertFalse(cache.get(playerA, key1).isPresent()); + assertFalse(cache.get(playerA, key2).isPresent()); + assertTrue(cache.get(playerB, key1).isPresent()); + assertEquals(20, cache.get(playerB, key1).get().getAsInt()); + assertTrue(cache.get(playerB, key2).isPresent()); + assertEquals(15, cache.get(playerB, key2).get().getAsInt()); + } + + @Test + @DisplayName("Given an existing entry, when putting a new entry with the same UUID and key, then the value is overwritten") + void put_overwritesExistingEntry_whenSameUuidAndKey() { + UUID uuid = UUID.randomUUID(); + NamespacedKey key = key("test", "score"); + cache.put(uuid, key, new StatisticEntry(key, StatisticType.INT, 100)); + assertEquals(100, cache.get(uuid, key).get().getAsInt()); + + cache.put(uuid, key, new StatisticEntry(key, StatisticType.INT, 200)); + assertEquals(200, cache.get(uuid, key).get().getAsInt()); + } + + @Test + @DisplayName("Given an empty cache, when invalidating by UUID, then no error occurs and cache remains empty") + void invalidate_byUuid_doesNotThrow_whenNoEntriesExist() { + UUID uuid = UUID.randomUUID(); + cache.invalidate(uuid); + assertEquals(0, cache.size()); + } + + @Test + @DisplayName("Given an empty cache, when invalidating by UUID and key, then no error occurs and cache remains empty") + void invalidate_byKey_doesNotThrow_whenNoEntryExists() { + UUID uuid = UUID.randomUUID(); + cache.invalidate(uuid, key("test", "nonexistent")); + assertEquals(0, cache.size()); + } + + @Test + @DisplayName("Given entries for multiple players, when putting overwrites one, then size does not change") + void put_overwrite_doesNotChangeSize() { + UUID uuid = UUID.randomUUID(); + NamespacedKey key = key("test", "xp"); + cache.put(uuid, key, new StatisticEntry(key, StatisticType.INT, 50)); + assertEquals(1, cache.size()); + + cache.put(uuid, key, new StatisticEntry(key, StatisticType.INT, 75)); + assertEquals(1, cache.size()); + } + + @Test + @DisplayName("Given entries for two players with same key, when invalidating one player's key, then the other player's entry remains") + void invalidate_byKey_doesNotAffectOtherPlayersWithSameKey() { + UUID playerA = UUID.randomUUID(); + UUID playerB = UUID.randomUUID(); + NamespacedKey key = key("test", "kills"); + cache.put(playerA, key, new StatisticEntry(key, StatisticType.INT, 10)); + cache.put(playerB, key, new StatisticEntry(key, StatisticType.INT, 20)); + + cache.invalidate(playerA, key); + + assertFalse(cache.get(playerA, key).isPresent()); + assertTrue(cache.get(playerB, key).isPresent()); + assertEquals(20, cache.get(playerB, key).get().getAsInt()); + } + + @Test + @DisplayName("Given entries added and then invalidated, when checking size, then size reflects removals") + void size_reflectsRemovals_afterInvalidate() { + UUID uuid = UUID.randomUUID(); + NamespacedKey key1 = key("test", "a"); + NamespacedKey key2 = key("test", "b"); + cache.put(uuid, key1, new StatisticEntry(key1, StatisticType.INT, 1)); + cache.put(uuid, key2, new StatisticEntry(key2, StatisticType.INT, 2)); + assertEquals(2, cache.size()); + + cache.invalidate(uuid, key1); + assertEquals(1, cache.size()); + + cache.invalidate(uuid, key2); + assertEquals(0, cache.size()); + } + + @Test + @DisplayName("Given entries of different statistic types, when retrieving them, then each returns the correct typed value") + void get_returnsCorrectTypedValues_forDifferentStatisticTypes() { + UUID uuid = UUID.randomUUID(); + NamespacedKey intKey = key("test", "int_stat"); + NamespacedKey longKey = key("test", "long_stat"); + NamespacedKey doubleKey = key("test", "double_stat"); + NamespacedKey stringKey = key("test", "string_stat"); + + cache.put(uuid, intKey, new StatisticEntry(intKey, StatisticType.INT, 42)); + cache.put(uuid, longKey, new StatisticEntry(longKey, StatisticType.LONG, 100_000L)); + cache.put(uuid, doubleKey, new StatisticEntry(doubleKey, StatisticType.DOUBLE, 3.14)); + cache.put(uuid, stringKey, new StatisticEntry(stringKey, StatisticType.STRING, "hello")); + + assertEquals(42, cache.get(uuid, intKey).get().getAsInt()); + assertEquals(100_000L, cache.get(uuid, longKey).get().getAsLong()); + assertEquals(3.14, cache.get(uuid, doubleKey).get().getAsDouble(), 0.001); + assertEquals("hello", cache.get(uuid, stringKey).get().getAsString()); + } }