提交 74ccfe35 编写于 作者: S Sam Brannen

Polish Spring's internal TestGroup support

上级 3fa4d638
......@@ -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]");
......
/*
* 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));
}
......
......@@ -35,7 +35,6 @@ import org.springframework.test.context.junit4.rules.BasicAnnotationConfigWacSpr
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.tests.TestGroup;
import org.springframework.tests.TestGroups;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assume.assumeTrue;
......@@ -86,8 +85,7 @@ public class SpringJUnit4ConcurrencyTests {
@BeforeClass
public static void abortIfLongRunningTestGroupIsNotEnabled() {
assumeTrue("TestGroup " + TestGroup.LONG_RUNNING + " is not active.",
TestGroups.isGroupActive(TestGroup.LONG_RUNNING));
assumeTrue("TestGroup " + TestGroup.LONG_RUNNING + " is not active.", TestGroup.LONG_RUNNING.isActive());
}
@Test
......
......@@ -26,15 +26,12 @@ import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
......@@ -59,28 +56,21 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
@SpringJUnitWebConfig
class MockMvcWebClientBuilderTests {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@BeforeEach
void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockMvcWebClientBuilderTests(WebApplicationContext wac) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
void mockMvcSetupNull() {
assertThatIllegalArgumentException().isThrownBy(() ->
MockMvcWebClientBuilder.mockMvcSetup(null));
assertThatIllegalArgumentException().isThrownBy(() -> MockMvcWebClientBuilder.mockMvcSetup(null));
}
@Test
void webAppContextSetupNull() {
assertThatIllegalArgumentException().isThrownBy(() ->
MockMvcWebClientBuilder.webAppContextSetup(null));
assertThatIllegalArgumentException().isThrownBy(() -> MockMvcWebClientBuilder.webAppContextSetup(null));
}
@Test
......@@ -88,7 +78,10 @@ class MockMvcWebClientBuilderTests {
WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build();
assertMockMvcUsed(client, "http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed(client, "https://spring.io/"));
if (TestGroup.PERFORMANCE.isActive()) {
assertMockMvcNotUsed(client, "https://spring.io/");
}
}
@Test
......@@ -97,7 +90,10 @@ class MockMvcWebClientBuilderTests {
WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherClient).build();
assertMockMvcUsed(client, "http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed(client, "https://spring.io/"));
if (TestGroup.PERFORMANCE.isActive()) {
assertMockMvcNotUsed(client, "https://spring.io/");
}
}
@Test // SPR-14066
......
......@@ -20,16 +20,13 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -52,30 +49,23 @@ class MockMvcHtmlUnitDriverBuilderTests {
private static final String EXPECTED_BODY = "MockMvcHtmlUnitDriverBuilderTests mvc";
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
private HtmlUnitDriver driver;
@BeforeEach
void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockMvcHtmlUnitDriverBuilderTests(WebApplicationContext wac) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
void webAppContextSetupNull() {
assertThatIllegalArgumentException().isThrownBy(() ->
MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null));
assertThatIllegalArgumentException().isThrownBy(() -> MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null));
}
@Test
void mockMvcSetupNull() {
assertThatIllegalArgumentException().isThrownBy(() ->
MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null));
assertThatIllegalArgumentException().isThrownBy(() -> MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null));
}
@Test
......@@ -84,7 +74,10 @@ class MockMvcHtmlUnitDriverBuilderTests {
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherDriver).build();
assertMockMvcUsed("http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("https://example.com/"));
if (TestGroup.PERFORMANCE.isActive()) {
assertMockMvcNotUsed("https://example.com/");
}
}
@Test
......@@ -92,7 +85,10 @@ class MockMvcHtmlUnitDriverBuilderTests {
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
assertMockMvcUsed("http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("https://example.com/"));
if (TestGroup.PERFORMANCE.isActive()) {
assertMockMvcNotUsed("https://example.com/");
}
}
@Test
......@@ -111,8 +107,7 @@ class MockMvcHtmlUnitDriverBuilderTests {
void cookieManagerShared() throws Exception {
WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc)
.withDelegate(otherDriver).build();
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherDriver).build();
assertThat(get("http://localhost/")).isEqualTo("");
Cookie cookie = new Cookie("localhost", "cookie", "cookieManagerShared");
......@@ -143,7 +138,7 @@ class MockMvcHtmlUnitDriverBuilderTests {
static class ContextPathController {
@RequestMapping("/test")
public String contextPath(HttpServletRequest request) {
String contextPath(HttpServletRequest request) {
return EXPECTED_BODY;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册