|
| 1 | +package com.gmail.nossr50.config.experience; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Answers.CALLS_REAL_METHODS; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | + |
| 7 | +import com.gmail.nossr50.config.BukkitConfig; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 13 | +import org.junit.jupiter.api.Nested; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.ValueSource; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit tests for {@link ExperienceConfig} combat XP ceiling settings. These guard against the |
| 20 | + * getter and the shipped experience.yml disagreeing on the config key, which silently ignores |
| 21 | + * whatever value the server admin sets (#5309). |
| 22 | + */ |
| 23 | +class ExperienceConfigTest { |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates an {@link ExperienceConfig} whose backing config is the given YAML, without running |
| 27 | + * the file-loading constructor. The getters under test only touch the backing config. |
| 28 | + */ |
| 29 | + private static ExperienceConfig experienceConfigBackedBy(final YamlConfiguration yaml) { |
| 30 | + final ExperienceConfig experienceConfig = mock(ExperienceConfig.class, CALLS_REAL_METHODS); |
| 31 | + try { |
| 32 | + final Field configField = BukkitConfig.class.getDeclaredField("config"); |
| 33 | + configField.setAccessible(true); |
| 34 | + configField.set(experienceConfig, yaml); |
| 35 | + } catch (ReflectiveOperationException e) { |
| 36 | + throw new IllegalStateException("Unable to inject backing config", e); |
| 37 | + } |
| 38 | + return experienceConfig; |
| 39 | + } |
| 40 | + |
| 41 | + /** Loads the experience.yml bundled with the plugin jar. */ |
| 42 | + private static YamlConfiguration shippedExperienceYaml() { |
| 43 | + final InputStream resource = |
| 44 | + ExperienceConfig.class.getClassLoader().getResourceAsStream("experience.yml"); |
| 45 | + assertThat(resource).as("bundled experience.yml resource").isNotNull(); |
| 46 | + return YamlConfiguration.loadConfiguration( |
| 47 | + new InputStreamReader(resource, StandardCharsets.UTF_8)); |
| 48 | + } |
| 49 | + |
| 50 | + @Nested |
| 51 | + class CombatXpCeiling { |
| 52 | + |
| 53 | + @Test |
| 54 | + void getCombatHPCeilingShouldReturnShippedDefaultWhenConfigIsUnchanged() { |
| 55 | + // Given - the experience.yml shipped with the plugin, with no admin edits |
| 56 | + final YamlConfiguration shippedYaml = shippedExperienceYaml(); |
| 57 | + final ExperienceConfig experienceConfig = experienceConfigBackedBy(shippedYaml); |
| 58 | + |
| 59 | + // Then - the shipped file defines the ceiling key the plugin documents |
| 60 | + assertThat(shippedYaml.isSet("ExploitFix.Combat.XPCeiling.Damage_Limit")) |
| 61 | + .as("shipped experience.yml should contain the documented ceiling key") |
| 62 | + .isTrue(); |
| 63 | + |
| 64 | + // And - the getter reports the shipped value |
| 65 | + assertThat(experienceConfig.getCombatHPCeiling()).isEqualTo(100); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest(name = "customized Damage_Limit {0} should be honored") |
| 69 | + @ValueSource(ints = {1, 50, 250, 5000}) |
| 70 | + void getCombatHPCeilingShouldHonorCustomizedDamageLimit(final int customLimit) { |
| 71 | + /* |
| 72 | + * Intent: this is the #5309 regression guard. The getter used to read |
| 73 | + * 'HP_Modifier_Limit', a key that never shipped, so any admin edit to 'Damage_Limit' |
| 74 | + * was silently ignored and the ceiling stayed at the hardcoded 100. |
| 75 | + */ |
| 76 | + |
| 77 | + // Given - a server admin customized the ceiling in their experience.yml |
| 78 | + final YamlConfiguration customizedYaml = shippedExperienceYaml(); |
| 79 | + customizedYaml.set("ExploitFix.Combat.XPCeiling.Damage_Limit", customLimit); |
| 80 | + final ExperienceConfig experienceConfig = experienceConfigBackedBy(customizedYaml); |
| 81 | + |
| 82 | + // When - the ceiling is read |
| 83 | + final int ceiling = experienceConfig.getCombatHPCeiling(); |
| 84 | + |
| 85 | + // Then - the customized value takes effect instead of the hardcoded default |
| 86 | + assertThat(ceiling).isEqualTo(customLimit); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void getCombatHPCeilingShouldFallBackTo100WhenKeyIsMissing() { |
| 91 | + // Given - a config with no XPCeiling section at all (e.g. a stripped-down user file) |
| 92 | + final ExperienceConfig experienceConfig = |
| 93 | + experienceConfigBackedBy(new YamlConfiguration()); |
| 94 | + |
| 95 | + // Then - the getter falls back to the historical default |
| 96 | + assertThat(experienceConfig.getCombatHPCeiling()).isEqualTo(100); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments