Skip to content

Commit ca18b17

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

File tree

6 files changed

+206
-1
lines changed

6 files changed

+206
-1
lines changed

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@
1717

1818
import com.tngtech.archunit.base.Function;
1919
import com.tngtech.archunit.core.domain.JavaMethod;
20+
import com.tngtech.archunit.core.domain.JavaModifier;
2021
import com.tngtech.archunit.lang.ArchCondition;
2122
import com.tngtech.archunit.lang.ClassesTransformer;
2223
import com.tngtech.archunit.lang.Priority;
24+
import com.tngtech.archunit.lang.conditions.ArchConditions;
25+
import com.tngtech.archunit.lang.syntax.elements.MethodsShould;
26+
import com.tngtech.archunit.lang.syntax.elements.MethodsShouldConjunction;
2327

24-
class MethodsShouldInternal extends AbstractCodeUnitsShouldInternal<JavaMethod, MethodsShouldInternal> {
28+
import static com.tngtech.archunit.lang.conditions.ArchConditions.not;
29+
30+
class MethodsShouldInternal
31+
extends AbstractCodeUnitsShouldInternal<JavaMethod, MethodsShouldInternal>
32+
implements MethodsShould<MethodsShouldInternal>, MethodsShouldConjunction {
2533

2634
MethodsShouldInternal(
2735
ClassesTransformer<? extends JavaMethod> classesTransformer,
@@ -53,4 +61,24 @@ private MethodsShouldInternal(
5361
MethodsShouldInternal copyWithNewCondition(ConditionAggregator<JavaMethod> newCondition) {
5462
return new MethodsShouldInternal(classesTransformer, priority, newCondition, prepareCondition);
5563
}
64+
65+
@Override
66+
public MethodsShouldInternal beStatic() {
67+
return addCondition(ArchConditions.haveModifier(JavaModifier.STATIC).as("be static"));
68+
}
69+
70+
@Override
71+
public MethodsShouldInternal notBeStatic() {
72+
return addCondition(not(ArchConditions.haveModifier(JavaModifier.STATIC)).as("not be static"));
73+
}
74+
75+
@Override
76+
public MethodsShouldInternal beFinal() {
77+
return addCondition(ArchConditions.haveModifier(JavaModifier.FINAL).as("be final"));
78+
}
79+
80+
@Override
81+
public MethodsShouldInternal notBeFinal() {
82+
return addCondition(not(ArchConditions.haveModifier(JavaModifier.FINAL)).as("not be final"));
83+
}
5684
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.tngtech.archunit.PublicAPI;
1919
import com.tngtech.archunit.base.DescribedPredicate;
2020
import com.tngtech.archunit.core.domain.JavaMethod;
21+
import com.tngtech.archunit.lang.ArchCondition;
2122

2223
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
2324

@@ -30,4 +31,12 @@ public interface GivenMethods extends GivenCodeUnits<JavaMethod> {
3031
@Override
3132
@PublicAPI(usage = ACCESS)
3233
GivenMethodsConjunction that(DescribedPredicate<? super JavaMethod> predicate);
34+
35+
@Override
36+
@PublicAPI(usage = ACCESS)
37+
MethodsShould<?> should();
38+
39+
@Override
40+
@PublicAPI(usage = ACCESS)
41+
MethodsShouldConjunction should(ArchCondition<? super JavaMethod> condition);
3342
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.tngtech.archunit.PublicAPI;
1919
import com.tngtech.archunit.base.DescribedPredicate;
2020
import com.tngtech.archunit.core.domain.JavaMethod;
21+
import com.tngtech.archunit.lang.ArchCondition;
2122

2223
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
2324

@@ -38,4 +39,12 @@ public interface GivenMethodsConjunction extends GivenCodeUnitsConjunction<JavaM
3839
@Override
3940
@PublicAPI(usage = ACCESS)
4041
GivenMethodsConjunction or(DescribedPredicate<? super JavaMethod> predicate);
42+
43+
@Override
44+
@PublicAPI(usage = ACCESS)
45+
MethodsShould<?> should();
46+
47+
@Override
48+
@PublicAPI(usage = ACCESS)
49+
MethodsShouldConjunction should(ArchCondition<? super JavaMethod> condition);
4150
}
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 MethodsShould<CONJUNCTION extends MethodsShouldConjunction> extends CodeUnitsShould<CONJUNCTION> {
23+
24+
/**
25+
* Asserts that methods are static.
26+
*
27+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
28+
*/
29+
@PublicAPI(usage = ACCESS)
30+
CONJUNCTION beStatic();
31+
32+
/**
33+
* Asserts that methods are non-static.
34+
*
35+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
36+
*/
37+
@PublicAPI(usage = ACCESS)
38+
CONJUNCTION notBeStatic();
39+
40+
/**
41+
* Asserts that methods are final.
42+
*
43+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
44+
*/
45+
@PublicAPI(usage = ACCESS)
46+
CONJUNCTION beFinal();
47+
48+
/**
49+
* Asserts that methods are non-final.
50+
*
51+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
52+
*/
53+
@PublicAPI(usage = ACCESS)
54+
CONJUNCTION notBeFinal();
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
import com.tngtech.archunit.core.domain.JavaMethod;
20+
import com.tngtech.archunit.lang.ArchCondition;
21+
22+
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
23+
24+
public interface MethodsShouldConjunction extends CodeUnitsShouldConjunction<JavaMethod> {
25+
26+
@Override
27+
@PublicAPI(usage = ACCESS)
28+
MethodsShouldConjunction andShould(ArchCondition<? super JavaMethod> condition);
29+
30+
@Override
31+
@PublicAPI(usage = ACCESS)
32+
MethodsShould<?> andShould();
33+
34+
@Override
35+
@PublicAPI(usage = ACCESS)
36+
MethodsShouldConjunction orShould(ArchCondition<? super JavaMethod> condition);
37+
38+
@Override
39+
@PublicAPI(usage = ACCESS)
40+
MethodsShould<?> orShould();
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.tngtech.archunit.lang.syntax.elements;
2+
3+
import com.google.common.collect.ImmutableSet;
4+
import com.tngtech.archunit.lang.ArchRule;
5+
import com.tngtech.archunit.lang.EvaluationResult;
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+
import java.util.Set;
14+
15+
import static com.tngtech.archunit.core.domain.TestUtils.importClasses;
16+
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
17+
import static com.tngtech.archunit.lang.syntax.elements.MembersShouldTest.parseMembers;
18+
import static com.tngtech.java.junit.dataprovider.DataProviders.$;
19+
import static com.tngtech.java.junit.dataprovider.DataProviders.$$;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
@RunWith(DataProviderRunner.class)
23+
public class MethodsShouldTest {
24+
25+
@DataProvider
26+
public static Object[][] restricted_property_rule_ends() {
27+
return $$(
28+
$(methods().should().beFinal(), ImmutableSet.of(METHOD_C, METHOD_D)),
29+
$(methods().should().notBeFinal(), ImmutableSet.of(METHOD_A, METHOD_B)),
30+
$(methods().should().beStatic(), ImmutableSet.of(METHOD_A, METHOD_C)),
31+
$(methods().should().notBeStatic(), ImmutableSet.of(METHOD_B, METHOD_D)),
32+
$(methods().should().notBeFinal().andShould().notBeStatic(), ImmutableSet.of(METHOD_A, METHOD_B, METHOD_D)),
33+
$(methods().should().notBeFinal().orShould().notBeStatic(), ImmutableSet.of(METHOD_B))
34+
);
35+
}
36+
37+
@Test
38+
@UseDataProvider("restricted_property_rule_ends")
39+
public void property_predicates(ArchRule ruleStart, Collection<String> expectedMembers) {
40+
EvaluationResult result = ruleStart.evaluate(importClasses(ClassWithVariousMembers.class));
41+
42+
Set<String> actualMethods = parseMembers(ClassWithVariousMembers.class, result.getFailureReport().getDetails());
43+
assertThat(actualMethods).containsOnlyElementsOf(expectedMembers);
44+
}
45+
46+
private static final String METHOD_A = "methodA([I)";
47+
private static final String METHOD_B = "methodB(boolean)";
48+
private static final String METHOD_C = "methodC(char)";
49+
private static final String METHOD_D = "methodD()";
50+
51+
@SuppressWarnings({"unused"})
52+
private static class ClassWithVariousMembers {
53+
public final void methodA(int[] array) {
54+
}
55+
protected static final void methodB(boolean flag) {
56+
}
57+
private void methodC(char ch) {
58+
}
59+
static int methodD() {
60+
return 0;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)