com.tngtech.archunit.lang.ArchRule Java Examples

The following examples show how to use com.tngtech.archunit.lang.ArchRule. 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: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("resideOutsideOfPackages_rules")
public void resideOutsideOfPackages(ArchRule rule, String... packageIdentifiers) {
    checkTestStillValid(packageIdentifiers,
            ImmutableSet.of(ArchRule.class, ArchConfiguration.class),
            ImmutableSet.<Class<?>>of(GivenObjects.class));

    EvaluationResult result = rule.evaluate(importClasses(
            ArchRule.class, ArchCondition.class, ArchConfiguration.class, GivenObjects.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should reside outside of packages ['%s']",
                    Joiner.on("', '").join(packageIdentifiers)))
            .containsPattern(doesntResideOutsideOfPackagesPatternFor(ArchRule.class, packageIdentifiers))
            .containsPattern(doesntResideOutsideOfPackagesPatternFor(ArchCondition.class, packageIdentifiers))
            .doesNotContain(String.format("%s", GivenObjects.class.getSimpleName()));
}
 
Example #2
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("onlyCallCodeUnitsThat_rules")
public void onlyCallCodeUnitsThat(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithFieldMethodAndConstructor.class, ClassAccessingFieldMethodAndConstructor.class,
            ClassAccessingWrongFieldMethodAndConstructor.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should only call code units that are declared in %s",
                    ClassWithFieldMethodAndConstructor.class.getName()))
            .containsPattern(callCodeUnitRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class,
                    ClassAccessingFieldMethodAndConstructor.class, CONSTRUCTOR_NAME, int.class, Date.class))
            .containsPattern(callCodeUnitRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class,
                    ClassAccessingFieldMethodAndConstructor.class, "call"))
            .doesNotMatch(accessesFieldRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class, "sets",
                    ClassAccessingFieldMethodAndConstructor.class, "wrongField"))
            .doesNotMatch(callCodeUnitRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class,
                    ClassAccessingFieldMethodAndConstructor.class, "wrongField"))
            .doesNotMatch(callCodeUnitRegex(
                    ClassAccessingFieldMethodAndConstructor.class,
                    ClassWithFieldMethodAndConstructor.class, ""));
}
 
Example #3
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("theClass_should_haveSimpleNameStartingWith_rules")
public void theClass_should_haveSimpleNameStartingWith(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    String simpleName = SomeClass.class.getSimpleName();
    String prefix = simpleName.substring(0, simpleName.length() - 1);
    assertThatRules(satisfiedRule, unsatisfiedRule, SomeClass.class, Object.class)
            .haveSuccessfulRuleText("the class %s should have simple name starting with '%s'",
                    SomeClass.class.getName(), prefix)
            .haveFailingRuleText("the class %s should have simple name not starting with '%s'",
                    SomeClass.class.getName(), prefix)
            .containFailureDetail(String.format("simple name of %s starts with '%s' in %s",
                    quote(SomeClass.class.getName()),
                    quote(prefix),
                    locationPattern(SomeClass.class)))
            .doNotContainFailureDetail(quote(Object.class.getName()));
}
 
Example #4
Source File: FreezingArchRuleTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void only_reports_relevant_lines_of_multi_line_events() {
    TestViolationStore violationStore = new TestViolationStore();

    createFrozen(violationStore, rule("some description").withViolations(
            new ViolatedEvent("first violation1", "second violation1"),
            new ViolatedEvent("first violation2", "second violation2")));

    ArchRule anotherViolation = rule("some description").withViolations(
            new ViolatedEvent("first violation1", "second violation1", "third violation1"),
            new ViolatedEvent("first violation2", "second violation2"));
    ArchRule frozenWithNewViolation = freeze(anotherViolation).persistIn(violationStore);

    assertThat(frozenWithNewViolation)
            .checking(importClasses(getClass()))
            .hasOnlyViolations("third violation1");
}
 
