Search in sources :

Example 41 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class ProxyRulesTest method detects_direct_self_call_to_method_annotated_with_specific_annotation.

@Test
@UseDataProvider("call_own_method_with_specific_annotation_rules")
public void detects_direct_self_call_to_method_annotated_with_specific_annotation(ArchRule rule, String expectedDescription) {
    class OtherClass {

        @ProxyAnnotation
        void otherProxied() {
        }
    }
    @SuppressWarnings("unused")
    class SomeClass {

        void evil() {
            selfProxied();
        }

        void okay() {
            new OtherClass().otherProxied();
            nonProxied();
        }

        void nonProxied() {
        }

        @ProxyAnnotation
        void selfProxied() {
        }
    }
    assertThatRule(rule).hasDescriptionContaining(expectedDescription).checking(new ClassFileImporter().importClasses(SomeClass.class, OtherClass.class)).hasOnlyOneViolationMatching(String.format(".*%s.* calls method .*%s.*", quoteMethod(SomeClass.class, "evil"), quoteMethod(SomeClass.class, "selfProxied")));
}
Also used : ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 42 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter 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 43 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter 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 44 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.

the class ComponentDependencyMetricsTest method component_dependency_metrics_of_an_isolated_component.

@Test
public void component_dependency_metrics_of_an_isolated_component() {
    JavaPackage javaPackage = new ClassFileImporter().importPackagesOf(SimpleWithoutDependencies.class).get(SimpleWithoutDependencies.class).getPackage();
    MetricsComponents<JavaClass> components = MetricsComponents.fromPackages(singleton(javaPackage));
    ComponentDependencyMetrics metrics = ArchitectureMetrics.componentDependencyMetrics(components);
    assertThat(metrics.getEfferentCoupling(javaPackage.getName())).as("Efferent Coupling of " + javaPackage.getName()).isEqualTo(0);
    assertThat(metrics.getAfferentCoupling(javaPackage.getName())).as("Afferent Coupling of " + javaPackage.getName()).isEqualTo(0);
    assertThat(metrics.getInstability(javaPackage.getName())).as("Instability of " + javaPackage.getName()).isEqualTo(1);
    assertThat(metrics.getAbstractness(javaPackage.getName())).as("Abstractness of " + javaPackage.getName()).isEqualTo(0);
    assertThat(metrics.getNormalizedDistanceFromMainSequence(javaPackage.getName())).as("Normalized Distance from Main Sequence of " + javaPackage.getName()).isEqualTo(0);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) SimpleWithoutDependencies(com.tngtech.archunit.library.metrics.testobjects.componentdependency.simple.SimpleWithoutDependencies) JavaPackage(com.tngtech.archunit.core.domain.JavaPackage) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 45 with ClassFileImporter

use of com.tngtech.archunit.core.importer.ClassFileImporter 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

ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)136 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)98 Test (org.junit.Test)76 Test (org.junit.jupiter.api.Test)52 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 JavaClass (com.tngtech.archunit.core.domain.JavaClass)14 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)13 Workspace (com.structurizr.Workspace)12 ArchRule (com.tngtech.archunit.lang.ArchRule)12 Application (org.archifacts.core.model.Application)12 Component (com.structurizr.model.Component)10 Container (com.structurizr.model.Container)10 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)10 ArtifactContainer (org.archifacts.core.model.ArtifactContainer)10 JavaPackage (com.tngtech.archunit.core.domain.JavaPackage)5 LayeredArchitecture (com.tngtech.archunit.library.Architectures.LayeredArchitecture)5 OnionArchitecture (com.tngtech.archunit.library.Architectures.OnionArchitecture)5 Optional (java.util.Optional)5 Artifact (org.archifacts.core.model.Artifact)5 MiscArtifact (org.archifacts.core.model.MiscArtifact)5