Skip to content

Commit efbbc32

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

File tree

7 files changed

+70
-11
lines changed

7 files changed

+70
-11
lines changed

archunit-example/example-junit4/src/test/java/com/tngtech/archunit/exampletest/junit4/CodingRulesTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ private void no_access_to_standard_streams_as_method(JavaClasses classes) {
4343
private final ArchRule loggers_should_be_private_static_final =
4444
fields().that().haveRawType(Logger.class)
4545
.should().bePrivate()
46-
.andShould().haveModifier(JavaModifier.STATIC)
47-
.andShould().haveModifier(JavaModifier.FINAL)
46+
.andShould().beStatic()
47+
.andShould().beFinal()
4848
.because("we agreed on this convention");
4949

5050
@ArchTest

archunit-example/example-junit5/src/test/java/com/tngtech/archunit/exampletest/junit5/CodingRulesTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ private void no_access_to_standard_streams_as_method(JavaClasses classes) {
4040
private final ArchRule loggers_should_be_private_static_final =
4141
fields().that().haveRawType(Logger.class)
4242
.should().bePrivate()
43-
.andShould().haveModifier(JavaModifier.STATIC)
44-
.andShould().haveModifier(JavaModifier.FINAL)
43+
.andShould().beStatic()
44+
.andShould().beFinal()
4545
.because("we agreed on this convention");
4646

4747
@ArchTest

archunit-example/example-plain/src/test/java/com/tngtech/archunit/exampletest/CodingRulesTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void classes_should_not_use_java_util_logging() {
4747
public void loggers_should_be_private_static_final() {
4848
fields().that().haveRawType(Logger.class)
4949
.should().bePrivate()
50-
.andShould().haveModifier(JavaModifier.STATIC)
51-
.andShould().haveModifier(JavaModifier.FINAL)
50+
.andShould().beStatic()
51+
.andShould().beFinal()
5252
.because("we agreed on this convention")
5353
.check(classes);
5454
}

archunit-integration-test/src/test/java/com/tngtech/archunit/integration/ExamplesIntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Stream<DynamicTest> CodingRulesTest() {
199199
expectThrownGenericExceptions(expectFailures);
200200

201201
expectFailures.ofRule("fields that have raw type java.util.logging.Logger should be private " +
202-
"and should have modifier STATIC and should have modifier FINAL, because we agreed on this convention")
202+
"and should be static and should be final, because we agreed on this convention")
203203
.by(ExpectedField.of(ClassViolatingCodingRules.class, "log").doesNotHaveModifier(JavaModifier.PRIVATE))
204204
.by(ExpectedField.of(ClassViolatingCodingRules.class, "log").doesNotHaveModifier(JavaModifier.FINAL));
205205

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

+21
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.tngtech.archunit.base.Function;
2020
import com.tngtech.archunit.core.domain.JavaClass;
2121
import com.tngtech.archunit.core.domain.JavaField;
22+
import com.tngtech.archunit.core.domain.JavaModifier;
2223
import com.tngtech.archunit.lang.ArchCondition;
2324
import com.tngtech.archunit.lang.ClassesTransformer;
2425
import com.tngtech.archunit.lang.Priority;
@@ -92,4 +93,24 @@ public FieldsShouldInternal haveRawType(DescribedPredicate<? super JavaClass> pr
9293
public FieldsShouldInternal notHaveRawType(DescribedPredicate<? super JavaClass> predicate) {
9394
return addCondition(not(ArchConditions.haveRawType(predicate)));
9495
}
96+
97+
@Override
98+
public FieldsShouldInternal beStatic() {
99+
return addCondition(ArchConditions.haveModifier(JavaModifier.STATIC).as("be static"));
100+
}
101+
102+
@Override
103+
public FieldsShouldInternal notBeStatic() {
104+
return addCondition(not(ArchConditions.haveModifier(JavaModifier.STATIC)).as("not be static"));
105+
}
106+
107+
@Override
108+
public FieldsShouldInternal beFinal() {
109+
return addCondition(ArchConditions.haveModifier(JavaModifier.FINAL).as("be final"));
110+
}
111+
112+
@Override
113+
public FieldsShouldInternal notBeFinal() {
114+
return addCondition(not(ArchConditions.haveModifier(JavaModifier.FINAL)).as("not be final"));
115+
}
95116
}

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

+32
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,36 @@ public interface FieldsShould<CONJUNCTION extends FieldsShouldConjunction> exten
143143
*/
144144
@PublicAPI(usage = ACCESS)
145145
CONJUNCTION notHaveRawType(DescribedPredicate<? super JavaClass> predicate);
146+
147+
/**
148+
* Asserts that fields are static.
149+
*
150+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
151+
*/
152+
@PublicAPI(usage = ACCESS)
153+
CONJUNCTION beStatic();
154+
155+
/**
156+
* Asserts that fields are non-static.
157+
*
158+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
159+
*/
160+
@PublicAPI(usage = ACCESS)
161+
CONJUNCTION notBeStatic();
162+
163+
/**
164+
* Asserts that fields are final.
165+
*
166+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
167+
*/
168+
@PublicAPI(usage = ACCESS)
169+
CONJUNCTION beFinal();
170+
171+
/**
172+
* Asserts that fields are non-final.
173+
*
174+
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
175+
*/
176+
@PublicAPI(usage = ACCESS)
177+
CONJUNCTION notBeFinal();
146178
}

archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/FieldsShouldTest.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Map;
66
import java.util.Set;
77

8+
import com.google.common.collect.ImmutableList;
89
import com.google.common.collect.ImmutableList;
910
import com.tngtech.archunit.lang.ArchRule;
1011
import com.tngtech.archunit.lang.EvaluationResult;
@@ -53,7 +54,12 @@ public static Object[][] restricted_property_rule_ends() {
5354
$(fields().should().haveRawType(equivalentTo(String.class).as(String.class.getName())), ImmutableList.of(FIELD_B, FIELD_C, FIELD_D)),
5455
$(fields().should().notHaveRawType(String.class), ImmutableList.of(FIELD_A)),
5556
$(fields().should().notHaveRawType(String.class.getName()), ImmutableList.of(FIELD_A)),
56-
$(fields().should().notHaveRawType(equivalentTo(String.class).as(String.class.getName())), ImmutableList.of(FIELD_A)));
57+
$(fields().should().notHaveRawType(equivalentTo(String.class).as(String.class.getName())), ImmutableList.of(FIELD_A)),
58+
$(fields().should().beFinal(), ImmutableList.of(FIELD_C, FIELD_D)),
59+
$(fields().should().notBeFinal(), ImmutableList.of(FIELD_A, FIELD_B)),
60+
$(fields().should().beStatic(), ImmutableList.of(FIELD_A, FIELD_C)),
61+
$(fields().should().notBeStatic(), ImmutableList.of(FIELD_B, FIELD_D))
62+
);
5763
}
5864

5965
@Test
@@ -73,11 +79,11 @@ public void property_predicates(ArchRule rule, Collection<String> expectedViolat
7379

7480
@SuppressWarnings({"unused"})
7581
private static class ClassWithVariousMembers {
76-
private String fieldA;
82+
private final String fieldA = "A";
7783
@A
78-
protected Object fieldB;
84+
protected static final Object fieldB = 'B';
7985
public List<?> fieldC;
80-
Map<?, ?> fieldD;
86+
static Map<?, ?> fieldD;
8187
}
8288

8389
private @interface A {

0 commit comments

Comments
 (0)