Java Code Examples for com.tngtech.archunit.lang.ArchRule#check()

The following examples show how to use com.tngtech.archunit.lang.ArchRule#check() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArchRuleExecution.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
Result evaluateOn(JavaClasses classes) {
    ArchRule rule = getValue(ruleField, testClass);
    try {
        rule.check(classes);
    } catch (Exception | AssertionError e) {
        return new NegativeResult(describeSelf(), e);
    }
    return new PositiveResult();
}
 
Example 2
Source File: ArchitectureTest.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Test
void exceptionsShouldBePlacedInExceptionPackage() {
  ArchRule myRule =
      classes()
          .that()
          .haveSimpleNameEndingWith("Exception")
          .should()
          .resideInAPackage("..exceptions..");

  myRule.check(importedClasses);
}
 
Example 3
Source File: PrimaryAdaptersComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void publicControllerMethodsShouldBeAnnotatedWithARequestMapping() {
  ArchRule rule = ArchRuleDefinition.methods()
    .that().arePublic()
    .and().areDeclaredInClassesThat().resideInAPackage("..adapters.primary.web..")
    .and().areDeclaredInClassesThat().haveSimpleNameEndingWith("Controller")
    .and().areDeclaredInClassesThat().areAnnotatedWith(Controller.class)
    .or().areDeclaredInClassesThat().areAnnotatedWith(RestController.class)
    .should().beAnnotatedWith(RequestMapping.class)
    .orShould().beAnnotatedWith(GetMapping.class)
    .orShould().beAnnotatedWith(PostMapping.class)
    .orShould().beAnnotatedWith(PatchMapping.class)
    .orShould().beAnnotatedWith(DeleteMapping.class);
  rule.check(classes);
}
 
Example 4
Source File: AggregateArchitectureTest.java    From ddd-with-spring with Apache License 2.0 5 votes vote down vote up
@ArchTest
public static void aggregateAnnotationRules(JavaClasses importedClasses) {
    ArchRule namingToAnnotation = classes().that().haveSimpleNameEndingWith("Aggregate").should().beAnnotatedWith(Aggregate.class);
    namingToAnnotation.check(importedClasses);

    ArchRule annotationToNaming = classes().that().areAnnotatedWith(Aggregate.class).should().haveSimpleNameEndingWith("Aggregate");
    annotationToNaming.check(importedClasses);
}
 
Example 5
Source File: SecondaryAdaptersComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void documentClassesShouldBeAnnotatedWithDocumentAnnotation() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().haveSimpleNameEndingWith("Document")
    .should().beAnnotatedWith(Document.class);
  rule.check(classes);
}
 
Example 6
Source File: SecondaryAdaptersComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void noClassesWithRepositoryAnnotationShouldResideOutsideOfSecondaryAdaptersPackages() {
  ArchRule rule = ArchRuleDefinition.noClasses()
    .that().areAnnotatedWith(Repository.class)
    .should().resideOutsideOfPackage(SECONDARY_ADAPTERS_PACKAGES);
  rule.check(classes);
}
 
Example 7
Source File: ExtensionIntegrationTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static void checkRuleAndIgnoreFailure(JavaClasses classes, ArchRule rule) {
    try {
        rule.check(classes);
        throw new RuntimeException("Should have thrown an " + AssertionError.class.getSimpleName());
    } catch (AssertionError ignored) {
    }
}
 
Example 8
Source File: PrimaryAdaptersComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void controllerClassesShouldBeAnnotatedWithControllerOrRestControllerAnnotation() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().haveSimpleNameEndingWith("Controller")
    .should().beAnnotatedWith(Controller.class)
    .orShould().beAnnotatedWith(RestController.class);
  rule.check(classes);
}
 
Example 9
Source File: UtilsTest.java    From netdata-java-orchestrator with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testUtils() {
	JavaClasses importedClasses = new ClassFileImporter().importPackages("org.firehol");

	final ArchRule rule = classes().that()
			.haveSimpleNameEndingWith("Utils")
			.should()
			.haveModifier(JavaModifier.FINAL)
			.andShould()
			.haveOnlyPrivateConstructors();

	rule.check(importedClasses);
}
 
