Skip to content

Commit 653eb62

Browse files
committed
Add validation and tests for OversizedChunkLocator
1 parent d226292 commit 653eb62

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎src/main/java/org/glavo/nbt/io/OversizedChunkLocator.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
@FunctionalInterface
2929
public interface OversizedChunkLocator<T> {
3030
static <T> OversizedChunkLocator<T> emptyLocator() {
31-
return (source, localX, localZ) -> null;
31+
return (source, chunkLocalX, chunkLocalZ) -> {
32+
Objects.requireNonNull(source);
33+
Objects.checkIndex(chunkLocalX, ChunkUtils.CHUNKS_PER_REGION_SIDE);
34+
Objects.checkIndex(chunkLocalZ, ChunkUtils.CHUNKS_PER_REGION_SIDE);
35+
return null;
36+
};
3237
}
3338

3439
static OversizedChunkLocator<Path> defaultLocator() {
@@ -40,7 +45,6 @@ final class Holder {
4045
Objects.checkIndex(chunkLocalX, ChunkUtils.CHUNKS_PER_REGION_SIDE);
4146
Objects.checkIndex(chunkLocalZ, ChunkUtils.CHUNKS_PER_REGION_SIDE);
4247

43-
4448
Path fileName = source.getFileName();
4549
if (fileName == null) {
4650
return null;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.glavo.nbt.io;
19+
20+
import org.glavo.nbt.internal.ChunkUtils;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.nio.file.Path;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
public final class OversizedChunkLocatorTest {
28+
29+
@Test
30+
void testEmptyLocator() {
31+
OversizedChunkLocator<Path> locator = OversizedChunkLocator.emptyLocator();
32+
33+
Path source = Path.of("r.0.0.mca");
34+
for (int i = 0; i < ChunkUtils.CHUNKS_PER_REGION_SIDE; i++) {
35+
for (int j = 0; j < ChunkUtils.CHUNKS_PER_REGION_SIDE; j++) {
36+
assertNull(locator.locate(source, i, j));
37+
}
38+
}
39+
40+
assertThrows(IndexOutOfBoundsException.class, () -> locator.locate(source, -1, 0));
41+
assertThrows(IndexOutOfBoundsException.class, () -> locator.locate(source, 0, -1));
42+
assertThrows(IndexOutOfBoundsException.class, () -> locator.locate(source, ChunkUtils.CHUNKS_PER_REGION_SIDE, 0));
43+
assertThrows(IndexOutOfBoundsException.class, () -> locator.locate(source, 32, 0));
44+
assertThrows(IndexOutOfBoundsException.class, () -> locator.locate(source, 0, 32));
45+
}
46+
47+
@Test
48+
void testDefaultLocator() {
49+
OversizedChunkLocator<Path> locator = OversizedChunkLocator.defaultLocator();
50+
51+
assertEquals(Path.of("c.0.0.mcc"), locator.locate(Path.of("r.0.0.mca"), 0, 0));
52+
assertEquals(Path.of("c.31.31.mcc"), locator.locate(Path.of("r.0.0.mca"), 31, 31));
53+
assertEquals(Path.of("c.37.70.mcc"), locator.locate(Path.of("r.1.2.mca"), 5, 6));
54+
assertEquals(Path.of("c.-27.70.mcc"), locator.locate(Path.of("r.-1.2.mca"), 5, 6));
55+
}
56+
}

0 commit comments

Comments
 (0)