Skip to content

Commit

Permalink
ArchUnit configuration and ignore file should also be loaded with the…
Browse files Browse the repository at this point in the history
… 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: societe-generale/arch-unit-maven-plugin#13

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric committed Jul 24, 2019
1 parent 19a7ae3 commit 1a47f4c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -107,7 +108,7 @@ public boolean apply(String message) {
}

private static Set<Pattern> readPatternsFrom(String fileNameInClassPath) {
URL ignorePatternsResource = Assertions.class.getResource('/' + fileNameInClassPath);
URL ignorePatternsResource = getCurrentClassLoader(Assertions.class).getResource(fileNameInClassPath);
if (ignorePatternsResource == null) {
return Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +49,7 @@ public void missing_property_file() {
public void empty_property_file() {
writeProperties(Collections.<String, String>emptyMap());

assertDefault(testConfiguration(PROPERTIES_RESOURCE_NAME));
assertDefault(testConfiguration(PROPERTIES_FILE_NAME));
}

@Test
Expand All @@ -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();
Expand All @@ -75,15 +74,15 @@ 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");
}

@Test
public void reset_works() {
ArchConfiguration configuration = testConfiguration(PROPERTIES_RESOURCE_NAME);
ArchConfiguration configuration = testConfiguration(PROPERTIES_FILE_NAME);
assertThat(configuration.resolveMissingDependenciesFromClassPath()).isTrue();

configuration.setResolveMissingDependenciesFromClassPath(false);
Expand All @@ -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"));

Expand All @@ -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);
Expand All @@ -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"));
Expand All @@ -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));
Expand All @@ -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(
Expand All @@ -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"))
Expand Down

0 comments on commit 1a47f4c

Please sign in to comment.