Example #5
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("noClass_should_bePackagePrivate_rules")
public void noClass_should_bePackagePrivate(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    assertThatRules(satisfiedRule, unsatisfiedRule, PackagePrivateClass.class, Object.class)
            .haveSuccessfulRuleText("no class %s should not be package private",
                    PackagePrivateClass.class.getName())
            .haveFailingRuleText("no class %s should be package private",
                    PackagePrivateClass.class.getName())
            .containFailureDetail(String.format("Class <%s> does not have modifier %s in %s "
                            + "and Class <%s> does not have modifier %s in %s "
                            + "and Class <%s> does not have modifier %s in %s",
                    quote(PackagePrivateClass.class.getName()),
                    quote(JavaModifier.PRIVATE.name()),
                    locationPattern(GivenClassShouldTest.class),
                    quote(PackagePrivateClass.class.getName()),
                    quote(JavaModifier.PROTECTED.name()),
                    locationPattern(GivenClassShouldTest.class),
                    quote(PackagePrivateClass.class.getName()),
                    quote(JavaModifier.PUBLIC.name()),
                    locationPattern(GivenClassShouldTest.class)))
            .doNotContainFailureDetail(quote(Object.class.getName()));
}
 
Example #6
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("theClass_should_haveSimpleNameEndingWith_rules")
public void theClass_should_haveSimpleNameEndingWith(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    String simpleName = SomeClass.class.getSimpleName();
    String suffix = simpleName.substring(1);
    assertThatRules(satisfiedRule, unsatisfiedRule, SomeClass.class, Object.class)
            .haveSuccessfulRuleText("the class %s should have simple name ending with '%s'",
                    SomeClass.class.getName(), suffix)
            .haveFailingRuleText("the class %s should have simple name not ending with '%s'",
                    SomeClass.class.getName(), suffix)
            .containFailureDetail(String.format("simple name of %s ends with '%s' in %s",
                    quote(SomeClass.class.getName()),
                    quote(suffix),
                    locationPattern(SomeClass.class)))
            .doNotContainFailureDetail(quote(Object.class.getName()));
}
 
Example #7
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("theClass_should_haveNameMatching_rules")
public void theClass_should_haveNameMatching(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    String regex = containsPartOfRegex(SomeClass.class.getSimpleName());

    assertThatRules(satisfiedRule, unsatisfiedRule, SomeClass.class, Object.class)
            .haveSuccessfulRuleText("the class %s should have name matching '%s'",
                    SomeClass.class.getName(), regex)
            .haveFailingRuleText("the class %s should have name not matching '%s'",
                    SomeClass.class.getName(), regex)
            .containFailureDetail(String.format("Class <%s> matches '%s' in %s",
                    quote(SomeClass.class.getName()),
                    quote(regex),
                    locationPattern(SomeClass.class)))
            .doNotContainFailureDetail(quote(regex));
}
 
Example #8
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("noClass_should_resideInAPackage_rules")
public void noClass_should_resideInAPackage(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    String thePackage = SomeClass.class.getPackage().getName();

    assertThatRules(satisfiedRule, unsatisfiedRule, SomeClass.class, Object.class)
            .haveSuccessfulRuleText("no class %s should reside outside of package '%s'",
                    SomeClass.class.getName(), thePackage)
            .haveFailingRuleText("no class %s should reside in a package '%s'",
                    SomeClass.class.getName(), thePackage)
            .containFailureDetail(String.format("Class <%s> does reside in a package '%s' in %s",
                    quote(SomeClass.class.getName()),
                    quote(thePackage),
                    locationPattern(SomeClass.class)))
            .doNotContainFailureDetail(quote(Object.class.getName()));
}
 
