|
| 1 | +package com.tpwalke2.bluemapsignmarkers.core.signs.persistence.loaders; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.tpwalke2.bluemapsignmarkers.core.markers.MarkerGroup; |
| 5 | +import com.tpwalke2.bluemapsignmarkers.core.markers.MarkerGroupMatchType; |
| 6 | +import com.tpwalke2.bluemapsignmarkers.core.markers.MarkerGroupType; |
| 7 | +import com.tpwalke2.bluemapsignmarkers.core.signs.SignEntryKey; |
| 8 | +import com.tpwalke2.bluemapsignmarkers.core.signs.persistence.models.MarkerTypeV2; |
| 9 | +import com.tpwalke2.bluemapsignmarkers.core.signs.persistence.models.SignEntryV2; |
| 10 | +import com.tpwalke2.bluemapsignmarkers.core.signs.persistence.models.SignLinesParseResultV2; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.io.TempDir; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +class Version1SignEntryLoaderTest { |
| 23 | + |
| 24 | + private static final Gson GSON = new Gson(); |
| 25 | + private static final MarkerGroup[] POI_GROUP = { |
| 26 | + new MarkerGroup("[poi]", MarkerGroupMatchType.STARTS_WITH, MarkerGroupType.POI, |
| 27 | + "[poi]", null, 0, 0, false, 0, 0) |
| 28 | + }; |
| 29 | + |
| 30 | + private static String load(Path tempDir, String legacyDimension) throws IOException { |
| 31 | + var path = tempDir.resolve("signs.json").toString(); |
| 32 | + var entry = new SignEntryV2( |
| 33 | + new SignEntryKey(0, 64, 0, legacyDimension), |
| 34 | + "player-1", |
| 35 | + new SignLinesParseResultV2(MarkerTypeV2.POI, "Town Hall", "Town Hall"), |
| 36 | + new SignLinesParseResultV2(null, "", "")); |
| 37 | + var content = GSON.toJson(new SignEntryV2[]{entry}); |
| 38 | + Files.writeString(Path.of(path), content, StandardCharsets.UTF_8); |
| 39 | + |
| 40 | + var result = Version1SignEntryLoader.loadSignEntries(path, content, POI_GROUP, GSON); |
| 41 | + |
| 42 | + return result[0].key().parentMap(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void normalizesTheThreeRecognizedLegacyShorthandDimensions(@TempDir Path tempDir) throws IOException { |
| 47 | + assertEquals("minecraft:the_nether", load(tempDir, "nether")); |
| 48 | + assertEquals("minecraft:the_end", load(tempDir, "end")); |
| 49 | + assertEquals("minecraft:overworld", load(tempDir, "overworld")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void legacyShorthandNormalizationIsCaseInsensitive(@TempDir Path tempDir) throws IOException { |
| 54 | + assertEquals("minecraft:the_nether", load(tempDir, "NETHER")); |
| 55 | + assertEquals("minecraft:the_end", load(tempDir, "End")); |
| 56 | + assertEquals("minecraft:overworld", load(tempDir, "OverWorld")); |
| 57 | + } |
| 58 | + |
| 59 | + // Documents the Low-severity finding: getNormalizedMapId always lowercases its input, even on the default |
| 60 | + // branch, so an unrecognized legacy dimension string doesn't pass through truly unchanged - it survives with |
| 61 | + // its casing silently altered, rather than being left exactly as-is or flagged as unrecognized. |
| 62 | + @Test |
| 63 | + void anUnrecognizedLegacyDimensionStringFallsThroughLowercasedButOtherwiseUnchanged(@TempDir Path tempDir) throws IOException { |
| 64 | + assertEquals("my_custom_dimension", load(tempDir, "My_Custom_Dimension")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void anAlreadyNamespacedDimensionStringPassesThroughUnchanged(@TempDir Path tempDir) throws IOException { |
| 69 | + assertEquals("minecraft:overworld", load(tempDir, "minecraft:overworld")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void loadSignEntriesBacksUpTheOriginalFileBeforeReturning(@TempDir Path tempDir) throws IOException { |
| 74 | + var path = tempDir.resolve("signs.json").toString(); |
| 75 | + var entry = new SignEntryV2( |
| 76 | + new SignEntryKey(0, 64, 0, "overworld"), |
| 77 | + "player-1", |
| 78 | + new SignLinesParseResultV2(MarkerTypeV2.POI, "Town Hall", "Town Hall"), |
| 79 | + new SignLinesParseResultV2(null, "", "")); |
| 80 | + var content = GSON.toJson(new SignEntryV2[]{entry}); |
| 81 | + Files.writeString(Path.of(path), content, StandardCharsets.UTF_8); |
| 82 | + |
| 83 | + Version1SignEntryLoader.loadSignEntries(path, content, POI_GROUP, GSON); |
| 84 | + |
| 85 | + var backup = Path.of(path + ".v1.bak"); |
| 86 | + assertTrue(Files.exists(backup), "the original V1 file should be backed up"); |
| 87 | + assertEquals(content, Files.readString(backup)); |
| 88 | + } |
| 89 | +} |
0 commit comments