diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 23a606e6..6606359b 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -4,27 +4,31 @@ on: [ push, pull_request ] jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + java: [ '11', '16' ] steps: - uses: actions/checkout@v2.3.4 - - name: Set up JDK 11 + - name: Set up JDK ${{ matrix.java }} uses: actions/setup-java@v2.2.0 with: distribution: 'adopt' - java-version: 11 + java-version: ${{ matrix.java }} - uses: actions/cache@v2.1.6 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: | ${{ runner.os }}-maven- - - name: Build with Maven - run: mvn -B package --file pom.xml - - name: Move built jars + - name: Build Project + run: mvn -B package --file pom.xml -Djava.version=${{ matrix.java }} + - name: Move Artifacts run: | mkdir staging + mv target staging/parent mv api/target staging/api mv yaml/target staging/yaml - uses: actions/upload-artifact@v2.2.4 with: - name: Artifacts + name: Artifacts-Java-${{ matrix.java }} path: staging diff --git a/.github/workflows/update-javadoc.yml b/.github/workflows/update-javadoc.yml index 926559f0..7599007e 100644 --- a/.github/workflows/update-javadoc.yml +++ b/.github/workflows/update-javadoc.yml @@ -8,11 +8,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2.3.4 - - name: Set up JDK 11 + - name: Set up JDK 16 uses: actions/setup-java@v2.2.0 with: distribution: 'adopt' - java-version: 11 + java-version: 16 - uses: actions/cache@v2.1.6 with: path: ~/.m2/repository @@ -20,7 +20,7 @@ jobs: restore-keys: | ${{ runner.os }}-maven- - name: Build with Maven - run: mvn -B package -D java.version=11 --file pom.xml + run: mvn -B package -Djava.version=16 --file pom.xml - name: Deploy pages uses: peaceiris/actions-gh-pages@v3.8.0 with: diff --git a/README.md b/README.md index 3d44f063..cd3f9f93 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Javadoc is [here](https://siroshun09.github.io/ConfigAPI/) com.github.siroshun09.configapi configapi - 4.3.1 + 4.4.0 compile ``` @@ -32,7 +32,7 @@ Javadoc is [here](https://siroshun09.github.io/ConfigAPI/) com.github.siroshun09.configapi configapi-yaml - 4.3.1 + 4.4.0 compile ``` diff --git a/api/pom.xml b/api/pom.xml index 1bf24a57..9b3d2b72 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -21,7 +21,7 @@ com.github.siroshun09.configapi parent - 4.3.1 + 4.4.0 4.0.0 diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/AbstractConfiguration.java b/api/src/main/java/com/github/siroshun09/configapi/api/AbstractConfiguration.java index c130c9a4..64e5dac2 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/AbstractConfiguration.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/AbstractConfiguration.java @@ -38,7 +38,7 @@ public abstract class AbstractConfiguration implements Configuration { @Override public @NotNull Object get(@NotNull String path, @NotNull Object def) { var value = get(path); - return value != null ? value : Objects.requireNonNull(path); + return value != null ? value : Objects.requireNonNull(def); } @Override @@ -71,7 +71,7 @@ public void set(@NotNull String path, @NotNull T value, @NotNull Serializer< @Override public @NotNull @Unmodifiable List getList(@NotNull String path, @NotNull List def) { var value = get(path); - return value instanceof List ? (List) value : def; + return value instanceof List ? (List) value : Objects.requireNonNull(def); } @Override @@ -180,7 +180,12 @@ public short getShort(@NotNull String path, short def) { @Override public @NotNull String getString(@NotNull String path, @NotNull String def) { var value = get(path); - return value instanceof String ? (String) value : def; + + if (value != null) { + return value instanceof String ? (String) value : value.toString(); + } else { + return Objects.requireNonNull(def); + } } @Override @@ -309,6 +314,7 @@ public short getShort(@NotNull String path, short def) { if (list != null) { return list.stream() + .filter(Objects::nonNull) .map(object -> object instanceof String ? (String) object : object.toString()) .collect(Collectors.toUnmodifiableList()); } else { diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/Configuration.java b/api/src/main/java/com/github/siroshun09/configapi/api/Configuration.java index 9d4b10fe..0c8272de 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/Configuration.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/Configuration.java @@ -118,18 +118,9 @@ public interface Configuration { */ void set(@NotNull String path, @NotNull T value, @NotNull Serializer serializer); - /** - * Gets the set of root paths included in this {@link Configuration}. - * - * @return set of root paths - * @deprecated use {@link Configuration#getKeyList()} - */ - @Deprecated(since = "4.3.0", forRemoval = true) - @NotNull @Unmodifiable Set getPaths(); - /** * Gets the list of root keys included in this {@link Configuration}. - * + *

* This method may not return to the correct order depending on the implementation. * * @return list of root keys @@ -357,7 +348,7 @@ public interface Configuration { /** * Gets the string of the specified path. *

- * If the string could not be obtained, this method returns 0. + * If the string could not be obtained, this method returns empty string. * * @param path the path to get the string * @return the string @@ -378,6 +369,9 @@ public interface Configuration { *

* If the boolean list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a boolean, it will be excluded. * * @param path the path to get the boolean list * @return the boolean list @@ -386,6 +380,9 @@ public interface Configuration { /** * Gets the boolean list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a boolean, it will be excluded. * * @param path the path to get the boolean list * @param def the default boolean list to return if the boolean list could not be obtained @@ -398,6 +395,12 @@ public interface Configuration { *

* If the byte list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the byte range, + * it will be converted to a byte using {@link Number#byteValue()}. * * @param path the path to get the byte list * @return the byte list @@ -406,6 +409,12 @@ public interface Configuration { /** * Gets the byte list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the byte range, + * it will be converted to a byte using {@link Number#byteValue()}. * * @param path the path to get the byte list * @param def the default byte list to return if the byte list could not be obtained @@ -418,6 +427,12 @@ public interface Configuration { *

* If the double list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the double range, + * it will be converted to a byte using {@link Number#doubleValue()}. * * @param path the path to get the double list * @return the double list @@ -426,6 +441,12 @@ public interface Configuration { /** * Gets the double list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the double range, + * it will be converted to a byte using {@link Number#doubleValue()}. * * @param path the path to get the double list * @param def the default double list to return if the double list could not be obtained @@ -438,6 +459,12 @@ public interface Configuration { *

* If the float list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the float range, + * it will be converted to a byte using {@link Number#floatValue()}. * * @param path the path to get the float list * @return the float list @@ -446,6 +473,12 @@ public interface Configuration { /** * Gets the float list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the float range, + * it will be converted to a byte using {@link Number#floatValue()}. * * @param path the path to get the float list * @param def the default float list to return if the float list could not be obtained @@ -458,6 +491,12 @@ public interface Configuration { *

* If the integer list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the int range, + * it will be converted to a byte using {@link Number#intValue()}. * * @param path the path to get the integer list * @return the integer list @@ -466,6 +505,12 @@ public interface Configuration { /** * Gets the integer list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the int range, + * it will be converted to a byte using {@link Number#intValue()}. * * @param path the path to get the integer list * @param def the default integer list to return if the integer list could not be obtained @@ -478,6 +523,12 @@ public interface Configuration { *

* If the long list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the long range, + * it will be converted to a byte using {@link Number#longValue()}. * * @param path the path to get the long list * @return the long list @@ -486,6 +537,12 @@ public interface Configuration { /** * Gets the long list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the long range, + * it will be converted to a byte using {@link Number#longValue()}. * * @param path the path to get the long list * @param def the default long list to return if the long list could not be obtained @@ -498,6 +555,12 @@ public interface Configuration { *

* If the short list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the short range, + * it will be converted to a byte using {@link Number#shortValue()}. * * @param path the path to get the short list * @return the short list @@ -506,6 +569,12 @@ public interface Configuration { /** * Gets the short list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} + * contains an object that cannot be converted to a number, it will be excluded. + *

+ * If the list contains a number that is outside the short range, + * it will be converted to a byte using {@link Number#shortValue()}. * * @param path the path to get the short list * @param def the default short list to return if the short list could not be obtained @@ -518,6 +587,10 @@ public interface Configuration { *

* If the string list could not be obtained, * this method returns {@link Collections#emptyList()}. + *

+ * If the list that obtained from {@link Configuration#getList(String)} contains non-string object, + * it will be converted to a string based on {@link Object#toString()} + * or the toString implementation of that object. * * @param path the path to get the string list * @return the string list @@ -526,6 +599,10 @@ public interface Configuration { /** * Gets the string list of the specified path. + *

+ * If the list that obtained from {@link Configuration#getList(String)} contains non-string object, + * it will be converted to a string based on {@link Object#toString()} + * or the toString implementation of that object. * * @param path the path to get the string list * @param def the default string list to return if the string list could not be obtained diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/MappedConfiguration.java b/api/src/main/java/com/github/siroshun09/configapi/api/MappedConfiguration.java index 8b9570b1..70ed35e3 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/MappedConfiguration.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/MappedConfiguration.java @@ -171,13 +171,6 @@ public void set(@NotNull String path, @Nullable Object value) { } } - @Override - public @NotNull @Unmodifiable Set getPaths() { - return map.keySet().stream() - .map(object -> object instanceof String ? (String) object : object.toString()) - .collect(Collectors.toUnmodifiableSet()); - } - @Override public @NotNull @Unmodifiable List getKeyList() { return map.keySet().stream() diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/file/PropertiesConfiguration.java b/api/src/main/java/com/github/siroshun09/configapi/api/file/PropertiesConfiguration.java index e93cd796..24eb1c45 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/file/PropertiesConfiguration.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/file/PropertiesConfiguration.java @@ -79,14 +79,6 @@ public void set(@NotNull String path, @Nullable Object value) { } } - @Override - public @NotNull @Unmodifiable Set getPaths() { - return properties.keySet() - .stream() - .map(object -> object instanceof String ? (String) object : object.toString()) - .collect(Collectors.toUnmodifiableSet()); - } - @Override public @NotNull @Unmodifiable List getKeyList() { return properties.keySet() diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/serializer/ConfigurationSerializer.java b/api/src/main/java/com/github/siroshun09/configapi/api/serializer/ConfigurationSerializer.java index 9b6df084..e84df22a 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/serializer/ConfigurationSerializer.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/serializer/ConfigurationSerializer.java @@ -50,7 +50,7 @@ public interface ConfigurationSerializer extends Serializer * Deserializes {@link Configuration} * * @param config the config to deserialize - * @return the deserialized value or {@code null} if could not deserialize + * @return the deserialized value or {@code null} if configuration could not be deserialized */ @Nullable T deserializeConfiguration(@NotNull Configuration config); } diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/serializer/StringSerializer.java b/api/src/main/java/com/github/siroshun09/configapi/api/serializer/StringSerializer.java index e0fa3be3..508f5292 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/serializer/StringSerializer.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/serializer/StringSerializer.java @@ -39,7 +39,7 @@ public interface StringSerializer extends Serializer { * Deserializes the {@link String}. * * @param source the string to deserialize - * @return the deserialized value or {@code null} if could not deserialize + * @return the deserialized value or {@code null} if string could not be deserialized */ @Nullable T deserializeString(@NotNull String source); } diff --git a/api/src/main/java/com/github/siroshun09/configapi/api/util/ResourceUtils.java b/api/src/main/java/com/github/siroshun09/configapi/api/util/ResourceUtils.java index a9d14cee..f3c8f72e 100644 --- a/api/src/main/java/com/github/siroshun09/configapi/api/util/ResourceUtils.java +++ b/api/src/main/java/com/github/siroshun09/configapi/api/util/ResourceUtils.java @@ -45,11 +45,7 @@ public static void copyFromClassLoader(@NotNull ClassLoader loader, Objects.requireNonNull(name); Objects.requireNonNull(target); - try (InputStream input = loader.getResourceAsStream(name)) { - if (input != null) { - Files.copy(input, target); - } - } + copy(() -> loader.getResourceAsStream(name), target); } /** @@ -87,12 +83,8 @@ public static void copyFromJar(@NotNull JarFile jar, var file = jar.getEntry(name); - if (file == null) { - return; - } - - try (var input = jar.getInputStream(file)) { - Files.copy(input, target); + if (file != null) { + copy(() -> jar.getInputStream(file), target); } } @@ -153,6 +145,27 @@ public static void copyFromJarIfNotExists(@NotNull Path jarPath, } } + private static void copy(@NotNull IOSupplier inputSupplier, @NotNull Path target) throws IOException { + var parent = target.getParent(); + + if (parent != null) { + FileUtils.createDirectoriesIfNotExists(parent); + } + + try (var input = inputSupplier.get()) { + if (input != null) { + Files.copy(input, target); + } + } + } + + @FunctionalInterface + private interface IOSupplier { + + T get() throws IOException; + + } + private ResourceUtils() { throw new UnsupportedOperationException(); } diff --git a/api/src/test/java/com/github/siroshun09/configapi/api/test/AbstractConfigurationTest.java b/api/src/test/java/com/github/siroshun09/configapi/api/test/AbstractConfigurationTest.java new file mode 100644 index 00000000..37e9622d --- /dev/null +++ b/api/src/test/java/com/github/siroshun09/configapi/api/test/AbstractConfigurationTest.java @@ -0,0 +1,444 @@ +/* + * Copyright 2021 Siroshun09 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.siroshun09.configapi.api.test; + +import com.github.siroshun09.configapi.api.Configuration; +import com.github.siroshun09.configapi.api.MappedConfiguration; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.Collections; +import java.util.List; + +public class AbstractConfigurationTest { + + @Test + void testDefaultValue() { + var config = newConfiguration(); + + var def = new Object(); + Assertions.assertSame(def, config.get("test", def)); + } + + @Test + void testList() { + var config = newConfiguration(); + + var list = new ArrayList<>(); + config.set("test-list", list); + + Assertions.assertSame(list, config.getList("test-list")); + Assertions.assertSame(Collections.emptyList(), config.getList("test")); + } + + @Test + void testBoolean() { + var config = newConfiguration(); + + config.set("test", true); + Assertions.assertTrue(config.getBoolean("test")); + + config.set("test-string", "true"); + Assertions.assertFalse(config.getBoolean("test-string")); + Assertions.assertTrue(config.getBoolean("test-string", true)); + } + + @Test + void testByte() { + var config = newConfiguration(); + + config.set("test-1", 5); + config.set("test-2", -5); + + Assertions.assertEquals(5, config.getByte("test-1")); + Assertions.assertEquals(-5, config.getByte("test-2")); + + config.set("test-3", 300); + Assertions.assertNotEquals(300, config.getByte("test-3")); + + config.set("test-min", Byte.MIN_VALUE); + config.set("test-max", Byte.MAX_VALUE); + + Assertions.assertEquals(Byte.MIN_VALUE, config.getByte("test-min")); + Assertions.assertEquals(Byte.MAX_VALUE, config.getByte("test-max")); + + Assertions.assertEquals(0, config.getByte("empty")); + Assertions.assertEquals(100, config.getByte("empty", (byte) 100)); + } + + @Test + void testDouble() { + var config = newConfiguration(); + + config.set("test-1", 5); + config.set("test-2", -5); + + Assertions.assertEquals(5, config.getDouble("test-1")); + Assertions.assertEquals(-5, config.getDouble("test-2")); + + config.set("test-min", Double.MIN_VALUE); + config.set("test-max", Double.MAX_VALUE); + + Assertions.assertEquals(Double.MIN_VALUE, config.getDouble("test-min")); + Assertions.assertEquals(Double.MAX_VALUE, config.getDouble("test-max")); + + Assertions.assertEquals(0, config.getDouble("empty")); + Assertions.assertEquals(100, config.getDouble("empty", 100)); + } + + @Test + void testFloat() { + var config = newConfiguration(); + + config.set("test-1", 5); + config.set("test-2", -5); + + Assertions.assertEquals(5, config.getFloat("test-1")); + Assertions.assertEquals(-5, config.getFloat("test-2")); + + config.set("test-3", Double.MAX_VALUE); + Assertions.assertNotEquals(Double.MAX_VALUE, config.getFloat("test-3")); + + config.set("test-min", Float.MIN_VALUE); + config.set("test-max", Float.MAX_VALUE); + + Assertions.assertEquals(Float.MIN_VALUE, config.getFloat("test-min")); + Assertions.assertEquals(Float.MAX_VALUE, config.getFloat("test-max")); + + Assertions.assertEquals(0, config.getFloat("empty")); + Assertions.assertEquals(100, config.getFloat("empty", 100)); + } + + @Test + void testInteger() { + var config = newConfiguration(); + + config.set("test-1", 5); + config.set("test-2", -5); + + Assertions.assertEquals(5, config.getInteger("test-1")); + Assertions.assertEquals(-5, config.getInteger("test-2")); + + config.set("test-3", Long.MAX_VALUE); + Assertions.assertNotEquals(Long.MAX_VALUE, config.getInteger("test-3")); + + config.set("test-min", Integer.MIN_VALUE); + config.set("test-max", Integer.MAX_VALUE); + + Assertions.assertEquals(Integer.MIN_VALUE, config.getInteger("test-min")); + Assertions.assertEquals(Integer.MAX_VALUE, config.getInteger("test-max")); + + Assertions.assertEquals(0, config.getInteger("empty")); + Assertions.assertEquals(100, config.getInteger("empty", 100)); + } + + @Test + void testLong() { + var config = newConfiguration(); + + config.set("test-1", 5); + config.set("test-2", -5); + + Assertions.assertEquals(5, config.getLong("test-1")); + Assertions.assertEquals(-5, config.getLong("test-2")); + + config.set("test-3", Double.MAX_VALUE); + Assertions.assertNotEquals(Double.MAX_VALUE, config.getLong("test-3")); + + config.set("test-min", Long.MIN_VALUE); + config.set("test-max", Long.MAX_VALUE); + + Assertions.assertEquals(Long.MIN_VALUE, config.getLong("test-min")); + Assertions.assertEquals(Long.MAX_VALUE, config.getLong("test-max")); + + Assertions.assertEquals(0, config.getLong("empty")); + Assertions.assertEquals(100, config.getLong("empty", 100)); + } + + @Test + void testShort() { + var config = newConfiguration(); + + config.set("test-1", 5); + config.set("test-2", -5); + + Assertions.assertEquals(5, config.getShort("test-1")); + Assertions.assertEquals(-5, config.getShort("test-2")); + + config.set("test-3", Double.MAX_VALUE); + Assertions.assertNotEquals(Double.MAX_VALUE, config.getShort("test-3")); + + config.set("test-min", Short.MIN_VALUE); + config.set("test-max", Short.MAX_VALUE); + + Assertions.assertEquals(Short.MIN_VALUE, config.getShort("test-min")); + Assertions.assertEquals(Short.MAX_VALUE, config.getShort("test-max")); + + Assertions.assertEquals(0, config.getShort("empty")); + Assertions.assertEquals(100, config.getShort("empty", (short) 100)); + } + + @Test + void testString() { + var config = newConfiguration(); + + config.set("test-1", "test"); + Assertions.assertEquals("test", config.getString("test-1")); + + config.set("test-2", 100); + Assertions.assertEquals("100", config.getString("test-2")); + + Assertions.assertEquals("", config.getString("empty")); + Assertions.assertEquals("100", config.getString("empty", "100")); + } + + @Test + void testBooleanTest() { + var list = List.of(new Object(), true, false, Boolean.TRUE, Boolean.FALSE, 100, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getBooleanList("test"); + + Assertions.assertEquals(4, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Boolean.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getBooleanList("empty")); + + var def = List.of(true, false, true, false); + Assertions.assertSame(def, config.getBooleanList("empty", def)); + } + + @Test + void testByteList() { + var list = List.of(new Object(), 5, -5, 300, true, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getByteList("test"); + + Assertions.assertEquals(3, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Byte.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getByteList("empty")); + + var def = List.of((byte) 5, (byte) -5); + Assertions.assertSame(def, config.getByteList("empty", def)); + } + + @Test + void testDoubleList() { + var list = List.of(new Object(), 5.5, -5.5, 300, true, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getDoubleList("test"); + + Assertions.assertEquals(3, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Double.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getDoubleList("empty")); + + var def = List.of(5.5, -5.5); + Assertions.assertSame(def, config.getDoubleList("empty", def)); + } + + @Test + void testFloatList() { + var list = List.of(new Object(), 5.5, -5.5, 300, true, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getFloatList("test"); + + Assertions.assertEquals(3, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Float.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getFloatList("empty")); + + var def = List.of(5.5f, -5.5f); + Assertions.assertSame(def, config.getFloatList("empty", def)); + } + + @Test + void testIntegerList() { + var list = List.of(new Object(), 5, -5, Long.MAX_VALUE, 5.5, true, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getIntegerList("test"); + + Assertions.assertEquals(4, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Integer.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getIntegerList("empty")); + + var def = List.of(5, -5); + Assertions.assertSame(def, config.getIntegerList("empty", def)); + } + + @Test + void testLongList() { + var list = List.of(new Object(), 5, -5, Long.MAX_VALUE, 5.5, true, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getLongList("test"); + + Assertions.assertEquals(4, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Long.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getLongList("empty")); + + var def = List.of(5L, -5L); + Assertions.assertSame(def, config.getLongList("empty", def)); + } + + @Test + void testShortList() { + var list = List.of(new Object(), 5, -5, Long.MAX_VALUE, 5.5, true, "test"); + + var config = newConfiguration(); + config.set("test", list); + + var actual = config.getShortList("test"); + + Assertions.assertEquals(4, actual.size()); + + for (Object object : actual) { + Assertions.assertEquals(Short.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getShortList("empty")); + + var def = List.of((short) 5, (short) -5); + Assertions.assertSame(def, config.getShortList("empty", def)); + } + + @Test + void testStringList() { + var list = List.of(new Object(), "test", "", 100, true); + + var config = newConfiguration(); + config.set("test-1", list); + + var actual1 = config.getStringList("test-1"); + + Assertions.assertEquals(5, actual1.size()); + + for (Object object : actual1) { + Assertions.assertEquals(String.class, object.getClass()); + } + + var includeNull = new ArrayList<>(); + includeNull.add("test"); + includeNull.add(null); + includeNull.add(100); + includeNull.add(true); + + config.set("test-2", includeNull); + + var actual2 = config.getStringList("test-2"); + + Assertions.assertEquals(3, actual2.size()); + + for (Object object : actual2) { + Assertions.assertEquals(String.class, object.getClass()); + } + + Assertions.assertSame(Collections.emptyList(), config.getShortList("empty")); + + var def = List.of("test", "test"); + Assertions.assertSame(def, config.getStringList("empty", def)); + } + + @Test + void testBytes() { + byte[] bytes = {-3, -1, 1, 3}; + + var config = newConfiguration(); + config.setBytes("test", bytes); + + var actual = config.getBytes("test"); + + Assertions.assertEquals(4, actual.length); + + for (int i = 0; i < actual.length; i++) { + Assertions.assertEquals(bytes[i], actual[i]); + } + + var encoded = Base64.getEncoder().encodeToString(bytes); + Assertions.assertEquals(encoded, config.getString("test")); + + Assertions.assertEquals(0, config.getBytes("empty").length); + + config.set("invalid", "*test*"); + Assertions.assertEquals(0, config.getBytes("invalid").length); + } + + @SuppressWarnings("ConstantConditions") + @Test + void testIllegalArguments() { + var config = newConfiguration(); + + Assertions.assertTrue(testArgument(() -> config.get("test", (Object) null))); + Assertions.assertTrue(testArgument(() -> config.getList(null))); + Assertions.assertTrue(testArgument(() -> config.getList("test", (List) null))); + } + + @Contract(" -> new") + private static @NotNull Configuration newConfiguration() { + return MappedConfiguration.create(); + } + + private static boolean testArgument(@NotNull Runnable runnable) { + try { + runnable.run(); + return false; + } catch (Throwable throwable) { + return throwable instanceof NullPointerException || throwable instanceof IllegalArgumentException; + } + } +} diff --git a/api/src/test/java/com/github/siroshun09/configapi/api/test/MappedConfigurationTest.java b/api/src/test/java/com/github/siroshun09/configapi/api/test/MappedConfigurationTest.java index 199ce7e0..e0d9bf27 100644 --- a/api/src/test/java/com/github/siroshun09/configapi/api/test/MappedConfigurationTest.java +++ b/api/src/test/java/com/github/siroshun09/configapi/api/test/MappedConfigurationTest.java @@ -79,11 +79,6 @@ void testSettingConfiguration() { public void set(@NotNull String path, @Nullable Object value) { } - @Override - public @NotNull @Unmodifiable Set getPaths() { - return Set.of("key", "map"); - } - @Override public @NotNull @Unmodifiable List getKeyList() { return List.of("key", "map"); diff --git a/api/src/test/java/com/github/siroshun09/configapi/api/test/util/ResourceUtilsTest.java b/api/src/test/java/com/github/siroshun09/configapi/api/test/util/ResourceUtilsTest.java index 621d5dd1..8971a3d6 100644 --- a/api/src/test/java/com/github/siroshun09/configapi/api/test/util/ResourceUtilsTest.java +++ b/api/src/test/java/com/github/siroshun09/configapi/api/test/util/ResourceUtilsTest.java @@ -30,29 +30,26 @@ public class ResourceUtilsTest { private static final Path TEXT_PATH = Path.of("test.txt"); @Test - void testCopyingFromClassLoader() throws IOException { + void testResourceCopy() throws IOException { if (Files.exists(JAR_PATH)) { Files.delete(JAR_PATH); } - ResourceUtils.copyFromClassLoader(getClass().getClassLoader(), "example.jar", JAR_PATH); - - Assertions.assertTrue(Files.exists(JAR_PATH)); - - Files.delete(JAR_PATH); - } - - @Test - void testCopyingFromJar() throws IOException { if (Files.exists(TEXT_PATH)) { Files.delete(TEXT_PATH); } - ResourceUtils.copyFromClassLoaderIfNotExists(getClass().getClassLoader(), "example.jar", JAR_PATH); + ResourceUtils.copyFromClassLoader(getClass().getClassLoader(), "example.jar", JAR_PATH); + + Assertions.assertTrue(Files.exists(JAR_PATH)); ResourceUtils.copyFromJar(JAR_PATH, "test.txt", TEXT_PATH); Assertions.assertTrue(Files.exists(TEXT_PATH)); - Files.delete(TEXT_PATH); + try { + Files.delete(JAR_PATH); + Files.delete(TEXT_PATH); + } catch (Exception ignored) { + } } } diff --git a/pom.xml b/pom.xml index a573283f..5fa1034c 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ com.github.siroshun09.configapi parent - 4.3.1 + 4.4.0 pom ConfigAPI diff --git a/yaml/pom.xml b/yaml/pom.xml index 1af033f4..aa5cc278 100644 --- a/yaml/pom.xml +++ b/yaml/pom.xml @@ -21,7 +21,7 @@ com.github.siroshun09.configapi parent - 4.3.1 + 4.4.0 4.0.0 @@ -31,7 +31,7 @@ com.github.siroshun09.configapi configapi - 4.3.1 + 4.4.0 compile diff --git a/yaml/src/main/java/com/github/siroshun09/configapi/yaml/YamlConfiguration.java b/yaml/src/main/java/com/github/siroshun09/configapi/yaml/YamlConfiguration.java index 361769db..252f160f 100644 --- a/yaml/src/main/java/com/github/siroshun09/configapi/yaml/YamlConfiguration.java +++ b/yaml/src/main/java/com/github/siroshun09/configapi/yaml/YamlConfiguration.java @@ -133,11 +133,6 @@ public void set(@NotNull String path, @Nullable Object value) { config.set(path, value); } - @Override - public @NotNull @Unmodifiable Set getPaths() { - return config != null ? config.getPaths() : Collections.emptySet(); - } - @Override public @NotNull @Unmodifiable List getKeyList() { return config != null ? config.getKeyList() : Collections.emptyList();