Skip to content

Commit cace881

Browse files
committed
DATACMNS-1598 - Polishing.
Add unit tests to back change. Guard invocations against null dereference. Join assertions. Original pull request: #409.
1 parent 36b95d3 commit cace881

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* projecting proxy in case the returned value is not of the return type of the invoked method.
4545
*
4646
* @author Oliver Gierke
47+
* @author Mark Paluch
4748
* @since 1.10
4849
*/
4950
@RequiredArgsConstructor
@@ -92,8 +93,9 @@ public Object invoke(@SuppressWarnings("null") @Nonnull MethodInvocation invocat
9293
private Object projectCollectionElements(Collection<?> sources, TypeInformation<?> type) {
9394

9495
Class<?> rawType = type.getType();
96+
TypeInformation<?> componentType = type.getComponentType();
9597
Collection<Object> result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType,
96-
type.getComponentType().getType(), sources.size());
98+
componentType != null ? componentType.getType() : null, sources.size());
9799

98100
for (Object source : sources) {
99101
result.add(getProjection(source, type.getRequiredComponentType().getType()));

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

+28-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.Collection;
2323
import java.util.Collections;
24+
import java.util.EnumSet;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Set;
@@ -39,6 +40,7 @@
3940
*
4041
* @author Oliver Gierke
4142
* @author Saulo Medeiros de Araujo
43+
* @author Mark Paluch
4244
*/
4345
@RunWith(MockitoJUnitRunner.class)
4446
public class ProjectingMethodInterceptorUnitTests {
@@ -105,8 +107,7 @@ public void appliesProjectionToNonEmptySets() throws Throwable {
105107
assertThat(result).isInstanceOf(Set.class);
106108

107109
Set<Object> projections = (Set<Object>) result;
108-
assertThat(projections).hasSize(1);
109-
assertThat(projections).hasOnlyElementsOfType(HelperProjection.class);
110+
assertThat(projections).hasSize(1).hasOnlyElementsOfType(HelperProjection.class);
110111
}
111112

112113
@Test // DATAREST-394, DATAREST-408
@@ -122,8 +123,7 @@ public void appliesProjectionToNonEmptyLists() throws Throwable {
122123

123124
List<Object> projections = (List<Object>) result;
124125

125-
assertThat(projections).hasSize(1);
126-
assertThat(projections).hasOnlyElementsOfType(HelperProjection.class);
126+
assertThat(projections).hasSize(1).hasOnlyElementsOfType(HelperProjection.class);
127127
}
128128

129129
@Test // DATAREST-394, DATAREST-408
@@ -138,8 +138,7 @@ public void allowsMaskingAnArrayIntoACollection() throws Throwable {
138138

139139
Collection<Object> projections = (Collection<Object>) result;
140140

141-
assertThat(projections).hasSize(1);
142-
assertThat(projections).hasOnlyElementsOfType(HelperProjection.class);
141+
assertThat(projections).hasSize(1).hasOnlyElementsOfType(HelperProjection.class);
143142
}
144143

145144
@Test // DATAREST-394, DATAREST-408
@@ -156,8 +155,7 @@ public void appliesProjectionToNonEmptyMap() throws Throwable {
156155

157156
Map<String, Object> projections = (Map<String, Object>) result;
158157

159-
assertThat(projections).hasSize(1);
160-
assertThat(projections).matches(map -> map.get("foo") instanceof HelperProjection);
158+
assertThat(projections).hasSize(1).matches(map -> map.get("foo") instanceof HelperProjection);
161159
}
162160

163161
@Test
@@ -173,8 +171,22 @@ public void returnsSingleElementCollectionForTargetThatReturnsNonCollection() th
173171

174172
Collection<?> collection = (Collection<?>) result;
175173

176-
assertThat(collection).hasSize(1);
177-
assertThat(collection).hasOnlyElementsOfType(HelperProjection.class);
174+
assertThat(collection).hasSize(1).hasOnlyElementsOfType(HelperProjection.class);
175+
}
176+
177+
@Test // DATACMNS-1598
178+
public void returnsEnumSet() throws Throwable {
179+
180+
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor,
181+
conversionService);
182+
183+
Object result = methodInterceptor
184+
.invoke(mockInvocationOf("getHelperEnumSet", Collections.singletonList(HelperEnum.Helpful)));
185+
186+
assertThat(result).isInstanceOf(EnumSet.class);
187+
188+
Collection<HelperEnum> collection = (Collection<HelperEnum>) result;
189+
assertThat(collection).containsOnly(HelperEnum.Helpful);
178190
}
179191

180192
/**
@@ -210,11 +222,17 @@ interface Helper {
210222
Map<String, HelperProjection> getHelperMap();
211223

212224
Collection<HelperProjection> getHelperArray();
225+
226+
EnumSet<HelperEnum> getHelperEnumSet();
213227
}
214228

215229
interface HelperProjection {
216230
Helper getHelper();
217231

218232
String getString();
219233
}
234+
235+
enum HelperEnum {
236+
Helpful, NotSoMuch;
237+
}
220238
}

0 commit comments

Comments
 (0)