From 81c31f6b7ffd9c4836e1f8a0303724eb7f05ce83 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Wed, 24 Jul 2019 17:45:30 +0200 Subject: [PATCH] ArchUnit configuration and ignore file should also be loaded with the ContextClassLoader, not just classes. Otherwise tools like the ArchUnit Maven Plugin that reconfigure the ContextClassLoader to import classes cannot load these files in a consistent way. See: https://github.com/societe-generale/arch-unit-maven-plugin/issues/13 Signed-off-by: Peter Gafert --- .../tngtech/archunit/ArchConfiguration.java | 5 ++-- .../com/tngtech/archunit/lang/ArchRule.java | 3 ++- .../archunit/ArchConfigurationTest.java | 23 +++++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/ArchConfiguration.java b/archunit/src/main/java/com/tngtech/archunit/ArchConfiguration.java index deb1d1d965..9d7e141e42 100644 --- a/archunit/src/main/java/com/tngtech/archunit/ArchConfiguration.java +++ b/archunit/src/main/java/com/tngtech/archunit/ArchConfiguration.java @@ -35,13 +35,14 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; +import static com.tngtech.archunit.base.ClassLoaders.getCurrentClassLoader; /** * Allows access to configured properties in {@value ARCHUNIT_PROPERTIES_RESOURCE_NAME}. */ public final class ArchConfiguration { @Internal // {@value ...} does not work on non public constants outside of the package - public static final String ARCHUNIT_PROPERTIES_RESOURCE_NAME = "/archunit.properties"; + public static final String ARCHUNIT_PROPERTIES_RESOURCE_NAME = "archunit.properties"; @Internal // {@value ...} does not work on non public constants outside of the package public static final String RESOLVE_MISSING_DEPENDENCIES_FROM_CLASS_PATH = "resolveMissingDependenciesFromClassPath"; static final String CLASS_RESOLVER = "classResolver"; @@ -84,7 +85,7 @@ private ArchConfiguration(String propertiesResourceName) { private void readProperties(String propertiesResourceName) { properties.clear(); - URL archUnitPropertiesUrl = getClass().getResource(propertiesResourceName); + URL archUnitPropertiesUrl = getCurrentClassLoader(getClass()).getResource(propertiesResourceName); if (archUnitPropertiesUrl == null) { LOG.debug("No configuration found in classpath at {} => Using default configuration", propertiesResourceName); return; diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/ArchRule.java b/archunit/src/main/java/com/tngtech/archunit/lang/ArchRule.java index e3f568f96a..b55a214bf8 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/ArchRule.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/ArchRule.java @@ -38,6 +38,7 @@ import static com.google.common.io.Resources.readLines; import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; +import static com.tngtech.archunit.base.ClassLoaders.getCurrentClassLoader; import static java.nio.charset.StandardCharsets.UTF_8; /** @@ -107,7 +108,7 @@ public boolean apply(String message) { } private static Set readPatternsFrom(String fileNameInClassPath) { - URL ignorePatternsResource = Assertions.class.getResource('/' + fileNameInClassPath); + URL ignorePatternsResource = getCurrentClassLoader(Assertions.class).getResource(fileNameInClassPath); if (ignorePatternsResource == null) { return Collections.emptySet(); } diff --git a/archunit/src/test/java/com/tngtech/archunit/ArchConfigurationTest.java b/archunit/src/test/java/com/tngtech/archunit/ArchConfigurationTest.java index 8681217798..09c9baeb38 100644 --- a/archunit/src/test/java/com/tngtech/archunit/ArchConfigurationTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/ArchConfigurationTest.java @@ -23,7 +23,6 @@ public class ArchConfigurationTest { private static final String PROPERTIES_FILE_NAME = "archconfigtest.properties"; - private static final String PROPERTIES_RESOURCE_NAME = "/" + PROPERTIES_FILE_NAME; private final File testPropsFile = new File(getClass().getResource("/").getFile(), PROPERTIES_FILE_NAME); @Rule @@ -50,7 +49,7 @@ public void missing_property_file() { public void empty_property_file() { writeProperties(Collections.emptyMap()); - assertDefault(testConfiguration(PROPERTIES_RESOURCE_NAME)); + assertDefault(testConfiguration(PROPERTIES_FILE_NAME)); } @Test @@ -60,7 +59,7 @@ public void simple_properties_explicitly_set() { ArchConfiguration.ENABLE_MD5_IN_CLASS_SOURCES, true )); - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); assertThat(configuration.resolveMissingDependenciesFromClassPath()).isTrue(); assertThat(configuration.md5InClassSourcesEnabled()).isTrue(); @@ -75,7 +74,7 @@ public void resolver_explicitly_set() { ArchConfiguration.CLASS_RESOLVER_ARGS, "one.foo,two.bar" )); - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); assertThat(configuration.getClassResolver()).contains("some.Resolver"); assertThat(configuration.getClassResolverArguments()).containsExactly("one.foo", "two.bar"); @@ -83,7 +82,7 @@ public void resolver_explicitly_set() { @Test public void reset_works() { - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); assertThat(configuration.resolveMissingDependenciesFromClassPath()).isTrue(); configuration.setResolveMissingDependenciesFromClassPath(false); @@ -102,7 +101,7 @@ public void global_access_to_configuration_has_no_error() { @Test public void can_set_extension_properties() { - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); configuration.setExtensionProperties("test", singleProperty("key", "value")); @@ -112,7 +111,7 @@ public void can_set_extension_properties() { @Test public void set_extension_properties_are_copied() { - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); Properties properties = singleProperty("key", "value"); configuration.setExtensionProperties("test", properties); @@ -124,7 +123,7 @@ public void set_extension_properties_are_copied() { @Test public void can_change_extension_properties() { - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); configuration.setExtensionProperties("test", properties("one", "valueOne", "two", "valueTwo")); @@ -141,14 +140,14 @@ public void can_change_extension_properties() { @Test public void if_no_extension_properties_are_found_empty_properties_are_returned() { - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); assertThat(configuration.getExtensionProperties("not-there")).isEmpty(); } @Test public void returned_properties_are_copied() { - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); String original = "value"; configuration.setExtensionProperties("test", singleProperty("key", original)); @@ -171,7 +170,7 @@ public void creates_extension_properties_from_prefix() { "extension.other-extension.other-prop", "other value" )); - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); Properties properties = configuration.getExtensionProperties("test-extension"); assertThat(properties).containsOnly( @@ -190,7 +189,7 @@ public void allows_to_specify_custom_properties() { "toignore", "toignore" )); - ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME); + ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME); assertThat(configuration.getProperty("some.custom.booleanproperty")).isEqualTo("true"); assertThat(configuration.getSubProperties("some.custom"))