Skip to content

Commit

Permalink
fix OnionArchitecture losing optionalLayers after as (#1226)
Browse files Browse the repository at this point in the history
The `OnionArchitecture.as(..)` method (and transitively `because(..)`)
didn't pass the `optionalLayers` property along when creating a `new
OnionArchitecture(..)` on return. Thus, once a test called `as(..)` or
`because(..)` on the rule text `optionalLayers` would be reset to
`false`.

Resolves: #1185
  • Loading branch information
codecholeric authored Mar 24, 2024
2 parents a5c4b2c + bc751ce commit fc18fa9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,14 +790,18 @@ private OnionArchitecture(
Optional<DescribedPredicate<? super JavaClass>> domainServicePredicate,
Optional<DescribedPredicate<? super JavaClass>> applicationPredicate,
Map<String, DescribedPredicate<? super JavaClass>> adapterPredicates,
boolean optionalLayers,
List<IgnoredDependency> ignoredDependencies,
Optional<String> overriddenDescription) {
Optional<String> overriddenDescription,
AllClassesAreContainedInArchitectureCheck allClassesAreContainedInArchitectureCheck) {
this.domainModelPredicate = domainModelPredicate;
this.domainServicePredicate = domainServicePredicate;
this.applicationPredicate = applicationPredicate;
this.adapterPredicates = adapterPredicates;
this.optionalLayers = optionalLayers;
this.ignoredDependencies = ignoredDependencies;
this.overriddenDescription = overriddenDescription;
this.allClassesAreContainedInArchitectureCheck = allClassesAreContainedInArchitectureCheck;
}

/**
Expand Down Expand Up @@ -1036,8 +1040,8 @@ public void check(JavaClasses classes) {
}

@Override
public ArchRule because(String reason) {
return ArchRule.Factory.withBecause(this, reason);
public OnionArchitecture because(String reason) {
return (OnionArchitecture) Factory.withBecause(this, reason);
}

/**
Expand All @@ -1051,9 +1055,15 @@ public ArchRule allowEmptyShould(boolean allowEmptyShould) {

@Override
public OnionArchitecture as(String newDescription) {
return new OnionArchitecture(domainModelPredicate, domainServicePredicate,
applicationPredicate, adapterPredicates, ignoredDependencies,
Optional.of(newDescription));
return new OnionArchitecture(
domainModelPredicate,
domainServicePredicate,
applicationPredicate,
adapterPredicates,
optionalLayers,
ignoredDependencies,
Optional.of(newDescription),
allClassesAreContainedInArchitectureCheck);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.tngtech.archunit.library;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import com.google.common.collect.ImmutableSet;
import com.tngtech.archunit.core.domain.JavaClasses;
Expand Down Expand Up @@ -40,6 +43,7 @@
import static com.tngtech.java.junit.dataprovider.DataProviders.testForEach;
import static java.beans.Introspector.decapitalize;
import static java.lang.System.lineSeparator;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -178,9 +182,24 @@ public void onion_architecture_rejects_empty_layers_by_default() {
assertFailureOnionArchitectureWithEmptyLayers(result);
}

@DataProvider
public static List<Object> ruleTextModifications() {
return Stream.<Function<OnionArchitecture, OnionArchitecture>>of(
Function.identity(),
onionArchitecture -> onionArchitecture
.as("Onion architecture consisting of (optional) changed"),
onionArchitecture -> onionArchitecture
.because("changed"),
onionArchitecture -> onionArchitecture
.as("Onion architecture consisting of (optional) changed")
.because("changed")
).collect(toList());
}

@Test
public void onion_architecture_allows_empty_layers_if_all_layers_are_optional() {
OnionArchitecture architecture = anOnionArchitectureWithEmptyLayers().withOptionalLayers(true);
@UseDataProvider("ruleTextModifications")
public void onion_architecture_allows_empty_layers_if_all_layers_are_optional(Function<OnionArchitecture, OnionArchitecture> modifyRule) {
OnionArchitecture architecture = modifyRule.apply(anOnionArchitectureWithEmptyLayers().withOptionalLayers(true));
assertThat(architecture.getDescription()).startsWith("Onion architecture consisting of (optional)");

JavaClasses classes = new ClassFileImporter().importPackages(absolute("onionarchitecture"));
Expand All @@ -201,12 +220,13 @@ public void onion_architecture_rejects_empty_layers_if_layers_are_explicitly_not
}

@Test
public void onion_architecture_ensure_all_classes_are_contained_in_architecture() {
@UseDataProvider("ruleTextModifications")
public void onion_architecture_ensure_all_classes_are_contained_in_architecture(Function<OnionArchitecture, OnionArchitecture> modifyRule) {
JavaClasses classes = new ClassFileImporter().importClasses(First.class, Second.class);

OnionArchitecture architectureNotCoveringAllClasses = onionArchitecture().withOptionalLayers(true)
OnionArchitecture architectureNotCoveringAllClasses = modifyRule.apply(onionArchitecture().withOptionalLayers(true)
.domainModels("..first..")
.ensureAllClassesAreContainedInArchitecture();
.ensureAllClassesAreContainedInArchitecture());

assertThatRule(architectureNotCoveringAllClasses).checking(classes)
.hasOnlyOneViolation("Class <" + Second.class.getName() + "> is not contained in architecture");
Expand Down

0 comments on commit fc18fa9

Please sign in to comment.