Search in sources :

Example 56 with ArchRule

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

the class FreezingArchRuleTest method allows_to_specify_a_custom_matcher_to_decide_which_violations_count_as_known.

// This is e.g. useful to ignore the line number in "message (Foo.java:xxx)"
@Test
public void allows_to_specify_a_custom_matcher_to_decide_which_violations_count_as_known() {
    TestViolationStore violationStore = new TestViolationStore();
    createFrozen(violationStore, rule("some description").withViolations("some #ignore_this# violation", "second #ignore_this# violation").create());
    ArchRule frozen = freeze(rule("some description").withViolations("some #now changed# violation", "second #now changed somehow# violation", "and new").create()).persistIn(violationStore).associateViolationLinesVia((lineFromFirstViolation, lineFromSecondViolation) -> {
        String storedCleanedUp = lineFromFirstViolation.replaceAll("#.*#", "");
        String actualCleanedUp = lineFromSecondViolation.replaceAll("#.*#", "");
        return storedCleanedUp.equals(actualCleanedUp);
    });
    assertThatRule(frozen).checking(importClasses(getClass())).hasOnlyViolations("and new");
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) Test(org.junit.Test)

Example 57 with ArchRule

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

the class FreezingArchRuleTest method freezes_violations_on_first_call.

@Test
public void freezes_violations_on_first_call() {
    ArchRule input = rule("some description").withViolations("first violation", "second violation").create();
    TestViolationStore violationStore = new TestViolationStore();
    ArchRule frozen = freeze(input).persistIn(violationStore);
    assertThatRule(frozen).checking(importClasses(getClass())).hasNoViolation();
    violationStore.verifyStoredRule("some description", "first violation", "second violation");
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) Test(org.junit.Test)

Example 58 with ArchRule

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

the class FreezingArchRuleTest method fails_on_violations_additional_to_frozen_ones.

@Test
public void fails_on_violations_additional_to_frozen_ones() {
    TestViolationStore violationStore = new TestViolationStore();
    createFrozen(violationStore, rule("some description").withViolations("first violation").create());
    ArchRule anotherViolation = rule("some description").withViolations("first violation", "second violation").create();
    ArchRule frozenWithNewViolation = freeze(anotherViolation).persistIn(violationStore);
    assertThatRule(frozenWithNewViolation).checking(importClasses(getClass())).hasOnlyViolations("second violation");
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) Test(org.junit.Test)

Example 59 with ArchRule

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

the class FreezingArchRuleTest method works_with_multi_line_violations_with_different_line_separators.

@Test
@UseDataProvider("different_line_separators_to_store_and_read")
public void works_with_multi_line_violations_with_different_line_separators(String lineSeparatorOnFreeze, String lineSeparatorOnCheck) {
    TestViolationStore violationStore = new TestViolationStore();
    RuleCreator ruleCreator = rule("some rule").withViolations(new ViolatedEvent("first violation1${lineSeparator}with multiple${lineSeparator}lines with several words", "second violation1${lineSeparator}with multiple${lineSeparator}lines with several words"), new ViolatedEvent("first violation2${lineSeparator}with multiple${lineSeparator}lines with several words", "second violation2${lineSeparator}with multiple${lineSeparator}lines with several words"));
    ArchRule storedRule = ruleCreator.withStringReplace("${lineSeparator}", lineSeparatorOnFreeze).create();
    createFrozen(violationStore, storedRule);
    ArchRule ruleToCheck = ruleCreator.withStringReplace("${lineSeparator}", lineSeparatorOnCheck).create();
    assertThatRule(freeze(ruleToCheck).persistIn(violationStore)).checking(importClasses(getClass())).hasNoViolation();
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 60 with ArchRule

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

the class ShouldClassesThatTest method transitivelyDependOnClassesThat_reports_all_transitive_dependencies.

@Test
@DataProvider(value = { "true", "false" })
public void transitivelyDependOnClassesThat_reports_all_transitive_dependencies(boolean viaPredicate) {
    Class<?> testClass = TransitivelyDependOnClassesThatTestCases.TestClass.class;
    Class<?> directlyDependentClass1 = TransitivelyDependOnClassesThatTestCases.DirectlyDependentClass1.class;
    Class<?> directlyDependentClass2 = TransitivelyDependOnClassesThatTestCases.DirectlyDependentClass2.class;
    Class<?> transitivelyDependentClass = TransitivelyDependOnClassesThatTestCases.TransitivelyDependentClass.class;
    JavaClasses classes = new ClassFileImporter().importClasses(testClass, directlyDependentClass1, directlyDependentClass2, transitivelyDependentClass);
    ClassesShould noClassesShould = noClasses().that().haveFullyQualifiedName(testClass.getName()).should();
    ArchRule rule = viaPredicate ? noClassesShould.transitivelyDependOnClassesThat(have(fullyQualifiedName(transitivelyDependentClass.getName()))) : noClassesShould.transitivelyDependOnClassesThat().haveFullyQualifiedName(transitivelyDependentClass.getName());
    assertThatRule(rule).checking(classes).hasViolations(2).hasViolationMatching(String.format(".*%s\\.%s.* has type .*%s.*", quote(directlyDependentClass1.getName()), "transitiveDependency1", quote(transitivelyDependentClass.getName()))).hasViolationMatching(String.format(".*%s\\.%s.* has type .*%s.*", quote(directlyDependentClass2.getName()), "transitiveDependency2", quote(transitivelyDependentClass.getName())));
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Test(org.junit.Test)

Aggregations

ArchRule (com.tngtech.archunit.lang.ArchRule)141 Test (org.junit.jupiter.api.Test)90 Test (org.junit.Test)37 ArchTest (com.tngtech.archunit.junit.ArchTest)19 CompositeArchRule (com.tngtech.archunit.lang.CompositeArchRule)19 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)18 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)13 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 DynamicTest (org.junit.jupiter.api.DynamicTest)12 TaskanaIntegrationTest (testapi.TaskanaIntegrationTest)12 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)6 JavaClass (com.tngtech.archunit.core.domain.JavaClass)5 Immutable (org.eclipse.sirius.components.annotations.Immutable)5 ArchConfiguration (com.tngtech.archunit.ArchConfiguration)4 JavaField (com.tngtech.archunit.core.domain.JavaField)4 CanBeAnnotatedTest (com.tngtech.archunit.core.domain.properties.CanBeAnnotatedTest)4 ArchCondition (com.tngtech.archunit.lang.ArchCondition)3 ClassViolatingCodingRules (com.tngtech.archunit.example.layers.ClassViolatingCodingRules)2 ImmutableProtocol (io.camunda.zeebe.protocol.record.ImmutableProtocol)2 Record (io.camunda.zeebe.protocol.record.Record)2