Example 10
Source File: SpringCodingRulesTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void springSingletonComponentsShouldOnlyHaveFinalFields() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().areAnnotatedWith(Service.class)
    .or().areAnnotatedWith(Component.class)
    .and().areNotAnnotatedWith(ConfigurationProperties.class)
    .or().areAnnotatedWith(Controller.class)
    .or().areAnnotatedWith(RestController.class)
    .or().areAnnotatedWith(Repository.class)
    .should().haveOnlyFinalFields();
  rule.check(classes);
}
 
Example 11
Source File: DomainComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void domainClassesShouldOnlyDependOnDomainOrStdLibClasses() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().resideInAPackage(DOMAIN_LAYER_PACKAGES)
    .should().onlyDependOnClassesThat().resideInAnyPackage(DOMAIN_LAYER_PACKAGES, "java..");
  rule.check(classes);
}
 
Example 12
Source File: DomainComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void domainClassesShouldOnlyBeAccessedByOtherDomainClassesOrTheApplicationLayer() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().resideInAPackage(DOMAIN_LAYER_PACKAGES)
    .should().onlyBeAccessed().byAnyPackage(DOMAIN_LAYER_PACKAGES, APPLICATION_LAYER_PACKAGES);
  rule.check(classes);
}
 
Example 13
Source File: ApplicationComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void useCaseClassesShouldHaveAnInvokeMethodWithASingleRequestParameterAndAResponseReturnType() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().haveSimpleNameEndingWith("UseCase")
    .should(new HaveAnInvokeMethodWithASingleRequestParameterAndAResponseReturnType());
  rule.check(classes);
}
 
Example 14
Source File: ApplicationComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void noClassesWithServiceAnnotationShouldResideOutsideTheApplicationLayer() {
  ArchRule rule = ArchRuleDefinition.noClasses()
    .that().areAnnotatedWith(Service.class)
    .should().resideOutsideOfPackage(APPLICATION_LAYER_PACKAGES);
  rule.check(classes);
}
 
Example 15
Source File: ApplicationComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void useCaseClassesShouldBeAnnotatedWithServiceAnnotation() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().haveSimpleNameEndingWith("UseCase")
    .should().beAnnotatedWith(Service.class);
  rule.check(classes);
}
 
Example 16
Source File: ArchitectureTest.java    From taskana with Apache License 2.0 4 votes vote down vote up
@Test
void onlyExceptionsShouldResideInExceptionPackage() {
  ArchRule myRule =
      classes().that().resideInAPackage("..exceptions").should().beAssignableTo(Throwable.class);
  myRule.check(importedClasses);
}
 
Example 17
Source File: GeneralCodingRulesTest.java    From archunit-examples with MIT License 4 votes vote down vote up
@Test
public void noClassesShouldUseStandardStreams() {
  ArchRule rule = ArchRuleDefinition.noClasses()
    .should(GeneralCodingRules.ACCESS_STANDARD_STREAMS);
  rule.check(classes);
}
 
Example 18
Source File: GeneralCodingRulesTest.java    From archunit-examples with MIT License 4 votes vote down vote up
@Test
public void noClassesShouldUseStandardLogging() {
  ArchRule rule = ArchRuleDefinition.noClasses()
    .should(GeneralCodingRules.USE_JAVA_UTIL_LOGGING);
  rule.check(classes);
}
 
Example 19
Source File: LayeredArchitectureTest.java    From archunit-examples with MIT License 4 votes vote down vote up
@Test
public void domainLayerShouldOnlyBeAccessedByApplicationLayer() {
  ArchRule rule = portsAndAdaptersArchitecture.whereLayer(DOMAIN_LAYER)
    .mayOnlyBeAccessedByLayers(APPLICATION_LAYER);
  rule.check(classes);
}
 
Example 20
Source File: LayeredArchitectureTest.java    From archunit-examples with MIT License 4 votes vote down vote up
@Test
public void adaptersLayerShouldNotBeAccessedByAnyLayer() {
  ArchRule rule = portsAndAdaptersArchitecture.whereLayer(ADAPTERS_LAYER)
    .mayNotBeAccessedByAnyLayer();
  rule.check(classes);
}