Skip to content

Commit

Permalink
make importPackages("") and importClasspath(..) consistent
Browse files Browse the repository at this point in the history
So far `ClassFileImporter.importPackages("")` would only return classes that returns, but not classes derived from the set classpath. 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 <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Sep 10, 2022
1 parent 45df801 commit fafabb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<String> classNamesOfDefaultPackageImport = new ClassFileImporter().withImportOption(importJavaBaseOrRtAndJUnitJarAndFilesOnTheClasspath())
.importPackages("")
.stream().map(JavaClass::getName).collect(toSet());
Set<String> classNamesOfClasspathImport = new ClassFileImporter()
.importClasspath(new ImportOptions().with(importJavaBaseOrRtAndJUnitJarAndFilesOnTheClasspath()))
.stream().map(JavaClass::getName).collect(toSet());

Set<String> classNamesOnlyInDefaultPackageImport = Sets.difference(classNamesOfDefaultPackageImport, classNamesOfClasspathImport);
assertThat(classNamesOnlyInDefaultPackageImport).as("Classes only contained in default package import").isEmpty();

Set<String> 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();
Expand Down

0 comments on commit fafabb3

Please sign in to comment.