Java Code Examples for com.tngtech.archunit.lang.ArchRule#evaluate()

The following examples show how to use com.tngtech.archunit.lang.ArchRule#evaluate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MembersShouldConjunctionTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("ANDed_conditions")
public void andShould_ANDs_conditions(ArchRule rule) {
    EvaluationResult result = rule
            .evaluate(importClasses(RightOne.class, RightTwo.class, WrongOne.class, WrongTwo.class));

    FailureReport report = result.getFailureReport();
    assertThat(report.toString())
            .contains(String.format(
                    "members should not be declared in %s and should not be declared in %s",
                    WrongOne.class.getName(), WrongTwo.class.getName()));
    assertThat(report.getDetails()).containsOnly(
            isDeclaredInMessage(WrongOne.class, CONSTRUCTOR_NAME, 111),
            isDeclaredInMessage(WrongOne.class, "wrongMethod1", 113),
            isDeclaredInMessage(WrongTwo.class, CONSTRUCTOR_NAME, 117),
            isDeclaredInMessage(WrongTwo.class, "wrongMethod2", 119));
}
 
Example 2
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("accessFieldWhere_rules")
public void accessFieldWhere(ArchRule rule, String accessTypePlural, String accessTypeSingularRegex) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithField.class, ClassAccessingField.class, ClassAccessingWrongField.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should %s field where target is %s",
                    accessTypePlural, ClassWithField.class.getSimpleName()))
            .containsPattern(accessesFieldRegex(
                    ClassAccessingWrongField.class, accessTypeSingularRegex,
                    ClassAccessingField.class, "classWithField"))
            .doesNotMatch(accessesFieldRegex(
                    ClassAccessingField.class, accessTypeSingularRegex,
                    ClassWithField.class, "field"));
}
 
Example 3
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("accessField_rules")
public void accessField(ArchRule rule, String accessTypePlural, String accessTypeSingularRegex) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithField.class, ClassAccessingField.class, ClassAccessingWrongField.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should %s field %s.%s",
                    accessTypePlural, ClassWithField.class.getSimpleName(), "field"))
            .containsPattern(accessesFieldRegex(
                    ClassAccessingWrongField.class, accessTypeSingularRegex,
                    ClassAccessingField.class, "classWithField"))
            .doesNotMatch(accessesFieldRegex(
                    ClassAccessingField.class, accessTypeSingularRegex,
                    ClassWithField.class, "field"));
}
 
Example 4
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("callConstructor_rules")
public void callConstructor(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithConstructor.class, ClassCallingConstructor.class, ClassCallingWrongConstructor.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should call constructor %s.<init>(%s)",
                    ClassWithConstructor.class.getSimpleName(), String.class.getSimpleName()))
            .containsPattern(callConstructorRegex(
                    ClassCallingWrongConstructor.class,
                    ClassCallingConstructor.class, int.class, Date.class))
            .doesNotMatch(callConstructorRegex(
                    ClassCallingConstructor.class,
                    ClassWithConstructor.class, String.class));
}
 
Example 5
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("onlyCallConstructorsThat_rules")
public void onlyCallConstructorsThat(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithConstructor.class, ClassCallingConstructor.class, ClassCallingWrongConstructor.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should only call constructors that are declared in %s",
                    ClassWithConstructor.class.getName()))
            .containsPattern(callConstructorRegex(
                    ClassCallingWrongConstructor.class,
                    ClassCallingConstructor.class, int.class, Date.class))
            .doesNotMatch(accessesFieldRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class, "sets",
                    ClassAccessingFieldMethodAndConstructor.class, "wrongField"))
            .doesNotMatch(callConstructorRegex(
                    ClassCallingConstructor.class,
                    ClassWithConstructor.class, String.class));
}
 
Example 6
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("beInterfaces_rules")
public void beInterfaces(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains("classes should be interfaces")
            .containsPattern(String.format("Class <%s> is not an interface in %s",
                    quote(violated.getName()),
                    locationPattern(violated)))
            .doesNotMatch(String.format(".*%s.* interface.*", quote(satisfied.getName())));
}
 
Example 7
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("beInnerClasses_rules")
public void beInnerClasses(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains("classes should be inner classes")
            .containsPattern(String.format("Class <%s> is not an inner class", quote(violated.getName())))
            .doesNotMatch(String.format(".*%s.* inner class.*", quote(satisfied.getName())));
}
 
Example 8
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("notAssignableTo_rules")
public void notAssignableTo(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should not be assignable to %s", Collection.class.getName()))
            .containsPattern(String.format("Class <%s> is assignable to %s in %s",
                    quote(violated.getName()),
                    quote(Collection.class.getName()),
                    locationPattern(violated)))
            .doesNotMatch(String.format(".*%s.* assignable.*", quote(satisfied.getName())));
}
 
Example 9
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("annotated_rules")
public void annotatedWith(ArchRule rule, Class<?> correctClass, Class<?> wrongClass) {
    EvaluationResult result = rule.evaluate(importClasses(correctClass, wrongClass));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should be annotated with @%s", RuntimeRetentionAnnotation.class.getSimpleName()))
            .containsPattern(String.format("Class <%s> is not annotated with @%s in %s",
                    quote(wrongClass.getName()),
                    RuntimeRetentionAnnotation.class.getSimpleName(),
                    locationPattern(String.class)))
            .doesNotMatch(String.format(".*<%s>.*annotated.*", quote(correctClass.getName())));
}
 
