Skip to content

Commit

Permalink
add fields().should().{be,notBe}{Static,Final} syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Hanke <Manfred.Hanke@tngtech.com>
  • Loading branch information
hankem committed Mar 29, 2019
1 parent c590ec8 commit efbbc32
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ private void no_access_to_standard_streams_as_method(JavaClasses classes) {
private final ArchRule loggers_should_be_private_static_final =
fields().that().haveRawType(Logger.class)
.should().bePrivate()
.andShould().haveModifier(JavaModifier.STATIC)
.andShould().haveModifier(JavaModifier.FINAL)
.andShould().beStatic()
.andShould().beFinal()
.because("we agreed on this convention");

@ArchTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ private void no_access_to_standard_streams_as_method(JavaClasses classes) {
private final ArchRule loggers_should_be_private_static_final =
fields().that().haveRawType(Logger.class)
.should().bePrivate()
.andShould().haveModifier(JavaModifier.STATIC)
.andShould().haveModifier(JavaModifier.FINAL)
.andShould().beStatic()
.andShould().beFinal()
.because("we agreed on this convention");

@ArchTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void classes_should_not_use_java_util_logging() {
public void loggers_should_be_private_static_final() {
fields().that().haveRawType(Logger.class)
.should().bePrivate()
.andShould().haveModifier(JavaModifier.STATIC)
.andShould().haveModifier(JavaModifier.FINAL)
.andShould().beStatic()
.andShould().beFinal()
.because("we agreed on this convention")
.check(classes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Stream<DynamicTest> CodingRulesTest() {
expectThrownGenericExceptions(expectFailures);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.tngtech.archunit.base.Function;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ClassesTransformer;
import com.tngtech.archunit.lang.Priority;
Expand Down Expand Up @@ -92,4 +93,24 @@ public FieldsShouldInternal haveRawType(DescribedPredicate<? super JavaClass> pr
public FieldsShouldInternal notHaveRawType(DescribedPredicate<? super JavaClass> predicate) {
return addCondition(not(ArchConditions.haveRawType(predicate)));
}

@Override
public FieldsShouldInternal beStatic() {
return addCondition(ArchConditions.haveModifier(JavaModifier.STATIC).as("be static"));
}

@Override
public FieldsShouldInternal notBeStatic() {
return addCondition(not(ArchConditions.haveModifier(JavaModifier.STATIC)).as("not be static"));
}

@Override
public FieldsShouldInternal beFinal() {
return addCondition(ArchConditions.haveModifier(JavaModifier.FINAL).as("be final"));
}

@Override
public FieldsShouldInternal notBeFinal() {
return addCondition(not(ArchConditions.haveModifier(JavaModifier.FINAL)).as("not be final"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,36 @@ public interface FieldsShould<CONJUNCTION extends FieldsShouldConjunction> exten
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notHaveRawType(DescribedPredicate<? super JavaClass> predicate);

/**
* Asserts that fields are static.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION beStatic();

/**
* Asserts that fields are non-static.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notBeStatic();

/**
* Asserts that fields are final.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION beFinal();

/**
* Asserts that fields are non-final.
*
* @return A syntax element that can either be used as working rule, or to continue specifying a more complex rule
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION notBeFinal();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.EvaluationResult;
Expand Down Expand Up @@ -53,7 +54,12 @@ public static Object[][] restricted_property_rule_ends() {
$(fields().should().haveRawType(equivalentTo(String.class).as(String.class.getName())), ImmutableList.of(FIELD_B, FIELD_C, FIELD_D)),
$(fields().should().notHaveRawType(String.class), ImmutableList.of(FIELD_A)),
$(fields().should().notHaveRawType(String.class.getName()), ImmutableList.of(FIELD_A)),
$(fields().should().notHaveRawType(equivalentTo(String.class).as(String.class.getName())), ImmutableList.of(FIELD_A)));
$(fields().should().notHaveRawType(equivalentTo(String.class).as(String.class.getName())), ImmutableList.of(FIELD_A)),
$(fields().should().beFinal(), ImmutableList.of(FIELD_C, FIELD_D)),
$(fields().should().notBeFinal(), ImmutableList.of(FIELD_A, FIELD_B)),
$(fields().should().beStatic(), ImmutableList.of(FIELD_A, FIELD_C)),
$(fields().should().notBeStatic(), ImmutableList.of(FIELD_B, FIELD_D))
);
}

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

@SuppressWarnings({"unused"})
private static class ClassWithVariousMembers {
private String fieldA;
private final String fieldA = "A";
@A
protected Object fieldB;
protected static final Object fieldB = 'B';
public List<?> fieldC;
Map<?, ?> fieldD;
static Map<?, ?> fieldD;
}

private @interface A {
Expand Down

0 comments on commit efbbc32

Please sign in to comment.