Skip to content

Commit

Permalink
describe a rule execution as belonging to the actual test class inste…
Browse files Browse the repository at this point in the history
…ad as belonging to the class that declared the rule

Resolves TNG#452

Signed-off-by: Christian Semrau <[email protected]>
  • Loading branch information
csemrau committed Oct 19, 2020
1 parent 50df43c commit 6479a30
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.junit.runner.Description;

class ArchRuleExecution extends ArchTestExecution {
private final Class<?> testJavaClass;
private final Field ruleField;

ArchRuleExecution(Class<?> testClass, Field ruleField, boolean ignore) {
ArchRuleExecution(Class<?> testJavaClass, Class<?> testClass, Field ruleField, boolean ignore) {
super(testClass, ignore);
this.testJavaClass = testJavaClass;

ArchTestInitializationException.check(ArchRule.class.isAssignableFrom(ruleField.getType()),
"Rule field %s.%s to check must be of type %s",
Expand All @@ -47,7 +49,7 @@ Result evaluateOn(JavaClasses classes) {

@Override
Description describeSelf() {
return Description.createTestDescription(testClass, ruleField.getName());
return Description.createTestDescription(testJavaClass, ruleField.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ private Set<ArchTestExecution> findArchRulesIn(FrameworkField ruleField) {
if (ruleField.getType() == ArchRules.class) {
return asTestExecutions(getArchRules(ruleField.getField()), ignore);
}
return Collections.<ArchTestExecution>singleton(new ArchRuleExecution(getTestClass().getJavaClass(), ruleField.getField(), ignore));
return Collections.<ArchTestExecution>singleton(new ArchRuleExecution(getTestClass().getJavaClass(), getTestClass().getJavaClass(), ruleField.getField(), ignore));
}

private Set<ArchTestExecution> asTestExecutions(ArchRules archRules, boolean forceIgnore) {
ExecutionTransformer executionTransformer = new ExecutionTransformer();
ExecutionTransformer executionTransformer = new ExecutionTransformer(getTestClass().getJavaClass());
for (ArchRuleDeclaration<?> declaration : toDeclarations(archRules, getTestClass().getJavaClass(), ArchTest.class, forceIgnore)) {
declaration.handleWith(executionTransformer);
}
Expand Down Expand Up @@ -170,10 +170,15 @@ void clear(Class<?> testClass) {

private static class ExecutionTransformer implements ArchRuleDeclaration.Handler {
private final ImmutableSet.Builder<ArchTestExecution> executions = ImmutableSet.builder();
private final Class<?> testJavaClass;

public ExecutionTransformer(Class<?> testJavaClass) {
this.testJavaClass = testJavaClass;
}

@Override
public void handleFieldDeclaration(Field field, Class<?> fieldOwner, boolean ignore) {
executions.add(new ArchRuleExecution(fieldOwner, field, ignore));
executions.add(new ArchRuleExecution(testJavaClass, fieldOwner, field, ignore));
}

@Override
Expand Down

0 comments on commit 6479a30

Please sign in to comment.