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 e868577def
commit d3e6ad104c
4 changed files with 88 additions and 87 deletions

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,29 @@ 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-89
*/
@Test
public void considersProjectionUsingAtValueNotClosed() {
ProjectionInformation information = factory.getProjectionInformation(CustomerExcerpt.class);
assertThat(information.isClosed(), is(false));
}
/**
* @see DATACMNS-820
*/
@@ -82,36 +109,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"));
}
/**
* @see DATACMNS-89
*/
@Test
public void considersProjectionUsingAtValueNotClosed() {
ProjectionInformation information = factory.getProjectionInformation(CustomerExcerpt.class);
assertThat(information.isClosed(), is(false));
exception.expect(NotWritablePropertyException.class);
projection.setFirstName("Carl");
}
static class Customer {