DATACMNS-820 - Polishing.

Fixed indentation to use tabs instead of spaces. Tweaked structure of if-clauses in PropertyAccessingMethodInterceptor to reduce nesting. Made helper method static.

Restructured test cases slightly and used ExpectedException rule to verify exceptions. Moved newly introduced test methods more to the end of the file (new tests last).

Added author and copyright year extensions where necessary.

Original pull request: #155.
This commit is contained in:
Oliver Gierke
2016-02-20 20:32:53 +01:00
parent 5ab8fd2583
commit 09ab409391
4 changed files with 77 additions and 76 deletions

View File

@@ -67,20 +67,19 @@ class PropertyAccessingMethodInterceptor implements MethodInterceptor {
throw new IllegalStateException("Invoked method is not a property accessor!");
}
if (isSetterMethod(method, descriptor)) {
if (invocation.getArguments().length != 1) {
throw new IllegalStateException("Invoked setter method requires exactly one argument!");
}
if (!isSetterMethod(method, descriptor)) {
return target.getPropertyValue(descriptor.getName());
}
target.setPropertyValue(descriptor.getName(), invocation.getArguments()[0]);
return null;
}
if (invocation.getArguments().length != 1) {
throw new IllegalStateException("Invoked setter method requires exactly one argument!");
}
return target.getPropertyValue(descriptor.getName());
target.setPropertyValue(descriptor.getName(), invocation.getArguments()[0]);
return null;
}
private boolean isSetterMethod(Method method, PropertyDescriptor descriptor) {
private static boolean isSetterMethod(Method method, PropertyDescriptor descriptor) {
return method.equals(descriptor.getWriteMethod());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -36,6 +36,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @since 1.10
*/
public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory implements BeanFactoryAware {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 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,6 +32,7 @@ import org.springframework.beans.NotWritablePropertyException;
* Unit tests for {@link PropertyAccessingMethodInterceptor}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class PropertyAccessingMethodInterceptorUnitTests {
@@ -63,50 +64,6 @@ public class PropertyAccessingMethodInterceptorUnitTests {
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
}
/**
* @see DATACMNS-820
*/
@Test
public void triggersWritePropertyAccessOnTarget() throws Throwable {
Source source = new Source();
source.firstname = "Dave";
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
MethodInterceptor interceptor = new PropertyAccessingMethodInterceptor(source);
interceptor.invoke(invocation);
assertThat(source.firstname, is((Object) "Carl"));
}
/**
* @see DATACMNS-820
*/
@Test(expected = IllegalStateException.class)
public void throwsAppropriateExceptionIfTheInvocationHasNoArguments() throws Throwable {
Source source = new Source();
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
when(invocation.getArguments()).thenReturn(new Object[0]);
MethodInterceptor interceptor = new PropertyAccessingMethodInterceptor(source);
interceptor.invoke(invocation);
}
/**
* @see DATACMNS-820
*/
@Test(expected = NotWritablePropertyException.class)
public void throwsAppropriateExceptionIfThePropertyCannotWritten() throws Throwable {
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setGarbage", String.class));
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
}
/**
* @see DATAREST-221
*/
@@ -114,6 +71,7 @@ public class PropertyAccessingMethodInterceptorUnitTests {
public void forwardsObjectMethodInvocation() throws Throwable {
when(invocation.getMethod()).thenReturn(Object.class.getMethod("toString"));
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
}
@@ -124,6 +82,50 @@ public class PropertyAccessingMethodInterceptorUnitTests {
public void rejectsNonAccessorMethod() throws Throwable {
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("someGarbage"));
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
}
/**
* @see DATACMNS-820
*/
@Test
public void triggersWritePropertyAccessOnTarget() throws Throwable {
Source source = new Source();
source.firstname = "Dave";
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
new PropertyAccessingMethodInterceptor(source).invoke(invocation);
assertThat(source.firstname, is((Object) "Carl"));
}
/**
* @see DATACMNS-820
*/
@Test(expected = IllegalStateException.class)
public void throwsAppropriateExceptionIfTheInvocationHasNoArguments() throws Throwable {
Source source = new Source();
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
when(invocation.getArguments()).thenReturn(new Object[0]);
new PropertyAccessingMethodInterceptor(source).invoke(invocation);
}
/**
* @see DATACMNS-820
*/
@Test(expected = NotWritablePropertyException.class)
public void throwsAppropriateExceptionIfThePropertyCannotWritten() throws Throwable {
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setGarbage", String.class));
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
}

View File

@@ -21,7 +21,9 @@ import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.factory.annotation.Value;
@@ -34,7 +36,9 @@ import org.springframework.beans.factory.annotation.Value;
*/
public class SpelAwareProxyProjectionFactoryUnitTests {
private SpelAwareProxyProjectionFactory factory;
public @Rule ExpectedException exception = ExpectedException.none();
SpelAwareProxyProjectionFactory factory;
@Before
public void setup() {
@@ -55,6 +59,18 @@ public class SpelAwareProxyProjectionFactoryUnitTests {
assertThat(excerpt.getFullName(), is("Dave Matthews"));
}
/**
* @see DATACMNS-630
*/
@Test
public void excludesAtValueAnnotatedMethodsForInputProperties() {
List<String> properties = factory.getInputProperties(CustomerExcerpt.class);
assertThat(properties, hasSize(1));
assertThat(properties, hasItem("firstname"));
}
/**
* @see DATACMNS-820
*/
@@ -82,25 +98,8 @@ public class SpelAwareProxyProjectionFactoryUnitTests {
ProjectionWithNotWriteableProperty projection = factory.createProjection(ProjectionWithNotWriteableProperty.class,
customer);
try {
projection.setFirstName("Carl");
fail("Missing NotWritablePropertyException");
} catch (NotWritablePropertyException e) {
assertThat(e, instanceOf(NotWritablePropertyException.class));
}
}
/**
* @see DATACMNS-630
*/
@Test
public void excludesAtValueAnnotatedMethodsForInputProperties() {
List<String> properties = factory.getInputProperties(CustomerExcerpt.class);
assertThat(properties, hasSize(1));
assertThat(properties, hasItem("firstname"));
exception.expect(NotWritablePropertyException.class);
projection.setFirstName("Carl");
}
static class Customer {