Example #9
Source File: ClassesShouldConjunctionTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("ORed_conditions")
public void orShould_ORs_conditions(ArchRule rule) {
    EvaluationResult result = rule
            .evaluate(importClasses(RightOne.class, RightTwo.class, Wrong.class));

    FailureReport report = result.getFailureReport();
    assertThat(report.toString())
            .contains(String.format(
                    "classes should have fully qualified name '%s' or should have fully qualified name '%s'",
                    RightOne.class.getName(), RightTwo.class.getName()));
    assertThat(report.getDetails()).containsOnly(
            String.format("%s and %s",
                    doesntHaveFqnMessage(Wrong.class, RightOne.class),
                    doesntHaveFqnMessage(Wrong.class, RightTwo.class)));
}
 
Example #10
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("callMethodWhere_rules")
public void callMethodWhere(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithMethod.class, ClassCallingMethod.class, ClassCallingWrongMethod.class, ClassCallingSelf.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should call method where target is %s",
                    ClassWithMethod.class.getSimpleName()))
            .containsPattern(callMethodRegex(
                    ClassCallingWrongMethod.class,
                    ClassCallingMethod.class, "call"))
            .containsPattern(callMethodRegex(
                    ClassCallingSelf.class,
                    ClassCallingSelf.class, "target"))
            .doesNotMatch(callMethodRegex(
                    ClassCallingMethod.class,
                    ClassWithMethod.class, "method", String.class));
}
 
Example #11
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("theClass_should_resideInAnyPackage_rules")
public void theClass_should_resideInAnyPackage(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    String firstPackage = SomeClass.class.getPackage().getName();
    String secondPackage = Object.class.getPackage().getName();
    String[] packageIdentifiers = {firstPackage, secondPackage};

    assertThatRules(satisfiedRule, unsatisfiedRule, SomeClass.class, Object.class)
            .haveSuccessfulRuleText("the class %s should reside in any package ['%s']",
                    SomeClass.class.getName(),
                    Joiner.on("', '").join(packageIdentifiers))
            .haveFailingRuleText("the class %s should reside outside of packages ['%s']",
                    SomeClass.class.getName(),
                    Joiner.on("', '").join(packageIdentifiers))
            .containFailureDetail(String.format("Class <%s> does not reside outside of packages \\['%s'\\] in %s",
                    quote(SomeClass.class.getName()),
                    quote(Joiner.on("', '").join(packageIdentifiers)),
                    locationPattern(SomeClass.class)))
            .doNotContainFailureDetail(quote(Object.class.getName()));
}
 
Example #12
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 #13
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("resideInAnyPackage_rules")
public void resideInAnyPackage(ArchRule rule, String... packageIdentifiers) {
    checkTestStillValid(packageIdentifiers,
            ImmutableSet.of(ArchRule.class, ArchConfiguration.class),
            ImmutableSet.<Class<?>>of(GivenObjects.class));

    EvaluationResult result = rule.evaluate(importClasses(
            ArchRule.class, ArchConfiguration.class, GivenObjects.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should reside in any package ['%s']",
                    Joiner.on("', '").join(packageIdentifiers)))
            .containsPattern(doesntResideInAnyPackagePatternFor(GivenObjects.class, packageIdentifiers))
            .doesNotContain(String.format("%s", ArchRule.class.getSimpleName()))
            .doesNotContain(String.format("%s", ArchConfiguration.class.getSimpleName()));
}
 
Example #14
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("onlyAccessFieldsThat_rules")
public void onlyAccessFieldsThat(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithField.class, ClassAccessingField.class, ClassAccessingWrongField.class));

    assertThat(singleLineFailureReportOf(result))
            .contains("classes should only access fields that are declared in " + ClassWithField.class.getName())
            .containsPattern(accessesFieldRegex(
                    ClassAccessingWrongField.class, "(sets|gets|accesses)",
                    ClassAccessingField.class, "classWithField"))
            .containsPattern(accessesFieldRegex(
                    ClassAccessingField.class, "(sets|gets|accesses)",
                    ClassAccessingField.class, "classWithField"))
            .doesNotMatch(accessesFieldRegex(
                    ClassAccessingField.class, "(sets|gets|accesses)",
                    ClassWithField.class, "field"))
            .doesNotMatch(accessesFieldRegex(
                    ClassWithField.class, "(sets|gets|accesses)",
                    ClassWithField.class, "field"));
}
 
