Skip to content

Commit

Permalink
describe a rule execution as belonging to the annotated test class in…
Browse files Browse the repository at this point in the history
…stead 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 bbceaff
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 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 @@ -24,10 +24,12 @@
import static com.tngtech.archunit.junit.ReflectionUtils.invokeMethod;

class ArchTestMethodExecution extends ArchTestExecution {
private final Class<?> testJavaClass;
private final Method testMethod;

ArchTestMethodExecution(Class<?> testClass, Method testMethod, boolean ignore) {
ArchTestMethodExecution(Class<?> testJavaClass, Class<?> testClass, Method testMethod, boolean ignore) {
super(testClass, ignore);
this.testJavaClass = testJavaClass;
this.testMethod = testMethod;
}

Expand All @@ -52,7 +54,7 @@ private void executeTestMethod(JavaClasses classes) {

@Override
Description describeSelf() {
return Description.createTestDescription(testClass, testMethod.getName());
return Description.createTestDescription(testJavaClass, testMethod.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 All @@ -133,7 +133,7 @@ private Collection<ArchTestExecution> findArchRuleMethods() {
List<ArchTestExecution> result = new ArrayList<>();
for (FrameworkMethod testMethod : getTestClass().getAnnotatedMethods(ArchTest.class)) {
boolean ignore = elementShouldBeIgnored(testMethod.getMethod());
result.add(new ArchTestMethodExecution(getTestClass().getJavaClass(), testMethod.getMethod(), ignore));
result.add(new ArchTestMethodExecution(getTestClass().getJavaClass(), getTestClass().getJavaClass(), testMethod.getMethod(), ignore));
}
return result;
}
Expand Down Expand Up @@ -170,15 +170,20 @@ 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
public void handleMethodDeclaration(Method method, Class<?> methodOwner, boolean ignore) {
executions.add(new ArchTestMethodExecution(methodOwner, method, ignore));
executions.add(new ArchTestMethodExecution(testJavaClass, methodOwner, method, ignore));
}

Set<ArchTestExecution> getExecutions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public void can_run_rule_method() {
}

@Test
public void describes_nested_rules_within_their_declaring_class() {
public void describes_nested_rules_within_their_testing_class() {
for (ArchTestExecution execution : runnerForRuleSet.getChildren()) {
assertThat(execution.describeSelf().getTestClass()).isEqualTo(Rules.class);
assertThat(execution.describeSelf().getTestClass()).isEqualTo(ArchTestWithRuleSet.class);
}
}

Expand Down

0 comments on commit bbceaff

Please sign in to comment.