Search in sources :

Example 56 with EvaluationResult

use of com.tngtech.archunit.lang.EvaluationResult in project ArchUnit by TNG.

the class CodeUnitsShouldTest method restricted_code_units_calls_by_constructors_predicate.

@Test
@UseDataProvider("restricted_code_unit_calls_by_constructors_rules")
public void restricted_code_units_calls_by_constructors_predicate(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(ClassWithMethodAndConstructor.class, ClassCorrectlyCallingMethodAndConstructor.class, ClassWronglyCallingMethodAndConstructor.class));
    assertThat(singleLineFailureReportOf(result)).contains("Rule 'code units should only be called by constructors that").containsPattern(String.format("Method <%s.%s> calls method <%s.%s>", quote(ClassWronglyCallingMethodAndConstructor.class.getName()), quote(METHOD_CALL_WRONGLY), quote(ClassWithMethodAndConstructor.class.getName()), quote(METHOD_ONE_ARG))).containsPattern(String.format("Method <%s.%s> calls constructor <%s.%s>", quote(ClassWronglyCallingMethodAndConstructor.class.getName()), quote(METHOD_CALL_WRONGLY), quote(ClassWithMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_ONE_ARG))).containsPattern(String.format("Method <%s.%s> calls constructor <%s.%s>", quote(ClassCorrectlyCallingMethodAndConstructor.class.getName()), quote(METHOD_ONE_ARG), quote(ClassWithMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_ONE_ARG))).containsPattern(String.format("Method <%s.%s> calls method <%s.%s>", quote(ClassCorrectlyCallingMethodAndConstructor.class.getName()), quote(METHOD_ONE_ARG), quote(ClassWithMethodAndConstructor.class.getName()), quote(METHOD_ONE_ARG)));
}
Also used : EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 57 with EvaluationResult

use of com.tngtech.archunit.lang.EvaluationResult in project ArchUnit by TNG.

the class CodeUnitsShouldTest method parameter_types_predicates.

@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);
}
Also used : ClassWithVariousMembers(com.tngtech.archunit.lang.syntax.elements.GivenCodeUnitsTest.ClassWithVariousMembers) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 58 with EvaluationResult

use of com.tngtech.archunit.lang.EvaluationResult in project ArchUnit by TNG.

the class CodeUnitsShouldTest method restricted_methods_calls_by_code_units_predicate.

@Test
@UseDataProvider("restricted_methods_calls_by_code_units_rules")
public void restricted_methods_calls_by_code_units_predicate(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(ClassWithMethodAndConstructor.class, ClassCorrectlyCallingMethodAndConstructor.class, ClassWronglyCallingMethodAndConstructor.class));
    assertThat(singleLineFailureReportOf(result)).contains("Rule 'methods should only be called by code units that").containsPattern(String.format("Method <%s.%s> calls method <%s.%s>", quote(ClassWronglyCallingMethodAndConstructor.class.getName()), quote(METHOD_CALL_WRONGLY), quote(ClassWithMethodAndConstructor.class.getName()), quote(METHOD_ONE_ARG)));
}
Also used : EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 59 with EvaluationResult

use of com.tngtech.archunit.lang.EvaluationResult in project ArchUnit by TNG.

the class CodeUnitsShouldTest method restricted_constructors_calls_by_constructors_predicate.

@Test
@UseDataProvider("restricted_constructors_calls_by_constructors_rules")
public void restricted_constructors_calls_by_constructors_predicate(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(ClassWithMethodAndConstructor.class, ClassCorrectlyCallingMethodAndConstructor.class, ClassWronglyCallingMethodAndConstructor.class));
    assertThat(singleLineFailureReportOf(result)).contains("Rule 'constructors should only be called by constructors that").containsPattern(String.format("Method <%s.%s> calls constructor <%s.%s>", quote(ClassWronglyCallingMethodAndConstructor.class.getName()), quote(METHOD_CALL_WRONGLY), quote(ClassWithMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_ONE_ARG))).containsPattern(String.format("Method <%s.%s> calls constructor <%s.%s>", quote(ClassCorrectlyCallingMethodAndConstructor.class.getName()), quote(METHOD_ONE_ARG), quote(ClassWithMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_ONE_ARG)));
}
Also used : EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 60 with EvaluationResult

use of com.tngtech.archunit.lang.EvaluationResult in project ArchUnit by TNG.

the class CodeUnitsShouldTest method complex_code_unit_syntax.

@Test
public void complex_code_unit_syntax() {
    EvaluationResult result = codeUnits().that(doNotHaveParametersOfType(List.class)).should().beAnnotatedWith(A.class).andShould().beProtected().orShould().haveRawReturnType(String.class).evaluate(importClasses(ClassWithVariousMembers.class));
    assertViolation(result);
    String failure = getOnlyElement(result.getFailureReport().getDetails());
    assertThat(failure).matches(String.format(".*Constructor .*%s.* does not have modifier PROTECTED.*", quote(CONSTRUCTOR_ONE_ARG))).contains("and Constructor").matches(String.format(".*Constructor .*%s.* is not annotated with @A.*", quote(CONSTRUCTOR_ONE_ARG))).matches(String.format(".*Constructor .*%s.* does not have raw return type %s.*", quote(CONSTRUCTOR_ONE_ARG), String.class.getName()));
}
Also used : A(com.tngtech.archunit.lang.syntax.elements.GivenCodeUnitsTest.A) ClassWithVariousMembers(com.tngtech.archunit.lang.syntax.elements.GivenCodeUnitsTest.ClassWithVariousMembers) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test)

Aggregations

EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)140 Test (org.junit.Test)132 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)107 CanBeAnnotatedTest (com.tngtech.archunit.core.domain.properties.CanBeAnnotatedTest)63 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)13 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)13 SomeClass (com.tngtech.archunit.lang.syntax.elements.testclasses.SomeClass)12 WrongNamedClass (com.tngtech.archunit.lang.syntax.elements.testclasses.WrongNamedClass)12 ArchCondition (com.tngtech.archunit.lang.ArchCondition)7 ArchRule (com.tngtech.archunit.lang.ArchRule)6 FailureReport (com.tngtech.archunit.lang.FailureReport)6 ClassWithVariousMembers (com.tngtech.archunit.lang.syntax.elements.GivenCodeUnitsTest.ClassWithVariousMembers)6 SimpleFieldAndMethod (com.tngtech.archunit.lang.syntax.elements.testclasses.SimpleFieldAndMethod)6 ArchTest (com.tngtech.archunit.junit.ArchTest)5 OnionArchitecture (com.tngtech.archunit.library.Architectures.OnionArchitecture)5 ArchConfiguration (com.tngtech.archunit.ArchConfiguration)4 JavaMember (com.tngtech.archunit.core.domain.JavaMember)4 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)4 ClassWithVariousMembers (com.tngtech.archunit.lang.syntax.elements.GivenMembersTest.ClassWithVariousMembers)3 Date (java.util.Date)3