Example #15
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("callCodeUnitWhere_rules")
public void callCodeUnitWhere(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithFieldMethodAndConstructor.class, ClassAccessingFieldMethodAndConstructor.class,
            ClassAccessingWrongFieldMethodAndConstructor.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should call code unit where target is %s",
                    ClassWithFieldMethodAndConstructor.class.getSimpleName()))
            .containsPattern(callCodeUnitRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class,
                    ClassAccessingFieldMethodAndConstructor.class, CONSTRUCTOR_NAME, int.class, Date.class))
            .containsPattern(callCodeUnitRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class,
                    ClassAccessingFieldMethodAndConstructor.class, "call"))
            .doesNotMatch(callCodeUnitRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class,
                    ClassAccessingFieldMethodAndConstructor.class, "wrongField"))
            .doesNotMatch(callCodeUnitRegex(
                    ClassAccessingFieldMethodAndConstructor.class,
                    ClassWithFieldMethodAndConstructor.class, ""));
}
 
Example #16
Source File: MembersShouldConjunctionTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("ORed_conditions")
public void orShould_ORs_conditions(ArchRule rule) {
    EvaluationResult result = rule
            .evaluate(importClasses(RightOne.class, RightTwo.class, WrongOne.class));

    FailureReport report = result.getFailureReport();
    assertThat(report.toString())
            .contains(String.format(
                    "members should be declared in %s or should be declared in %s",
                    RightOne.class.getName(), RightTwo.class.getName()));
    assertThat(report.getDetails()).containsOnly(
            String.format("%s and %s",
                    isNotDeclaredInMessage(WrongOne.class, CONSTRUCTOR_NAME, RightOne.class, 111),
                    isNotDeclaredInMessage(WrongOne.class, CONSTRUCTOR_NAME, RightTwo.class, 111)),
            String.format("%s and %s",
                    isNotDeclaredInMessage(WrongOne.class, "wrongMethod1", RightOne.class, 113),
                    isNotDeclaredInMessage(WrongOne.class, "wrongMethod1", RightTwo.class, 113)));
}
 
Example #17
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("onlyCallMethodsThat_rules")
public void onlyCallMethodsThat(ArchRule rule) {
    EvaluationResult result = rule.evaluate(importClasses(
            ClassWithMethod.class, ClassCallingMethod.class, ClassCallingWrongMethod.class));

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should only call methods that are declared in %s",
                    ClassWithMethod.class.getName()))
            .containsPattern(callMethodRegex(
                    ClassCallingWrongMethod.class,
                    ClassCallingMethod.class, "call"))
            .doesNotMatch(accessesFieldRegex(
                    ClassAccessingWrongFieldMethodAndConstructor.class, "sets",
                    ClassAccessingFieldMethodAndConstructor.class, "wrongField"))
            .doesNotMatch(callMethodRegex(
                    ClassCallingMethod.class,
                    ClassWithMethod.class, "method", String.class));
}
 
Example #18
Source File: ArchitecturesTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void onion_architecture_because_clause() {
    ArchRule architecture = onionArchitecture()
            .domainModels("onionarchitecture.domain.model..")
            .domainServices("onionarchitecture.domain.service..")
            .applicationServices("onionarchitecture.application..")
            .adapter("cli", "onionarchitecture.adapter.cli..")
            .adapter("persistence", "onionarchitecture.adapter.persistence..")
            .adapter("rest", "onionarchitecture.adapter.rest.command..", "onionarchitecture.adapter.rest.query..")
            .as("overridden")
            .because("some reason");

    assertThat(architecture.getDescription()).isEqualTo("overridden, because some reason");
}
 
