|
| 1 | +package de.rieckpil.blog.archunit; |
| 2 | + |
| 3 | +import java.time.Clock; |
| 4 | +import java.time.LocalDate; |
| 5 | + |
| 6 | +import com.tngtech.archunit.core.domain.JavaClasses; |
| 7 | +import com.tngtech.archunit.core.importer.ClassFileImporter; |
| 8 | +import com.tngtech.archunit.core.importer.ImportOption; |
| 9 | +import com.tngtech.archunit.lang.ArchRule; |
| 10 | +import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; |
| 11 | +import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; |
| 15 | + |
| 16 | +class ArchitectureTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void servicesShouldNotDependOnControllers() { |
| 20 | + JavaClasses importedClasses = new ClassFileImporter().importPackages("de.rieckpil.blog"); |
| 21 | + |
| 22 | + noClasses() |
| 23 | + .that().resideInAPackage("de.rieckpil.blog.service") |
| 24 | + .should().dependOnClassesThat().resideInAPackage("de.rieckpil.blog.controller") |
| 25 | + .check(importedClasses); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void localDateNowShouldUseClock() { |
| 30 | + ArchRule rule = noClasses().should() |
| 31 | + .callMethod(LocalDate.class, "now"); |
| 32 | + |
| 33 | + rule.check(new ClassFileImporter() |
| 34 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) |
| 35 | + .importPackages("de.rieckpil")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void noCyclicDependencies() { |
| 40 | + JavaClasses importedClasses = new ClassFileImporter().importPackages("de.rieckpil"); |
| 41 | + |
| 42 | + SlicesRuleDefinition.slices() |
| 43 | + .matching("de.rieckpil.(*)..") |
| 44 | + .should().beFreeOfCycles() |
| 45 | + .check(importedClasses); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void serviceClassesShouldHaveProperNaming() { |
| 50 | + JavaClasses importedClasses = new ClassFileImporter().importPackages("de.rieckpil"); |
| 51 | + |
| 52 | + ArchRuleDefinition.classes() |
| 53 | + .that().resideInAPackage("de.rieckpil.blog.service") |
| 54 | + .should().haveSimpleNameEndingWith("Service") |
| 55 | + .check(importedClasses); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void shouldNotUseJavaUtilLogging() { |
| 60 | + JavaClasses importedClasses = new ClassFileImporter() |
| 61 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) |
| 62 | + .importPackages("de.rieckpil"); |
| 63 | + |
| 64 | + noClasses() |
| 65 | + .should().dependOnClassesThat().belongToAnyOf(java.util.logging.Logger.class) |
| 66 | + .check(importedClasses); |
| 67 | + } |
| 68 | + |
| 69 | + public static final ArchRule serviceLayerShouldNotAccessRepositoriesDirectly = |
| 70 | + noClasses() |
| 71 | + .that().resideInAPackage("de.rieckpil") |
| 72 | + .should().dependOnClassesThat().resideInAPackage("de.rieckpil"); |
| 73 | +} |
0 commit comments