Polish Spring's internal TestGroup support
This commit is contained in:
@@ -43,10 +43,8 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
* @author Sam Brannen
|
||||
* @since 3.2
|
||||
* @see EnabledForTestGroups @EnabledForTestGroups
|
||||
* @see #group(TestGroup)
|
||||
* @see #group(TestGroup, Executable)
|
||||
* @see #notLogging(Log)
|
||||
* @see TestGroup
|
||||
* @see TestGroups
|
||||
*/
|
||||
public abstract class Assume {
|
||||
|
||||
@@ -61,26 +59,11 @@ public abstract class Assume {
|
||||
*/
|
||||
@Deprecated
|
||||
public static void group(TestGroup group) {
|
||||
Set<TestGroup> testGroups = TestGroups.loadTestGroups();
|
||||
Set<TestGroup> testGroups = TestGroup.loadTestGroups();
|
||||
assumeTrue(testGroups.contains(group),
|
||||
() -> "Requires inactive test group " + group + "; active test groups: " + testGroups);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assume that a particular {@link TestGroup} is active before executing the
|
||||
* supplied {@link Executable}.
|
||||
* <p>If the assumption fails, the executable will not be executed, but
|
||||
* no {@link org.opentest4j.TestAbortedException} will be thrown.
|
||||
* @param group the group that must be active
|
||||
* @param executable the executable to execute if the test group is active
|
||||
* @since 4.2
|
||||
*/
|
||||
public static void group(TestGroup group, Executable executable) throws Exception {
|
||||
if (TestGroups.loadTestGroups().contains(group)) {
|
||||
executable.execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assume that the specified log is not set to Trace or Debug.
|
||||
* @param log the log to test
|
||||
|
||||
@@ -25,7 +25,9 @@ import org.opentest4j.TestAbortedException;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.tests.Assume.TEST_GROUPS_SYSTEM_PROPERTY;
|
||||
import static org.springframework.tests.TestGroup.CI;
|
||||
import static org.springframework.tests.TestGroup.LONG_RUNNING;
|
||||
@@ -61,28 +63,24 @@ class AssumeTests {
|
||||
@SuppressWarnings("deprecation")
|
||||
void assumeGroupWithNoActiveTestGroups() {
|
||||
setTestGroups("");
|
||||
Assume.group(LONG_RUNNING);
|
||||
fail("assumption should have failed");
|
||||
|
||||
assertThatExceptionOfType(TestAbortedException.class).isThrownBy(() -> Assume.group(LONG_RUNNING));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void assumeGroupWithNoMatchingActiveTestGroup() {
|
||||
setTestGroups(PERFORMANCE, CI);
|
||||
Assume.group(LONG_RUNNING);
|
||||
fail("assumption should have failed");
|
||||
assertThatExceptionOfType(TestAbortedException.class).isThrownBy(() -> Assume.group(LONG_RUNNING));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void assumeGroupWithMatchingActiveTestGroup() {
|
||||
setTestGroups(LONG_RUNNING);
|
||||
try {
|
||||
Assume.group(LONG_RUNNING);
|
||||
}
|
||||
catch (TestAbortedException ex) {
|
||||
fail("assumption should NOT have failed");
|
||||
}
|
||||
assertThatCode(() -> Assume.group(LONG_RUNNING))
|
||||
.as("assumption should NOT have failed")
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -101,22 +99,17 @@ class AssumeTests {
|
||||
//
|
||||
// java.lang.IllegalStateException: Failed to parse 'testGroups' system property:
|
||||
// Unable to find test group 'bogus' when parsing testGroups value: 'all-bogus'.
|
||||
// Available groups include: [LONG_RUNNING,PERFORMANCE,JMXMP,CI]
|
||||
// Available groups include: [LONG_RUNNING,PERFORMANCE,CI]
|
||||
|
||||
setTestGroups(testGroups);
|
||||
try {
|
||||
Assume.group(LONG_RUNNING);
|
||||
fail("assumption should have failed");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertThat(ex.getMessage()).
|
||||
startsWith("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY + "' system property: ");
|
||||
|
||||
assertThat(ex.getCause()).isInstanceOf(IllegalArgumentException.class);
|
||||
assertThat(ex.getCause().getMessage()).
|
||||
isEqualTo("Unable to find test group 'bogus' when parsing testGroups value: '" + testGroups
|
||||
+ "'. Available groups include: [LONG_RUNNING,PERFORMANCE,CI]");
|
||||
}
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> Assume.group(LONG_RUNNING))
|
||||
.withMessageStartingWith("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY + "' system property: ")
|
||||
.withCauseInstanceOf(IllegalArgumentException.class)
|
||||
.satisfies(ex ->
|
||||
assertThat(ex.getCause().getMessage()).isEqualTo(
|
||||
"Unable to find test group 'bogus' when parsing testGroups value: '" + testGroups +
|
||||
"'. Available groups include: [LONG_RUNNING,PERFORMANCE,CI]"));
|
||||
}
|
||||
|
||||
private void setTestGroups(TestGroup... testGroups) {
|
||||
|
||||
@@ -57,6 +57,31 @@ public enum TestGroup {
|
||||
CI;
|
||||
|
||||
|
||||
/**
|
||||
* Determine if this {@link TestGroup} is active.
|
||||
* @since 5.2
|
||||
*/
|
||||
public boolean isActive() {
|
||||
return loadTestGroups().contains(this);
|
||||
}
|
||||
|
||||
|
||||
private static final String TEST_GROUPS_SYSTEM_PROPERTY = "testGroups";
|
||||
|
||||
/**
|
||||
* Load test groups dynamically instead of during static initialization in
|
||||
* order to avoid a {@link NoClassDefFoundError} being thrown while attempting
|
||||
* to load collaborator classes.
|
||||
*/
|
||||
static Set<TestGroup> loadTestGroups() {
|
||||
try {
|
||||
return TestGroup.parse(System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY +
|
||||
"' system property: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Parse the specified comma separated string of groups.
|
||||
* @param value the comma separated string of groups
|
||||
@@ -64,7 +89,7 @@ public enum TestGroup {
|
||||
* @throws IllegalArgumentException if any specified group name is not a
|
||||
* valid {@link TestGroup}
|
||||
*/
|
||||
public static Set<TestGroup> parse(String value) throws IllegalArgumentException {
|
||||
static Set<TestGroup> parse(String value) throws IllegalArgumentException {
|
||||
if (!StringUtils.hasText(value)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@@ -25,67 +25,64 @@ import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link TestGroup}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class TestGroupTests {
|
||||
class TestGroupTests {
|
||||
|
||||
@Test
|
||||
public void parseNull() {
|
||||
void parseNull() {
|
||||
assertThat(TestGroup.parse(null)).isEqualTo(Collections.emptySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseEmptyString() {
|
||||
void parseEmptyString() {
|
||||
assertThat(TestGroup.parse("")).isEqualTo(Collections.emptySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseBlankString() {
|
||||
void parseBlankString() {
|
||||
assertThat(TestGroup.parse(" ")).isEqualTo(Collections.emptySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithSpaces() {
|
||||
assertThat(TestGroup.parse(" PERFORMANCE, PERFORMANCE ")).containsOnly(
|
||||
TestGroup.PERFORMANCE);
|
||||
void parseWithSpaces() {
|
||||
assertThat(TestGroup.parse(" PERFORMANCE, PERFORMANCE ")).containsOnly(TestGroup.PERFORMANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseInMixedCase() {
|
||||
assertThat(TestGroup.parse("performance, PERFormaNCE")).containsOnly(
|
||||
TestGroup.PERFORMANCE);
|
||||
void parseInMixedCase() {
|
||||
assertThat(TestGroup.parse("performance, PERFormaNCE")).containsOnly(TestGroup.PERFORMANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMissing() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
TestGroup.parse("performance, missing"))
|
||||
void parseMissing() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> TestGroup.parse("performance, missing"))
|
||||
.withMessageContaining("Unable to find test group 'missing' when parsing " +
|
||||
"testGroups value: 'performance, missing'. Available groups include: " +
|
||||
"[LONG_RUNNING,PERFORMANCE,CI]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseAll() {
|
||||
void parseAll() {
|
||||
assertThat(TestGroup.parse("all")).isEqualTo(EnumSet.allOf(TestGroup.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseAllExceptPerformance() {
|
||||
void parseAllExceptPerformance() {
|
||||
Set<TestGroup> expected = EnumSet.allOf(TestGroup.class);
|
||||
expected.remove(TestGroup.PERFORMANCE);
|
||||
assertThat(TestGroup.parse("all-performance")).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseAllExceptMissing() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
TestGroup.parse("all-missing"))
|
||||
void parseAllExceptMissing() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> TestGroup.parse("all-missing"))
|
||||
.withMessageContaining("Unable to find test group 'missing' when parsing " +
|
||||
"testGroups value: 'all-missing'. Available groups include: " +
|
||||
"[LONG_RUNNING,PERFORMANCE,CI]");
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 org.springframework.tests;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Utility methods for working with {@link TestGroup}s.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Rob Winch
|
||||
* @author Phillip Webb
|
||||
* @since 5.2
|
||||
*/
|
||||
public abstract class TestGroups {
|
||||
|
||||
static final String TEST_GROUPS_SYSTEM_PROPERTY = "testGroups";
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the provided {@link TestGroup} is active.
|
||||
* @param group the group to check
|
||||
* @since 5.2
|
||||
*/
|
||||
public static boolean isGroupActive(TestGroup group) {
|
||||
return loadTestGroups().contains(group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load test groups dynamically instead of during static initialization in
|
||||
* order to avoid a {@link NoClassDefFoundError} being thrown while attempting
|
||||
* to load the {@link Assume} class.
|
||||
*/
|
||||
static Set<TestGroup> loadTestGroups() {
|
||||
try {
|
||||
return TestGroup.parse(System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY
|
||||
+ "' system property: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class TestGroupsCondition implements ExecutionCondition {
|
||||
}
|
||||
TestGroup[] testGroups = optional.get().value();
|
||||
Assert.state(testGroups.length > 0, "You must declare at least one TestGroup in @EnabledForTestGroups");
|
||||
return (Arrays.stream(testGroups).anyMatch(TestGroups::isGroupActive)) ?
|
||||
return (Arrays.stream(testGroups).anyMatch(TestGroup::isActive)) ?
|
||||
enabled("Enabled for TestGroups: " + Arrays.toString(testGroups)) :
|
||||
disabled("Disabled for TestGroups: " + Arrays.toString(testGroups));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user