Search in sources :

Example 51 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project ArchUnit by TNG.

the class GivenSlicesTest method restricting_slices_that_should_not_depend_on_each_other.

@Test
public void restricting_slices_that_should_not_depend_on_each_other() {
    archConfigurationRule.setFailOnEmptyShould(false);
    GivenSlices givenSlices = slices().matching("..testclasses.(*)..");
    JavaClasses classes = new ClassFileImporter().importPackages("com.tngtech.archunit.library.testclasses");
    EvaluationResult result = givenSlices.that(DescribedPredicate.<Slice>alwaysFalse()).and(DescribedPredicate.<Slice>alwaysTrue()).should().notDependOnEachOther().evaluate(classes);
    assertThat(result.hasViolation()).as("Result has violation").isFalse();
    result = givenSlices.that(DescribedPredicate.<Slice>alwaysTrue()).or(DescribedPredicate.<Slice>alwaysFalse()).should().notDependOnEachOther().evaluate(classes);
    assertThat(result.hasViolation()).as("Result has violation").isTrue();
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) GivenSlices(com.tngtech.archunit.library.dependencies.syntax.GivenSlices) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ArchitecturesTest(com.tngtech.archunit.library.ArchitecturesTest) Test(org.junit.Test)

Example 52 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project ArchUnit by TNG.

the class PlantUmlArchConditionTest method getClassesFrom.

private static JavaClasses getClassesFrom(String pkg) {
    String packageName = "com.tngtech.archunit.library.diagramtests." + pkg;
    JavaClasses classes = new ClassFileImporter().importPackages(packageName);
    if (isEmpty(classes)) {
        throw new IllegalStateException(String.format("No classes were imported from '%s'", packageName));
    }
    return classes;
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter)

Example 53 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project ArchUnit by TNG.

the class PlantUmlArchConditionTest method class_must_be_contained_within_the_diagram_if_it_has_relevant_dependencies.

@Test
public void class_must_be_contained_within_the_diagram_if_it_has_relevant_dependencies() {
    File file = TestDiagram.in(temporaryFolder).component("SomeComponent").withStereoTypes("..someStereotype.").write();
    JavaClasses notContained = importClasses(Object.class);
    PlantUmlArchCondition condition = adhereToPlantUmlDiagram(file, consideringAllDependencies());
    classes().should(condition.ignoreDependenciesWithOrigin(equivalentTo(Object.class))).check(notContained);
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage(String.format("Class %s is not contained in any component", getOnlyElement(notContained).getName()));
    classes().should(condition).check(notContained);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) File(java.io.File) Test(org.junit.Test)

Example 54 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses 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)

Example 55 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project ArchUnit by TNG.

the class SlicesShouldTest method slice_rules_should_ignore_configured_dependencies.

@Test
@UseDataProvider("rules")
public void slice_rules_should_ignore_configured_dependencies(SliceRule rule) {
    JavaClasses classes = new ClassFileImporter().importPackages(TEST_CLASSES_PACKAGE);
    assertViolations(classes, rule).contain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).contain(FirstAnyPkgClass.class, SomePkgSubclass.class).contain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(classIn(".*\\.first\\.three\\..*"), DescribedPredicate.<JavaClass>alwaysTrue());
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).contain(FirstAnyPkgClass.class, SomePkgSubclass.class).contain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(FirstAnyPkgClass.class.getName(), SomePkgSubclass.class.getName());
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).doNotContain(FirstAnyPkgClass.class, SomePkgSubclass.class).contain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(SecondAnyClass.class, FirstAnyPkgClass.class);
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).doNotContain(FirstAnyPkgClass.class, SomePkgSubclass.class).doNotContain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(DescribedPredicate.<JavaClass>alwaysTrue(), classIn(".*\\.some\\.pkg\\..*"));
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).doNotContain(FirstAnyPkgClass.class, SomePkgSubclass.class).doNotContain(SecondAnyClass.class, FirstAnyPkgClass.class).doNotContain(SecondThreeAnyClass.class, SomePkgClass.class);
}
Also used : SecondThreeAnyClass(com.tngtech.archunit.library.testclasses.second.three.any.SecondThreeAnyClass) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) SecondAnyClass(com.tngtech.archunit.library.testclasses.second.any.pkg.SecondAnyClass) FirstThreeAnyClass(com.tngtech.archunit.library.testclasses.first.three.any.FirstThreeAnyClass) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) FirstAnyPkgClass(com.tngtech.archunit.library.testclasses.first.any.pkg.FirstAnyPkgClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)248 Test (org.junit.Test)184 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)98 JavaClass (com.tngtech.archunit.core.domain.JavaClass)72 Test (org.junit.jupiter.api.Test)54 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 ArchRule (com.tngtech.archunit.lang.ArchRule)19 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)15 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)14 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)13 Workspace (com.structurizr.Workspace)12 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)12 Set (java.util.Set)12 Application (org.archifacts.core.model.Application)12 Component (com.structurizr.model.Component)10 Container (com.structurizr.model.Container)10 ArtifactContainer (org.archifacts.core.model.ArtifactContainer)10