Skip to content

Commit

Permalink
add predefined ImportOption to exclude package-info.class files
Browse files Browse the repository at this point in the history
Signed-off-by: Tomer Figenblat <[email protected]>
  • Loading branch information
TomerFi authored and codecholeric committed Feb 18, 2022
1 parent 5564a77 commit 432589d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*");
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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;

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

0 comments on commit 432589d

Please sign in to comment.