forked from TNG/ArchUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple ArchRules can be checked at once via CompositeArchRule.of(rule1).and(rule2). Also added sample tests for both plain, junit4 and junit5 Issue: TNG#78 Signed-off-by: Moritz Bogs <moritz.bogs@tngtech.com>
- Loading branch information
1 parent
13ae498
commit f7eaf7f
Showing
10 changed files
with
232 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
archunit/src/main/java/com/tngtech/archunit/lang/CompositeArchRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2018 TNG Technology Consulting GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.tngtech.archunit.lang; | ||
|
||
import java.util.List; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.tngtech.archunit.PublicAPI; | ||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; | ||
import static com.tngtech.archunit.lang.ArchRule.Factory.createBecauseDescription; | ||
import static java.util.Collections.singletonList; | ||
|
||
public final class CompositeArchRule implements ArchRule { | ||
private final List<ArchRule> rules; | ||
private final String description; | ||
|
||
private CompositeArchRule(List<ArchRule> rules, String description) { | ||
this.rules = checkNotNull(rules); | ||
this.description = checkNotNull(description); | ||
} | ||
|
||
@PublicAPI(usage = ACCESS) | ||
public static CompositeArchRule of(ArchRule rule) { | ||
return new CompositeArchRule(singletonList(rule), rule.getDescription()); | ||
} | ||
|
||
@PublicAPI(usage = ACCESS) | ||
public CompositeArchRule and(ArchRule rule) { | ||
List<ArchRule> newRules = ImmutableList.<ArchRule>builder().addAll(rules).add(rule).build(); | ||
String newDescription = description + " and " + rule.getDescription(); | ||
return new CompositeArchRule(newRules, newDescription); | ||
} | ||
|
||
@Override | ||
public void check(JavaClasses classes) { | ||
Assertions.check(this, classes); | ||
} | ||
|
||
@Override | ||
public CompositeArchRule because(String reason) { | ||
return new CompositeArchRule(rules, createBecauseDescription(this, reason)); | ||
} | ||
|
||
@Override | ||
public EvaluationResult evaluate(JavaClasses classes) { | ||
EvaluationResult result = new EvaluationResult(this, Priority.MEDIUM); | ||
for (ArchRule rule : rules) { | ||
result.add(rule.evaluate(classes)); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public CompositeArchRule as(String newDescription) { | ||
return new CompositeArchRule(rules, newDescription); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
archunit/src/test/java/com/tngtech/archunit/lang/CompositeArchRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.tngtech.archunit.lang; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.java.junit.dataprovider.DataProvider; | ||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static com.tngtech.archunit.core.domain.TestUtils.importClasses; | ||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
import static com.tngtech.java.junit.dataprovider.DataProviders.$; | ||
import static com.tngtech.java.junit.dataprovider.DataProviders.$$; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(DataProviderRunner.class) | ||
public class CompositeArchRuleTest { | ||
private static final boolean SATISFIED = true; | ||
private static final boolean UNSATISFIED = false; | ||
|
||
@DataProvider | ||
public static Object[][] rules_to_AND() { | ||
return $$( | ||
$(archRuleThatSucceeds(), archRuleThatSucceeds(), SATISFIED), | ||
$(archRuleThatSucceeds(), archRuleThatFails(), UNSATISFIED), | ||
$(archRuleThatFails(), archRuleThatSucceeds(), UNSATISFIED), | ||
$(archRuleThatFails(), archRuleThatFails(), UNSATISFIED) | ||
); | ||
} | ||
|
||
@Test | ||
@UseDataProvider("rules_to_AND") | ||
public void rules_are_ANDed(ArchRule first, ArchRule second, boolean expectedSatisfied) { | ||
EvaluationResult result = CompositeArchRule.of(first).and(second).evaluate(importClasses(getClass())); | ||
|
||
assertThat(result.hasViolation()).as("result has violation").isEqualTo(!expectedSatisfied); | ||
} | ||
|
||
@Test | ||
public void description_is_modified_correctly() { | ||
ArchRule input = classes().should().bePublic(); | ||
CompositeArchRule start = CompositeArchRule.of(input); | ||
|
||
assertThat(start.getDescription()).isEqualTo(input.getDescription()); | ||
|
||
CompositeArchRule modified = start.and(classes().should().bePrivate().as("inner changed")); | ||
|
||
assertThat(modified.getDescription()).isEqualTo(start.getDescription() + " and inner changed"); | ||
|
||
modified = modified.as("overridden") | ||
.because("reason") | ||
.and(classes().should().bePublic().as("changed")); | ||
|
||
assertThat(modified.getDescription()).isEqualTo("overridden, because reason and changed"); | ||
} | ||
|
||
private static ArchRule archRuleThatSucceeds() { | ||
return createArchRuleWithSatisfied(true); | ||
} | ||
|
||
private static ArchRule archRuleThatFails() { | ||
return createArchRuleWithSatisfied(false); | ||
} | ||
|
||
private static ArchRule createArchRuleWithSatisfied(final boolean satisfied) { | ||
return ArchRule.Factory.create(new AbstractClassesTransformer<JavaClass>("irrelevant") { | ||
@Override | ||
public Iterable<JavaClass> doTransform(JavaClasses collection) { | ||
return collection; | ||
} | ||
}, new ArchCondition<JavaClass>("irrelevant") { | ||
@Override | ||
public void check(JavaClass item, ConditionEvents events) { | ||
events.add(new SimpleConditionEvent(item, satisfied, "irrelevant")); | ||
} | ||
}, Priority.MEDIUM).as(String.format("%s rule", satisfied ? "satisfied" : "failing")); | ||
} | ||
} |
Oops, something went wrong.