Example 10
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("haveSimpleNameStartingWith_rules")
public void haveSimpleNameStartingWith(ArchRule rule, String prefix) {
    EvaluationResult result = rule.evaluate(importClasses(
            SomeClass.class, WrongNamedClass.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should have simple name starting with '%s'", prefix))
            .containsPattern(String.format("simple name of %s does not start with '%s' in %s",
                    quote(WrongNamedClass.class.getName()),
                    quote(prefix),
                    locationPattern(WrongNamedClass.class)))
            .doesNotContain(SomeClass.class.getName());
}
 
Example 11
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("haveNameMatching_rules")
public void haveNameMatching(ArchRule rule, String regex) {
    EvaluationResult result = rule.evaluate(importClasses(
            SomeClass.class, WrongNamedClass.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should have name matching '%s'", regex))
            .containsPattern(String.format("Class <%s> does not match '%s' in %s",
                    quote(WrongNamedClass.class.getName()),
                    quote(regex),
                    locationPattern(WrongNamedClass.class)))
            .doesNotContain(String.format("%s", SomeClass.class.getSimpleName()));
}
 
Example 12
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("haveSimpleName_rules")
public void haveSimpleName(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            SomeClass.class, WrongNamedClass.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should have simple name '%s'", SomeClass.class.getSimpleName()))
            .containsPattern(String.format("Class <%s> does not have simple name '%s' in %s",
                    quote(WrongNamedClass.class.getName()),
                    quote(SomeClass.class.getSimpleName()),
                    locationPattern(WrongNamedClass.class)))
            .doesNotMatch(String.format(".*<%s>.*simple name.*", SomeClass.class.getSimpleName()));
}
 
Example 13
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("beTopLevelClasses_rules")
public void beTopLevelClasses(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains("classes should be top level classes")
            .containsPattern(String.format("Class <%s> is not a top level class", quote(violated.getName())))
            .doesNotMatch(String.format(".*%s.* top level class.*", quote(satisfied.getName())));
}
 
Example 14
Source File: CodeUnitsShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("restricted_throwable_type_rules")
public void throwable_type_predicates(ArchRule rule, Collection<String> expectedMembers) {
    EvaluationResult result = rule.evaluate(importClasses(ClassWithVariousMembers.class));

    Set<String> actualMembers = parseMembers(ClassWithVariousMembers.class, result.getFailureReport().getDetails());
    assertThat(actualMembers).containsOnlyElementsOf(expectedMembers);
}
 
Example 15
Source File: FieldsShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("restricted_property_rule_ends")
public void property_predicates(ArchRule rule, Collection<String> expectedViolatingFields) {
    EvaluationResult result = rule
            .evaluate(importClasses(ClassWithVariousMembers.class));

    Set<String> actualFields = parseMembers(ClassWithVariousMembers.class, result.getFailureReport().getDetails());
    assertThat(actualFields).containsOnlyElementsOf(expectedViolatingFields);
}
 
Example 16
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("notBeInnerClasses_rules")
public void notBeInnerClasses(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains("classes should not be inner classes")
            .containsPattern(String.format("Class <%s> is an inner class", quote(violated.getName())))
            .doesNotMatch(String.format(".*%s.* inner class.*", quote(satisfied.getName())));
}
 
Example 17
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("assignableTo_rules")
public void assignableTo(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should be assignable to %s", Collection.class.getName()))
            .containsPattern(String.format("Class <%s> is not assignable to %s in %s",
                    quote(violated.getName()),
                    quote(Collection.class.getName()),
                    locationPattern(violated)))
            .doesNotMatch(String.format(".*%s.* assignable.*", quote(satisfied.getName())));
}
 
Example 18
Source File: CodeUnitsShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("restricted_parameter_types_rules")
public void parameter_types_predicates(ArchRule rule, Collection<String> expectedMembers) {
    EvaluationResult result = rule.evaluate(importClasses(ClassWithVariousMembers.class));

    Set<String> actualMembers = parseMembers(ClassWithVariousMembers.class, result.getFailureReport().getDetails());
    assertThat(actualMembers).containsOnlyElementsOf(expectedMembers);
}
 
Example 19
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("beClass_rules")
public void beClass(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

    assertThat(singleLineFailureReportOf(result))
            .contains("classes should be " + satisfied.getName())
            .containsPattern(String.format("Class <%s> is not %s in %s",
                    quote(violated.getName()),
                    quote(satisfied.getName()),
                    locationPattern(violated)))
            .doesNotMatch(String.format(".*<%s>.* is .*", quote(satisfied.getName())));
}
 
Example 20
Source File: ArchRuleCheckAssertion.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
ArchRuleCheckAssertion(ArchRule rule, JavaClasses classes) {
    evaluationResult = rule.evaluate(classes);
    error = checkRule(rule, classes);
}