Skip to content

Commit

Permalink
Add configurable path for validation files
Browse files Browse the repository at this point in the history
  • Loading branch information
Morakir authored Oct 3, 2023
1 parent acf5a8f commit a00762f
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 17 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
35 changes: 35 additions & 0 deletions configuration-test/build.gradle
Original file line number Diff line number Diff line change
@@ -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()
}
16 changes: 16 additions & 0 deletions configuration-test/gradle.lockfile
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de.cronn.configurationtest.config.TestConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/test/resources/data/output
23 changes: 12 additions & 11 deletions gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rootProject.name = 'validation-file-assertions'
include ':configuration-test'
38 changes: 34 additions & 4 deletions src/main/java/de/cronn/assertions/validationfile/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration> serviceLoader = ServiceLoader.load(Configuration.class);
Iterator<Configuration> 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() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.cronn.assertions.validationfile.config;

import java.nio.file.Path;

public interface Configuration {

Path getDataDirectory();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion updateDependencies.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

./gradlew --refresh-dependencies dependencies --update-locks '*:*'
./gradlew --refresh-dependencies dependencies configuration-test:dependencies --update-locks '*:*'

0 comments on commit a00762f

Please sign in to comment.