|
| 1 | +package com.tngtech.archunit.lang.syntax.elements; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableSet; |
| 4 | +import com.tngtech.archunit.lang.EvaluationResult; |
| 5 | +import com.tngtech.archunit.lang.syntax.elements.GivenMembersTest.*; |
| 6 | +import com.tngtech.java.junit.dataprovider.DataProvider; |
| 7 | +import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
| 8 | +import com.tngtech.java.junit.dataprovider.UseDataProvider; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | + |
| 12 | +import java.util.Collection; |
| 13 | + |
| 14 | +import static com.tngtech.archunit.core.domain.TestUtils.importClasses; |
| 15 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; |
| 16 | +import static com.tngtech.archunit.lang.syntax.elements.GivenMembersTest.*; |
| 17 | +import static com.tngtech.java.junit.dataprovider.DataProviders.$; |
| 18 | +import static com.tngtech.java.junit.dataprovider.DataProviders.$$; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +@RunWith(DataProviderRunner.class) |
| 22 | +public class GivenMethodsTest { |
| 23 | + |
| 24 | + @DataProvider |
| 25 | + public static Object[][] restricted_property_rule_starts() { |
| 26 | + return $$( |
| 27 | + $(described(methods().that().areFinal()), ImmutableSet.of(METHOD_A, METHOD_B)), |
| 28 | + $(described(methods().that().areNotFinal()), ImmutableSet.of(METHOD_C, METHOD_D)), |
| 29 | + $(described(methods().that().areStatic()), ImmutableSet.of(METHOD_B, METHOD_D)), |
| 30 | + $(described(methods().that().areNotStatic()), ImmutableSet.of(METHOD_A, METHOD_C)), |
| 31 | + $(described(methods().that().areFinal().and().areStatic()), ImmutableSet.of(METHOD_B)), |
| 32 | + $(described(methods().that().areFinal().or().areStatic()), ImmutableSet.of(METHOD_A, METHOD_B, METHOD_D)) |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + @UseDataProvider("restricted_property_rule_starts") |
| 38 | + public void property_predicates(DescribedRuleStart ruleStart, Collection<String> expectedMembers) { |
| 39 | + EvaluationResult result = ruleStart.should(everythingViolationPrintMemberName()) |
| 40 | + .evaluate(importClasses(ClassWithVariousMembers.class)); |
| 41 | + |
| 42 | + assertThat(result.getFailureReport().getDetails()).containsOnlyElementsOf(expectedMembers); |
| 43 | + } |
| 44 | + |
| 45 | + private static final String METHOD_A = "methodA([I)"; |
| 46 | + private static final String METHOD_B = "methodB(boolean)"; |
| 47 | + private static final String METHOD_C = "methodC(char)"; |
| 48 | + private static final String METHOD_D = "methodD()"; |
| 49 | + |
| 50 | + @SuppressWarnings({"unused"}) |
| 51 | + private static class ClassWithVariousMembers { |
| 52 | + public final void methodA(int[] array) { |
| 53 | + } |
| 54 | + protected static final void methodB(boolean flag) { |
| 55 | + } |
| 56 | + private void methodC(char ch) { |
| 57 | + } |
| 58 | + static int methodD() { |
| 59 | + return 0; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments