Skip to content
Closed
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 @@ -15,9 +15,6 @@
*/
package com.tngtech.archunit.core.domain;

import java.util.Collections;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix you code formatting as described in https://github.com/TNG/ArchUnit/blob/main/CONTRIBUTING.md#formatting

import java.util.Set;

import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.base.ChainableFunction;
import com.tngtech.archunit.base.DescribedPredicate;
Expand All @@ -29,6 +26,9 @@
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation;
import com.tngtech.archunit.core.importer.DomainBuilders.JavaAccessBuilder;

import java.util.Collections;
import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
Expand Down Expand Up @@ -112,7 +112,7 @@ public <T> Set<T> convertTo(Class<T> type) {
@Override
public String toString() {
return getClass().getSimpleName() +
"{origin=" + origin + ", target=" + target + ", lineNumber=" + getLineNumber() + additionalToStringFields() + '}';
"{origin=" + origin + ", target=" + target + ", lineNumber=" + getLineNumber() + additionalToStringFields() + '}';
}

String additionalToStringFields() {
Expand Down Expand Up @@ -168,12 +168,16 @@ public static DescribedPredicate<JavaAccess<?>> originOwnerEqualsTargetOwner() {

@PublicAPI(usage = ACCESS)
public static DescribedPredicate<JavaAccess<?>> targetOwner(DescribedPredicate<? super JavaClass> predicate) {
return target(Get.<JavaClass>owner().is(predicate));
DescribedPredicate<JavaAccess<?>> targetPredicate =
predicate.onResultOf(JavaAccess.Functions.Get.target()
.then(HasOwner.Functions.Get.owner()));
return targetPredicate.as("owner %s", predicate.getDescription());
}

@PublicAPI(usage = ACCESS)
public static DescribedPredicate<JavaAccess<?>> target(DescribedPredicate<? super AccessTarget> predicate) {
return new TargetPredicate<>(predicate);
DescribedPredicate<JavaAccess<?>> targetPredicate = predicate.onResultOf(JavaAccess.Functions.Get.target());
return targetPredicate.as("target %s", predicate.getDescription());
}

private static class OriginOwnerEqualsTargetOwnerPredicate extends DescribedPredicate<JavaAccess<?>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ public void origin_predicate() {
assertThat(predicate).rejects(anyAccess());
}

@Test
public void target_predicate() {
DescribedPredicate<JavaAccess<?>> predicate =
JavaAccess.Predicates.target(DescribedPredicate.<AccessTarget>alwaysTrue().as("some text"));

assertThat(predicate)
.hasDescription("target some text")
.accepts(anyAccess());

predicate = JavaAccess.Predicates.target(alwaysFalse());
assertThat(predicate).rejects(anyAccess());
}

@Test
public void targetOwner_predicate() {
DescribedPredicate<JavaAccess<?>> predicate =
JavaAccess.Predicates.targetOwner(DescribedPredicate.<JavaClass>alwaysTrue().as("some text"));

assertThat(predicate)
.hasDescription("owner some text")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this text doesn't specify that the owner is related in any way to the target or any hint what is owned.
Is it the target owner or the origin owner?

I think the issue you are fixing here also exists for the origin(Owner) in exactly the same constellation.

The test also doesn't test that the correct object is passed to the nested predicate (which also not done by origin_predicate)

.accepts(anyAccess());

predicate = JavaAccess.Predicates.targetOwner(alwaysFalse());
assertThat(predicate).rejects(anyAccess());
}

@Test
public void convertTo() {
TestJavaAccess access = javaAccessFrom(importClassWithContext(String.class), "toString")
Expand Down