From cac48f9c8e4c18a261e56e530a2d4698f1c5c77e Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Sun, 11 Sep 2022 19:01:46 +0700 Subject: [PATCH 1/2] remove unused method `NormalizedResourceName.belongsToClassFile()` Signed-off-by: Peter Gafert --- .../archunit/core/importer/NormalizedResourceName.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java b/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java index 7b391b5899..3a82f9b037 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java @@ -59,10 +59,6 @@ String toAbsolutePath() { return result; } - boolean belongsToClassFile() { - return resourceName.endsWith(".class"); - } - /** * @return The resourceName as if it was an entry of an archive * (i.e. not starting with '/', but ending with '/' in case of directories) From 2c6619616a17cd87198159c53961ab6c8e34dabd Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Sat, 10 Sep 2022 21:58:35 +0700 Subject: [PATCH 2/2] make `importPackages("")` and `importClasspath(..)` consistent So far `ClassFileImporter.importPackages("")` would only return classes returned by `ClassLoader.getResources("")`, but not other classes derived from the configured URL classpath (as done for other prefixes like `com`, which would be searched through the whole classpath as well). The reason is that this corner case has been overlooked in `NormalizedResourceName.startsWith(..)`. I.e. if the prefix is empty, then the resource would only be included if it starts with a `/`, but resources in archives do not start with a `/`. Signed-off-by: Peter Gafert --- .../core/importer/NormalizedResourceName.java | 4 ++-- .../importer/ClassFileImporterSlowTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java b/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java index 3a82f9b037..4929d5a7a4 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java @@ -38,8 +38,8 @@ boolean isStartOf(String string) { return string.startsWith(resourceName); } - public boolean startsWith(NormalizedResourceName prefix) { - return equals(prefix) || isAncestorPath(prefix); + boolean startsWith(NormalizedResourceName prefix) { + return prefix.resourceName.isEmpty() || equals(prefix) || isAncestorPath(prefix); } private boolean isAncestorPath(NormalizedResourceName prefix) { diff --git a/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterSlowTest.java b/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterSlowTest.java index 7a5beeb474..a628c03696 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterSlowTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterSlowTest.java @@ -4,9 +4,11 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.List; +import java.util.Set; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import com.tngtech.archunit.Slow; import com.tngtech.archunit.core.domain.JavaClass; import com.tngtech.archunit.core.domain.JavaClasses; @@ -28,6 +30,7 @@ import static com.tngtech.archunit.testutil.Assertions.assertThatTypes; import static com.tngtech.archunit.testutil.TestUtils.urlOf; import static java.util.jar.Attributes.Name.CLASS_PATH; +import static java.util.stream.Collectors.toSet; @Category(Slow.class) public class ClassFileImporterSlowTest { @@ -53,6 +56,22 @@ public void imports_the_classpath() { assertThatTypes(classes).contain(ClassFileImporter.class, getClass(), Rule.class, File.class); } + @Test + public void importing_the_default_package_equals_importing_the_classpath() { + Set classNamesOfDefaultPackageImport = new ClassFileImporter().withImportOption(importJavaBaseOrRtAndJUnitJarAndFilesOnTheClasspath()) + .importPackages("") + .stream().map(JavaClass::getName).collect(toSet()); + Set classNamesOfClasspathImport = new ClassFileImporter() + .importClasspath(new ImportOptions().with(importJavaBaseOrRtAndJUnitJarAndFilesOnTheClasspath())) + .stream().map(JavaClass::getName).collect(toSet()); + + Set classNamesOnlyInDefaultPackageImport = Sets.difference(classNamesOfDefaultPackageImport, classNamesOfClasspathImport); + assertThat(classNamesOnlyInDefaultPackageImport).as("Classes only contained in default package import").isEmpty(); + + Set classNamesOnlyInClasspathImport = Sets.difference(classNamesOfClasspathImport, classNamesOfDefaultPackageImport); + assertThat(classNamesOnlyInClasspathImport).as("Classes only contained in classpath import").isEmpty(); + } + @Test public void respects_ImportOptions_when_using_the_default_importClasspath_method() { JavaClasses classes = new ClassFileImporter().withImportOption(DO_NOT_INCLUDE_TESTS).importClasspath();