Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layered architecture exceptions #42

Merged
merged 3 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added failing integration test for 'layeredArchitecture()...ignoreDep…
…endency(..)'
  • Loading branch information
codecholeric committed Nov 8, 2017
commit 59456d9006567f57627de35c66a27822f2da8a9d
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tngtech.archunit.exampletest.junit;

import com.tngtech.archunit.example.SomeMediator;
import com.tngtech.archunit.example.service.ServiceViolatingLayerRules;
import com.tngtech.archunit.exampletest.Example;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
Expand All @@ -24,4 +26,17 @@ public class LayeredArchitectureTest {
.whereLayer("Controllers").mayNotBeAccessedByAnyLayer()
.whereLayer("Services").mayOnlyBeAccessedByLayers("Controllers")
.whereLayer("Persistence").mayOnlyBeAccessedByLayers("Services");

@ArchTest
public static final ArchRule layer_dependencies_are_respected_with_exception = layeredArchitecture()

.layer("Controllers").definedBy("com.tngtech.archunit.example.controller..")
.layer("Services").definedBy("com.tngtech.archunit.example.service..")
.layer("Persistence").definedBy("com.tngtech.archunit.example.persistence..")

.whereLayer("Controllers").mayNotBeAccessedByAnyLayer()
.whereLayer("Services").mayOnlyBeAccessedByLayers("Controllers")
.whereLayer("Persistence").mayOnlyBeAccessedByLayers("Services")

.ignoreDependency(SomeMediator.class, ServiceViolatingLayerRules.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@
@RunWith(ArchUnitIntegrationTestRunner.class)
@AnalyzeClasses(packages = "com.tngtech.archunit.example")
public class LayeredArchitectureIntegrationTest {

@ArchTest
@ExpectedViolationFrom(location = LayeredArchitectureIntegrationTest.class, method = "expectLayerViolations")
public static final ArchRule layer_dependencies_are_respected = LayeredArchitectureTest.layer_dependencies_are_respected;
public static final ArchRule layer_dependencies_are_respected =
LayeredArchitectureTest.layer_dependencies_are_respected;

@ArchTest
@ExpectedViolationFrom(location = LayeredArchitectureIntegrationTest.class, method = "expectLayerViolationsWithException")
public static final ArchRule layer_dependencies_are_respected_with_exception =
LayeredArchitectureTest.layer_dependencies_are_respected_with_exception;

@CalledByArchUnitIntegrationTestRunner
static void expectLayerViolations(ExpectsViolations expectsViolations) {
expectLayerViolationsWithException(expectsViolations);
expectsViolations
.by(callFrom(SomeMediator.class, "violateLayerRulesIndirectly")
.toMethod(ServiceViolatingLayerRules.class, "doSomething")
.inLine(15)
.asDependency());
}

@CalledByArchUnitIntegrationTestRunner
static void expectLayerViolationsWithException(ExpectsViolations expectsViolations) {
expectsViolations.ofRule("Layered architecture consisting of" + lineSeparator() +
"layer 'Controllers' ('com.tngtech.archunit.example.controller..')" + lineSeparator() +
"layer 'Services' ('com.tngtech.archunit.example.service..')" + lineSeparator() +
Expand All @@ -45,11 +62,6 @@ static void expectLayerViolations(ExpectsViolations expectsViolations) {
.inLine(14)
.asDependency())

.by(callFrom(SomeMediator.class, "violateLayerRulesIndirectly")
.toMethod(ServiceViolatingLayerRules.class, "doSomething")
.inLine(15)
.asDependency())

.by(callFrom(ServiceViolatingLayerRules.class, "illegalAccessToController")
.toConstructor(UseCaseTwoController.class)
.inLine(12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public LayeredArchitecture as(String newDescription) {
return new LayeredArchitecture(layerDefinitions, dependencySpecifications, Optional.of(newDescription));
}

public LayeredArchitecture ignoreDependency(Class<?> from, Class<?> to) {
return this;
}

private String[] toArray(Set<String> strings) {
return strings.toArray(new String[strings.size()]);
}
Expand Down