Repository navigation
[Unit Tests] Add coverage for SkillExperienceContext, StateTypeWarningLog, and PendingReward #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/test/java/us/eunoians/mcrpg/combat/state/StateTypeWarningLogTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package us.eunoians.mcrpg.combat.state; | ||
|
|
||
| import org.bukkit.NamespacedKey; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import us.eunoians.mcrpg.McRPGBaseTest; | ||
|
|
||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| @DisplayName("StateTypeWarningLog") | ||
| class StateTypeWarningLogTest extends McRPGBaseTest { | ||
|
|
||
| private Logger mockLogger; | ||
| private StateTypeWarningLog warningLog; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| mockLogger = mock(Logger.class); | ||
| warningLog = new StateTypeWarningLog(mockLogger); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("warnOnce") | ||
| class WarnOnce { | ||
|
|
||
| @Test | ||
| @DisplayName("logs warning on first call for a key") | ||
| @SuppressWarnings("deprecation") | ||
| void warnOnce_firstCall_logsWarning() { | ||
| NamespacedKey key = new NamespacedKey("mcrpg", "test_state"); | ||
| String message = "resolver threw for test_state"; | ||
|
|
||
| warningLog.warnOnce(key, message, null); | ||
|
|
||
| verify(mockLogger).log(Level.WARNING, message, (Throwable) null); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("does not log on second call for the same key") | ||
| @SuppressWarnings("deprecation") | ||
| void warnOnce_secondCallSameKey_doesNotLog() { | ||
| NamespacedKey key = new NamespacedKey("mcrpg", "test_state"); | ||
|
|
||
| warningLog.warnOnce(key, "first warning", null); | ||
| warningLog.warnOnce(key, "second warning", null); | ||
|
|
||
| verify(mockLogger).log(Level.WARNING, "first warning", (Throwable) null); | ||
| verify(mockLogger, never()).log(Level.WARNING, "second warning", (Throwable) null); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("logs warning for each distinct key") | ||
| @SuppressWarnings("deprecation") | ||
| void warnOnce_differentKeys_logsBoth() { | ||
| NamespacedKey key1 = new NamespacedKey("mcrpg", "state_a"); | ||
| NamespacedKey key2 = new NamespacedKey("mcrpg", "state_b"); | ||
|
|
||
| warningLog.warnOnce(key1, "warning for A", null); | ||
| warningLog.warnOnce(key2, "warning for B", null); | ||
|
|
||
| verify(mockLogger).log(Level.WARNING, "warning for A", (Throwable) null); | ||
| verify(mockLogger).log(Level.WARNING, "warning for B", (Throwable) null); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("passes throwable cause to the logger") | ||
| @SuppressWarnings("deprecation") | ||
| void warnOnce_withCause_passesCauseToLogger() { | ||
| NamespacedKey key = new NamespacedKey("mcrpg", "broken_resolver"); | ||
| RuntimeException cause = new RuntimeException("resolver failed"); | ||
|
|
||
| warningLog.warnOnce(key, "resolver threw", cause); | ||
|
|
||
| verify(mockLogger).log(Level.WARNING, "resolver threw", cause); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("suppresses duplicate even when cause differs") | ||
| @SuppressWarnings("deprecation") | ||
| void warnOnce_sameKeyDifferentCause_suppresses() { | ||
| NamespacedKey key = new NamespacedKey("mcrpg", "same_key"); | ||
| RuntimeException cause1 = new RuntimeException("first"); | ||
| RuntimeException cause2 = new RuntimeException("second"); | ||
|
|
||
| warningLog.warnOnce(key, "warning", cause1); | ||
| warningLog.warnOnce(key, "warning", cause2); | ||
|
|
||
| verify(mockLogger).log(Level.WARNING, "warning", cause1); | ||
| verify(mockLogger, never()).log(Level.WARNING, "warning", cause2); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("keys from different namespaces are treated as distinct") | ||
| @SuppressWarnings("deprecation") | ||
| void warnOnce_differentNamespaces_logsBoth() { | ||
| NamespacedKey key1 = new NamespacedKey("mcrpg", "my_state"); | ||
| NamespacedKey key2 = new NamespacedKey("thirdparty", "my_state"); | ||
|
|
||
| warningLog.warnOnce(key1, "mcrpg warning", null); | ||
| warningLog.warnOnce(key2, "thirdparty warning", null); | ||
|
|
||
| verify(mockLogger).log(Level.WARNING, "mcrpg warning", (Throwable) null); | ||
| verify(mockLogger).log(Level.WARNING, "thirdparty warning", (Throwable) null); | ||
| } | ||
| } | ||
| } | ||
129 changes: 129 additions & 0 deletions
129
src/test/java/us/eunoians/mcrpg/quest/reward/PendingRewardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package us.eunoians.mcrpg.quest.reward; | ||
|
|
||
| import org.bukkit.NamespacedKey; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import us.eunoians.mcrpg.McRPGBaseTest; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotSame; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| @DisplayName("PendingReward") | ||
| @SuppressWarnings("deprecation") | ||
| class PendingRewardTest extends McRPGBaseTest { | ||
|
|
||
| private static final UUID REWARD_ID = UUID.randomUUID(); | ||
| private static final UUID PLAYER_UUID = UUID.randomUUID(); | ||
| private static final NamespacedKey REWARD_TYPE_KEY = new NamespacedKey("mcrpg", "command"); | ||
| private static final NamespacedKey QUEST_KEY = new NamespacedKey("mcrpg", "test_quest"); | ||
| private static final long CREATED_AT = 1000L; | ||
| private static final long EXPIRES_AT = 2000L; | ||
|
|
||
| @Nested | ||
| @DisplayName("accessors") | ||
| class Accessors { | ||
|
|
||
| @Test | ||
| @DisplayName("getId returns the provided ID") | ||
| void getId_returnsProvidedId() { | ||
| PendingReward reward = createReward(Map.of("cmd", "give diamonds")); | ||
| assertEquals(REWARD_ID, reward.getId()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getPlayerUUID returns the provided player UUID") | ||
| void getPlayerUUID_returnsProvidedUUID() { | ||
| PendingReward reward = createReward(Map.of("cmd", "give diamonds")); | ||
| assertEquals(PLAYER_UUID, reward.getPlayerUUID()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getRewardTypeKey returns the provided type key") | ||
| void getRewardTypeKey_returnsProvidedKey() { | ||
| PendingReward reward = createReward(Map.of("cmd", "give diamonds")); | ||
| assertEquals(REWARD_TYPE_KEY, reward.getRewardTypeKey()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getQuestKey returns the provided quest key") | ||
| void getQuestKey_returnsProvidedKey() { | ||
| PendingReward reward = createReward(Map.of("cmd", "give diamonds")); | ||
| assertEquals(QUEST_KEY, reward.getQuestKey()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getCreatedAt returns the provided timestamp") | ||
| void getCreatedAt_returnsProvidedTimestamp() { | ||
| PendingReward reward = createReward(Map.of("cmd", "give diamonds")); | ||
| assertEquals(CREATED_AT, reward.getCreatedAt()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("getExpiresAt returns the provided timestamp") | ||
| void getExpiresAt_returnsProvidedTimestamp() { | ||
| PendingReward reward = createReward(Map.of("cmd", "give diamonds")); | ||
| assertEquals(EXPIRES_AT, reward.getExpiresAt()); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("serialized config immutability") | ||
| class SerializedConfigImmutability { | ||
|
|
||
| @Test | ||
| @DisplayName("getSerializedConfig returns the correct entries") | ||
| void getSerializedConfig_returnsCorrectEntries() { | ||
| Map<String, Object> config = Map.of("command", "give {player} diamond 5", "amount", 5); | ||
| PendingReward reward = createReward(config); | ||
| assertEquals(config, reward.getSerializedConfig()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("modifications to the source map do not affect the reward") | ||
| void getSerializedConfig_sourceMapModification_doesNotAffectReward() { | ||
| Map<String, Object> mutableConfig = new HashMap<>(); | ||
| mutableConfig.put("key", "original"); | ||
|
|
||
| PendingReward reward = createReward(mutableConfig); | ||
| mutableConfig.put("key", "modified"); | ||
| mutableConfig.put("extra", "sneaky"); | ||
|
|
||
| assertEquals("original", reward.getSerializedConfig().get("key")); | ||
| assertEquals(1, reward.getSerializedConfig().size()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("returned config map is unmodifiable") | ||
| void getSerializedConfig_returnedMap_isUnmodifiable() { | ||
| PendingReward reward = createReward(Map.of("cmd", "test")); | ||
| Map<String, Object> returnedConfig = reward.getSerializedConfig(); | ||
| assertThrows(UnsupportedOperationException.class, () -> returnedConfig.put("new", "value")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("empty config map is handled correctly") | ||
| void getSerializedConfig_emptyMap_isHandled() { | ||
| PendingReward reward = createReward(Map.of()); | ||
| assertEquals(0, reward.getSerializedConfig().size()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("returned config is a defensive copy, not the internal reference") | ||
| void getSerializedConfig_returnedMap_isDefensiveCopy() { | ||
| Map<String, Object> config = new HashMap<>(); | ||
| config.put("key", "value"); | ||
| PendingReward reward = createReward(config); | ||
| assertNotSame(config, reward.getSerializedConfig()); | ||
| } | ||
| } | ||
|
|
||
| private PendingReward createReward(Map<String, Object> config) { | ||
| return new PendingReward(REWARD_ID, PLAYER_UUID, REWARD_TYPE_KEY, config, QUEST_KEY, CREATED_AT, EXPIRES_AT); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: DiamondDagger590/McRPG
Length of output: 8643
Use plain JUnit for
StateTypeWarningLogTest.This class only mocks
Logger, constructsStateTypeWarningLog, and usesNamespacedKey. RemoveMcRPGBaseTestso each test instance does not initialize MockBukkit and loadMcRPG.Suggested fix
🤖 Prompt for AI Agents