Search in sources :

Example 36 with JavaClasses

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

the class C4ModelBuilderTest method assert_that_computation_rule_is_applied_to_artifact.

@Test
void assert_that_computation_rule_is_applied_to_artifact() {
    final JavaClasses javaClasses = new ClassFileImporter().importPackages("org.archifacts.integration.c4.model.domain");
    final Application application = Application.builder().descriptor(buildingBlockType1Descriptor).descriptor(defaultContainerDescriptor).buildApplication(javaClasses);
    final C4ModelBuilder c4ModelBuilder = C4Model.builder(new Workspace(this.getClass().getSimpleName(), null));
    final Set<ArtifactContainer> artifactContainers = application.getContainersOfType(containerType1);
    assertThat(artifactContainers).hasSize(1);
    final ArtifactContainer defaultContainer = artifactContainers.iterator().next();
    application.getBuildingBlocksOfType(buildingBlockType1).forEach(c4ModelBuilder::artifact);
    c4ModelBuilder.artifactRule().predicate(artifact -> artifact.getName().equals("Class1ForBuildingBlockType1")).computation((artifact, lookup) -> Set.of(lookup.container(defaultContainer).addComponent("CustomName1", null, "CustomTechnology1"), lookup.container(defaultContainer).addComponent("CustomName2", null, "CustomTechnology2")));
    final C4Model c4Model = c4ModelBuilder.build();
    final Set<Container> containers = c4Model.softwareSystem().getContainers();
    assertThat(containers).hasSize(1).extracting(Container::getName, Container::getTechnology).containsExactly(tuple("DefaultContainer", "ContainerType1"));
    final Set<Component> components = containers.iterator().next().getComponents();
    assertThat(components).hasSize(2).extracting(Component::getName, Component::getTechnology).containsExactlyInAnyOrder(tuple("CustomName1", "CustomTechnology1"), tuple("CustomName2", "CustomTechnology2"));
}
Also used : ArtifactContainer(org.archifacts.core.model.ArtifactContainer) ArtifactContainerType(org.archifacts.core.model.ArtifactContainerType) ReplaceUnderscores(org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArtifactContainerDescriptor(org.archifacts.core.descriptor.ArtifactContainerDescriptor) Component(com.structurizr.model.Component) JavaField(com.tngtech.archunit.core.domain.JavaField) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Artifact(org.archifacts.core.model.Artifact) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) SourceBasedArtifactRelationshipDescriptor(org.archifacts.core.descriptor.SourceBasedArtifactRelationshipDescriptor) Container(com.structurizr.model.Container) MiscArtifact(org.archifacts.core.model.MiscArtifact) Assertions.tuple(org.assertj.core.api.Assertions.tuple) DisplayNameGeneration(org.junit.jupiter.api.DisplayNameGeneration) Set(java.util.Set) Workspace(com.structurizr.Workspace) Test(org.junit.jupiter.api.Test) Application(org.archifacts.core.model.Application) Stream(java.util.stream.Stream) ArtifactRelationshipRole(org.archifacts.core.model.ArtifactRelationshipRole) Relationship(com.structurizr.model.Relationship) BuildingBlockDescriptor(org.archifacts.core.descriptor.BuildingBlockDescriptor) BuildingBlock(org.archifacts.core.model.BuildingBlock) Optional(java.util.Optional) BuildingBlockType(org.archifacts.core.model.BuildingBlockType) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ArtifactContainer(org.archifacts.core.model.ArtifactContainer) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ArtifactContainer(org.archifacts.core.model.ArtifactContainer) Container(com.structurizr.model.Container) Component(com.structurizr.model.Component) Application(org.archifacts.core.model.Application) Workspace(com.structurizr.Workspace) Test(org.junit.jupiter.api.Test)

Example 37 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project jhipster-architectural-comparison by SelimHorri.

the class ArchTest method servicesAndRepositoriesShouldNotDependOnWebLayer.

@Test
void servicesAndRepositoriesShouldNotDependOnWebLayer() {
    JavaClasses importedClasses = new ClassFileImporter().withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS).importPackages("com.selim.pack");
    noClasses().that().resideInAnyPackage("com.selim.pack.service..").or().resideInAnyPackage("com.selim.pack.repository..").should().dependOnClassesThat().resideInAnyPackage("..com.selim.pack.web..").because("Services and repositories should not depend on web layer").check(importedClasses);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.jupiter.api.Test)

Example 38 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project micrometer by micrometer-metrics.

the class JvmGcMetricsTest method noJvmImplementationSpecificApiSignatures.

@Test
void noJvmImplementationSpecificApiSignatures() {
    JavaClasses importedClasses = new ClassFileImporter().importPackages("io.micrometer.core.instrument.binder.jvm");
    ArchRule noSunManagementInMethodSignatures = methods().should().notHaveRawReturnType(resideInAPackage("com.sun.management..")).andShould().notHaveRawParameterTypes(DescribedPredicate.anyElementThat(resideInAPackage("com.sun.management..")));
    noSunManagementInMethodSignatures.check(importedClasses);
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.jupiter.api.Test)

Example 39 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project conjob by ScottG489.

the class ArchUnitTest method coreLibraryShouldOnlyDependOnItself.

@Test
@DisplayName("Given a class in the core library, " + "when it depends on another class, " + "then the dependency should be in the core library, " + "or the dependency should be an external dependency.")
public void coreLibraryShouldOnlyDependOnItself() {
    JavaClasses importedClasses = new ClassFileImporter().importPackages("conjob..");
    classes().that().resideInAPackage("conjob.core..").should().onlyDependOnClassesThat(resideInAPackage("conjob.core..").or(doNot(resideInAPackage("conjob..")))).because("lower level libraries shouldn't have dependencies higher level ones.").check(importedClasses);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 40 with JavaClasses

use of com.tngtech.archunit.core.domain.JavaClasses in project SORMAS-Project by hzi-braunschweig.

the class DatabaseExportServiceTest method test_all_entities_have_export_configuration.

@Test
public void test_all_entities_have_export_configuration() {
    Collection<String> exportableTables = DatabaseExportService.EXPORT_CONFIGS.values();
    Set<String> missingEntities = new HashSet<>();
    Set<String> exportedButNotWanted = new HashSet<>();
    JavaClasses classes = new ClassFileImporter().importPackages("de.symeda.sormas.backend");
    for (JavaClass clazz : classes) {
        if (clazz.isAnnotatedWith(Entity.class)) {
            Entity entityAnnotation = clazz.getAnnotationOfType(Entity.class);
            String tableName = entityAnnotation.name();
            if (StringUtils.isBlank(tableName)) {
                tableName = clazz.getSimpleName().toLowerCase();
            }
            if (!exportableTables.contains(tableName)) {
                missingEntities.add(clazz.getSimpleName());
            } else if (NOT_EXPORTED_ENTITIES.contains(clazz.reflect())) {
                exportedButNotWanted.add(clazz.getSimpleName());
            }
        }
    }
    // remove not exported entities from the list of missing ones
    NOT_EXPORTED_ENTITIES.forEach(e -> missingEntities.remove(e.getSimpleName()));
    assertThat("Missing export configuration for entities [" + String.join(", ", missingEntities) + "]", missingEntities, hasSize(0));
    assertThat("Export configuration not wanted for entities [" + String.join(", ", exportedButNotWanted) + "]", exportedButNotWanted, hasSize(0));
}
Also used : Entity(javax.persistence.Entity) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) HashSet(java.util.HashSet) 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