Polishing

(cherry picked from commit 667fc7e)
This commit is contained in:
Juergen Hoeller
2015-09-08 14:48:05 +02:00
parent af5f4e6fb4
commit 38db9fa855
11 changed files with 73 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ public class AspectJProxyFactory extends ProxyCreatorSupport {
* Create a new {@code AspectJProxyFactory}. * Create a new {@code AspectJProxyFactory}.
* No target, only interfaces. Must add interceptors. * No target, only interfaces. Must add interceptors.
*/ */
public AspectJProxyFactory(Class<?>[] interfaces) { public AspectJProxyFactory(Class<?>... interfaces) {
setInterfaces(interfaces); setInterfaces(interfaces);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,9 +32,9 @@ import static org.junit.Assert.*;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public final class AspectProxyFactoryTests { public class AspectProxyFactoryTests {
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testWithNonAspect() { public void testWithNonAspect() {
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean()); AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
proxyFactory.addAspect(TestBean.class); proxyFactory.addAspect(TestBean.class);
@@ -70,7 +70,7 @@ public final class AspectProxyFactoryTests {
assertEquals(2, proxy1.getAge()); assertEquals(2, proxy1.getAge());
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testWithInstanceWithNonAspect() throws Exception { public void testWithInstanceWithNonAspect() throws Exception {
AspectJProxyFactory pf = new AspectJProxyFactory(); AspectJProxyFactory pf = new AspectJProxyFactory();
pf.addAspect(new TestBean()); pf.addAspect(new TestBean());
@@ -96,14 +96,14 @@ public final class AspectProxyFactoryTests {
assertEquals(target.getAge() * multiple, serializedProxy.getAge()); assertEquals(target.getAge() * multiple, serializedProxy.getAge());
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testWithNonSingletonAspectInstance() throws Exception { public void testWithNonSingletonAspectInstance() throws Exception {
AspectJProxyFactory pf = new AspectJProxyFactory(); AspectJProxyFactory pf = new AspectJProxyFactory();
pf.addAspect(new PerThisAspect()); pf.addAspect(new PerThisAspect());
} }
public static interface ITestBean { public interface ITestBean {
int getAge(); int getAge();
} }

View File

@@ -393,17 +393,14 @@ public abstract class YamlProcessor {
*/ */
protected static class StrictMapAppenderConstructor extends Constructor { protected static class StrictMapAppenderConstructor extends Constructor {
public StrictMapAppenderConstructor() {
super();
}
@Override @Override
protected Map<Object, Object> constructMapping(MappingNode node) { protected Map<Object, Object> constructMapping(MappingNode node) {
try { try {
return super.constructMapping(node); return super.constructMapping(node);
} catch (IllegalStateException e) { }
catch (IllegalStateException ex) {
throw new ParserException("while parsing MappingNode", throw new ParserException("while parsing MappingNode",
node.getStartMark(), e.getMessage(), node.getEndMark()); node.getStartMark(), ex.getMessage(), node.getEndMark());
} }
} }
@@ -414,7 +411,7 @@ public abstract class YamlProcessor {
@Override @Override
public Object put(Object key, Object value) { public Object put(Object key, Object value) {
if (delegate.containsKey(key)) { if (delegate.containsKey(key)) {
throw new IllegalStateException("duplicate key: " + key); throw new IllegalStateException("Duplicate key: " + key);
} }
return delegate.put(key, value); return delegate.put(key, value);
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ import static org.junit.Assert.*;
* @author Chris Beams * @author Chris Beams
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public final class CglibProxyTests extends AbstractAopProxyTests implements Serializable { public class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
private static final String DEPENDENCY_CHECK_CONTEXT = private static final String DEPENDENCY_CHECK_CONTEXT =
CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml"; CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml";
@@ -74,6 +74,7 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
return true; return true;
} }
@Test @Test
public void testNullConfig() { public void testNullConfig() {
try { try {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.io.Serializable;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
@@ -28,7 +29,6 @@ import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean; import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/** /**
* @since 13.03.2003 * @since 13.03.2003
@@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.*;
* @author Chris Beams * @author Chris Beams
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable { public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
@Override @Override
protected Object createProxy(ProxyCreatorSupport as) { protected Object createProxy(ProxyCreatorSupport as) {
@@ -52,6 +52,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
return new JdkDynamicAopProxy(as); return new JdkDynamicAopProxy(as);
} }
@Test
public void testNullConfig() { public void testNullConfig() {
try { try {
new JdkDynamicAopProxy(null); new JdkDynamicAopProxy(null);
@@ -62,6 +64,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
} }
} }
@Test
public void testProxyIsJustInterface() throws Throwable { public void testProxyIsJustInterface() throws Throwable {
TestBean raw = new TestBean(); TestBean raw = new TestBean();
raw.setAge(32); raw.setAge(32);
@@ -74,32 +77,32 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
assertTrue(!(proxy instanceof TestBean)); assertTrue(!(proxy instanceof TestBean));
} }
@Test
public void testInterceptorIsInvokedWithNoTarget() throws Throwable { public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
// Test return value // Test return value
int age = 25; final Integer age = 25;
MethodInterceptor mi = mock(MethodInterceptor.class); MethodInterceptor mi = (invocation -> age);
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class }); AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
pc.addAdvice(mi); pc.addAdvice(mi);
AopProxy aop = createAopProxy(pc); AopProxy aop = createAopProxy(pc);
given(mi.invoke(null)).willReturn(age);
ITestBean tb = (ITestBean) aop.getProxy(); ITestBean tb = (ITestBean) aop.getProxy();
assertTrue("correct return value", tb.getAge() == age); assertTrue("correct return value", tb.getAge() == age);
} }
@Test
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable { public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() { final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
@Override @Override
protected void assertions(MethodInvocation invocation) { protected void assertions(MethodInvocation invocation) {
assertTrue(invocation.getThis() == this); assertTrue(invocation.getThis() == this);
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
invocation.getMethod().getDeclaringClass() == ITestBean.class); invocation.getMethod().getDeclaringClass() == ITestBean.class);
} }
}; };
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class, IOther.class }); AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class});
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
TrapTargetInterceptor tii = new TrapTargetInterceptor() { TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override @Override
@@ -126,10 +129,11 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
//assertTrue(target.invocation == tii.invocation); //assertTrue(target.invocation == tii.invocation);
} }
@Test
public void testProxyNotWrappedIfIncompatible() { public void testProxyNotWrappedIfIncompatible() {
FooBar bean = new FooBar(); FooBar bean = new FooBar();
ProxyCreatorSupport as = new ProxyCreatorSupport(); ProxyCreatorSupport as = new ProxyCreatorSupport();
as.setInterfaces(new Class<?>[] {Foo.class}); as.setInterfaces(Foo.class);
as.setTarget(bean); as.setTarget(bean);
Foo proxy = (Foo) createProxy(as); Foo proxy = (Foo) createProxy(as);
@@ -138,6 +142,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
} }
@Test
public void testEqualsAndHashCodeDefined() throws Exception { public void testEqualsAndHashCodeDefined() throws Exception {
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class}); AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class});
as.setTarget(new Person()); as.setTarget(new Person());
@@ -149,7 +154,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
} }
public static interface Foo { public interface Foo {
Bar getBarThis(); Bar getBarThis();
@@ -157,8 +162,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
} }
public static interface Bar { public interface Bar {
} }
@@ -176,7 +180,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
} }
public static interface Named { public interface Named {
String getName(); String getName();
@@ -201,11 +205,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
final Person person = (Person) o;
if (!name.equals(person.name)) return false; if (!name.equals(person.name)) return false;
return true; return true;
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -35,11 +35,11 @@ import org.springframework.util.ReflectionUtils;
public class ReflectiveMethodExecutor implements MethodExecutor { public class ReflectiveMethodExecutor implements MethodExecutor {
private final Method method; private final Method method;
private final Integer varargsPosition; private final Integer varargsPosition;
private boolean computedPublicDeclaringClass = false; private boolean computedPublicDeclaringClass = false;
private Class<?> publicDeclaringClass; private Class<?> publicDeclaringClass;
private boolean argumentConversionOccurred = false; private boolean argumentConversionOccurred = false;
@@ -58,10 +58,10 @@ public class ReflectiveMethodExecutor implements MethodExecutor {
public Method getMethod() { public Method getMethod() {
return this.method; return this.method;
} }
/** /**
* Find the first public class in the methods declaring class hierarchy that declares this method. * Find the first public class in the methods declaring class hierarchy that declares this method.
* Sometimes the reflective method discovery logic finds a suitable method that can easily be * Sometimes the reflective method discovery logic finds a suitable method that can easily be
* called via reflection but cannot be called from generated code when compiling the expression * called via reflection but cannot be called from generated code when compiling the expression
* because of visibility restrictions. For example if a non public class overrides toString(), this * because of visibility restrictions. For example if a non public class overrides toString(), this
* helper method will walk up the type hierarchy to find the first public type that declares the * helper method will walk up the type hierarchy to find the first public type that declares the
@@ -80,20 +80,21 @@ public class ReflectiveMethodExecutor implements MethodExecutor {
try { try {
clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
return clazz; return clazz;
} catch (NoSuchMethodException nsme) { }
catch (NoSuchMethodException ex) {
// Continue below...
} }
} }
Class<?>[] intfaces = clazz.getInterfaces(); Class<?>[] ifcs = clazz.getInterfaces();
for (Class<?> intface: intfaces) { for (Class<?> ifc: ifcs) {
discoverPublicClass(method, intface); discoverPublicClass(method, ifc);
} }
if (clazz.getSuperclass() != null) { if (clazz.getSuperclass() != null) {
return discoverPublicClass(method, clazz.getSuperclass()); return discoverPublicClass(method, clazz.getSuperclass());
} }
return null; return null;
} }
public boolean didArgumentConversionOccur() { public boolean didArgumentConversionOccur() {
return this.argumentConversionOccurred; return this.argumentConversionOccurred;
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -58,9 +58,8 @@ public class StompDecoder {
/** /**
* Configure a * Configure a {@link MessageHeaderInitializer} to apply to the headers of
* {@link org.springframework.messaging.support.MessageHeaderInitializer MessageHeaderInitializer} * {@link Message}s from decoded STOMP frames.
* to apply to the headers of {@link Message}s from decoded STOMP frames.
*/ */
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) { public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) {
this.headerInitializer = headerInitializer; this.headerInitializer = headerInitializer;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -38,22 +38,21 @@ public class ResponseBodyTests {
@Test @Test
public void json() throws Exception { public void json() throws Exception {
standaloneSetup(new PersonController()).build() standaloneSetup(new PersonController()).build()
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON)) .perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.name").value("Lee")); .andExpect(jsonPath("$.name").value("Lee"));
} }
@Controller @Controller
private class PersonController { private class PersonController {
@RequestMapping(value="/person/{name}") @RequestMapping(value="/person/{name}")
@ResponseBody @ResponseBody
public Person get(@PathVariable String name) { public Person get(@PathVariable String name) {
Person person = new Person(name); return new Person(name);
return person;
} }
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -234,7 +234,7 @@ public class ContentNegotiationManagerFactoryBean
strategies.add(new HeaderContentNegotiationStrategy()); strategies.add(new HeaderContentNegotiationStrategy());
} }
if(this.defaultNegotiationStrategy != null) { if (this.defaultNegotiationStrategy != null) {
strategies.add(defaultNegotiationStrategy); strategies.add(defaultNegotiationStrategy);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@ import java.util.List;
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.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.http.converter.GenericHttpMessageConverter;
@@ -31,12 +30,12 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Response extractor that uses the given {@linkplain HttpMessageConverter entity * Response extractor that uses the given {@linkplain HttpMessageConverter entity converters}
* converters} to convert the response into a type {@code T}. * to convert the response into a type {@code T}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @see RestTemplate
* @since 3.0 * @since 3.0
* @see RestTemplate
*/ */
public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> { public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
@@ -48,19 +47,18 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
private final Log logger; private final Log logger;
/** /**
* Creates a new instance of the {@code HttpMessageConverterExtractor} with the given * Create a new instance of the {@code HttpMessageConverterExtractor} with the given response
* response type and message converters. The given converters must support the response * type and message converters. The given converters must support the response type.
* type.
*/ */
public HttpMessageConverterExtractor(Class<T> responseType, List<HttpMessageConverter<?>> messageConverters) { public HttpMessageConverterExtractor(Class<T> responseType, List<HttpMessageConverter<?>> messageConverters) {
this((Type) responseType, messageConverters); this((Type) responseType, messageConverters);
} }
/** /**
* Creates a new instance of the {@code HttpMessageConverterExtractor} with the given * Creates a new instance of the {@code HttpMessageConverterExtractor} with the given response
* response type and message converters. The given converters must support the response * type and message converters. The given converters must support the response type.
* type.
*/ */
public HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverter<?>> messageConverters) { public HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverter<?>> messageConverters) {
this(responseType, messageConverters, LogFactory.getLog(HttpMessageConverterExtractor.class)); this(responseType, messageConverters, LogFactory.getLog(HttpMessageConverterExtractor.class));
@@ -76,12 +74,12 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
this.logger = logger; this.logger = logger;
} }
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public T extractData(ClientHttpResponse response) throws IOException {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public T extractData(ClientHttpResponse response) throws IOException {
MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response); MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
if(!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) { if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
return null; return null;
} }
MediaType contentType = getContentType(responseWrapper); MediaType contentType = getContentType(responseWrapper);
@@ -107,9 +105,9 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
} }
} }
} }
throw new RestClientException(
"Could not extract response: no suitable HttpMessageConverter found for response type [" + throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " +
this.responseType + "] and content type [" + contentType + "]"); "for response type [" + this.responseType + "] and content type [" + contentType + "]");
} }
private MediaType getContentType(ClientHttpResponse response) { private MediaType getContentType(ClientHttpResponse response) {

View File

@@ -61,7 +61,7 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
responseStatus == HttpStatus.NOT_MODIFIED) { responseStatus == HttpStatus.NOT_MODIFIED) {
return false; return false;
} }
else if(this.getHeaders().getContentLength() == 0) { else if (this.getHeaders().getContentLength() == 0) {
return false; return false;
} }
return true; return true;