Search in sources :

Example 46 with ClassFileImporter

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

Example 47 with ClassFileImporter

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

the class VisibilityMetricsTest method calculates_visibility_metrics_for_Java_classes_according_to_their_modifier.

@Test
public void calculates_visibility_metrics_for_Java_classes_according_to_their_modifier() {
    JavaClasses classes = new ClassFileImporter().importPackagesOf(VisibleOne.class, VisibleTwo.class);
    JavaPackage packageOne = classes.getPackage(VisibleOne.class.getPackage().getName());
    JavaPackage packageTwo = classes.getPackage(VisibleTwo.class.getPackage().getName());
    VisibilityMetrics metrics = ArchitectureMetrics.visibilityMetrics(MetricsComponents.fromPackages(ImmutableSet.of(packageOne, packageTwo)));
    assertThat(metrics.getRelativeVisibility(packageOne.getName())).isEqualTo(0.5);
    assertThat(metrics.getRelativeVisibility(packageTwo.getName())).isEqualTo(1.0);
    assertThat(metrics.getAverageRelativeVisibility()).isEqualTo(0.75);
    assertThat(metrics.getGlobalRelativeVisibility()).isCloseTo(0.66, offset(0.01));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaPackage(com.tngtech.archunit.core.domain.JavaPackage) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 48 with ClassFileImporter

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

the class ClassCacheTest method if_whole_classpath_is_set_true_then_the_whole_classpath_is_imported.

@Test
public void if_whole_classpath_is_set_true_then_the_whole_classpath_is_imported() {
    TestAnalysisRequest defaultOptions = new TestAnalysisRequest().withWholeClasspath(true);
    Class<?>[] expectedImportResult = new Class[] { getClass() };
    doReturn(new ClassFileImporter().importClasses(expectedImportResult)).when(cacheClassFileImporter).importClasses(any(ImportOptions.class), ArgumentMatchers.<Location>anyCollection());
    JavaClasses classes = cache.getClassesToAnalyzeFor(TestClass.class, defaultOptions);
    assertThatTypes(classes).matchExactly(expectedImportResult);
    verify(cacheClassFileImporter).importClasses(any(ImportOptions.class), locationCaptor.capture());
    assertThat(locationCaptor.getValue()).has(locationContaining("archunit")).has(locationContaining("asm")).has(locationContaining("google")).has(locationContaining("mockito"));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ImportOptions(com.tngtech.archunit.core.importer.ImportOptions) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) CacheClassFileImporter(com.tngtech.archunit.junit.internal.ClassCache.CacheClassFileImporter) Test(org.junit.Test)

Example 49 with ClassFileImporter

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

the class JavaClassTest method predicate_containAnyMembersThat.

@Test
public void predicate_containAnyMembersThat() {
    @SuppressWarnings("unused")
    class Match {

        String right;
    }
    @SuppressWarnings("unused")
    class Mismatch {

        String wrong;
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Match.class, Mismatch.class);
    assertThat(containAnyMembersThat(name("right"))).hasDescription("contain any members that name 'right'").accepts(classes.get(Match.class)).rejects(classes.get(Mismatch.class));
}
Also used : ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.Test)

Example 50 with ClassFileImporter

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

the class JavaClassTest method erased_type_of_class_is_the_class_itself.

@Test
public void erased_type_of_class_is_the_class_itself() {
    class SimpleClass {
    }
    JavaType type = new ClassFileImporter().importClass(SimpleClass.class);
    assertThat(type.toErasure()).isEqualTo(type);
}
Also used : ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) 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