Skip to content

Commit

Permalink
Review: We should also be able to adjust the priority.
Browse files Browse the repository at this point in the history
Issue: TNG#78
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Dec 16, 2018
1 parent f7eaf7f commit 16bad98
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@
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 com.tngtech.archunit.lang.Priority.MEDIUM;
import static java.util.Collections.singletonList;

public final class CompositeArchRule implements ArchRule {
private final Priority priority;
private final List<ArchRule> rules;
private final String description;

private CompositeArchRule(List<ArchRule> rules, String description) {
private CompositeArchRule(Priority priority, List<ArchRule> rules, String description) {
this.priority = priority;
this.rules = checkNotNull(rules);
this.description = checkNotNull(description);
}

@PublicAPI(usage = ACCESS)
public static CompositeArchRule of(ArchRule rule) {
return new CompositeArchRule(singletonList(rule), rule.getDescription());
return priority(MEDIUM).of(rule);
}

@PublicAPI(usage = ACCESS)
public static Creator priority(Priority priority) {
return new Creator(priority);
}

@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);
return new CompositeArchRule(priority, newRules, newDescription);
}

@Override
Expand All @@ -54,12 +62,12 @@ public void check(JavaClasses classes) {

@Override
public CompositeArchRule because(String reason) {
return new CompositeArchRule(rules, createBecauseDescription(this, reason));
return new CompositeArchRule(priority, rules, createBecauseDescription(this, reason));
}

@Override
public EvaluationResult evaluate(JavaClasses classes) {
EvaluationResult result = new EvaluationResult(this, Priority.MEDIUM);
EvaluationResult result = new EvaluationResult(this, priority);
for (ArchRule rule : rules) {
result.add(rule.evaluate(classes));
}
Expand All @@ -68,11 +76,25 @@ public EvaluationResult evaluate(JavaClasses classes) {

@Override
public CompositeArchRule as(String newDescription) {
return new CompositeArchRule(rules, newDescription);
return new CompositeArchRule(priority, rules, newDescription);
}

@Override
public String getDescription() {
return description;
}

@PublicAPI(usage = ACCESS)
public static final class Creator {
private final Priority priority;

private Creator(Priority priority) {
this.priority = priority;
}

@PublicAPI(usage = ACCESS)
public final CompositeArchRule of(ArchRule rule) {
return new CompositeArchRule(priority, singletonList(rule), rule.getDescription());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.junit.runner.RunWith;

import static com.tngtech.archunit.core.domain.TestUtils.importClasses;
import static com.tngtech.archunit.lang.Priority.HIGH;
import static com.tngtech.archunit.lang.Priority.MEDIUM;
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.$$;
Expand All @@ -35,6 +37,7 @@ public void rules_are_ANDed(ArchRule first, ArchRule second, boolean expectedSat
EvaluationResult result = CompositeArchRule.of(first).and(second).evaluate(importClasses(getClass()));

assertThat(result.hasViolation()).as("result has violation").isEqualTo(!expectedSatisfied);
assertPriority(result.getFailureReport().toString(), MEDIUM);
}

@Test
Expand All @@ -55,6 +58,22 @@ public void description_is_modified_correctly() {
assertThat(modified.getDescription()).isEqualTo("overridden, because reason and changed");
}

@Test
public void priority_is_passed() {
Priority priority = HIGH;
CompositeArchRule rule = CompositeArchRule
.priority(priority)
.of(classes().should().bePublic()).and(classes().should().bePrivate());

String failureMessage = rule.evaluate(importClasses(getClass())).getFailureReport().toString();

assertPriority(failureMessage, priority);
}

private void assertPriority(String failureMessage, Priority priority) {
assertThat(failureMessage).contains(String.format("[Priority: %s]", priority));
}

private static ArchRule archRuleThatSucceeds() {
return createArchRuleWithSatisfied(true);
}
Expand Down

0 comments on commit 16bad98

Please sign in to comment.