Search in sources :

Example 46 with JavaClasses

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

the class ArchitecturesTest method layered_architecture_gathers_may_only_access_layers_violations.

@Test
@UseDataProvider("layeredArchitectureMayOnlyAccessLayersDefinitions")
public void layered_architecture_gathers_may_only_access_layers_violations(LayeredArchitecture architecture) {
    JavaClasses classes = new ClassFileImporter().importPackages(absolute("mayonlyaccesslayers"));
    EvaluationResult result = architecture.evaluate(classes);
    assertPatternMatches(result.getFailureReport().getDetails(), ImmutableSet.of(expectedAccessViolationPattern(MayOnlyAccessLayersOriginClass.class, "call", MayOnlyAccessLayersForbiddenClass.class, "callMe"), expectedAccessViolationPattern(MayOnlyAccessLayersOriginClass.class, CONSTRUCTOR_NAME, Object.class, CONSTRUCTOR_NAME), expectedAccessViolationPattern(MayOnlyAccessLayersForbiddenClass.class, CONSTRUCTOR_NAME, Object.class, CONSTRUCTOR_NAME), expectedAccessViolationPattern(MayOnlyAccessLayersForbiddenClass.class, "callMe", MayOnlyAccessLayersOriginClass.class, CONSTRUCTOR_NAME), expectedInheritancePattern(MayOnlyAccessLayersOriginClass.class, Object.class), expectedInheritancePattern(MayOnlyAccessLayersForbiddenClass.class, Object.class), expectedFieldTypePattern(MayOnlyAccessLayersOriginClass.class, "illegalTarget", MayOnlyAccessLayersForbiddenClass.class)));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 47 with JavaClasses

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

the class ArchitecturesTest method onion_architecture_gathers_all_violations.

@Test
public void onion_architecture_gathers_all_violations() {
    OnionArchitecture architecture = getTestOnionArchitecture();
    JavaClasses classes = new ClassFileImporter().importPackages(absolute("onionarchitecture"));
    EvaluationResult result = architecture.evaluate(classes);
    assertPatternMatches(result.getFailureReport().getDetails(), getExpectedOnionViolations().toPatterns());
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) OnionArchitecture(com.tngtech.archunit.library.Architectures.OnionArchitecture) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 48 with JavaClasses

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

the class ArchitecturesTest method layered_architecture_combines_multiple_ignores.

@Test
public void layered_architecture_combines_multiple_ignores() {
    JavaClasses classes = new ClassFileImporter().importClasses(FirstAnyPkgClass.class, SomePkgSubclass.class, SecondThreeAnyClass.class, SomePkgClass.class);
    LayeredArchitecture layeredArchitecture = layeredArchitecture().layer("One").definedBy(absolute("some.pkg..")).whereLayer("One").mayNotBeAccessedByAnyLayer().ignoreDependency(FirstAnyPkgClass.class, SomePkgSubclass.class);
    assertThat(layeredArchitecture.evaluate(classes).hasViolation()).as("result has violation").isTrue();
    layeredArchitecture = layeredArchitecture.ignoreDependency(SecondThreeAnyClass.class, SomePkgClass.class);
    assertThat(layeredArchitecture.evaluate(classes).hasViolation()).as("result has violation").isFalse();
}
Also used : SecondThreeAnyClass(com.tngtech.archunit.library.testclasses.second.three.any.SecondThreeAnyClass) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) SomePkgClass(com.tngtech.archunit.library.testclasses.some.pkg.SomePkgClass) LayeredArchitecture(com.tngtech.archunit.library.Architectures.LayeredArchitecture) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 49 with JavaClasses

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

the class ArchitecturesTest method onion_architecture_allows_empty_layers_if_all_layers_are_optional.

@Test
public void onion_architecture_allows_empty_layers_if_all_layers_are_optional() {
    OnionArchitecture architecture = anOnionArchitectureWithEmptyLayers().withOptionalLayers(true);
    assertThat(architecture.getDescription()).startsWith("Onion architecture consisting of (optional)");
    JavaClasses classes = new ClassFileImporter().importPackages(absolute("onionarchitecture"));
    EvaluationResult result = architecture.evaluate(classes);
    assertThat(result.hasViolation()).as("result of evaluating empty layers has violation").isFalse();
    assertThat(result.getFailureReport().isEmpty()).as("failure report").isTrue();
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) OnionArchitecture(com.tngtech.archunit.library.Architectures.OnionArchitecture) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 50 with JavaClasses

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

the class ArchitecturesTest method onion_architecture_with_overwritten_description_retains_ignored_dependencies.

@Test
public void onion_architecture_with_overwritten_description_retains_ignored_dependencies() {
    ArchRule onionIgnoringOriginApplicationLayerClass = getTestOnionArchitecture().ignoreDependency(equivalentTo(ApplicationLayerClass.class), DescribedPredicate.<JavaClass>alwaysTrue()).because("some reason causing description to be overwritten");
    JavaClasses classes = new ClassFileImporter().importPackages(absolute("onionarchitecture"));
    EvaluationResult result = onionIgnoringOriginApplicationLayerClass.evaluate(classes);
    ExpectedOnionViolations expectedViolations = getExpectedOnionViolations().withoutViolationsWithOrigin(ApplicationLayerClass.class);
    assertPatternMatches(result.getFailureReport().getDetails(), expectedViolations.toPatterns());
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

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