adjust unit tests

This commit is contained in:
Armin Schrenk
2026-01-21 15:34:08 +01:00
parent 046372f95b
commit 7ee0606306
2 changed files with 32 additions and 9 deletions
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -27,7 +28,10 @@ public class AdminPropertiesSetterTest {
@DisplayName("UTF-8 is supported")
void loadUTF8Properties(@TempDir Path path) throws IOException {
var config = path.resolve("config.properties");
setupValidProperties(config);
try (var out = Files.newOutputStream(config, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
var bytes = PROPS.getBytes(StandardCharsets.UTF_8);
out.write(bytes);
}
var properties = AdminPropertiesSetter.loadAdminProperties(config);
Assertions.assertAll(List.of( //
@@ -37,7 +41,7 @@ public class AdminPropertiesSetterTest {
}
@Test
@DisplayName("Loading not existing file")
@DisplayName("Loading not existing file returns empty properties")
void loadNotExistingFile(@TempDir Path path) {
var config = path.resolve("config.properties");
var properties = AdminPropertiesSetter.loadAdminProperties(config);
@@ -45,15 +49,29 @@ public class AdminPropertiesSetterTest {
}
@Test
@DisplayName("Loading invalid properties file")
@DisplayName("Loading invalid file returns empty properties")
void loadEmptyFile(@TempDir Path path) throws IOException {
}
void setupValidProperties(Path p) throws IOException {
try (var out = Files.newOutputStream(p, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
var bytes = PROPS.getBytes(StandardCharsets.UTF_8);
var config = path.resolve("config.properties");
try (var out = Files.newOutputStream(config, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
var bytes = "method=\\u2u20".getBytes(StandardCharsets.UTF_8); //only one "u" is allowed in a Unicode escape sequence
out.write(bytes);
}
var properties = AdminPropertiesSetter.loadAdminProperties(config);
MatcherAssert.assertThat(properties, anEmptyMap());
}
@Test
@DisplayName("Loading too big file returns empty properties")
void loadTooBigFile(@TempDir Path path) throws IOException {
var config = path.resolve("config.properties");
try (var channel = Files.newByteChannel(config, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
channel.position(10_000);
channel.write(ByteBuffer.wrap("test=test".getBytes()));
}
var properties = AdminPropertiesSetter.loadAdminProperties(config);
MatcherAssert.assertThat(properties, anEmptyMap());
}
}