diff --git a/README.md b/README.md index 34c3853..47b4671 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,14 @@ testImplementation 'de.cronn:validation-file-assertions:{version}' * Pick suitable `assertWithFile` method and enjoy your first validation file assertion. +### Custom validation files directory + +It is possible to customize path where validation files are stored, in order to do that: + +* Implement `de.cronn.assertions.validationfile.config.Configure` and override method `getDataDirectory()` with path to desired location. + +* Register implemented configuration via Java Service Provider interface (namely: put fully qualified configuration class name in `resources/META-INF/services/de.cronn.assertions.validationfile.config.Configuration`) + ### See also * [Intellij plugin for validation file comparison][intellij_plugin] diff --git a/configuration-test/build.gradle b/configuration-test/build.gradle new file mode 100644 index 0000000..d76df94 --- /dev/null +++ b/configuration-test/build.gradle @@ -0,0 +1,35 @@ +plugins { + id 'java' +} + +group = 'de.cronn' +version = '0.0.1-SNAPSHOT' + +sourceCompatibility = JavaVersion.VERSION_1_8 +targetCompatibility = JavaVersion.VERSION_1_8 + +compileJava.options.encoding = 'UTF-8' + +java { + withSourcesJar() + withJavadocJar() +} + +repositories { + mavenCentral() +} + +dependencies { + implementation project(':') + + testImplementation "org.junit.jupiter:junit-jupiter:latest.release" + testImplementation "org.assertj:assertj-core:latest.release" +} + +tasks.named('test') { + useJUnitPlatform() +} + +dependencyLocking { + lockAllConfigurations() +} diff --git a/configuration-test/gradle.lockfile b/configuration-test/gradle.lockfile new file mode 100644 index 0000000..5783cf4 --- /dev/null +++ b/configuration-test/gradle.lockfile @@ -0,0 +1,16 @@ +# This is a Gradle generated file for dependency locking. +# Manual edits can break the build and are not advised. +# This file is expected to be part of source control. +com.googlecode.java-diff-utils:diffutils:1.3.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +net.bytebuddy:byte-buddy:1.12.21=testCompileClasspath,testRuntimeClasspath +org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath +org.assertj:assertj-core:3.24.2=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter-api:5.10.0=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter-engine:5.10.0=testRuntimeClasspath +org.junit.jupiter:junit-jupiter-params:5.10.0=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter:5.10.0=testCompileClasspath,testRuntimeClasspath +org.junit.platform:junit-platform-commons:1.10.0=testCompileClasspath,testRuntimeClasspath +org.junit.platform:junit-platform-engine:1.10.0=testRuntimeClasspath +org.junit:junit-bom:5.10.0=testCompileClasspath,testRuntimeClasspath +org.opentest4j:opentest4j:1.3.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +empty=annotationProcessor,testAnnotationProcessor diff --git a/configuration-test/src/test/java/de/cronn/configurationtest/ConfigurablePathTest.java b/configuration-test/src/test/java/de/cronn/configurationtest/ConfigurablePathTest.java new file mode 100644 index 0000000..1de6abf --- /dev/null +++ b/configuration-test/src/test/java/de/cronn/configurationtest/ConfigurablePathTest.java @@ -0,0 +1,16 @@ +package de.cronn.configurationtest; + +import de.cronn.assertions.validationfile.TestData; + +import de.cronn.assertions.validationfile.junit5.JUnit5ValidationFileAssertions; + +import org.junit.jupiter.api.Test; + +public class ConfigurablePathTest implements JUnit5ValidationFileAssertions { + + @Test + void testPath_withImplementedConfiguration() { + assertWithFile(TestData.TEST_OUTPUT_DATA_DIR.toString()); + } + +} diff --git a/configuration-test/src/test/java/de/cronn/configurationtest/config/TestConfiguration.java b/configuration-test/src/test/java/de/cronn/configurationtest/config/TestConfiguration.java new file mode 100644 index 0000000..10500e6 --- /dev/null +++ b/configuration-test/src/test/java/de/cronn/configurationtest/config/TestConfiguration.java @@ -0,0 +1,16 @@ +package de.cronn.configurationtest.config; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import de.cronn.assertions.validationfile.config.Configuration; + +public class TestConfiguration implements Configuration { + + public static final Path PATH = Paths.get("src", "test", "resources", "data"); + + @Override + public Path getDataDirectory() { + return PATH; + } +} diff --git a/configuration-test/src/test/resources/META-INF/services/de.cronn.assertions.validationfile.config.Configuration b/configuration-test/src/test/resources/META-INF/services/de.cronn.assertions.validationfile.config.Configuration new file mode 100644 index 0000000..1075b27 --- /dev/null +++ b/configuration-test/src/test/resources/META-INF/services/de.cronn.assertions.validationfile.config.Configuration @@ -0,0 +1 @@ +de.cronn.configurationtest.config.TestConfiguration diff --git a/configuration-test/src/test/resources/data/validation/ConfigurablePathTest_testPath_withImplementedConfiguration.txt b/configuration-test/src/test/resources/data/validation/ConfigurablePathTest_testPath_withImplementedConfiguration.txt new file mode 100644 index 0000000..6ad4f4c --- /dev/null +++ b/configuration-test/src/test/resources/data/validation/ConfigurablePathTest_testPath_withImplementedConfiguration.txt @@ -0,0 +1 @@ +src/test/resources/data/output diff --git a/gradle.lockfile b/gradle.lockfile index b292004..9310060 100644 --- a/gradle.lockfile +++ b/gradle.lockfile @@ -7,14 +7,15 @@ com.fasterxml.jackson.core:jackson-databind:2.15.2=testCompileClasspath,testRunt com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2=testCompileClasspath,testRuntimeClasspath com.fasterxml.jackson:jackson-bom:2.15.2=testCompileClasspath,testRuntimeClasspath com.googlecode.java-diff-utils:diffutils:1.3.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -org.apiguardian:apiguardian-api:1.1.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -org.assertj:assertj-core:3.19.0=testCompileClasspath,testRuntimeClasspath -org.junit.jupiter:junit-jupiter-api:5.7.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -org.junit.jupiter:junit-jupiter-engine:5.7.1=testRuntimeClasspath -org.junit.jupiter:junit-jupiter-params:5.7.1=testCompileClasspath,testRuntimeClasspath -org.junit.jupiter:junit-jupiter:5.7.1=testCompileClasspath,testRuntimeClasspath -org.junit.platform:junit-platform-commons:1.7.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -org.junit.platform:junit-platform-engine:1.7.1=testRuntimeClasspath -org.junit:junit-bom:5.7.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -org.opentest4j:opentest4j:1.2.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -empty=annotationProcessor,testAnnotationProcessor +net.bytebuddy:byte-buddy:1.12.21=testCompileClasspath,testRuntimeClasspath +org.apiguardian:apiguardian-api:1.1.2=compileClasspath,testCompileClasspath +org.assertj:assertj-core:3.24.2=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter-api:5.10.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter-engine:5.10.0=testRuntimeClasspath +org.junit.jupiter:junit-jupiter-params:5.10.0=testCompileClasspath,testRuntimeClasspath +org.junit.jupiter:junit-jupiter:5.10.0=testCompileClasspath,testRuntimeClasspath +org.junit.platform:junit-platform-commons:1.10.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.junit.platform:junit-platform-engine:1.10.0=testRuntimeClasspath +org.junit:junit-bom:5.10.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.opentest4j:opentest4j:1.3.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +empty=annotationProcessor,signatures,testAnnotationProcessor diff --git a/settings.gradle b/settings.gradle index 7b88501..d871afd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ rootProject.name = 'validation-file-assertions' +include ':configuration-test' diff --git a/src/main/java/de/cronn/assertions/validationfile/TestData.java b/src/main/java/de/cronn/assertions/validationfile/TestData.java index 1099e05..dfd636a 100644 --- a/src/main/java/de/cronn/assertions/validationfile/TestData.java +++ b/src/main/java/de/cronn/assertions/validationfile/TestData.java @@ -2,12 +2,42 @@ import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Iterator; +import java.util.ServiceLoader; + +import de.cronn.assertions.validationfile.config.Configuration; public final class TestData { - private static final Path TEST_DATA_DIR = Paths.get("data", "test"); - public static final Path TEST_TEMPORARY_DATA_DIR = TEST_DATA_DIR.resolve(TestDataDir.TMP.getDir()); - public static final Path TEST_VALIDATION_DATA_DIR = TEST_DATA_DIR.resolve(TestDataDir.VALIDATION.getDir()); - public static final Path TEST_OUTPUT_DATA_DIR = TEST_DATA_DIR.resolve(TestDataDir.OUTPUT.getDir()); + private static final Path TEST_DATA_DIR; + public static final Path TEST_TEMPORARY_DATA_DIR; + public static final Path TEST_VALIDATION_DATA_DIR; + public static final Path TEST_OUTPUT_DATA_DIR; + + static { + Path path = getPath(); + if (path != null) { + TEST_DATA_DIR = path; + } else { + TEST_DATA_DIR = Paths.get("data", "test"); + } + TEST_TEMPORARY_DATA_DIR = TEST_DATA_DIR.resolve(TestDataDir.TMP.getDir()); + TEST_VALIDATION_DATA_DIR = TEST_DATA_DIR.resolve(TestDataDir.VALIDATION.getDir()); + TEST_OUTPUT_DATA_DIR = TEST_DATA_DIR.resolve(TestDataDir.OUTPUT.getDir()); + } + + private static Path getPath() { + ServiceLoader serviceLoader = ServiceLoader.load(Configuration.class); + Iterator iterator = serviceLoader.iterator(); + if (iterator.hasNext()) { + Configuration loader = iterator.next(); + if (iterator.hasNext()) { + throw new IllegalArgumentException("More than one validation files configuration found."); + } + return loader.getDataDirectory(); + } else { + return null; + } + } private TestData() { } diff --git a/src/main/java/de/cronn/assertions/validationfile/config/Configuration.java b/src/main/java/de/cronn/assertions/validationfile/config/Configuration.java new file mode 100644 index 0000000..05eba4b --- /dev/null +++ b/src/main/java/de/cronn/assertions/validationfile/config/Configuration.java @@ -0,0 +1,9 @@ +package de.cronn.assertions.validationfile.config; + +import java.nio.file.Path; + +public interface Configuration { + + Path getDataDirectory(); + +} diff --git a/src/test/java/de/cronn/assertions/validationfile/ValidationFileAssertionsTest_OverrideValidationFileName.java b/src/test/java/de/cronn/assertions/validationfile/ValidationFileAssertionsTest_OverrideValidationFileName.java index bbc1996..712e847 100644 --- a/src/test/java/de/cronn/assertions/validationfile/ValidationFileAssertionsTest_OverrideValidationFileName.java +++ b/src/test/java/de/cronn/assertions/validationfile/ValidationFileAssertionsTest_OverrideValidationFileName.java @@ -36,7 +36,7 @@ public String getValidationFileName(String baseName, FileExtension extension) { } @Test - void assertValidationFileName() throws Exception { + void assertValidationFileName() { Path file = TestData.TEST_VALIDATION_DATA_DIR.resolve( "de/cronn/assertions/validationfile/ValidationFileAssertionsTest_OverrideValidationFileName_assertValidationFileName.txt"); diff --git a/updateDependencies.sh b/updateDependencies.sh index ef15aba..de96303 100755 --- a/updateDependencies.sh +++ b/updateDependencies.sh @@ -1,3 +1,3 @@ #!/bin/bash -./gradlew --refresh-dependencies dependencies --update-locks '*:*' +./gradlew --refresh-dependencies dependencies configuration-test:dependencies --update-locks '*:*'