Browse Source

Polish parameterized tests

pull/23493/head
Sam Brannen 6 years ago
parent
commit
cf1bf3d98c
  1. 104
      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

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

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

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

Loading…
Cancel
Save