Skip to content

Commit d3e6ad1

Browse files
committed
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.
1 parent e868577 commit d3e6ad1

File tree

4 files changed

+86
-85
lines changed

4 files changed

+86
-85
lines changed

src/main/java/org/springframework/data/projection/PropertyAccessingMethodInterceptor.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,19 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
6767
throw new IllegalStateException("Invoked method is not a property accessor!");
6868
}
6969

70-
if (isSetterMethod(method, descriptor)) {
71-
if (invocation.getArguments().length != 1) {
72-
throw new IllegalStateException("Invoked setter method requires exactly one argument!");
73-
}
70+
if (!isSetterMethod(method, descriptor)) {
71+
return target.getPropertyValue(descriptor.getName());
72+
}
7473

75-
target.setPropertyValue(descriptor.getName(), invocation.getArguments()[0]);
76-
return null;
77-
}
74+
if (invocation.getArguments().length != 1) {
75+
throw new IllegalStateException("Invoked setter method requires exactly one argument!");
76+
}
7877

79-
return target.getPropertyValue(descriptor.getName());
78+
target.setPropertyValue(descriptor.getName(), invocation.getArguments()[0]);
79+
return null;
8080
}
8181

82-
private boolean isSetterMethod(Method method, PropertyDescriptor descriptor) {
82+
private static boolean isSetterMethod(Method method, PropertyDescriptor descriptor) {
8383
return method.equals(descriptor.getWriteMethod());
8484
}
85-
8685
}

src/main/java/org/springframework/data/projection/SpelAwareProxyProjectionFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Oliver Gierke
3838
* @author Thomas Darimont
39+
* @author Mark Paluch
3940
* @since 1.10
4041
*/
4142
public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory implements BeanFactoryAware {

src/test/java/org/springframework/data/projection/PropertyAccessingMethodInterceptorUnitTests.java

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* Unit tests for {@link PropertyAccessingMethodInterceptor}.
3333
*
3434
* @author Oliver Gierke
35+
* @author Mark Paluch
3536
*/
3637
@RunWith(MockitoJUnitRunner.class)
3738
public class PropertyAccessingMethodInterceptorUnitTests {
@@ -63,67 +64,68 @@ public void throwsAppropriateExceptionIfThePropertyCannotBeFound() throws Throwa
6364
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
6465
}
6566

66-
/**
67-
* @see DATACMNS-820
68-
*/
69-
@Test
70-
public void triggersWritePropertyAccessOnTarget() throws Throwable {
67+
/**
68+
* @see DATAREST-221
69+
*/
70+
@Test
71+
public void forwardsObjectMethodInvocation() throws Throwable {
7172

72-
Source source = new Source();
73-
source.firstname = "Dave";
73+
when(invocation.getMethod()).thenReturn(Object.class.getMethod("toString"));
7474

75-
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
76-
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
77-
MethodInterceptor interceptor = new PropertyAccessingMethodInterceptor(source);
75+
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
76+
}
7877

79-
interceptor.invoke(invocation);
78+
/**
79+
* @see DATACMNS-630
80+
*/
81+
@Test(expected = IllegalStateException.class)
82+
public void rejectsNonAccessorMethod() throws Throwable {
8083

81-
assertThat(source.firstname, is((Object) "Carl"));
82-
}
84+
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("someGarbage"));
8385

84-
/**
85-
* @see DATACMNS-820
86-
*/
87-
@Test(expected = IllegalStateException.class)
88-
public void throwsAppropriateExceptionIfTheInvocationHasNoArguments() throws Throwable {
86+
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
87+
}
8988

90-
Source source = new Source();
89+
/**
90+
* @see DATACMNS-820
91+
*/
92+
@Test
93+
public void triggersWritePropertyAccessOnTarget() throws Throwable {
9194

92-
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
93-
when(invocation.getArguments()).thenReturn(new Object[0]);
94-
MethodInterceptor interceptor = new PropertyAccessingMethodInterceptor(source);
95+
Source source = new Source();
96+
source.firstname = "Dave";
9597

96-
interceptor.invoke(invocation);
97-
}
98+
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
99+
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
98100

99-
/**
100-
* @see DATACMNS-820
101-
*/
102-
@Test(expected = NotWritablePropertyException.class)
103-
public void throwsAppropriateExceptionIfThePropertyCannotWritten() throws Throwable {
101+
new PropertyAccessingMethodInterceptor(source).invoke(invocation);
104102

105-
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setGarbage", String.class));
106-
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
107-
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
108-
}
103+
assertThat(source.firstname, is((Object) "Carl"));
104+
}
109105

