Polish parameterized tests

This commit is contained in:
Sam Brannen
2019-08-20 12:41:32 +02:00
parent 617863ae4b
commit cf1bf3d98c
4 changed files with 88 additions and 88 deletions

View File

@@ -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;
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,69 +51,10 @@ 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 {
}
@ContextConfiguration("classpath:context.xml")
class ExplicitClasspathLocationsTestCase {
}
@ContextConfiguration("file:/testing/directory/context.xml")
class ExplicitFileLocationsTestCase {
}
@ContextConfiguration("https://example.com/context.xml")
class ExplicitUrlLocationsTestCase {
}
@ContextConfiguration({ "context1.xml", "classpath:context2.xml", "/context3.xml",
"file:/testing/directory/context.xml", "https://example.com/context.xml" })
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());
Class<?> testClass = ClassUtils.forName(getClass().getName() + "$" + testClassName, getClass().getClassLoader());
final ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
final ContextLoader contextLoader = new GenericXmlContextLoader();
@@ -129,4 +71,62 @@ class GenericXmlContextLoaderResourceLocationsTests {
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
class ClasspathNonExistentDefaultLocationsTestCase {
}
@ContextConfiguration
class ClasspathExistentDefaultLocationsTestCase {
}
@ContextConfiguration({ "context1.xml", "context2.xml" })
class ImplicitClasspathLocationsTestCase {
}
@ContextConfiguration("classpath:context.xml")
class ExplicitClasspathLocationsTestCase {
}
@ContextConfiguration("file:/testing/directory/context.xml")
class ExplicitFileLocationsTestCase {
}
@ContextConfiguration("https://example.com/context.xml")
class ExplicitUrlLocationsTestCase {
}
@ContextConfiguration({ "context1.xml", "classpath:context2.xml", "/context3.xml",
"file:/testing/directory/context.xml", "https://example.com/context.xml" })
class ExplicitMixedPathTypesLocationsTestCase {
}
}