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 4 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 @@ -30,7 +30,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(TestUtils.createPlatformSpecificFilePath("/tmp/config.yml"));
dhugo0022 marked this conversation as resolved.
Show resolved Hide resolved

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

class SerializersTest {

private final String tmpConfigPath = TestUtils.createPlatformSpecificFilePath("/tmp/config.yml");
/*
If found a way to implement the newline char test on the file name without
giving errors on windows. The following field ought to be changed to do so.
*/
private final String tmpWithUnderscorePath = TestUtils.createPlatformSpecificFilePath("/tmp/with_underscore.yml");
private final String tmpPath = TestUtils.createPlatformSpecificFilePath("/tmp");
dhugo0022 marked this conversation as resolved.
Show resolved Hide resolved

@Test
void booleanSerializer() {
Serializer<Boolean, Boolean> serializer = new Serializers.BooleanSerializer();
Expand Down Expand Up @@ -461,42 +470,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(tmpConfigPath);
File file2 = new File(tmpWithUnderscorePath);
File file3 = new File(tmpPath);

File file1 = new File(path1);
File file2 = new File(path2);
File file3 = new File(path3);
assertThat(serializer.serialize(file1), is(tmpConfigPath));
assertThat(serializer.serialize(file2), is(tmpWithUnderscorePath));
assertThat(serializer.serialize(file3), is(tmpPath));

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(tmpConfigPath), is(file1));
assertThat(serializer.deserialize(tmpWithUnderscorePath), is(file2));
assertThat(serializer.deserialize(tmpPath), 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(tmpConfigPath);
Path file2 = Path.of(tmpWithUnderscorePath);
Path file3 = Path.of(tmpPath);

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

assertThat(serializer.deserialize(path1), is(file1));
assertThat(serializer.deserialize(path2), is(file2));
assertThat(serializer.deserialize(path3), is(file3));
assertThat(serializer.deserialize(tmpConfigPath), is(file1));
assertThat(serializer.deserialize(tmpWithUnderscorePath), is(file2));
assertThat(serializer.deserialize(tmpPath), 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", TestUtils.createPlatformSpecificFilePath("/tmp")),
entry("a1_path", TestUtils.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", TestUtils.createListOfPlatformSpecificFilePaths("/tmp", "/tmp/config.yml", "/tmp/with_underscore.yml")),
entry("a1_listPath", TestUtils.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", TestUtils.createPlatformSpecificFilePath("/tmp/config.yml")),
entry("a2_path", TestUtils.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", TestUtils.createListOfPlatformSpecificFilePaths("/tmp/config.yml", "/tmp/with_underscore.yml", "with_underscore.yml")),
entry("a2_listPath", TestUtils.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
@@ -1,5 +1,7 @@
package de.exlll.configlib.configurations;

import de.exlll.configlib.TestUtils;

import java.awt.Point;
import java.io.File;
import java.math.BigDecimal;
Expand Down Expand Up @@ -59,15 +61,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 File FILE_1 = new File(TestUtils.createPlatformSpecificFilePath("/tmp"));
private static final File FILE_2 = new File(TestUtils.createPlatformSpecificFilePath("/tmp/config.yml"));
private static final File FILE_3 = new File(TestUtils.createPlatformSpecificFilePath("/tmp/with_underscore.yml"));
private static final File FILE_4 = new File(TestUtils.createPlatformSpecificFilePath("with_underscore.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 Path PATH_1 = Path.of(TestUtils.createPlatformSpecificFilePath("/tmp"));
private static final Path PATH_2 = Path.of(TestUtils.createPlatformSpecificFilePath("/tmp/config.yml"));
private static final Path PATH_3 = Path.of(TestUtils.createPlatformSpecificFilePath("/tmp/with_underscore.yml"));
private static final Path PATH_4 = Path.of(TestUtils.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 @@ -19,7 +19,7 @@

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

@BeforeEach
void setUp() throws IOException {
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 = TestUtils.createPlatformSpecificFilePath("/tmp/config.yml");
private final Path yamlFile = fs.getPath(yamlFilePath);

private final String abcFilePath = TestUtils.createPlatformSpecificFilePath("/a/b/c.yml");
dhugo0022 marked this conversation as resolved.
Show resolved Hide resolved

@BeforeEach
void setUp() throws IOException {
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 @@ -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 @@ -15,7 +15,7 @@
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(TestUtils.createPlatformSpecificFilePath("/tmp/config.yml"));

@BeforeEach
void setUp() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@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(TestUtils.createPlatformSpecificFilePath("/tmp/config.yml"));

@BeforeEach
void setUp() throws IOException {
Expand Down