Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -44,6 +44,7 @@ public class EmbeddedDatabaseBuilder {
|
||||
|
||||
private final ResourceLoader resourceLoader;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new embedded database builder.
|
||||
*/
|
||||
@@ -62,6 +63,7 @@ public class EmbeddedDatabaseBuilder {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the name of the embedded database.
|
||||
* <p>Defaults to "testdb" if not called.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -30,15 +30,13 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Encapsulates information about a bean method consisting of a
|
||||
* {@linkplain #getMethod() method} and a {@linkplain #getBean() bean}. Provides
|
||||
* convenient access to method parameters, the method return value, method
|
||||
* annotations.
|
||||
* Encapsulates information about a bean method consisting of a {@link #getMethod() method}
|
||||
* and a {@link #getBean() bean}. Provides convenient access to method parameters,
|
||||
* method return value, method annotations.
|
||||
*
|
||||
* <p>The class may be created with a bean instance or with a bean name (e.g. lazy
|
||||
* bean, prototype bean). Use {@link #createWithResolvedBean()} to obtain an
|
||||
* {@link HandlerMethod} instance with a bean instance initialized through the
|
||||
* bean factory.
|
||||
* <p>The class may be created with a bean instance or with a bean name (e.g. lazy bean,
|
||||
* prototype bean). Use {@link #createWithResolvedBean()} to obtain an {@link HandlerMethod}
|
||||
* instance with a bean instance initialized through the bean factory.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -51,21 +49,21 @@ public class HandlerMethod {
|
||||
|
||||
private final Object bean;
|
||||
|
||||
private final Method method;
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private final MethodParameter[] parameters;
|
||||
private final Method method;
|
||||
|
||||
private final Method bridgedMethod;
|
||||
|
||||
private final MethodParameter[] parameters;
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance from a bean instance and a method.
|
||||
*/
|
||||
public HandlerMethod(Object bean, Method method) {
|
||||
Assert.notNull(bean, "bean must not be null");
|
||||
Assert.notNull(method, "method must not be null");
|
||||
Assert.notNull(bean, "Bean is required");
|
||||
Assert.notNull(method, "Method is required");
|
||||
this.bean = bean;
|
||||
this.beanFactory = null;
|
||||
this.method = method;
|
||||
@@ -78,12 +76,12 @@ public class HandlerMethod {
|
||||
* @throws NoSuchMethodException when the method cannot be found
|
||||
*/
|
||||
public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
|
||||
Assert.notNull(bean, "bean must not be null");
|
||||
Assert.notNull(methodName, "method must not be null");
|
||||
Assert.notNull(bean, "Bean is required");
|
||||
Assert.notNull(methodName, "Method name is required");
|
||||
this.bean = bean;
|
||||
this.beanFactory = null;
|
||||
this.method = bean.getClass().getMethod(methodName, parameterTypes);
|
||||
this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
|
||||
this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method);
|
||||
this.parameters = initMethodParameters();
|
||||
}
|
||||
|
||||
@@ -93,11 +91,11 @@ public class HandlerMethod {
|
||||
* re-create the {@code HandlerMethod} with an initialized the bean.
|
||||
*/
|
||||
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
|
||||
Assert.hasText(beanName, "beanName must not be null");
|
||||
Assert.notNull(beanFactory, "beanFactory must not be null");
|
||||
Assert.notNull(method, "method must not be null");
|
||||
Assert.hasText(beanName, "Bean name is required");
|
||||
Assert.notNull(beanFactory, "BeanFactory is required");
|
||||
Assert.notNull(method, "Method is required");
|
||||
Assert.isTrue(beanFactory.containsBean(beanName),
|
||||
"Bean factory [" + beanFactory + "] does not contain bean [" + beanName + "]");
|
||||
"BeanFactory [" + beanFactory + "] does not contain bean [" + beanName + "]");
|
||||
this.bean = beanName;
|
||||
this.beanFactory = beanFactory;
|
||||
this.method = method;
|
||||
@@ -109,7 +107,7 @@ public class HandlerMethod {
|
||||
* Copy constructor for use in sub-classes.
|
||||
*/
|
||||
protected HandlerMethod(HandlerMethod handlerMethod) {
|
||||
Assert.notNull(handlerMethod, "HandlerMethod must not be null");
|
||||
Assert.notNull(handlerMethod, "HandlerMethod is required");
|
||||
this.bean = handlerMethod.bean;
|
||||
this.beanFactory = handlerMethod.beanFactory;
|
||||
this.method = handlerMethod.method;
|
||||
@@ -121,8 +119,8 @@ public class HandlerMethod {
|
||||
* Re-create HandlerMethod with the resolved handler.
|
||||
*/
|
||||
private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
|
||||
Assert.notNull(handlerMethod, "handlerMethod must not be null");
|
||||
Assert.notNull(handler, "handler must not be null");
|
||||
Assert.notNull(handlerMethod, "HandlerMethod is required");
|
||||
Assert.notNull(handler, "Handler object is required");
|
||||
this.bean = handler;
|
||||
this.beanFactory = handlerMethod.beanFactory;
|
||||
this.method = handlerMethod.method;
|
||||
|
||||
@@ -316,7 +316,6 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
|
||||
}
|
||||
|
||||
private void registerMvcSingletons(StubWebApplicationContext wac) {
|
||||
|
||||
StandaloneConfiguration config = new StandaloneConfiguration();
|
||||
|
||||
StaticRequestMappingHandlerMapping hm = config.getHandlerMapping();
|
||||
@@ -343,16 +342,13 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
|
||||
}
|
||||
|
||||
private List<ViewResolver> initViewResolvers(WebApplicationContext wac) {
|
||||
|
||||
this.viewResolvers = (this.viewResolvers == null) ?
|
||||
Arrays.<ViewResolver>asList(new InternalResourceViewResolver()) : this.viewResolvers;
|
||||
|
||||
for (Object viewResolver : this.viewResolvers) {
|
||||
if (viewResolver instanceof WebApplicationObjectSupport) {
|
||||
((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac);
|
||||
}
|
||||
}
|
||||
|
||||
return this.viewResolvers;
|
||||
}
|
||||
|
||||
@@ -360,7 +356,6 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
|
||||
/** Using the MVC Java configuration as the starting point for the "standalone" setup */
|
||||
private class StandaloneConfiguration extends WebMvcConfigurationSupport {
|
||||
|
||||
|
||||
public StaticRequestMappingHandlerMapping getHandlerMapping() {
|
||||
StaticRequestMappingHandlerMapping handlerMapping = new StaticRequestMappingHandlerMapping();
|
||||
handlerMapping.setEmbeddedValueResolver(new StaticStringValueResolver(placeHolderValues));
|
||||
@@ -368,7 +363,6 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
|
||||
handlerMapping.setUseTrailingSlashMatch(useTrailingSlashPatternMatch);
|
||||
handlerMapping.setOrder(0);
|
||||
handlerMapping.setInterceptors(getInterceptors());
|
||||
|
||||
if (removeSemicolonContent != null) {
|
||||
handlerMapping.setRemoveSemicolonContent(removeSemicolonContent);
|
||||
}
|
||||
@@ -437,24 +431,29 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
|
||||
}
|
||||
}
|
||||
|
||||
/** A {@code RequestMappingHandlerMapping} that allows registration of controllers */
|
||||
|
||||
/**
|
||||
* A {@code RequestMappingHandlerMapping} that allows registration of controllers.
|
||||
*/
|
||||
private static class StaticRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
|
||||
|
||||
public void registerHandlers(Object...handlers) {
|
||||
for (Object handler : handlers) {
|
||||
super.detectHandlerMethods(handler);
|
||||
detectHandlerMethods(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A static resolver placeholder for values embedded in request mappings */
|
||||
|
||||
/**
|
||||
* A static resolver placeholder for values embedded in request mappings.
|
||||
*/
|
||||
private static class StaticStringValueResolver implements StringValueResolver {
|
||||
|
||||
private final PropertyPlaceholderHelper helper;
|
||||
|
||||
private final PlaceholderResolver resolver;
|
||||
|
||||
|
||||
public StaticStringValueResolver(final Map<String, String> values) {
|
||||
this.helper = new PropertyPlaceholderHelper("${", "}", ":", false);
|
||||
this.resolver = new PlaceholderResolver() {
|
||||
@@ -471,7 +470,10 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
|
||||
}
|
||||
}
|
||||
|
||||
/** A {@link ViewResolver} that always returns same View */
|
||||
|
||||
/**
|
||||
* A {@link ViewResolver} that always returns same View.
|
||||
*/
|
||||
private static class StaticViewResolver implements ViewResolver {
|
||||
|
||||
private final View view;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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,7 +32,7 @@ import org.springframework.core.OrderComparator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Central helper that manages resources and transaction synchronizations per thread.
|
||||
* Central delegate that manages resources and transaction synchronizations per thread.
|
||||
* To be used by resource management code but not by typical application code.
|
||||
*
|
||||
* <p>Supports one resource per key without overwriting, that is, a resource needs
|
||||
@@ -284,7 +284,7 @@ public abstract class TransactionSynchronizationManager {
|
||||
* @see org.springframework.core.Ordered
|
||||
*/
|
||||
public static void registerSynchronization(TransactionSynchronization synchronization)
|
||||
throws IllegalStateException {
|
||||
throws IllegalStateException {
|
||||
|
||||
Assert.notNull(synchronization, "TransactionSynchronization must not be null");
|
||||
if (!isSynchronizationActive()) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -30,15 +30,13 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Encapsulates information about a bean method consisting of a
|
||||
* {@linkplain #getMethod() method} and a {@linkplain #getBean() bean}. Provides
|
||||
* convenient access to method parameters, the method return value, method
|
||||
* annotations.
|
||||
* Encapsulates information about a bean method consisting of a {@link #getMethod() method}
|
||||
* and a {@link #getBean() bean}. Provides convenient access to method parameters,
|
||||
* method return value, method annotations.
|
||||
*
|
||||
* <p>The class may be created with a bean instance or with a bean name (e.g. lazy
|
||||
* bean, prototype bean). Use {@link #createWithResolvedBean()} to obtain an
|
||||
* {@link HandlerMethod} instance with a bean instance initialized through the
|
||||
* bean factory.
|
||||
* <p>The class may be created with a bean instance or with a bean name (e.g. lazy bean,
|
||||
* prototype bean). Use {@link #createWithResolvedBean()} to obtain an {@link HandlerMethod}
|
||||
* instance with a bean instance initialized through the bean factory.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -51,21 +49,21 @@ public class HandlerMethod {
|
||||
|
||||
private final Object bean;
|
||||
|
||||
private final Method method;
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
private final MethodParameter[] parameters;
|
||||
private final Method method;
|
||||
|
||||
private final Method bridgedMethod;
|
||||
|
||||
private final MethodParameter[] parameters;
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance from a bean instance and a method.
|
||||
*/
|
||||
public HandlerMethod(Object bean, Method method) {
|
||||
Assert.notNull(bean, "bean is required");
|
||||
Assert.notNull(method, "method is required");
|
||||
Assert.notNull(bean, "Bean is required");
|
||||
Assert.notNull(method, "Method is required");
|
||||
this.bean = bean;
|
||||
this.beanFactory = null;
|
||||
this.method = method;
|
||||
@@ -73,26 +71,17 @@ public class HandlerMethod {
|
||||
this.parameters = initMethodParameters();
|
||||
}
|
||||
|
||||
private MethodParameter[] initMethodParameters() {
|
||||
int count = this.bridgedMethod.getParameterTypes().length;
|
||||
MethodParameter[] result = new MethodParameter[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
result[i] = new HandlerMethodParameter(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance from a bean instance, method name, and parameter types.
|
||||
* @throws NoSuchMethodException when the method cannot be found
|
||||
*/
|
||||
public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
|
||||
Assert.notNull(bean, "bean is required");
|
||||
Assert.notNull(methodName, "method is required");
|
||||
Assert.notNull(bean, "Bean is required");
|
||||
Assert.notNull(methodName, "Method name is required");
|
||||
this.bean = bean;
|
||||
this.beanFactory = null;
|
||||
this.method = bean.getClass().getMethod(methodName, parameterTypes);
|
||||
this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
|
||||
this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method);
|
||||
this.parameters = initMethodParameters();
|
||||
}
|
||||
|
||||
@@ -102,11 +91,11 @@ public class HandlerMethod {
|
||||
* re-create the {@code HandlerMethod} with an initialized the bean.
|
||||
*/
|
||||
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
|
||||
Assert.hasText(beanName, "beanName is required");
|
||||
Assert.notNull(beanFactory, "beanFactory is required");
|
||||
Assert.notNull(method, "method is required");
|
||||
Assert.hasText(beanName, "Bean name is required");
|
||||
Assert.notNull(beanFactory, "BeanFactory is required");
|
||||
Assert.notNull(method, "Method is required");
|
||||
Assert.isTrue(beanFactory.containsBean(beanName),
|
||||
"Bean factory [" + beanFactory + "] does not contain bean [" + beanName + "]");
|
||||
"BeanFactory [" + beanFactory + "] does not contain bean [" + beanName + "]");
|
||||
this.bean = beanName;
|
||||
this.beanFactory = beanFactory;
|
||||
this.method = method;
|
||||
@@ -130,8 +119,8 @@ public class HandlerMethod {
|
||||
* Re-create HandlerMethod with the resolved handler.
|
||||
*/
|
||||
private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
|
||||
Assert.notNull(handlerMethod, "handlerMethod is required");
|
||||
Assert.notNull(handler, "handler is required");
|
||||
Assert.notNull(handlerMethod, "HandlerMethod is required");
|
||||
Assert.notNull(handler, "Handler object is required");
|
||||
this.bean = handler;
|
||||
this.beanFactory = handlerMethod.beanFactory;
|
||||
this.method = handlerMethod.method;
|
||||
@@ -139,6 +128,16 @@ public class HandlerMethod {
|
||||
this.parameters = handlerMethod.parameters;
|
||||
}
|
||||
|
||||
|
||||
private MethodParameter[] initMethodParameters() {
|
||||
int count = this.bridgedMethod.getParameterTypes().length;
|
||||
MethodParameter[] result = new MethodParameter[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
result[i] = new HandlerMethodParameter(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bean for this handler method.
|
||||
*/
|
||||
@@ -280,7 +279,7 @@ public class HandlerMethod {
|
||||
|
||||
@Override
|
||||
public Class<?> getParameterType() {
|
||||
return (this.returnValue != null) ? this.returnValue.getClass() : super.getParameterType();
|
||||
return (this.returnValue != null ? this.returnValue.getClass() : super.getParameterType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -94,7 +94,8 @@ public class InvocableHandlerMethodTests {
|
||||
try {
|
||||
handlerMethod.invokeForRequest(webRequest, null);
|
||||
fail("Expected exception");
|
||||
} catch (IllegalStateException ex) {
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertTrue(ex.getMessage().contains("No suitable resolver for argument [0] [type=java.lang.Integer]"));
|
||||
}
|
||||
}
|
||||
@@ -131,7 +132,8 @@ public class InvocableHandlerMethodTests {
|
||||
try {
|
||||
handlerMethod.invokeForRequest(webRequest, null);
|
||||
fail("Expected exception");
|
||||
} catch (HttpMessageNotReadableException ex) {
|
||||
}
|
||||
catch (HttpMessageNotReadableException ex) {
|
||||
// Expected..
|
||||
// Allow HandlerMethodArgumentResolver exceptions to propagate..
|
||||
}
|
||||
@@ -150,7 +152,8 @@ public class InvocableHandlerMethodTests {
|
||||
try {
|
||||
handlerMethod.invokeForRequest(webRequest, null);
|
||||
fail("Expected exception");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertNotNull("Exception not wrapped", ex.getCause());
|
||||
assertTrue(ex.getCause() instanceof IllegalArgumentException);
|
||||
assertTrue(ex.getMessage().contains("Controller ["));
|
||||
@@ -166,28 +169,32 @@ public class InvocableHandlerMethodTests {
|
||||
Throwable expected = new RuntimeException("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (RuntimeException actual) {
|
||||
}
|
||||
catch (RuntimeException actual) {
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
expected = new Error("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (Error actual) {
|
||||
}
|
||||
catch (Error actual) {
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
expected = new Exception("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (Exception actual) {
|
||||
}
|
||||
catch (Exception actual) {
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
expected = new Throwable("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (IllegalStateException actual) {
|
||||
}
|
||||
catch (IllegalStateException actual) {
|
||||
assertNotNull(actual.getCause());
|
||||
assertSame(expected, actual.getCause());
|
||||
assertTrue(actual.getMessage().contains("Failed to invoke controller method"));
|
||||
|
||||
Reference in New Issue
Block a user