Merge branch '2.0.x' into 2.1.x

Closes gh-17078
This commit is contained in:
Andy Wilkinson
2019-06-07 10:50:34 +01:00
2691 changed files with 27746 additions and 46049 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-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.
@@ -55,8 +55,7 @@ public class TestCompiler {
this(ToolProvider.getSystemJavaCompiler(), temporaryFolder);
}
public TestCompiler(JavaCompiler compiler, TemporaryFolder temporaryFolder)
throws IOException {
public TestCompiler(JavaCompiler compiler, TemporaryFolder temporaryFolder) throws IOException {
this.compiler = compiler;
this.fileManager = compiler.getStandardFileManager(null, null, null);
this.outputLocation = temporaryFolder.newFolder();
@@ -66,8 +65,7 @@ public class TestCompiler {
}
public TestCompilationTask getTask(Collection<File> sourceFiles) {
Iterable<? extends JavaFileObject> javaFileObjects = this.fileManager
.getJavaFileObjectsFromFiles(sourceFiles);
Iterable<? extends JavaFileObject> javaFileObjects = this.fileManager.getJavaFileObjectsFromFiles(sourceFiles);
return getTask(javaFileObjects);
}
@@ -76,10 +74,9 @@ public class TestCompiler {
return getTask(javaFileObjects);
}
private TestCompilationTask getTask(
Iterable<? extends JavaFileObject> javaFileObjects) {
return new TestCompilationTask(this.compiler.getTask(null, this.fileManager, null,
null, null, javaFileObjects));
private TestCompilationTask getTask(Iterable<? extends JavaFileObject> javaFileObjects) {
return new TestCompilationTask(
this.compiler.getTask(null, this.fileManager, null, null, null, javaFileObjects));
}
public File getOutputLocation() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-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.
@@ -50,12 +50,11 @@ public abstract class AbstractConfigurationClassTests {
public void allBeanMethodsArePublic() throws IOException {
Set<String> nonPublicBeanMethods = new HashSet<>();
for (AnnotationMetadata configurationClass : findConfigurationClasses()) {
Set<MethodMetadata> beanMethods = configurationClass
.getAnnotatedMethods(Bean.class.getName());
Set<MethodMetadata> beanMethods = configurationClass.getAnnotatedMethods(Bean.class.getName());
for (MethodMetadata methodMetadata : beanMethods) {
if (!isPublic(methodMetadata)) {
nonPublicBeanMethods.add(methodMetadata.getDeclaringClassName() + "."
+ methodMetadata.getMethodName());
nonPublicBeanMethods
.add(methodMetadata.getDeclaringClassName() + "." + methodMetadata.getMethodName());
}
}
}
@@ -64,16 +63,13 @@ public abstract class AbstractConfigurationClassTests {
private Set<AnnotationMetadata> findConfigurationClasses() throws IOException {
Set<AnnotationMetadata> configurationClasses = new HashSet<>();
Resource[] resources = this.resolver.getResources("classpath*:"
+ getClass().getPackage().getName().replace('.', '/') + "/**/*.class");
Resource[] resources = this.resolver
.getResources("classpath*:" + getClass().getPackage().getName().replace('.', '/') + "/**/*.class");
for (Resource resource : resources) {
if (!isTestClass(resource)) {
MetadataReader metadataReader = new SimpleMetadataReaderFactory()
.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader
.getAnnotationMetadata();
if (annotationMetadata.getAnnotationTypes()
.contains(Configuration.class.getName())) {
MetadataReader metadataReader = new SimpleMetadataReaderFactory().getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (annotationMetadata.getAnnotationTypes().contains(Configuration.class.getName())) {
configurationClasses.add(annotationMetadata);
}
}
@@ -82,13 +78,11 @@ public abstract class AbstractConfigurationClassTests {
}
private boolean isTestClass(Resource resource) throws IOException {
return resource.getFile().getAbsolutePath()
.contains("target" + File.separator + "test-classes");
return resource.getFile().getAbsolutePath().contains("target" + File.separator + "test-classes");
}
private boolean isPublic(MethodMetadata methodMetadata) {
int access = (Integer) new DirectFieldAccessor(methodMetadata)
.getPropertyValue("access");
int access = (Integer) new DirectFieldAccessor(methodMetadata).getPropertyValue("access");
return (access & Opcodes.ACC_PUBLIC) != 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-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.
@@ -67,8 +67,7 @@ import org.springframework.util.StringUtils;
*/
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern
.compile(".*classpath(\\d+)?\\.jar");
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(".*classpath(\\d+)?\\.jar");
public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError {
super(testClass);
@@ -88,15 +87,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
@Override
protected Object createTest() throws Exception {
ModifiedClassPathTestClass testClass = (ModifiedClassPathTestClass) getTestClass();
return testClass.doWithModifiedClassPathThreadContextClassLoader(
() -> ModifiedClassPathRunner.super.createTest());
return testClass
.doWithModifiedClassPathThreadContextClassLoader(() -> ModifiedClassPathRunner.super.createTest());
}
private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exception {
ClassLoader classLoader = this.getClass().getClassLoader();
return new ModifiedClassPathClassLoader(
processUrls(extractUrls(classLoader), testClass), classLoader.getParent(),
classLoader);
return new ModifiedClassPathClassLoader(processUrls(extractUrls(classLoader), testClass),
classLoader.getParent(), classLoader);
}
private URL[] extractUrls(ClassLoader classLoader) throws Exception {
@@ -116,8 +114,8 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
if (classLoader instanceof URLClassLoader) {
return Stream.of(((URLClassLoader) classLoader).getURLs());
}
return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath()
.split(File.pathSeparator)).map(this::toURL);
return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
.map(this::toURL);
}
private URL toURL(String entry) {
@@ -167,8 +165,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private String[] getClassPath(URL booterJar) throws Exception {
Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
return StringUtils.delimitedListToStringArray(
attributes.getValue(Attributes.Name.CLASS_PATH), " ");
return StringUtils.delimitedListToStringArray(attributes.getValue(Attributes.Name.CLASS_PATH), " ");
}
private Attributes getManifestMainAttributesFromUrl(URL url) throws Exception {
@@ -190,8 +187,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
private List<URL> getAdditionalUrls(Class<?> testClass) throws Exception {
ClassPathOverrides overrides = AnnotationUtils.findAnnotation(testClass,
ClassPathOverrides.class);
ClassPathOverrides overrides = AnnotationUtils.findAnnotation(testClass, ClassPathOverrides.class);
if (overrides == null) {
return Collections.emptyList();
}
@@ -199,26 +195,19 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
private List<URL> resolveCoordinates(String[] coordinates) throws Exception {
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils
.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class,
BasicRepositoryConnectorFactory.class);
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
RepositorySystem repositorySystem = serviceLocator
.getService(RepositorySystem.class);
RepositorySystem repositorySystem = serviceLocator.getService(RepositorySystem.class);
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepository = new LocalRepository(
System.getProperty("user.home") + "/.m2/repository");
session.setLocalRepositoryManager(
repositorySystem.newLocalRepositoryManager(session, localRepository));
CollectRequest collectRequest = new CollectRequest(null,
Arrays.asList(new RemoteRepository.Builder("central", "default",
"https://repo.maven.apache.org/maven2").build()));
LocalRepository localRepository = new LocalRepository(System.getProperty("user.home") + "/.m2/repository");
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
CollectRequest collectRequest = new CollectRequest(null, Arrays.asList(
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build()));
collectRequest.setDependencies(createDependencies(coordinates));
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
DependencyResult result = repositorySystem.resolveDependencies(session,
dependencyRequest);
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
List<URL> resolvedArtifacts = new ArrayList<>();
for (ArtifactResult artifact : result.getArtifactResults()) {
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
@@ -246,8 +235,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private ClassPathEntryFilter(Class<?> testClass) throws Exception {
this.exclusions = new ArrayList<>();
this.exclusions.add("log4j-*.jar");
ClassPathExclusions exclusions = AnnotationUtils.findAnnotation(testClass,
ClassPathExclusions.class);
ClassPathExclusions exclusions = AnnotationUtils.findAnnotation(testClass, ClassPathExclusions.class);
if (exclusions != null) {
this.exclusions.addAll(Arrays.asList(exclusions.value()));
}
@@ -275,15 +263,13 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private final ClassLoader classLoader;
ModifiedClassPathTestClass(ClassLoader classLoader, String testClassName)
throws ClassNotFoundException {
ModifiedClassPathTestClass(ClassLoader classLoader, String testClassName) throws ClassNotFoundException {
super(classLoader.loadClass(testClassName));
this.classLoader = classLoader;
}
@Override
public List<FrameworkMethod> getAnnotatedMethods(
Class<? extends Annotation> annotationClass) {
public List<FrameworkMethod> getAnnotatedMethods(Class<? extends Annotation> annotationClass) {
try {
return getAnnotatedMethods(annotationClass.getName());
}
@@ -293,28 +279,24 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
@SuppressWarnings("unchecked")
private List<FrameworkMethod> getAnnotatedMethods(String annotationClassName)
throws ClassNotFoundException {
private List<FrameworkMethod> getAnnotatedMethods(String annotationClassName) throws ClassNotFoundException {
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) this.classLoader
.loadClass(annotationClassName);
List<FrameworkMethod> methods = super.getAnnotatedMethods(annotationClass);
return wrapFrameworkMethods(methods);
}
private List<FrameworkMethod> wrapFrameworkMethods(
List<FrameworkMethod> methods) {
private List<FrameworkMethod> wrapFrameworkMethods(List<FrameworkMethod> methods) {
List<FrameworkMethod> wrapped = new ArrayList<>(methods.size());
for (FrameworkMethod frameworkMethod : methods) {
wrapped.add(new ModifiedClassPathFrameworkMethod(
frameworkMethod.getMethod()));
wrapped.add(new ModifiedClassPathFrameworkMethod(frameworkMethod.getMethod()));
}
return wrapped;
}
private <T, E extends Throwable> T doWithModifiedClassPathThreadContextClassLoader(
ModifiedClassPathTcclAction<T, E> action) throws E {
ClassLoader originalClassLoader = Thread.currentThread()
.getContextClassLoader();
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.classLoader);
try {
return action.perform();
@@ -345,11 +327,9 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
}
@Override
public Object invokeExplosively(Object target, Object... params)
throws Throwable {
public Object invokeExplosively(Object target, Object... params) throws Throwable {
return doWithModifiedClassPathThreadContextClassLoader(
() -> ModifiedClassPathFrameworkMethod.super.invokeExplosively(
target, params));
() -> ModifiedClassPathFrameworkMethod.super.invokeExplosively(target, params));
}
}
@@ -363,8 +343,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private final ClassLoader junitLoader;
ModifiedClassPathClassLoader(URL[] urls, ClassLoader parent,
ClassLoader junitLoader) {
ModifiedClassPathClassLoader(URL[] urls, ClassLoader parent, ClassLoader junitLoader) {
super(urls, parent);
this.junitLoader = junitLoader;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-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.
@@ -47,12 +47,10 @@ class Container implements TestRule {
}
@SuppressWarnings({ "unchecked", "resource" })
<T extends GenericContainer<T>> Container(String dockerImageName, int port,
Consumer<T> customizer) {
<T extends GenericContainer<T>> Container(String dockerImageName, int port, Consumer<T> customizer) {
this.port = port;
this.containerFactory = () -> {
T container = (T) new GenericContainer<>(dockerImageName)
.withExposedPorts(port);
T container = (T) new GenericContainer<>(dockerImageName).withExposedPorts(port);
if (customizer != null) {
customizer.accept(container);
}
@@ -69,8 +67,7 @@ class Container implements TestRule {
return new SkipStatement();
}
this.container = this.containerFactory.get();
return ((FailureDetectingExternalResource) this.container).apply(base,
description);
return ((FailureDetectingExternalResource) this.container).apply(base, description);
}
public int getMappedPort() {
@@ -81,8 +78,7 @@ class Container implements TestRule {
@Override
public void evaluate() {
throw new AssumptionViolatedException(
"Could not find a valid Docker environment.");
throw new AssumptionViolatedException("Could not find a valid Docker environment.");
}
}

View File

@@ -52,14 +52,12 @@ public class SkippableContainer<T> implements TestRule {
return new SkipStatement();
}
this.container = this.containerFactory.get();
return ((FailureDetectingExternalResource) this.container).apply(base,
description);
return ((FailureDetectingExternalResource) this.container).apply(base, description);
}
public T getContainer() {
if (this.container == null) {
throw new IllegalStateException(
"Container cannot be accessed prior to test invocation");
throw new IllegalStateException("Container cannot be accessed prior to test invocation");
}
return this.container;
}
@@ -68,8 +66,7 @@ public class SkippableContainer<T> implements TestRule {
@Override
public void evaluate() {
throw new AssumptionViolatedException(
"Could not find a valid Docker environment.");
throw new AssumptionViolatedException("Could not find a valid Docker environment.");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-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.
@@ -41,8 +41,8 @@ public class ExampleFilter implements Filter {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
response.getWriter().write("[");
chain.doFilter(request, response);
response.getWriter().write("]");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-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.
@@ -48,8 +48,7 @@ public class ExampleServlet extends GenericServlet {
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
String content = "Hello World";
if (this.echoRequestInfo) {
content += " scheme=" + request.getScheme();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-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.
@@ -65,36 +65,27 @@ public abstract class MockServletWebServer {
private void initialize() {
try {
this.servletContext = mock(ServletContext.class);
given(this.servletContext.addServlet(anyString(), any(Servlet.class)))
.willAnswer((invocation) -> {
RegisteredServlet registeredServlet = new RegisteredServlet(
invocation.getArgument(1));
MockServletWebServer.this.registeredServlets
.add(registeredServlet);
return registeredServlet.getRegistration();
});
given(this.servletContext.addFilter(anyString(), any(Filter.class)))
.willAnswer((invocation) -> {
RegisteredFilter registeredFilter = new RegisteredFilter(
invocation.getArgument(1));
MockServletWebServer.this.registeredFilters.add(registeredFilter);
return registeredFilter.getRegistration();
});
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willAnswer((invocation) -> {
RegisteredServlet registeredServlet = new RegisteredServlet(invocation.getArgument(1));
MockServletWebServer.this.registeredServlets.add(registeredServlet);
return registeredServlet.getRegistration();
});
given(this.servletContext.addFilter(anyString(), any(Filter.class))).willAnswer((invocation) -> {
RegisteredFilter registeredFilter = new RegisteredFilter(invocation.getArgument(1));
MockServletWebServer.this.registeredFilters.add(registeredFilter);
return registeredFilter.getRegistration();
});
final Map<String, String> initParameters = new HashMap<>();
given(this.servletContext.setInitParameter(anyString(), anyString()))
.will((invocation) -> {
initParameters.put(invocation.getArgument(0),
invocation.getArgument(1));
return null;
});
given(this.servletContext.setInitParameter(anyString(), anyString())).will((invocation) -> {
initParameters.put(invocation.getArgument(0), invocation.getArgument(1));
return null;
});
given(this.servletContext.getInitParameterNames())
.willReturn(Collections.enumeration(initParameters.keySet()));
given(this.servletContext.getInitParameter(anyString())).willAnswer(
(invocation) -> initParameters.get(invocation.getArgument(0)));
given(this.servletContext.getAttributeNames())
.willReturn(Collections.emptyEnumeration());
given(this.servletContext.getNamedDispatcher("default"))
.willReturn(mock(RequestDispatcher.class));
given(this.servletContext.getInitParameter(anyString()))
.willAnswer((invocation) -> initParameters.get(invocation.getArgument(0)));
given(this.servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
given(this.servletContext.getNamedDispatcher("default")).willReturn(mock(RequestDispatcher.class));
for (Initializer initializer : this.initializers) {
initializer.onStartup(this.servletContext);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-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.
@@ -32,8 +32,7 @@ import static org.hamcrest.CoreMatchers.isA;
@ClassPathExclusions("hibernate-validator-*.jar")
public class ModifiedClassPathRunnerExclusionsTests {
private static final String EXCLUDED_RESOURCE = "META-INF/services/"
+ "javax.validation.spi.ValidationProvider";
private static final String EXCLUDED_RESOURCE = "META-INF/services/" + "javax.validation.spi.ValidationProvider";
@Test
public void entriesAreFilteredFromTestClassClassLoader() {
@@ -42,8 +41,7 @@ public class ModifiedClassPathRunnerExclusionsTests {
@Test
public void entriesAreFilteredFromThreadContextClassLoader() {
assertThat(Thread.currentThread().getContextClassLoader()
.getResource(EXCLUDED_RESOURCE)).isNull();
assertThat(Thread.currentThread().getContextClassLoader().getResource(EXCLUDED_RESOURCE)).isNull();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-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.
@@ -35,14 +35,14 @@ public class ModifiedClassPathRunnerOverridesTests {
@Test
public void classesAreLoadedFromOverride() {
assertThat(ApplicationContext.class.getProtectionDomain().getCodeSource()
.getLocation().toString()).endsWith("spring-context-4.1.0.RELEASE.jar");
assertThat(ApplicationContext.class.getProtectionDomain().getCodeSource().getLocation().toString())
.endsWith("spring-context-4.1.0.RELEASE.jar");
}
@Test
public void classesAreLoadedFromTransitiveDependencyOfOverride() {
assertThat(StringUtils.class.getProtectionDomain().getCodeSource().getLocation()
.toString()).endsWith("spring-core-4.1.0.RELEASE.jar");
assertThat(StringUtils.class.getProtectionDomain().getCodeSource().getLocation().toString())
.endsWith("spring-core-4.1.0.RELEASE.jar");
}
}