From 432589d3fd52efda10ef9e8465577473779f3aaf Mon Sep 17 00:00:00 2001 From: Tomer Figenblat Date: Tue, 15 Feb 2022 13:44:15 +0200 Subject: [PATCH] add predefined `ImportOption` to exclude `package-info.class` files Signed-off-by: Tomer Figenblat --- .../archunit/core/importer/ImportOption.java | 23 ++++++++++++++++ .../core/importer/ImportOptionsTest.java | 26 +++++++++++++++++++ .../importer/testexamples/package-info.java | 5 ++++ 3 files changed, 54 insertions(+) create mode 100644 archunit/src/test/java/com/tngtech/archunit/core/importer/testexamples/package-info.java diff --git a/archunit/src/main/java/com/tngtech/archunit/core/importer/ImportOption.java b/archunit/src/main/java/com/tngtech/archunit/core/importer/ImportOption.java index 0701674252..72647ec006 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/importer/ImportOption.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/importer/ImportOption.java @@ -76,6 +76,17 @@ public boolean includes(Location location) { public boolean includes(Location location) { return doNotIncludeArchives.includes(location); } + }, + /** + * @see DoNotIncludePackageInfos + */ + DO_NOT_INCLUDE_PACKAGE_INFOS { + private final DoNotIncludePackageInfos doNotIncludePackageInfos = new DoNotIncludePackageInfos(); + + @Override + public boolean includes(Location location) { + return doNotIncludePackageInfos.includes(location); + } }; static final PatternPredicate MAVEN_TEST_PATTERN = new PatternPredicate(".*/target/test-classes/.*"); @@ -137,4 +148,16 @@ public boolean includes(Location location) { return !location.isArchive(); } } + + /** + * Excludes {@code package-info.class} files. + */ + final class DoNotIncludePackageInfos implements ImportOption { + private static final Pattern PACKAGE_INFO_PATTERN = Pattern.compile(".*package-info\\.class$"); + + @Override + public boolean includes(Location location) { + return !location.matches(PACKAGE_INFO_PATTERN); + } + } } diff --git a/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportOptionsTest.java b/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportOptionsTest.java index a6b0cca2a6..ef6502a212 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportOptionsTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportOptionsTest.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -9,6 +10,7 @@ import com.google.common.collect.ImmutableList; import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeArchives; import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeJars; +import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludePackageInfos; import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeTests; import com.tngtech.archunit.core.importer.ImportOption.OnlyIncludeTests; import com.tngtech.java.junit.dataprovider.DataProvider; @@ -24,6 +26,7 @@ import static com.google.common.collect.Iterables.getLast; import static com.tngtech.archunit.core.importer.ImportOption.Predefined.DO_NOT_INCLUDE_ARCHIVES; import static com.tngtech.archunit.core.importer.ImportOption.Predefined.DO_NOT_INCLUDE_JARS; +import static com.tngtech.archunit.core.importer.ImportOption.Predefined.DO_NOT_INCLUDE_PACKAGE_INFOS; import static com.tngtech.archunit.core.importer.ImportOption.Predefined.DO_NOT_INCLUDE_TESTS; import static com.tngtech.archunit.core.importer.ImportOption.Predefined.ONLY_INCLUDE_TESTS; import static com.tngtech.java.junit.dataprovider.DataProviders.$; @@ -157,6 +160,29 @@ public void detects_archives_correctly(ImportOption doNotIncludeArchives) { .isFalse(); } + @DataProvider + public static Object[][] do_not_include_package_info_classes() { + return testForEach(new DoNotIncludePackageInfos(), DO_NOT_INCLUDE_PACKAGE_INFOS); + } + + @Test + @UseDataProvider("do_not_include_package_info_classes") + public void detect_package_info_class(ImportOption doNotIncludePackageInfoClasses) throws URISyntaxException { + Location packageInfoLocation = Location.of(getClass().getResource(testExampleResourcePath("package-info.class")).toURI()); + assertThat(doNotIncludePackageInfoClasses.includes(packageInfoLocation)) + .as("doNotIncludePackageInfoClasses includes package-info.class") + .isFalse(); + + Location thisClassLocation = locationOf(getClass()); + assertThat(doNotIncludePackageInfoClasses.includes(thisClassLocation)) + .as("doNotIncludePackageInfoClasses includes test class location") + .isTrue(); + } + + private String testExampleResourcePath(String resourceName) { + return "/" + getClass().getPackage().getName().replace(".", "/") + "/testexamples/" + resourceName; + } + private static Location locationOf(Class clazz) { return getLast(Locations.ofClass(clazz)); } diff --git a/archunit/src/test/java/com/tngtech/archunit/core/importer/testexamples/package-info.java b/archunit/src/test/java/com/tngtech/archunit/core/importer/testexamples/package-info.java new file mode 100644 index 0000000000..5a2816e1f0 --- /dev/null +++ b/archunit/src/test/java/com/tngtech/archunit/core/importer/testexamples/package-info.java @@ -0,0 +1,5 @@ +// The annotation forces the creation of a compiled `package-info.class` +@SomeAnnotation(mandatory = "any", mandatoryEnum = SOME_VALUE) +package com.tngtech.archunit.core.importer.testexamples; + +import static com.tngtech.archunit.core.importer.testexamples.SomeEnum.SOME_VALUE; \ No newline at end of file