Search in sources :

Example 31 with JavaClass

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

the class JavaClassDiagramAssociationTest method class_resides_in_multiple_packages.

@Test
public void class_resides_in_multiple_packages() {
    JavaClassDiagramAssociation javaClassDiagramAssociation = createAssociation(TestDiagram.in(temporaryFolder).component("A").withStereoTypes("..foopackage..").component("B").withStereoTypes("..barpackage").write());
    JavaClass classContainedInTwoComponents = importClassWithContext(ClassInFooAndBarPackage.class);
    thrown.expect(ComponentIntersectionException.class);
    thrown.expectMessage(String.format("Class %s may not be contained in more than one component, but is contained in [A, B]", ClassInFooAndBarPackage.class.getName()));
    javaClassDiagramAssociation.getTargetPackageIdentifiers(classContainedInTwoComponents);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 32 with JavaClass

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

the class JavaClassDiagramAssociationTest method rejects_class_not_contained_in_any_component.

@Test
public void rejects_class_not_contained_in_any_component() {
    JavaClassDiagramAssociation javaClassDiagramAssociation = createAssociation(TestDiagram.in(temporaryFolder).component("SomeComponent").withStereoTypes("..someStereotype.").write());
    JavaClass classNotContained = importClassWithContext(Object.class);
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage(String.format("Class %s is not contained in any component", Object.class.getName()));
    javaClassDiagramAssociation.getTargetPackageIdentifiers(classNotContained);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 33 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass 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 34 with JavaClass

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

the class ShouldClassesThatTest method containAnyStaticInitializersThat.

@Test
@UseDataProvider("no_classes_should_that_rule_starts")
public void containAnyStaticInitializersThat(ClassesThat<ClassesShouldConjunction> noClassesShouldThatRuleStart) {
    ArchRule rule = noClassesShouldThatRuleStart.containAnyStaticInitializersThat(have(fullNameMatching(quote(Data_of_containAnyMembersThat.ViolatingTarget.class.getName()) + ".*")));
    Set<JavaClass> classes = filterClassesAppearingInFailureReport(rule).on(Data_of_containAnyMembersThat.OkayOrigin.class, Data_of_containAnyMembersThat.ViolatingOrigin.class, Data_of_containAnyMembersThat.OkayTarget.class, Data_of_containAnyMembersThat.ViolatingTarget.class);
    assertThatTypes(classes).matchInAnyOrder(Data_of_containAnyMembersThat.ViolatingOrigin.class, Data_of_containAnyMembersThat.ViolatingTarget.class);
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 35 with JavaClass

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

the class ShouldOnlyByClassesThatTest method containAnyStaticInitializersThat.

@Test
@UseDataProvider("should_only_be_by_rule_starts")
public void containAnyStaticInitializersThat(ClassesThat<ClassesShouldConjunction> classesShouldOnlyBeBy) {
    ArchRule rule = classesShouldOnlyBeBy.containAnyStaticInitializersThat(have(fullNameMatching(quote(Data_of_containAnyMembersThat.OkayOrigin.class.getName()) + ".*")));
    Set<JavaClass> classes = filterClassesAppearingInFailureReport(rule).on(Data_of_containAnyMembersThat.OkayOrigin.class, Data_of_containAnyMembersThat.ViolatingOrigin.class, Data_of_containAnyMembersThat.Target.class);
    assertThatTypes(classes).matchInAnyOrder(Data_of_containAnyMembersThat.ViolatingOrigin.class, Data_of_containAnyMembersThat.Target.class);
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9