Polishing
This commit is contained in:
parent
e1cce309ae
commit
efd7f9bf72
|
@ -545,7 +545,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
|||
* Return the primary Validator to apply after each binding step, if any.
|
||||
*/
|
||||
public Validator getValidator() {
|
||||
return this.validators.size() > 0 ? this.validators.get(0) : null;
|
||||
return (this.validators.size() > 0 ? this.validators.get(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.lang.reflect.Method;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.internal.runners.model.ReflectiveCallable;
|
||||
|
@ -92,8 +91,8 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
private static final Method withRulesMethod;
|
||||
|
||||
static {
|
||||
withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules", FrameworkMethod.class,
|
||||
Object.class, Statement.class);
|
||||
withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules",
|
||||
FrameworkMethod.class, Object.class, Statement.class);
|
||||
if (withRulesMethod == null) {
|
||||
throw new IllegalStateException(
|
||||
"Failed to find withRules() method: SpringJUnit4ClassRunner requires JUnit 4.9 or higher.");
|
||||
|
@ -107,12 +106,12 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
private static void ensureSpringRulesAreNotPresent(Class<?> testClass) {
|
||||
for (Field field : testClass.getFields()) {
|
||||
if (SpringClassRule.class.isAssignableFrom(field.getType())) {
|
||||
throw new IllegalStateException(String.format("Detected SpringClassRule field in test class [%s], but "
|
||||
+ "SpringClassRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName()));
|
||||
throw new IllegalStateException(String.format("Detected SpringClassRule field in test class [%s], " +
|
||||
"but SpringClassRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName()));
|
||||
}
|
||||
if (SpringMethodRule.class.isAssignableFrom(field.getType())) {
|
||||
throw new IllegalStateException(String.format("Detected SpringMethodRule field in test class [%s], "
|
||||
+ "but SpringMethodRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName()));
|
||||
throw new IllegalStateException(String.format("Detected SpringMethodRule field in test class [%s], " +
|
||||
"but SpringMethodRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +126,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
public SpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
|
||||
super(clazz);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("SpringJUnit4ClassRunner constructor called with [" + clazz + "].");
|
||||
logger.debug("SpringJUnit4ClassRunner constructor called with [" + clazz + "]");
|
||||
}
|
||||
ensureSpringRulesAreNotPresent(clazz);
|
||||
this.testContextManager = createTestContextManager(clazz);
|
||||
|
@ -272,7 +271,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
Object testInstance;
|
||||
try {
|
||||
testInstance = new ReflectiveCallable() {
|
||||
|
||||
@Override
|
||||
protected Object runReflectiveCall() throws Throwable {
|
||||
return createTest();
|
||||
|
@ -290,7 +288,6 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
statement = withRulesReflectively(frameworkMethod, testInstance, statement);
|
||||
statement = withPotentialRepeat(frameworkMethod, testInstance, statement);
|
||||
statement = withPotentialTimeout(frameworkMethod, testInstance, statement);
|
||||
|
||||
return statement;
|
||||
}
|
||||
|
||||
|
@ -309,8 +306,8 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
*/
|
||||
protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod) {
|
||||
Method method = frameworkMethod.getMethod();
|
||||
return (method.isAnnotationPresent(Ignore.class) || !ProfileValueUtils.isTestEnabledInThisEnvironment(method,
|
||||
getTestClass().getJavaClass()));
|
||||
return (method.isAnnotationPresent(Ignore.class) ||
|
||||
!ProfileValueUtils.isTestEnabledInThisEnvironment(method, getTestClass().getJavaClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -334,7 +331,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
*/
|
||||
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) {
|
||||
Test test = frameworkMethod.getAnnotation(Test.class);
|
||||
return ((test != null) && (test.expected() != Test.None.class) ? test.expected() : null);
|
||||
return (test != null && test.expected() != Test.None.class ? test.expected() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -356,11 +353,9 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
long springTimeout = getSpringTimeout(frameworkMethod);
|
||||
long junitTimeout = getJUnitTimeout(frameworkMethod);
|
||||
if (springTimeout > 0 && junitTimeout > 0) {
|
||||
String msg = String.format(
|
||||
"Test method [%s] has been configured with Spring's @Timed(millis=%s) and "
|
||||
+ "JUnit's @Test(timeout=%s) annotations, but only one declaration of "
|
||||
+ "a 'timeout' is permitted per test method.",
|
||||
frameworkMethod.getMethod(), springTimeout, junitTimeout);
|
||||
String msg = String.format("Test method [%s] has been configured with Spring's @Timed(millis=%s) and " +
|
||||
"JUnit's @Test(timeout=%s) annotations, but only one declaration of a 'timeout' is " +
|
||||
"permitted per test method.", frameworkMethod.getMethod(), springTimeout, junitTimeout);
|
||||
logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
|
@ -385,7 +380,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
*/
|
||||
protected long getJUnitTimeout(FrameworkMethod frameworkMethod) {
|
||||
Test test = frameworkMethod.getAnnotation(Test.class);
|
||||
return ((test != null) && (test.timeout() > 0) ? test.timeout() : 0);
|
||||
return (test != null && test.timeout() > 0 ? test.timeout() : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -410,7 +405,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
|
||||
Statement junitBefores = super.withBefores(frameworkMethod, testInstance, statement);
|
||||
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(),
|
||||
getTestContextManager());
|
||||
getTestContextManager());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -424,7 +419,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
|
|||
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
|
||||
Statement junitAfters = super.withAfters(frameworkMethod, testInstance, statement);
|
||||
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(),
|
||||
getTestContextManager());
|
||||
getTestContextManager());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -214,8 +214,8 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
|
|||
if (inputMessage instanceof MappingJacksonInputMessage) {
|
||||
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
|
||||
if (deserializationView != null) {
|
||||
return this.objectMapper.readerWithView(deserializationView)
|
||||
.withType(javaType).readValue(inputMessage.getBody());
|
||||
return this.objectMapper.readerWithView(deserializationView).withType(javaType).
|
||||
readValue(inputMessage.getBody());
|
||||
}
|
||||
}
|
||||
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
|
||||
|
@ -245,8 +245,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
|
|||
serializationView = container.getSerializationView();
|
||||
filters = container.getFilters();
|
||||
}
|
||||
if (jackson26Available && type != null && value != null
|
||||
&& TypeUtils.isAssignable(type, value.getClass())) {
|
||||
if (jackson26Available && type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
|
||||
javaType = getJavaType(type, null);
|
||||
}
|
||||
ObjectWriter objectWriter;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
|
@ -73,6 +73,7 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance of the HttpComponentsHttpInvokerRequestExecutor with a default
|
||||
* {@link HttpClient} that uses a default {@code org.apache.http.impl.conn.PoolingClientConnectionManager}.
|
||||
|
@ -82,20 +83,6 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
.setSocketTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS).build());
|
||||
}
|
||||
|
||||
private static HttpClient createDefaultHttpClient() {
|
||||
Registry<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", SSLConnectionSocketFactory.getSocketFactory())
|
||||
.build();
|
||||
|
||||
PoolingHttpClientConnectionManager connectionManager
|
||||
= new PoolingHttpClientConnectionManager(schemeRegistry);
|
||||
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
|
||||
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
|
||||
|
||||
return HttpClientBuilder.create().setConnectionManager(connectionManager).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the HttpComponentsClientHttpRequestFactory
|
||||
* with the given {@link HttpClient} instance.
|
||||
|
@ -110,6 +97,21 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
this.requestConfig = requestConfig;
|
||||
}
|
||||
|
||||
|
||||
private static HttpClient createDefaultHttpClient() {
|
||||
Registry<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", SSLConnectionSocketFactory.getSocketFactory())
|
||||
.build();
|
||||
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(schemeRegistry);
|
||||
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
|
||||
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
|
||||
|
||||
return HttpClientBuilder.create().setConnectionManager(connectionManager).build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the {@link HttpClient} instance to use for this request executor.
|
||||
*/
|
||||
|
@ -134,8 +136,7 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
*/
|
||||
public void setConnectTimeout(int timeout) {
|
||||
Assert.isTrue(timeout >= 0, "Timeout must be a non-negative value");
|
||||
this.requestConfig = cloneRequestConfig()
|
||||
.setConnectTimeout(timeout).build();
|
||||
this.requestConfig = cloneRequestConfig().setConnectTimeout(timeout).build();
|
||||
setLegacyConnectionTimeout(getHttpClient(), timeout);
|
||||
}
|
||||
|
||||
|
@ -156,8 +157,7 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
@SuppressWarnings("deprecation")
|
||||
private void setLegacyConnectionTimeout(HttpClient client, int timeout) {
|
||||
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
|
||||
client.getParams().setIntParameter(
|
||||
org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
|
||||
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -171,8 +171,7 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
* @see RequestConfig#getConnectionRequestTimeout()
|
||||
*/
|
||||
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
|
||||
this.requestConfig = cloneRequestConfig()
|
||||
.setConnectionRequestTimeout(connectionRequestTimeout).build();
|
||||
this.requestConfig = cloneRequestConfig().setConnectionRequestTimeout(connectionRequestTimeout).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -186,8 +185,7 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
*/
|
||||
public void setReadTimeout(int timeout) {
|
||||
Assert.isTrue(timeout >= 0, "Timeout must be a non-negative value");
|
||||
this.requestConfig = cloneRequestConfig()
|
||||
.setSocketTimeout(timeout).build();
|
||||
this.requestConfig = cloneRequestConfig().setSocketTimeout(timeout).build();
|
||||
setLegacySocketTimeout(getHttpClient(), timeout);
|
||||
}
|
||||
|
||||
|
@ -201,15 +199,15 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
|||
@SuppressWarnings("deprecation")
|
||||
private void setLegacySocketTimeout(HttpClient client, int timeout) {
|
||||
if (org.apache.http.impl.client.AbstractHttpClient.class.isInstance(client)) {
|
||||
client.getParams().setIntParameter(
|
||||
org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, timeout);
|
||||
client.getParams().setIntParameter(org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
private RequestConfig.Builder cloneRequestConfig() {
|
||||
return this.requestConfig != null ? RequestConfig.copy(this.requestConfig) : RequestConfig.custom();
|
||||
return (this.requestConfig != null ? RequestConfig.copy(this.requestConfig) : RequestConfig.custom());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute the given request through the HttpClient.
|
||||
* <p>This method implements the basic processing workflow:
|
||||
|
|
|
@ -177,7 +177,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
|
|||
HttpServletResponse response = getResponse();
|
||||
if (lastModifiedTimestamp >= 0 && !this.notModified) {
|
||||
if (response == null || HttpStatus.valueOf(response.getStatus()).is2xxSuccessful()) {
|
||||
this.notModified = isTimeStampNotModified(lastModifiedTimestamp);
|
||||
this.notModified = isTimestampNotModified(lastModifiedTimestamp);
|
||||
if (response != null) {
|
||||
if (this.notModified && supportsNotModifiedStatus()) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||
|
@ -192,7 +192,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
|
|||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private boolean isTimeStampNotModified(long lastModifiedTimestamp) {
|
||||
private boolean isTimestampNotModified(long lastModifiedTimestamp) {
|
||||
long ifModifiedSince = -1;
|
||||
try {
|
||||
ifModifiedSince = getRequest().getDateHeader(HEADER_IF_MODIFIED_SINCE);
|
||||
|
@ -270,7 +270,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
|
|||
if (StringUtils.hasLength(etag) && !this.notModified) {
|
||||
if (response == null || HttpStatus.valueOf(response.getStatus()).is2xxSuccessful()) {
|
||||
etag = addEtagPadding(etag);
|
||||
this.notModified = isETagNotModified(etag) && isTimeStampNotModified(lastModifiedTimestamp);
|
||||
this.notModified = isETagNotModified(etag) && isTimestampNotModified(lastModifiedTimestamp);
|
||||
if (response != null) {
|
||||
if (this.notModified && supportsNotModifiedStatus()) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||
|
|
Loading…
Reference in New Issue