Skip to content

Commit 6b13196

Browse files
committed
add methods().that().are[Not]{Static,Final} syntax
Signed-off-by: Manfred Hanke <[email protected]>
1 parent efbbc32 commit 6b13196

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

archunit/src/main/java/com/tngtech/archunit/lang/syntax/GivenMethodsInternal.java

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ private GivenMethodsInternal(
5757
super(factory, priority, classesTransformer, prepareCondition, relevantObjectsPredicates, overriddenDescription);
5858
}
5959

60+
@Override
61+
public MethodsThatInternal that() {
62+
return new MethodsThatInternal(this, currentPredicate());
63+
}
64+
65+
@Override
66+
public MethodsThatInternal and() {
67+
return new MethodsThatInternal(this, currentPredicate().thatANDs());
68+
}
69+
70+
@Override
71+
public MethodsThatInternal or() {
72+
return new MethodsThatInternal(this, currentPredicate().thatORs());
73+
}
74+
6075
@Override
6176
public MethodsShouldInternal should() {
6277
return new MethodsShouldInternal(finishedClassesTransformer(), priority, prepareCondition);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2019 TNG Technology Consulting GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.tngtech.archunit.lang.syntax;
17+
18+
import com.tngtech.archunit.core.domain.JavaMethod;
19+
import com.tngtech.archunit.lang.syntax.elements.MethodsThat;
20+
21+
class MethodsThatInternal
22+
extends CodeUnitsThatInternal<JavaMethod, GivenMethodsInternal>
23+
implements MethodsThat<GivenMethodsInternal> {
24+
25+
MethodsThatInternal(GivenMethodsInternal givenMethods, PredicateAggregator<JavaMethod> currentPredicate) {
26+
super(givenMethods, currentPredicate);
27+
}
28+
}

archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethods.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
public interface GivenMethods extends GivenCodeUnits<JavaMethod> {
2525

26+
@Override
27+
@PublicAPI(usage = ACCESS)
28+
MethodsThat<?> that();
29+
2630
@Override
2731
@PublicAPI(usage = ACCESS)
2832
GivenMethodsConjunction that(DescribedPredicate<? super JavaMethod> predicate);

archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/GivenMethodsConjunction.java

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
public interface GivenMethodsConjunction extends GivenCodeUnitsConjunction<JavaMethod> {
2525

26+
@Override
27+
@PublicAPI(usage = ACCESS)
28+
MethodsThat<?> and();
29+
30+
@Override
31+
@PublicAPI(usage = ACCESS)
32+
MethodsThat<?> or();
33+
2634
@Override
2735
@PublicAPI(usage = ACCESS)
2836
GivenMethodsConjunction and(DescribedPredicate<? super JavaMethod> predicate);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 TNG Technology Consulting GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.tngtech.archunit.lang.syntax.elements;
17+
18+
import com.tngtech.archunit.PublicAPI;
19+
20+
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
21+
22+
public interface MethodsThat<CONJUNCTION extends GivenMethodsConjunction> extends CodeUnitsThat<CONJUNCTION> {
23+
24+
/**
25+
* Matches static methods.
26+
*
27+
* @return A syntax conjunction element, which can be completed to form a full rule
28+
*/
29+
@PublicAPI(usage = ACCESS)
30+
CONJUNCTION areStatic();
31+
32+
/**
33+
* Matches non-static methods.
34+
*
35+
* @return A syntax conjunction element, which can be completed to form a full rule
36+
*/
37+
@PublicAPI(usage = ACCESS)
38+
CONJUNCTION areNotStatic();
39+
40+
/**
41+
* Matches final methods.
42+
*
43+
* @return A syntax conjunction element, which can be completed to form a full rule
44+
*/
45+
@PublicAPI(usage = ACCESS)
46+
CONJUNCTION areFinal();
47+
48+
/**
49+
* Matches non-final methods.
50+
*
51+
* @return A syntax conjunction element, which can be completed to form a full rule
52+
*/
53+
@PublicAPI(usage = ACCESS)
54+
CONJUNCTION areNotFinal();
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)