From 53f6d1945009a8e1f7250d4cc4b69b9f2271e0ed Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Sat, 4 Jan 2020 19:22:27 +0100 Subject: [PATCH] Add API method to JavaPackage to access package-info.java Issue #263 Signed-off-by: Roland Weisleder --- .../archunit/core/domain/JavaPackage.java | 14 ++++++++++ .../archunit/core/domain/JavaPackageTest.java | 26 +++++++++++++++++++ .../annotated/PackageLevelAnnotation.java | 11 ++++++++ .../annotated/package-info.java | 2 ++ 4 files changed, 53 insertions(+) create mode 100644 archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/PackageLevelAnnotation.java create mode 100644 archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/package-info.java diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaPackage.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaPackage.java index 8c0963b423..f4c8d83e97 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaPackage.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaPackage.java @@ -33,6 +33,7 @@ import com.tngtech.archunit.base.DescribedPredicate; import com.tngtech.archunit.base.Optional; import com.tngtech.archunit.base.Predicate; +import com.tngtech.archunit.core.domain.properties.HasAnnotations; import com.tngtech.archunit.core.domain.properties.HasName; import static com.google.common.base.Preconditions.checkArgument; @@ -49,6 +50,7 @@ public final class JavaPackage implements HasName { private final String name; private final String relativeName; private final Set classes; + private final Optional packageInfo; private final Map subPackages; private Optional parent = Optional.absent(); @@ -56,6 +58,7 @@ private JavaPackage(String name, Set classes, Map tryGetPackageInfo() { + return packageInfo; + } + /** * @return the parent package, e.g. {@code java} for package {@code java.lang} */ diff --git a/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaPackageTest.java b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaPackageTest.java index adfff322f7..813883348d 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaPackageTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaPackageTest.java @@ -23,6 +23,7 @@ import com.tngtech.archunit.core.domain.packageexamples.unrelated.AnyClass; import com.tngtech.archunit.core.domain.properties.HasName; import com.tngtech.archunit.core.importer.ClassFileImporter; +import org.assertj.core.api.ThrowableAssert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -36,6 +37,7 @@ import static com.tngtech.archunit.testutil.Assertions.assertThatDependencies; import static com.tngtech.archunit.testutil.Assertions.assertThatPackages; import static java.util.regex.Pattern.quote; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class JavaPackageTest { @Rule @@ -320,6 +322,30 @@ public void has_package_dependencies_from_other_packages() { .isEmpty(); } + @Test + public void test_getPackageInfo() { + JavaPackage annotatedPackage = importPackage("packageexamples.annotated"); + final JavaPackage notAnnotatedPackage = importPackage("packageexamples"); + + assertThat(annotatedPackage.getPackageInfo()).isNotNull(); + + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { + @Override + public void call() { + notAnnotatedPackage.getPackageInfo(); + } + }).isInstanceOf(IllegalArgumentException.class).hasMessageContaining(".packageexamples does not contain a package-info.java"); + } + + @Test + public void test_tryGetPackageInfo() { + JavaPackage annotatedPackage = importPackage("packageexamples.annotated"); + JavaPackage notAnnotatedPackage = importPackage("packageexamples"); + + assertThat(annotatedPackage.tryGetPackageInfo()).isPresent(); + assertThat(notAnnotatedPackage.tryGetPackageInfo()).isAbsent(); + } + @Test public void function_GET_RELATIVE_NAME() { JavaPackage defaultPackage = importDefaultPackage(Object.class); diff --git a/archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/PackageLevelAnnotation.java b/archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/PackageLevelAnnotation.java new file mode 100644 index 0000000000..314699e93a --- /dev/null +++ b/archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/PackageLevelAnnotation.java @@ -0,0 +1,11 @@ +package com.tngtech.archunit.core.domain.packageexamples.annotated; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PACKAGE) +public @interface PackageLevelAnnotation { +} diff --git a/archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/package-info.java b/archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/package-info.java new file mode 100644 index 0000000000..5132c8172a --- /dev/null +++ b/archunit/src/test/java/com/tngtech/archunit/core/domain/packageexamples/annotated/package-info.java @@ -0,0 +1,2 @@ +@PackageLevelAnnotation +package com.tngtech.archunit.core.domain.packageexamples.annotated;