Skip to content

Commit

Permalink
extend test case in FieldsShouldTest.java
Browse files Browse the repository at this point in the history
Issue: #857
Signed-off-by: Leonard Husmann <[email protected]>
  • Loading branch information
leonardhusmann authored and codecholeric committed Oct 8, 2023
1 parent f77932e commit a5dee38
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -76,13 +82,19 @@ public void property_predicates(ArchRule rule, Collection<String> 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";
@A
protected static final Object fieldB = 'B';
public List<?> fieldC;
static Map<?, ?> fieldD;

public void methodA() {
Map<?, ?> tmp = fieldD;
}
}

private @interface A {
Expand Down

0 comments on commit a5dee38

Please sign in to comment.