110106
/**
111-
* @see DATAREST-221
107+
* @see DATACMNS-820
112108
*/
113-
@Test
114-
public void forwardsObjectMethodInvocation() throws Throwable {
109+
@Test(expected = IllegalStateException.class)
110+
public void throwsAppropriateExceptionIfTheInvocationHasNoArguments() throws Throwable {
115111

116-
when(invocation.getMethod()).thenReturn(Object.class.getMethod("toString"));
117-
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
112+
Source source = new Source();
113+
114+
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setFirstname", String.class));
115+
when(invocation.getArguments()).thenReturn(new Object[0]);
116+
117+
new PropertyAccessingMethodInterceptor(source).invoke(invocation);
118118
}
119119

120120
/**
121-
* @see DATACMNS-630
121+
* @see DATACMNS-820
122122
*/
123-
@Test(expected = IllegalStateException.class)
124-
public void rejectsNonAccessorMethod() throws Throwable {
123+
@Test(expected = NotWritablePropertyException.class)
124+
public void throwsAppropriateExceptionIfThePropertyCannotWritten() throws Throwable {
125+
126+
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("setGarbage", String.class));
127+
when(invocation.getArguments()).thenReturn(new Object[] { "Carl" });
125128

126-
when(invocation.getMethod()).thenReturn(Projection.class.getMethod("someGarbage"));
127129
new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation);
128130
}
129131

src/test/java/org/springframework/data/projection/SpelAwareProxyProjectionFactoryUnitTests.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.util.List;
2222

2323
import org.junit.Before;
24+
import org.junit.Rule;
2425
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
2527
import org.springframework.beans.NotWritablePropertyException;
2628
import org.springframework.beans.factory.annotation.Value;
2729

@@ -34,7 +36,9 @@
3436
*/
3537
public class SpelAwareProxyProjectionFactoryUnitTests {
3638

37-
private SpelAwareProxyProjectionFactory factory;
39+
public @Rule ExpectedException exception = ExpectedException.none();
40+
41+
SpelAwareProxyProjectionFactory factory;
3842

3943
@Before
4044
public void setup() {
@@ -55,6 +59,29 @@ public void exposesSpelInvokingMethod() {
5559
assertThat(excerpt.getFullName(), is("Dave Matthews"));
5660
}
5761

62+
/**
63+
* @see DATACMNS-630
64+
*/
65+
@Test
66+
public void excludesAtValueAnnotatedMethodsForInputProperties() {
67+
68+
List<String> properties = factory.getInputProperties(CustomerExcerpt.class);
69+
70+
assertThat(properties, hasSize(1));
71+
assertThat(properties, hasItem("firstname"));
72+
}
73+
74+
/**
75+
* @see DATACMNS-89
76+
*/
77+
@Test
78+
public void considersProjectionUsingAtValueNotClosed() {
79+
80+
ProjectionInformation information = factory.getProjectionInformation(CustomerExcerpt.class);
81+
82+
assertThat(information.isClosed(), is(false));
83+
}
84+
5885
/**
5986
* @see DATACMNS-820
6087
*/
@@ -82,36 +109,8 @@ public void settingNotWriteablePropertyFails() {
82109
ProjectionWithNotWriteableProperty projection = factory.createProjection(ProjectionWithNotWriteableProperty.class,
83110
customer);
84111

85-
try {
86-
projection.setFirstName("Carl");
87-
fail("Missing NotWritablePropertyException");
88-
} catch (NotWritablePropertyException e) {
89-
assertThat(e, instanceOf(NotWritablePropertyException.class));
90-
}
91-
92-
}
93-
94-
/**
95-
* @see DATACMNS-630
96-
*/
97-
@Test
98-
public void excludesAtValueAnnotatedMethodsForInputProperties() {
99-
100-
List<String> properties = factory.getInputProperties(CustomerExcerpt.class);
101-
102-
assertThat(properties, hasSize(1));
103-
assertThat(properties, hasItem("firstname"));
104-
}
105-
106-
/**
107-
* @see DATACMNS-89
108-
*/
109-
@Test
110-
public void considersProjectionUsingAtValueNotClosed() {
111-
112-
ProjectionInformation information = factory.getProjectionInformation(CustomerExcerpt.class);
113-
114-
assertThat(information.isClosed(), is(false));
112+
exception.expect(NotWritablePropertyException.class);
113+
projection.setFirstName("Carl");
115114
}
116115

117116
static class Customer {

0 commit comments

Comments
 (0)