Skip to content

Commit

Permalink
support executing single @ArchTest members (#1280)
Browse files Browse the repository at this point in the history
Currently there is no convenient way to execute a single `@ArchTest`
rule of a test running with JUnit 4 or 5 support.
As a simple mitigation we now provide the possibility to filter a single
test via `archunit.properties` / system property.
This change allows to e.g. pass
```
-Darchunit.junit.testFilter=rule_field_one,rule_field_two
```
to a test run, which will then filter the tests and only run the
`@Archtest` fields or methods with the given name(s).

For now we don't support any wildcards or narrowing the match down by
surrounding class name
to see if this feature is even missed by users. Basic filtering for
classes or packages should already be supported
by the respective test launcher (i.e. both JUnit 4 and 5 support should
respect filter requests for classes or packages already).

Issue: #641
  • Loading branch information
codecholeric authored Apr 10, 2024
2 parents 72c2f80 + 14d7f4d commit 23c483b
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.tngtech.archunit.lang.ArchRule;
import org.junit.runner.Description;

import static com.tngtech.archunit.junit.internal.DisplayNameResolver.determineDisplayName;

class ArchRuleExecution extends ArchTestExecution {
private final Field ruleField;

Expand All @@ -50,8 +48,7 @@ Result evaluateOn(JavaClasses classes) {

@Override
Description describeSelf() {
String testName = formatWithPath(ruleField.getName());
return Description.createTestDescription(getTestClass(), determineDisplayName(testName), ruleField.getAnnotations());
return createDescription(ruleField);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package com.tngtech.archunit.junit.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -24,6 +28,7 @@
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

import static com.tngtech.archunit.junit.internal.DisplayNameResolver.determineDisplayName;
import static com.tngtech.archunit.junit.internal.ReflectionUtils.getValueOrThrowException;
import static java.util.stream.Collectors.joining;

Expand All @@ -47,11 +52,16 @@ public String toString() {
return describeSelf().toString();
}

Class<?> getTestClass() {
return testClassPath.get(0);
<T extends Member & AnnotatedElement> Description createDescription(T member) {
Annotation[] annotations = Stream.concat(
Arrays.stream(member.getAnnotations()),
Stream.of(new ArchTestMetaInfo.Instance(member.getName()))
).toArray(Annotation[]::new);
String testName = formatWithPath(member.getName());
return Description.createTestDescription(testClassPath.get(0), determineDisplayName(testName), annotations);
}

String formatWithPath(String testName) {
private String formatWithPath(String testName) {
if (testClassPath.size() <= 1) {
return testName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2014-2024 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.junit.internal;

import java.lang.annotation.Annotation;
import java.util.Objects;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;

/**
* Hack to transport meta-information from {@link ArchTestExecution} to {@link ArchUnitSystemPropertyTestFilterJunit4}.
* Unfortunately, the {@link Filter} interface doesn't allow access to the original child of the {@link Runner},
* but only the {@link Description}, which is not suitable to obtain the original member name reliably.
*/
@interface ArchTestMetaInfo {
String memberName();

class Instance implements ArchTestMetaInfo, Annotation {
private final String memberName;

Instance(String memberName) {
this.memberName = memberName;
}

@Override
public String memberName() {
return memberName;
}

@Override
public Class<? extends Annotation> annotationType() {
return ArchTestMetaInfo.class;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Instance instance = (Instance) o;
return Objects.equals(memberName, instance.memberName);
}

@Override
public int hashCode() {
return Objects.hash(memberName);
}

@Override
public String toString() {
return "@" + ArchTestMetaInfo.class.getSimpleName() + "(" + memberName + ")";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.tngtech.archunit.junit.ArchTest;
import org.junit.runner.Description;

import static com.tngtech.archunit.junit.internal.DisplayNameResolver.determineDisplayName;
import static com.tngtech.archunit.junit.internal.ReflectionUtils.invokeMethod;

class ArchTestMethodExecution extends ArchTestExecution {
Expand Down Expand Up @@ -55,8 +54,7 @@ private void executeTestMethod(JavaClasses classes) {

@Override
Description describeSelf() {
String testName = formatWithPath(testMethod.getName());
return Description.createTestDescription(getTestClass(), determineDisplayName(testName), testMethod.getAnnotations());
return createDescription(testMethod);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.tngtech.archunit.junit.CacheMode;
import com.tngtech.archunit.junit.LocationProvider;
import org.junit.runner.Description;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.FrameworkField;
Expand All @@ -53,6 +54,12 @@ final class ArchUnitRunnerInternal extends ParentRunner<ArchTestExecution> imple
ArchUnitRunnerInternal(Class<?> testClass) throws InitializationError {
super(testClass);
checkAnnotation(testClass);

try {
ArchUnitSystemPropertyTestFilterJunit4.filter(this);
} catch (NoTestsRemainException e) {
throw new InitializationError(e);
}
}

private static AnalyzeClasses checkAnnotation(Class<?> testClass) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2014-2024 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.junit.internal;

import java.util.List;

import com.google.common.base.Splitter;
import com.tngtech.archunit.ArchConfiguration;
import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runners.ParentRunner;

import static java.util.Objects.requireNonNull;

class ArchUnitSystemPropertyTestFilterJunit4 extends Filter {
private static final String JUNIT_TEST_FILTER_PROPERTY_NAME = "junit.testFilter";
private final List<String> memberNames;

private ArchUnitSystemPropertyTestFilterJunit4(List<String> memberNames) {
this.memberNames = memberNames;
}

@Override
public boolean shouldRun(Description description) {
ArchTestMetaInfo metaInfo = requireNonNull(description.getAnnotation(ArchTestMetaInfo.class));
return memberNames.contains(metaInfo.memberName());
}

@Override
public String describe() {
return JUNIT_TEST_FILTER_PROPERTY_NAME + " = " + memberNames;
}

static void filter(ParentRunner<?> runner) throws NoTestsRemainException {
ArchConfiguration configuration = ArchConfiguration.get();
if (!configuration.containsProperty(JUNIT_TEST_FILTER_PROPERTY_NAME)) {
return;
}

String testFilterProperty = configuration.getProperty(JUNIT_TEST_FILTER_PROPERTY_NAME);
runner.filter(new ArchUnitSystemPropertyTestFilterJunit4(Splitter.on(",").splitToList(testFilterProperty)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Method;
import java.util.Collection;

import com.tngtech.archunit.ArchConfiguration;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.junit.AnalyzeClasses;
Expand All @@ -13,6 +14,7 @@
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.testutil.ArchConfigurationRule;
import org.assertj.core.api.iterable.Extractor;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -49,12 +51,17 @@
import static com.tngtech.archunit.testutil.TestUtils.invoke;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class ArchUnitRunnerRunsRuleSetsTest {
@Rule
public final MockitoRule mockitoRule = MockitoJUnit.rule();
@Rule
public final ArchConfigurationRule archConfigurationRule = new ArchConfigurationRule();

@Mock
private SharedCache cache;
Expand Down Expand Up @@ -138,6 +145,25 @@ public void can_run_rule_method() {
run(someMethodRuleName, runnerForRuleSet, verifyTestRan());
}

@Test
public void allows_to_run_single_rule_via_configuration() {
ArchConfiguration.get().setProperty("junit.testFilter", someFieldRuleName);

newRunnerFor(ArchTestWithRuleSet.class).run(runNotifier);

verifyOnlyTestsRan("Rules > " + someFieldRuleName);
}

@Test
public void allows_to_run_selected_rules_via_configuration() {
ArchConfiguration.get().setProperty("junit.testFilter",
someFieldRuleName + "," + someOtherMethodRuleName + ",some_non_existing_rule");

newRunnerFor(ArchTestWithRuleLibrary.class).run(runNotifier);

verifyOnlyTestsRan("ArchTestWithRuleSet > Rules > " + someFieldRuleName, someOtherMethodRuleName);
}

@Test
public void ignores_field_rule_of_ignored_rule_set() {
run(someFieldRuleName, runnerForIgnoredRuleSet, verifyTestIgnored());
Expand Down Expand Up @@ -246,6 +272,24 @@ private Runnable verifyTestRan() {
};
}

private void verifyOnlyTestsRan(String... memberNames) {
// Ignore these invocations as they are irrelevant
verify(runNotifier, atLeast(0)).fireTestSuiteStarted(any());
verify(runNotifier, atLeast(0)).fireTestFailure(any());
verify(runNotifier, atLeast(0)).fireTestSuiteFinished(any());

for (String memberName : memberNames) {
verify(runNotifier).fireTestStarted(descriptionMemberName(memberName));
verify(runNotifier).fireTestFinished(descriptionMemberName(memberName));
}

verifyNoMoreInteractions(runNotifier);
}

private static Description descriptionMemberName(String memberName) {
return argThat(description -> description.getMethodName().equals(memberName));
}

// extractingResultOf(..) only looks for public methods
private Extractor<Object, Object> resultOf(String methodName) {
return input -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2014-2024 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.junit.internal;

import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.tngtech.archunit.ArchConfiguration;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;

import static com.tngtech.archunit.junit.internal.ArchUnitTestDescriptor.FIELD_SEGMENT_TYPE;
import static com.tngtech.archunit.junit.internal.ArchUnitTestDescriptor.METHOD_SEGMENT_TYPE;

class ArchUnitSystemPropertyTestFilterJUnit5 {
private static final String JUNIT_TEST_FILTER_PROPERTY_NAME = "junit.testFilter";
private static final Set<String> MEMBER_SEGMENT_TYPES = ImmutableSet.of(FIELD_SEGMENT_TYPE, METHOD_SEGMENT_TYPE);

void filter(TestDescriptor descriptor) {
ArchConfiguration configuration = ArchConfiguration.get();
if (!configuration.containsProperty(JUNIT_TEST_FILTER_PROPERTY_NAME)) {
return;
}

String testFilterProperty = configuration.getProperty(JUNIT_TEST_FILTER_PROPERTY_NAME);
List<String> memberNames = Splitter.on(",").splitToList(testFilterProperty);
Predicate<TestDescriptor> shouldRunPredicate = testDescriptor -> memberNameMatches(testDescriptor, memberNames);
removeNonMatching(descriptor, shouldRunPredicate);
}

private void removeNonMatching(TestDescriptor descriptor, Predicate<TestDescriptor> shouldRunPredicate) {
ImmutableSet.copyOf(descriptor.getChildren())
.forEach(child -> removeNonMatching(child, shouldRunPredicate));

if (descriptor.getChildren().isEmpty() && !shouldRunPredicate.test(descriptor)) {
descriptor.removeFromHierarchy();
}
}

private static boolean memberNameMatches(TestDescriptor testDescriptor, List<String> memberNames) {
UniqueId.Segment lastSegment = testDescriptor.getUniqueId().getLastSegment();
return MEMBER_SEGMENT_TYPES.contains(lastSegment.getType())
&& memberNames.stream().anyMatch(it -> lastSegment.getValue().equals(it));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
public final class ArchUnitTestEngine extends HierarchicalTestEngine<ArchUnitEngineExecutionContext> {
static final String UNIQUE_ID = "archunit";

private final ArchUnitSystemPropertyTestFilterJUnit5 systemPropertyTestFilter = new ArchUnitSystemPropertyTestFilterJUnit5();

@SuppressWarnings("FieldMayBeFinal")
private SharedCache cache = new SharedCache(); // NOTE: We want to change this in tests -> no static/final reference

Expand All @@ -90,6 +92,8 @@ public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId
resolveRequestedFields(discoveryRequest, uniqueId, result);
resolveRequestedUniqueIds(discoveryRequest, uniqueId, result);

systemPropertyTestFilter.filter(result);

return result;
}

Expand Down
Loading

0 comments on commit 23c483b

Please sign in to comment.