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..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) { @@ -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) 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();