From a5dee38355dfb7762f18a85bc49d0429a7ec096e Mon Sep 17 00:00:00 2001 From: Leonard Husmann Date: Sun, 30 Apr 2023 00:52:06 +0200 Subject: [PATCH] extend test case in FieldsShouldTest.java Issue: #857 Signed-off-by: Leonard Husmann --- .../lang/syntax/elements/FieldsShouldTest.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/FieldsShouldTest.java b/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/FieldsShouldTest.java index c1f65fbafb..b62985a100 100644 --- a/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/FieldsShouldTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/FieldsShouldTest.java @@ -6,6 +6,7 @@ import java.util.Set; import com.google.common.collect.ImmutableList; +import com.tngtech.archunit.base.DescribedPredicate; import com.tngtech.archunit.lang.ArchRule; import com.tngtech.archunit.lang.EvaluationResult; import com.tngtech.java.junit.dataprovider.DataProvider; @@ -36,13 +37,18 @@ public void complex_field_syntax() { .should().beAnnotatedWith(A.class) .andShould().notBePublic() .orShould().haveRawType(Map.class) + .andShould().notBeAccessedByMethodsThat(DescribedPredicate.alwaysTrue()) .evaluate(importClasses(ClassWithVariousMembers.class)); assertViolation(result); - String failure = getOnlyElement(result.getFailureReport().getDetails()); - assertThat(failure) + String fieldAFailures = getOnlyElement(result.filterDescriptionsMatching(s -> s.contains(FIELD_A)).getFailureReport().getDetails()); + assertThat(fieldAFailures) .containsPattern(String.format("%s.* does not have raw type %s", FIELD_A, Map.class.getName())) .containsPattern(String.format("%s.* is not annotated with @%s", FIELD_A, A.class.getSimpleName())); + + String fieldDFailures = + getOnlyElement(result.filterDescriptionsMatching(description -> description.contains(FIELD_D)).getFailureReport().getDetails()); + assertThat(fieldDFailures).containsPattern(String.format("%s.* gets field .*%s", METHOD_A, FIELD_D)); } @DataProvider @@ -76,6 +82,8 @@ public void property_predicates(ArchRule rule, Collection expectedViolat private static final String FIELD_C = "fieldC"; private static final String FIELD_D = "fieldD"; + private static final String METHOD_A = "methodA()"; + @SuppressWarnings({"unused"}) private static class ClassWithVariousMembers { private final String fieldA = "A"; @@ -83,6 +91,10 @@ private static class ClassWithVariousMembers { protected static final Object fieldB = 'B'; public List fieldC; static Map fieldD; + + public void methodA() { + Map tmp = fieldD; + } } private @interface A {