From 6b13196835cbf4de188e294291b4b8002d62f90a Mon Sep 17 00:00:00 2001 From: Manfred Hanke Date: Thu, 21 Mar 2019 20:42:31 +0100 Subject: [PATCH] add methods().that().are[Not]{Static,Final} syntax Signed-off-by: Manfred Hanke --- .../lang/syntax/GivenMethodsInternal.java | 15 +++++ .../lang/syntax/MethodsThatInternal.java | 28 +++++++++ .../lang/syntax/elements/GivenMethods.java | 4 ++ .../elements/GivenMethodsConjunction.java | 8 +++ .../lang/syntax/elements/MethodsThat.java | 55 ++++++++++++++++ .../syntax/elements/GivenMethodsTest.java | 62 +++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 archunit/src/main/java/com/tngtech/archunit/lang/syntax/MethodsThatInternal.java create mode 100644 archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/MethodsThat.java create mode 100644 archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsTest.java diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/GivenMethodsInternal.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/GivenMethodsInternal.java index a05018124f..d1dd49c355 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/GivenMethodsInternal.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/GivenMethodsInternal.java @@ -57,6 +57,21 @@ private GivenMethodsInternal( super(factory, priority, classesTransformer, prepareCondition, relevantObjectsPredicates, overriddenDescription); } + @Override + public MethodsThatInternal that() { + return new MethodsThatInternal(this, currentPredicate()); + } + + @Override + public MethodsThatInternal and() { + return new MethodsThatInternal(this, currentPredicate().thatANDs()); + } + + @Override + public MethodsThatInternal or() { + return new MethodsThatInternal(this, currentPredicate().thatORs()); + } + @Override public MethodsShouldInternal should() { return new MethodsShouldInternal(finishedClassesTransformer(), priority, prepareCondition); diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MethodsThatInternal.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MethodsThatInternal.java new file mode 100644 index 0000000000..49376f9978 --- /dev/null +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MethodsThatInternal.java @@ -0,0 +1,28 @@ +/* + * Copyright 2019 TNG Technology Consulting GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tngtech.archunit.lang.syntax; + +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.lang.syntax.elements.MethodsThat; + +class MethodsThatInternal + extends CodeUnitsThatInternal + implements MethodsThat { + + MethodsThatInternal(GivenMethodsInternal givenMethods, PredicateAggregator currentPredicate) { + super(givenMethods, currentPredicate); + } +} diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethods.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethods.java index 6176d688f3..f7e5ff5577 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethods.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethods.java @@ -23,6 +23,10 @@ public interface GivenMethods extends GivenCodeUnits { + @Override + @PublicAPI(usage = ACCESS) + MethodsThat that(); + @Override @PublicAPI(usage = ACCESS) GivenMethodsConjunction that(DescribedPredicate predicate); diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsConjunction.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsConjunction.java index 73b2e60b3d..f0267dc493 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsConjunction.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsConjunction.java @@ -23,6 +23,14 @@ public interface GivenMethodsConjunction extends GivenCodeUnitsConjunction { + @Override + @PublicAPI(usage = ACCESS) + MethodsThat and(); + + @Override + @PublicAPI(usage = ACCESS) + MethodsThat or(); + @Override @PublicAPI(usage = ACCESS) GivenMethodsConjunction and(DescribedPredicate predicate); diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/MethodsThat.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/MethodsThat.java new file mode 100644 index 0000000000..0a23a6fa43 --- /dev/null +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/MethodsThat.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019 TNG Technology Consulting GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tngtech.archunit.lang.syntax.elements; + +import com.tngtech.archunit.PublicAPI; + +import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; + +public interface MethodsThat extends CodeUnitsThat { + + /** + * Matches static methods. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areStatic(); + + /** + * Matches non-static methods. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areNotStatic(); + + /** + * Matches final methods. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areFinal(); + + /** + * Matches non-final methods. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areNotFinal(); +} diff --git a/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsTest.java b/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsTest.java new file mode 100644 index 0000000000..3adbb41295 --- /dev/null +++ b/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsTest.java @@ -0,0 +1,62 @@ +package com.tngtech.archunit.lang.syntax.elements; + +import com.google.common.collect.ImmutableSet; +import com.tngtech.archunit.lang.EvaluationResult; +import com.tngtech.archunit.lang.syntax.elements.GivenMembersTest.*; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Collection; + +import static com.tngtech.archunit.core.domain.TestUtils.importClasses; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; +import static com.tngtech.archunit.lang.syntax.elements.GivenMembersTest.*; +import static com.tngtech.java.junit.dataprovider.DataProviders.$; +import static com.tngtech.java.junit.dataprovider.DataProviders.$$; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(DataProviderRunner.class) +public class GivenMethodsTest { + + @DataProvider + public static Object[][] restricted_property_rule_starts() { + return $$( + $(described(methods().that().areFinal()), ImmutableSet.of(METHOD_A, METHOD_B)), + $(described(methods().that().areNotFinal()), ImmutableSet.of(METHOD_C, METHOD_D)), + $(described(methods().that().areStatic()), ImmutableSet.of(METHOD_B, METHOD_D)), + $(described(methods().that().areNotStatic()), ImmutableSet.of(METHOD_A, METHOD_C)), + $(described(methods().that().areFinal().and().areStatic()), ImmutableSet.of(METHOD_B)), + $(described(methods().that().areFinal().or().areStatic()), ImmutableSet.of(METHOD_A, METHOD_B, METHOD_D)) + ); + } + + @Test + @UseDataProvider("restricted_property_rule_starts") + public void property_predicates(DescribedRuleStart ruleStart, Collection expectedMembers) { + EvaluationResult result = ruleStart.should(everythingViolationPrintMemberName()) + .evaluate(importClasses(ClassWithVariousMembers.class)); + + assertThat(result.getFailureReport().getDetails()).containsOnlyElementsOf(expectedMembers); + } + + private static final String METHOD_A = "methodA([I)"; + private static final String METHOD_B = "methodB(boolean)"; + private static final String METHOD_C = "methodC(char)"; + private static final String METHOD_D = "methodD()"; + + @SuppressWarnings({"unused"}) + private static class ClassWithVariousMembers { + public final void methodA(int[] array) { + } + protected static final void methodB(boolean flag) { + } + private void methodC(char ch) { + } + static int methodD() { + return 0; + } + } +}