Skip to content

Commit

Permalink
Add API method to JavaPackage to access package-info.java
Browse files Browse the repository at this point in the history
Issue TNG#263

Signed-off-by: Roland Weisleder <[email protected]>
  • Loading branch information
rweisleder authored and codecholeric committed Apr 7, 2020
1 parent bc7ddf7 commit 9a9bebc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,13 +50,15 @@ public final class JavaPackage implements HasName {
private final String name;
private final String relativeName;
private final Set<JavaClass> classes;
private final Optional<? extends HasAnnotations> packageInfo;
private final Map<String, JavaPackage> subPackages;
private Optional<JavaPackage> parent = Optional.absent();

private JavaPackage(String name, Set<JavaClass> classes, Map<String, JavaPackage> subPackages) {
this.name = checkNotNull(name);
relativeName = name.substring(name.lastIndexOf(".") + 1);
this.classes = ImmutableSet.copyOf(classes);
this.packageInfo = tryGetClassWithSimpleName("package-info");
this.subPackages = ImmutableMap.copyOf(subPackages);
}

Expand All @@ -76,6 +79,17 @@ public String getRelativeName() {
return relativeName;
}

@PublicAPI(usage = ACCESS)
public HasAnnotations getPackageInfo() {
return tryGetPackageInfo().getOrThrow(
new IllegalArgumentException(String.format("Package %s does not contain a package-info.java", getName())));
}

@PublicAPI(usage = ACCESS)
public Optional<? extends HasAnnotations> tryGetPackageInfo() {
return packageInfo;
}

/**
* @return the parent package, e.g. {@code java} for package {@code java.lang}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@PackageLevelAnnotation
package com.tngtech.archunit.core.domain.packageexamples.annotated;

0 comments on commit 9a9bebc

Please sign in to comment.