Browse Source

Polish parameterized tests

pull/23493/head
Sam Brannen 6 years ago
parent
commit
cf1bf3d98c
  1. 118
      spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java
  2. 0
      spring-test/src/test/resources/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$ClasspathExistentDefaultLocationsTestCase-context.xml
  3. 4
      spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
  4. 48
      spring-web/src/test/java/org/springframework/web/context/request/ServletWebRequestHttpMethodsTests.java

118
spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java

@ -16,12 +16,12 @@ @@ -16,12 +16,12 @@
package org.springframework.test.context.support;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.core.annotation.AnnotationUtils;
@ -31,6 +31,7 @@ import org.springframework.util.ClassUtils; @@ -31,6 +31,7 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/**
* Unit test which verifies proper
@ -50,83 +51,82 @@ class GenericXmlContextLoaderResourceLocationsTests { @@ -50,83 +51,82 @@ class GenericXmlContextLoaderResourceLocationsTests {
private static final Log logger = LogFactory.getLog(GenericXmlContextLoaderResourceLocationsTests.class);
static Collection<Object[]> contextConfigurationLocationsData() {
@ContextConfiguration
class ClasspathNonExistentDefaultLocationsTestCase {
}
@ContextConfiguration
class ClasspathExistentDefaultLocationsTestCase {
}
@ContextConfiguration({ "context1.xml", "context2.xml" })
class ImplicitClasspathLocationsTestCase {
}
@ParameterizedTest(name = "{0}")
@MethodSource("contextConfigurationLocationsData")
void assertContextConfigurationLocations(String testClassName, String[] expectedLocations) throws Exception {
Class<?> testClass = ClassUtils.forName(getClass().getName() + "$" + testClassName, getClass().getClassLoader());
@ContextConfiguration("classpath:context.xml")
class ExplicitClasspathLocationsTestCase {
}
final ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
final ContextLoader contextLoader = new GenericXmlContextLoader();
final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
final String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
@ContextConfiguration("file:/testing/directory/context.xml")
class ExplicitFileLocationsTestCase {
if (logger.isDebugEnabled()) {
logger.debug("----------------------------------------------------------------------");
logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
logger.debug("Expected locations: " + ObjectUtils.nullSafeToString(expectedLocations));
logger.debug("Processed locations: " + ObjectUtils.nullSafeToString(processedLocations));
}
@ContextConfiguration("https://example.com/context.xml")
class ExplicitUrlLocationsTestCase {
}
assertThat(processedLocations).as("Verifying locations for test [" + testClass + "].").isEqualTo(expectedLocations);
}
@ContextConfiguration({ "context1.xml", "classpath:context2.xml", "/context3.xml",
"file:/testing/directory/context.xml", "https://example.com/context.xml" })
class ExplicitMixedPathTypesLocationsTestCase {
}
static Stream<Arguments> contextConfigurationLocationsData() {
return Stream.of(
arguments(ClasspathNonExistentDefaultLocationsTestCase.class.getSimpleName(), array()),
return Arrays.asList(new Object[][] {
arguments(ClasspathExistentDefaultLocationsTestCase.class.getSimpleName(), array(
"classpath:org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$ClasspathExistentDefaultLocationsTestCase-context.xml")),
{ ClasspathNonExistentDefaultLocationsTestCase.class.getSimpleName(), new String[] {} },
arguments(ImplicitClasspathLocationsTestCase.class.getSimpleName(),
array("classpath:/org/springframework/test/context/support/context1.xml",
"classpath:/org/springframework/test/context/support/context2.xml")),
{
ClasspathExistentDefaultLocationsTestCase.class.getSimpleName(),
new String[] { "classpath:org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$1ClasspathExistentDefaultLocationsTestCase-context.xml" } },
arguments(ExplicitClasspathLocationsTestCase.class.getSimpleName(), array("classpath:context.xml")),
{
ImplicitClasspathLocationsTestCase.class.getSimpleName(),
new String[] { "classpath:/org/springframework/test/context/support/context1.xml",
"classpath:/org/springframework/test/context/support/context2.xml" } },
arguments(ExplicitFileLocationsTestCase.class.getSimpleName(),
array("file:/testing/directory/context.xml")),
{ ExplicitClasspathLocationsTestCase.class.getSimpleName(), new String[] { "classpath:context.xml" } },
arguments(ExplicitUrlLocationsTestCase.class.getSimpleName(), array("https://example.com/context.xml")),
{ ExplicitFileLocationsTestCase.class.getSimpleName(), new String[] { "file:/testing/directory/context.xml" } },
arguments(ExplicitMixedPathTypesLocationsTestCase.class.getSimpleName(),
array("classpath:/org/springframework/test/context/support/context1.xml", "classpath:context2.xml",
"classpath:/context3.xml", "file:/testing/directory/context.xml",
"https://example.com/context.xml"))
);
}
{ ExplicitUrlLocationsTestCase.class.getSimpleName(), new String[] { "https://example.com/context.xml" } },
private static String[] array(String... elements) {
return elements;
}
{
ExplicitMixedPathTypesLocationsTestCase.class.getSimpleName(),
new String[] { "classpath:/org/springframework/test/context/support/context1.xml",
"classpath:context2.xml", "classpath:/context3.xml", "file:/testing/directory/context.xml",
"https://example.com/context.xml" } }
@ContextConfiguration
class ClasspathNonExistentDefaultLocationsTestCase {
}
});
@ContextConfiguration
class ClasspathExistentDefaultLocationsTestCase {
}
@ContextConfiguration({ "context1.xml", "context2.xml" })
class ImplicitClasspathLocationsTestCase {
}
@ParameterizedTest(name = "{0}")
@MethodSource("contextConfigurationLocationsData")
void assertContextConfigurationLocations(String testClassName, String[] expectedLocations) throws Exception {
Class<?> testClass = ClassUtils.forName(getClass().getName() + "$1" + testClassName, getClass().getClassLoader());
@ContextConfiguration("classpath:context.xml")
class ExplicitClasspathLocationsTestCase {
}
final ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
final ContextLoader contextLoader = new GenericXmlContextLoader();
final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
final String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
@ContextConfiguration("file:/testing/directory/context.xml")
class ExplicitFileLocationsTestCase {
}
if (logger.isDebugEnabled()) {
logger.debug("----------------------------------------------------------------------");
logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
logger.debug("Expected locations: " + ObjectUtils.nullSafeToString(expectedLocations));
logger.debug("Processed locations: " + ObjectUtils.nullSafeToString(processedLocations));
}
@ContextConfiguration("https://example.com/context.xml")
class ExplicitUrlLocationsTestCase {
}
assertThat(processedLocations).as("Verifying locations for test [" + testClass + "].").isEqualTo(expectedLocations);
@ContextConfiguration({ "context1.xml", "classpath:context2.xml", "/context3.xml",
"file:/testing/directory/context.xml", "https://example.com/context.xml" })
class ExplicitMixedPathTypesLocationsTestCase {
}
}

0
spring-test/src/test/resources/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$1ClasspathExistentDefaultLocationsTestCase-context.xml → spring-test/src/test/resources/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$ClasspathExistentDefaultLocationsTestCase-context.xml

4
spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

@ -82,13 +82,13 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests { @@ -82,13 +82,13 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ParameterizedTest
@ParameterizedTest(name = "{0}")
@MethodSource("clientHttpRequestFactories")
@interface ParameterizedRestTemplateTest {
}
@SuppressWarnings("deprecation")
static Stream<? extends ClientHttpRequestFactory> clientHttpRequestFactories() {
static Stream<ClientHttpRequestFactory> clientHttpRequestFactories() {
return Stream.of(
new SimpleClientHttpRequestFactory(),
new HttpComponentsClientHttpRequestFactory(),

48
spring-web/src/test/java/org/springframework/web/context/request/ServletWebRequestHttpMethodsTests.java

@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Markus Malkusch
* @author Sam Brannen
*/
public class ServletWebRequestHttpMethodsTests {
class ServletWebRequestHttpMethodsTests {
private static final String CURRENT_TIME = "Wed, 9 Apr 2014 09:57:42 GMT";
@ -54,7 +54,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -54,7 +54,7 @@ public class ServletWebRequestHttpMethodsTests {
@ParameterizedHttpMethodTest
public void checkNotModifiedNon2xxStatus(String method) {
void checkNotModifiedNon2xxStatus(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
@ -67,7 +67,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -67,7 +67,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest // SPR-13516
public void checkNotModifiedInvalidStatus(String method) {
void checkNotModifiedInvalidStatus(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
@ -78,7 +78,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -78,7 +78,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest // SPR-14559
public void checkNotModifiedInvalidIfNoneMatchHeader(String method) {
void checkNotModifiedInvalidIfNoneMatchHeader(String method) {
setUpRequest(method);
String etag = "\"etagvalue\"";
@ -89,7 +89,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -89,7 +89,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedHeaderAlreadySet(String method) {
void checkNotModifiedHeaderAlreadySet(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
@ -103,7 +103,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -103,7 +103,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedTimestamp(String method) {
void checkNotModifiedTimestamp(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
@ -115,7 +115,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -115,7 +115,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkModifiedTimestamp(String method) {
void checkModifiedTimestamp(String method) {
setUpRequest(method);
long oneMinuteAgo = currentDate.getTime() - (1000 * 60);
@ -127,7 +127,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -127,7 +127,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedETag(String method) {
void checkNotModifiedETag(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
@ -139,7 +139,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -139,7 +139,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedETagWithSeparatorChars(String method) {
void checkNotModifiedETagWithSeparatorChars(String method) {
setUpRequest(method);
String etag = "\"Foo, Bar\"";
@ -152,7 +152,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -152,7 +152,7 @@ public class ServletWebRequestHttpMethodsTests {
@ParameterizedHttpMethodTest
public void checkModifiedETag(String method) {
void checkModifiedETag(String method) {
setUpRequest(method);
String currentETag = "\"Foo\"";
@ -165,7 +165,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -165,7 +165,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedUnpaddedETag(String method) {
void checkNotModifiedUnpaddedETag(String method) {
setUpRequest(method);
String etag = "Foo";
@ -178,7 +178,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -178,7 +178,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkModifiedUnpaddedETag(String method) {
void checkModifiedUnpaddedETag(String method) {
setUpRequest(method);
String currentETag = "Foo";
@ -191,7 +191,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -191,7 +191,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedWildcardIsIgnored(String method) {
void checkNotModifiedWildcardIsIgnored(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
@ -203,7 +203,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -203,7 +203,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedETagAndTimestamp(String method) {
void checkNotModifiedETagAndTimestamp(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
@ -217,7 +217,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -217,7 +217,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest // SPR-14224
public void checkNotModifiedETagAndModifiedTimestamp(String method) {
void checkNotModifiedETagAndModifiedTimestamp(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
@ -233,7 +233,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -233,7 +233,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkModifiedETagAndNotModifiedTimestamp(String method) {
void checkModifiedETagAndNotModifiedTimestamp(String method) {
setUpRequest(method);
String currentETag = "\"Foo\"";
@ -249,7 +249,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -249,7 +249,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedETagWeakStrong(String method) {
void checkNotModifiedETagWeakStrong(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
@ -262,7 +262,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -262,7 +262,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedETagStrongWeak(String method) {
void checkNotModifiedETagStrongWeak(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
@ -274,7 +274,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -274,7 +274,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedMultipleETags(String method) {
void checkNotModifiedMultipleETags(String method) {
setUpRequest(method);
String etag = "\"Bar\"";
@ -287,7 +287,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -287,7 +287,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedTimestampWithLengthPart(String method) {
void checkNotModifiedTimestampWithLengthPart(String method) {
setUpRequest(method);
long epochTime = ZonedDateTime.parse(CURRENT_TIME, RFC_1123_DATE_TIME).toInstant().toEpochMilli();
@ -300,7 +300,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -300,7 +300,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkModifiedTimestampWithLengthPart(String method) {
void checkModifiedTimestampWithLengthPart(String method) {
setUpRequest(method);
long epochTime = ZonedDateTime.parse(CURRENT_TIME, RFC_1123_DATE_TIME).toInstant().toEpochMilli();
@ -313,7 +313,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -313,7 +313,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedTimestampConditionalPut(String method) {
void checkNotModifiedTimestampConditionalPut(String method) {
setUpRequest(method);
long currentEpoch = currentDate.getTime();
@ -327,7 +327,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -327,7 +327,7 @@ public class ServletWebRequestHttpMethodsTests {
}
@ParameterizedHttpMethodTest
public void checkNotModifiedTimestampConditionalPutConflict(String method) {
void checkNotModifiedTimestampConditionalPutConflict(String method) {
setUpRequest(method);
long currentEpoch = currentDate.getTime();
@ -348,7 +348,7 @@ public class ServletWebRequestHttpMethodsTests { @@ -348,7 +348,7 @@ public class ServletWebRequestHttpMethodsTests {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ParameterizedTest
@ParameterizedTest(name = "{0}")
@ValueSource(strings = { "GET", "HEAD" })
@interface ParameterizedHttpMethodTest {
}

Loading…
Cancel
Save