Example #19
Source File: GivenClassShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("noClass_should_haveFullyQualifiedName_rules")
public void noClass_should_haveFullyQualifiedName(ArchRule satisfiedRule, ArchRule unsatisfiedRule) {
    assertThatRules(satisfiedRule, unsatisfiedRule, SomeClass.class, Object.class)
            .haveSuccessfulRuleText("no class %s should not have fully qualified name '%s'",
                    SomeClass.class.getName(), SomeClass.class.getName())
            .haveFailingRuleText("no class %s should have fully qualified name '%s'",
                    SomeClass.class.getName(), SomeClass.class.getName())
            .containFailureDetail(String.format("Class <%s> has fully qualified name '%s' in %s",
                    quote(SomeClass.class.getName()),
                    quote(SomeClass.class.getName()),
                    locationPattern(SomeClass.class)))
            .doNotContainFailureDetail(quote(Object.class.getName()));
}
 
Example #20
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 #21
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("implement_satisfied_rules")
public void implement_satisfied(ArchRule rule, Class<?> satisfied) {
    EvaluationResult result = rule.evaluate(importHierarchies(satisfied));

    assertThat(singleLineFailureReportOf(result))
            .doesNotMatch(String.format(".*%s.* implement.*", quote(satisfied.getName())));
}
 
Example #22
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("notBeNestedClasses_rules")
public void notBeNestedClasses(ArchRule rule, Class<?> satisfied, Class<?> violated) {
    EvaluationResult result = rule.evaluate(importClasses(satisfied, violated));

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

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

    assertThat(singleLineFailureReportOf(result))
            .contains(String.format("classes should not be assignable from %s", List.class.getName()))
            .containsPattern(String.format("Class <%s> is assignable from %s in %s",
                    quote(violated.getName()),
                    quote(List.class.getName()),
                    locationPattern(violated)))
            .doesNotMatch(String.format(".*%s.* assignable.*", quote(satisfied.getName())));
}
 
Example #25
Source File: FreezingArchRuleTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private void createFrozen(TestViolationStore violationStore, ArchRule rule) {
    FreezingArchRule frozen = freeze(rule).persistIn(violationStore);

    assertThat(frozen)
            .checking(importClasses(getClass()))
            .hasNoViolation();
}
 
Example #26
Source File: FreezingArchRuleTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
ArchRule withViolations(final String... messages) {
    final Collection<ViolatedEvent> violatedEvents = new ArrayList<>();
    for (String message : messages) {
        violatedEvents.add(new ViolatedEvent(message));
    }
    return createArchRuleWithViolations(violatedEvents);
}
 
Example #27
Source File: ArchitecturesTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void layered_architecture_because_clause() {
    ArchRule architecture = layeredArchitecture()
            .layer("One").definedBy("some.pkg..")
            .whereLayer("One").mayNotBeAccessedByAnyLayer()
            .as("overridden")
            .because("some reason");

    assertThat(architecture.getDescription()).isEqualTo("overridden, because some reason");
}
 
Example #28
Source File: ExtensionIntegrationTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static void checkRuleAndIgnoreFailure(JavaClasses classes, ArchRule rule) {
    try {
        rule.check(classes);
        throw new RuntimeException("Should have thrown an " + AssertionError.class.getSimpleName());
    } catch (AssertionError ignored) {
    }
}
 
Example #29
Source File: ClassesShouldTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] resideOutsideOfPackages_rules() {
    String firstPackage = ArchRule.class.getPackage().getName();
    String secondPackage = ArchConfiguration.class.getPackage().getName();
    return $$(
            $(classes().should().resideOutsideOfPackages(firstPackage, secondPackage),
                    new String[]{firstPackage, secondPackage}),
            $(classes().should(ArchConditions.resideOutsideOfPackages(firstPackage, secondPackage)),
                    new String[]{firstPackage, secondPackage})
    );
}
 
Example #30
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());
}