Search in sources :

Example 61 with EvaluationResult

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

the class CodeUnitsShouldTest method restricted_code_units_calls_by_methods_predicate.

@Test
@UseDataProvider("restricted_code_unit_calls_by_methods_rules")
public void restricted_code_units_calls_by_methods_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 methods 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("Constructor <%s.%s> calls constructor <%s.%s>", quote(ClassCorrectlyCallingMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_ONE_ARG), quote(ClassWithMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_ONE_ARG))).containsPattern(String.format("Constructor <%s.%s> calls method <%s.%s>", quote(ClassCorrectlyCallingMethodAndConstructor.class.getName()), quote(CONSTRUCTOR_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 62 with EvaluationResult

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

the class CodeUnitsShouldTest method types_match_for_methods.

@Test
public void types_match_for_methods() {
    EvaluationResult result = methods().that().arePrivate().should(new ArchCondition<JavaMember>("exist") {

        @Override
        public void check(JavaMember item, ConditionEvents events) {
        }
    }).andShould(new ArchCondition<JavaMethod>("not exist") {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            events.add(SimpleConditionEvent.violated(method, "expected violation"));
        }
    }).evaluate(importClasses(ClassWithVariousMembers.class));
    assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
Also used : ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ClassWithVariousMembers(com.tngtech.archunit.lang.syntax.elements.GivenCodeUnitsTest.ClassWithVariousMembers) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 63 with EvaluationResult

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

the class FieldsShouldTest method property_predicates.

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

Example 64 with EvaluationResult

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

the class ViolationHandlingTest method multiple_accesses_necessary_for_violation_are_reported_together.

@Test
public void multiple_accesses_necessary_for_violation_are_reported_together() {
    EvaluationResult result = classes().should().accessField(Target.class, "notExisting").evaluate(importClasses(Origin.class, Target.class));
    FieldAccessRecorder fieldAccessRecorder = new FieldAccessRecorder();
    result.handleViolations(fieldAccessRecorder);
    assertThat(fieldAccessRecorder.getRecords()).as("number of violations").hasSize(1);
    ReportedViolation<JavaFieldAccess> reportedViolation = getOnlyElement(fieldAccessRecorder.getRecords());
    assertThatAccesses(reportedViolation.accesses).contain(accessToTargetField("fieldOne")).contain(accessToTargetField("fieldTwo")).contain(accessToTargetField("fieldThree"));
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test)

Example 65 with EvaluationResult

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

the class ViolationHandlingTest method method_calls_are_handled.

@Test
public void method_calls_are_handled() {
    EvaluationResult result = noClasses().should().accessClassesThat().haveFullyQualifiedName(Target.class.getName()).evaluate(importClasses(Origin.class, Target.class));
    MethodCallRecorder methodCallRecorder = new MethodCallRecorder();
    result.handleViolations(methodCallRecorder);
    assertThatAccesses(methodCallRecorder.getAccesses()).containOnly(expectedAccess().from(Origin.class, "call").to(Target.class, "callMeOne"), expectedAccess().from(Origin.class, "call").to(Target.class, "callMeTwo"));
}
Also used : 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