Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window Test Support #25

Merged
merged 6 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/core-config.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
testFixturesApi("org.mockito:mockito-inline:4.7.0")
testFixturesApi("org.mockito:mockito-junit-jupiter:4.7.0")
testFixturesApi("org.hamcrest:hamcrest-all:1.3")
testFixturesApi("com.google.jimfs:jimfs:1.2")
testFixturesApi("com.google.jimfs:jimfs:1.3.0")
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.nio.file.Path;
import java.util.Map;

import static de.exlll.configlib.TestUtils.createPlatformSpecificFilePath;
import static de.exlll.configlib.configurations.ExampleConfigurationsSerialized.*;
import static de.exlll.configlib.configurations.ExampleEqualityAsserter.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -30,7 +31,7 @@ class ExampleConfigurationTests {
.build();

private final FileSystem fs = Jimfs.newFileSystem();
private final Path yamlFile = fs.getPath("/tmp/config.yml");
private final Path yamlFile = fs.getPath(createPlatformSpecificFilePath("/tmp/config.yml"));

@BeforeEach
void setUp() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
import static org.hamcrest.Matchers.*;

class SerializersTest {

private final String TMP_CONFIG_PATH = createPlatformSpecificFilePath("/tmp/config.yml");
private final String TMP_WITH_UNDERSCORE_PATH = createPlatformSpecificFilePath("/tmp/with_underscore.yml");
private final String TMP_PATH = createPlatformSpecificFilePath("/tmp");

@Test
void booleanSerializer() {
Serializer<Boolean, Boolean> serializer = new Serializers.BooleanSerializer();
Expand Down Expand Up @@ -461,42 +466,34 @@ void uuidSerializer() {
void fileSerializer() {
Serializer<File, String> serializer = new Serializers.FileSerializer();

String path1 = "/tmp/config.yml";
String path2 = "/tmp/with \n new \n lines.yml";
String path3 = "/tmp";
File file1 = new File(TMP_CONFIG_PATH);
File file2 = new File(TMP_WITH_UNDERSCORE_PATH);
File file3 = new File(TMP_PATH);

File file1 = new File(path1);
File file2 = new File(path2);
File file3 = new File(path3);
assertThat(serializer.serialize(file1), is(TMP_CONFIG_PATH));
assertThat(serializer.serialize(file2), is(TMP_WITH_UNDERSCORE_PATH));
assertThat(serializer.serialize(file3), is(TMP_PATH));

assertThat(serializer.serialize(file1), is(path1));
assertThat(serializer.serialize(file2), is(path2));
assertThat(serializer.serialize(file3), is(path3));

assertThat(serializer.deserialize(path1), is(file1));
assertThat(serializer.deserialize(path2), is(file2));
assertThat(serializer.deserialize(path3), is(file3));
assertThat(serializer.deserialize(TMP_CONFIG_PATH), is(file1));
assertThat(serializer.deserialize(TMP_WITH_UNDERSCORE_PATH), is(file2));
assertThat(serializer.deserialize(TMP_PATH), is(file3));
}

@Test
void pathSerializer() {
Serializer<Path, String> serializer = new Serializers.PathSerializer();

String path1 = "/tmp/config.yml";
String path2 = "/tmp/with \n new \n lines.yml";
String path3 = "/tmp";

Path file1 = Path.of(path1);
Path file2 = Path.of(path2);
Path file3 = Path.of(path3);
Path file1 = Path.of(TMP_CONFIG_PATH);
Path file2 = Path.of(TMP_WITH_UNDERSCORE_PATH);
Path file3 = Path.of(TMP_PATH);

assertThat(serializer.serialize(file1), is(path1));
assertThat(serializer.serialize(file2), is(path2));
assertThat(serializer.serialize(file3), is(path3));
assertThat(serializer.serialize(file1), is(TMP_CONFIG_PATH));
assertThat(serializer.serialize(file2), is(TMP_WITH_UNDERSCORE_PATH));
assertThat(serializer.serialize(file3), is(TMP_PATH));

assertThat(serializer.deserialize(path1), is(file1));
assertThat(serializer.deserialize(path2), is(file2));
assertThat(serializer.deserialize(path3), is(file3));
assertThat(serializer.deserialize(TMP_CONFIG_PATH), is(file1));
assertThat(serializer.deserialize(TMP_WITH_UNDERSCORE_PATH), is(file2));
assertThat(serializer.deserialize(TMP_PATH), is(file3));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.function.Executable;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigInteger;
Expand Down Expand Up @@ -289,4 +290,26 @@ public static ConfigurationElement<?> fieldAsElement(Class<?> type, String field
Field field = getField(type, fieldName);
return new ConfigurationElements.FieldElement(field);
}
}

/*
There were absolute path errors when trying to pass the unit tests
on different platforms like Windows. Currently, Jimfs(1.3.0) lacks support
for both absolutes paths and relative paths on Windows, see:
- https://github.com/google/jimfs/issues/69
- https://github.com/google/jimfs/blob/master/jimfs/src/main/java/com/google/common/jimfs/WindowsPathType.java

So, in order to run unit tests on Windows. We have to translate the current
path declarations to fulfill the non-unix system's needs.
*/
public static String createPlatformSpecificFilePath(String path) {
final String platform = System.getProperty("os.name");

if (!platform.contains("Windows")) return path;

return String.format("C:%s", path.replace("/", File.separator));
}

public static List<String> createListOfPlatformSpecificFilePaths(String... paths) {
return Stream.of(paths).map(TestUtils::createPlatformSpecificFilePath).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public final class ExampleConfigurationsSerialized {
entry("a1_localDateTime", "2000-01-01T00:00"),
entry("a1_instant", "0000-01-01T00:00:00Z"),
entry("a1_uuid", "d50f3bdd-ac66-4b74-a01f-4617b24d68c0"),
entry("a1_file", "/tmp"),
entry("a1_path", "/tmp"),
entry("a1_file", createPlatformSpecificFilePath("/tmp")),
entry("a1_path", createPlatformSpecificFilePath("/tmp")),
entry("a1_url", "https://example.com"),
entry("a1_uri", "https://example.com"),
entry("a1_Enm", "A"),
Expand All @@ -146,8 +146,8 @@ public final class ExampleConfigurationsSerialized {
entry("a1_listLocalDateTime", List.of("2000-01-01T00:00", "2000-01-02T00:00", "2000-01-03T00:00")),
entry("a1_listInstant", List.of("0000-01-01T00:00:00Z", "0001-01-01T00:00:00Z", "-1000000000-01-01T00:00:00Z")),
entry("a1_listUuid", List.of("d50f3bdd-ac66-4b74-a01f-4617b24d68c0", "d50f3bdd-ac66-4b74-a01f-4617b24d68c1", "d50f3bdd-ac66-4b74-a01f-4617b24d68c2")),
entry("a1_listFile", List.of("/tmp", "/tmp/config.yml", "/tmp/with \n new \n lines.yml")),
entry("a1_listPath", List.of("/tmp", "/tmp/config.yml", "/tmp/with \n new \n lines.yml")),
entry("a1_listFile", createListOfPlatformSpecificFilePaths("/tmp", "/tmp/config.yml", "/tmp/with_underscore.yml")),
entry("a1_listPath", createListOfPlatformSpecificFilePaths("/tmp", "/tmp/config.yml", "/tmp/with_underscore.yml")),
entry("a1_listUrl", List.of("https://example.com", "https://example.com?query=yes", "https://example.com?query=yes#fragment=true")),
entry("a1_listUri", List.of("https://example.com", "https://example.com?query=yes", "https://example.com?query=yes#fragment=true")),
entry("a1_listEnm", List.of("A", "B", "C")),
Expand Down Expand Up @@ -299,8 +299,8 @@ public final class ExampleConfigurationsSerialized {
entry("a2_localDateTime", "2000-01-02T00:00"),
entry("a2_instant", "0001-01-01T00:00:00Z"),
entry("a2_uuid", "d50f3bdd-ac66-4b74-a01f-4617b24d68c1"),
entry("a2_file", "/tmp/config.yml"),
entry("a2_path", "/tmp/config.yml"),
entry("a2_file", createPlatformSpecificFilePath("/tmp/config.yml")),
entry("a2_path", createPlatformSpecificFilePath("/tmp/config.yml")),
entry("a2_url", "https://example.com?query=yes"),
entry("a2_uri", "https://example.com?query=yes"),
entry("a2_Enm", "B"),
Expand All @@ -324,8 +324,8 @@ public final class ExampleConfigurationsSerialized {
entry("a2_listLocalDateTime", List.of("2000-01-02T00:00", "2000-01-03T00:00", "2000-01-04T00:00")),
entry("a2_listInstant", List.of("0001-01-01T00:00:00Z", "-1000000000-01-01T00:00:00Z", "+1000000000-12-31T23:59:59.999999999Z")),
entry("a2_listUuid", List.of("d50f3bdd-ac66-4b74-a01f-4617b24d68c1", "d50f3bdd-ac66-4b74-a01f-4617b24d68c2", "d50f3bdd-ac66-4b74-a01f-4617b24d68c3")),
entry("a2_listFile", List.of("/tmp/config.yml", "/tmp/with \n new \n lines.yml", "with \n new \n lines.yml")),
entry("a2_listPath", List.of("/tmp/config.yml", "/tmp/with \n new \n lines.yml", "with \n new \n lines.yml")),
entry("a2_listFile", createListOfPlatformSpecificFilePaths("/tmp/config.yml", "/tmp/with_underscore.yml", "with_underscore.yml")),
entry("a2_listPath", createListOfPlatformSpecificFilePaths("/tmp/config.yml", "/tmp/with_underscore.yml", "with_underscore.yml")),
entry("a2_listUrl", List.of("https://example.com?query=yes", "https://example.com?query=yes#fragment=true", "https://example.com#fragment=true")),
entry("a2_listUri", List.of("https://example.com?query=yes", "https://example.com?query=yes#fragment=true", "https://example.com#fragment=true")),
entry("a2_listEnm", List.of("B", "C", "D")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public final class ExampleInitializer {
private static final UUID UUID_4 = UUID.fromString("d50f3bdd-ac66-4b74-a01f-4617b24d68c3");
private static final UUID UUID_5 = UUID.fromString("d50f3bdd-ac66-4b74-a01f-4617b24d68c4");

private static final File FILE_1 = new File("/tmp");
private static final File FILE_2 = new File("/tmp/config.yml");
private static final File FILE_3 = new File("/tmp/with \n new \n lines.yml");
private static final File FILE_4 = new File("with \n new \n lines.yml");

private static final Path PATH_1 = Path.of("/tmp");
private static final Path PATH_2 = Path.of("/tmp/config.yml");
private static final Path PATH_3 = Path.of("/tmp/with \n new \n lines.yml");
private static final Path PATH_4 = Path.of("with \n new \n lines.yml");
private static final File FILE_1 = new File(createPlatformSpecificFilePath("/tmp"));
private static final File FILE_2 = new File(createPlatformSpecificFilePath("/tmp/config.yml"));
private static final File FILE_3 = new File(createPlatformSpecificFilePath("/tmp/with_underscore.yml"));
private static final File FILE_4 = new File(createPlatformSpecificFilePath("with_underscore.yml"));

private static final Path PATH_1 = Path.of(createPlatformSpecificFilePath("/tmp"));
private static final Path PATH_2 = Path.of(createPlatformSpecificFilePath("/tmp/config.yml"));
private static final Path PATH_3 = Path.of(createPlatformSpecificFilePath("/tmp/with_underscore.yml"));
private static final Path PATH_4 = Path.of(createPlatformSpecificFilePath("with_underscore.yml"));

private static final URL URL_1 = createUrl("https://example.com");
private static final URL URL_2 = createUrl("https://example.com?query=yes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import java.nio.file.Files;
import java.nio.file.Path;

import static de.exlll.configlib.TestUtils.*;
import static de.exlll.configlib.configurations.ExampleEqualityAsserter.*;

final class ExampleConfigurationYamlTests {
private final FileSystem fs = Jimfs.newFileSystem();
private final Path yamlFile = fs.getPath("/tmp/config.yml");
private final Path yamlFile = fs.getPath(createPlatformSpecificFilePath("/tmp/config.yml"));

@BeforeEach
void setUp() throws IOException {
Expand All @@ -34,7 +35,7 @@ void tearDown() throws IOException {
@Test
void yamlStoreSavesAndLoadsExampleConfigurationA2() {
var properties = YamlConfigurationProperties.newBuilder()
.addSerializer(Point.class, TestUtils.POINT_SERIALIZER)
.addSerializer(Point.class, POINT_SERIALIZER)
.build();
var store = new YamlConfigurationStore<>(ExampleConfigurationA2.class, properties);
ExampleConfigurationA2 cfg1 = ExampleInitializer.newExampleConfigurationA2();
Expand All @@ -46,7 +47,7 @@ void yamlStoreSavesAndLoadsExampleConfigurationA2() {
@Test
void yamlStoreSavesAndLoadsExampleConfigurationNullsWithNullCollectionElements1() {
var properties = YamlConfigurationProperties.newBuilder()
.addSerializer(Point.class, TestUtils.POINT_SERIALIZER)
.addSerializer(Point.class, POINT_SERIALIZER)
.outputNulls(true)
.inputNulls(true)
.build();
Expand All @@ -61,7 +62,7 @@ void yamlStoreSavesAndLoadsExampleConfigurationNullsWithNullCollectionElements1(
@Test
void yamlStoreSavesAndLoadsExampleConfigurationNullsWithoutNullCollectionElements1() {
var properties = YamlConfigurationProperties.newBuilder()
.addSerializer(Point.class, TestUtils.POINT_SERIALIZER)
.addSerializer(Point.class, POINT_SERIALIZER)
.build();
var store = new YamlConfigurationStore<>(ExampleConfigurationNulls.class, properties);
ExampleConfigurationNulls cfg1 = ExampleInitializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

class YamlConfigurationStoreTest {
private final FileSystem fs = Jimfs.newFileSystem();
private final Path yamlFile = fs.getPath("/tmp/config.yml");

private final String yamlFilePath = createPlatformSpecificFilePath("/tmp/config.yml");
private final Path yamlFile = fs.getPath(yamlFilePath);

private final String abcFilePath = createPlatformSpecificFilePath("/a/b/c.yml");

@BeforeEach
void setUp() throws IOException {
Expand Down Expand Up @@ -93,7 +97,7 @@ void save() {
# The
# Footer\
""";
assertEquals(expected, TestUtils.readFile(yamlFile));
assertEquals(expected, readFile(yamlFile));
}

@Test
Expand All @@ -120,7 +124,7 @@ record R(String s, @Comment("A comment") Integer i) {}
# The
# Footer\
""";
assertEquals(expected, TestUtils.readFile(yamlFile));
assertEquals(expected, readFile(yamlFile));
}

@Configuration
Expand Down Expand Up @@ -208,7 +212,7 @@ void loadInvalidYaml() throws IOException {

assertThrowsConfigurationException(
() -> store.load(yamlFile),
"The configuration file at /tmp/config.yml does not contain valid YAML."
String.format("The configuration file at %s does not contain valid YAML.", yamlFilePath)
);
}

Expand All @@ -220,7 +224,7 @@ void loadEmptyYaml() throws IOException {

assertThrowsConfigurationException(
() -> store.load(yamlFile),
"The configuration file at /tmp/config.yml is empty or only contains null."
String.format("The configuration file at %s is empty or only contains null.", yamlFilePath)
);
}

Expand All @@ -232,9 +236,9 @@ void loadNonMapYaml() throws IOException {

assertThrowsConfigurationException(
() -> store.load(yamlFile),
"The contents of the YAML file at /tmp/config.yml do not represent a " +
String.format("The contents of the YAML file at %s do not represent a " +
"configuration. A valid configuration file contains a YAML map but instead a " +
"'class java.lang.String' was found."
"'class java.lang.String' was found.", yamlFilePath)
);
}

Expand All @@ -246,7 +250,7 @@ static final class D {
@Test
void saveConfigurationWithInvalidTargetType() {
YamlConfigurationProperties properties = YamlConfigurationProperties.newBuilder()
.addSerializer(Point.class, TestUtils.POINT_IDENTITY_SERIALIZER)
.addSerializer(Point.class, POINT_IDENTITY_SERIALIZER)
.build();
YamlConfigurationStore<D> store = new YamlConfigurationStore<>(D.class, properties);

Expand All @@ -261,7 +265,7 @@ void saveConfigurationWithInvalidTargetType() {
void saveCreatesParentDirectoriesIfPropertyTrue() {
YamlConfigurationStore<A> store = newDefaultStore(A.class);

Path file = fs.getPath("/a/b/c.yml");
Path file = fs.getPath(abcFilePath);
store.save(new A(), file);

assertTrue(Files.exists(file.getParent()));
Expand All @@ -275,10 +279,10 @@ void saveDoesNotCreateParentDirectoriesIfPropertyFalse() {
.build();
YamlConfigurationStore<A> store = new YamlConfigurationStore<>(A.class, properties);

Path file = fs.getPath("/a/b/c.yml");
Path file = fs.getPath(abcFilePath);
assertThrowsRuntimeException(
() -> store.save(new A(), file),
"java.nio.file.NoSuchFileException: /a/b/c.yml"
String.format("java.nio.file.NoSuchFileException: %s", abcFilePath)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import java.nio.file.Files;
import java.nio.file.Path;

import static de.exlll.configlib.TestUtils.createPlatformSpecificFilePath;
import static org.junit.jupiter.api.Assertions.assertEquals;

class YamlConfigurationsTest {
private static final FieldFilter includeI = field -> field.getName().equals("i");
private final FileSystem fs = Jimfs.newFileSystem();
private final Path yamlFile = fs.getPath("/tmp/config.yml");
private final Path yamlFile = fs.getPath(createPlatformSpecificFilePath("/tmp/config.yml"));

@BeforeEach
void setUp() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import java.util.Queue;
import java.util.function.Consumer;

import static de.exlll.configlib.TestUtils.createPlatformSpecificFilePath;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SuppressWarnings("unused")
class YamlFileWriterTest {
private final FileSystem fs = Jimfs.newFileSystem();
private final Path yamlFile = fs.getPath("/tmp/config.yml");
private final Path yamlFile = fs.getPath(createPlatformSpecificFilePath("/tmp/config.yml"));

@BeforeEach
void setUp() throws IOException {
Expand Down