Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make importPackages("") and importClasspath(..) consistent #954

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -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)
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