-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support executing single
@ArchTest
members (#1280)
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
Showing
15 changed files
with
548 additions
and
74 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
72 changes: 72 additions & 0 deletions
72
...unit-junit/junit4/src/main/java/com/tngtech/archunit/junit/internal/ArchTestMetaInfo.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,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 + ")"; | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...main/java/com/tngtech/archunit/junit/internal/ArchUnitSystemPropertyTestFilterJunit4.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,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))); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...main/java/com/tngtech/archunit/junit/internal/ArchUnitSystemPropertyTestFilterJUnit5.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,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)); | ||
} | ||
} |
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
Oops, something went wrong.