|
| 1 | +/* |
| 2 | + * Copyright 2026 Glavo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.glavo.nbt.tag; |
| 17 | + |
| 18 | +import org.glavo.nbt.TestResources; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.EnumSource; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.channels.Channels; |
| 24 | +import java.nio.channels.FileChannel; |
| 25 | +import java.nio.channels.ReadableByteChannel; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.StandardOpenOption; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.*; |
| 31 | + |
| 32 | +public final class TagLoaderTest { |
| 33 | + |
| 34 | + enum Loader { |
| 35 | + INPUT_STREAM { |
| 36 | + @Override |
| 37 | + <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException { |
| 38 | + return Tag.Loader.ofPath(tagClass).load(file); |
| 39 | + } |
| 40 | + }, |
| 41 | + FILE_CHANNEL { |
| 42 | + @Override |
| 43 | + <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException { |
| 44 | + try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) { |
| 45 | + return Tag.Loader.ofByteChannel(tagClass).load(channel); |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + READABLE_BYTE_CHANNEL { |
| 50 | + @Override |
| 51 | + <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException { |
| 52 | + try (ReadableByteChannel channel = Channels.newChannel(Files.newInputStream(file))) { |
| 53 | + return Tag.Loader.ofByteChannel(tagClass).load(channel); |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
| 57 | + PATH { |
| 58 | + @Override |
| 59 | + <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException { |
| 60 | + return Tag.Loader.ofPath(tagClass).load(file); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + abstract <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException; |
| 65 | + } |
| 66 | + |
| 67 | + /// Loads a level.dat file (compressed with gzip) |
| 68 | + @ParameterizedTest |
| 69 | + @EnumSource |
| 70 | + void testLoadLevelDat(Loader loader) throws IOException { |
| 71 | + Path levelDatPath = TestResources.getResource("/assets/nbt/level.dat"); |
| 72 | + CompoundTag levelDat = loader.load(levelDatPath, CompoundTag.class); |
| 73 | + assertEquals("", levelDat.getName()); |
| 74 | + assertEquals(1, levelDat.size()); |
| 75 | + |
| 76 | + Tag dataTag = levelDat.get("Data"); |
| 77 | + assertNotNull(dataTag); |
| 78 | + assertInstanceOf(CompoundTag.class, dataTag); |
| 79 | + } |
| 80 | +} |
0 commit comments