From cace881a131935e7653fc07374fd2db7043f0530 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 28 Oct 2019 08:44:24 +0100 Subject: [PATCH] DATACMNS-1598 - Polishing. Add unit tests to back change. Guard invocations against null dereference. Join assertions. Original pull request: #409. --- .../ProjectingMethodInterceptor.java | 4 +- .../ProjectingMethodInterceptorUnitTests.java | 38 ++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java b/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java index a52692b9e..a79a3c2bc 100644 --- a/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java +++ b/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java @@ -44,6 +44,7 @@ import org.springframework.util.ObjectUtils; * projecting proxy in case the returned value is not of the return type of the invoked method. * * @author Oliver Gierke + * @author Mark Paluch * @since 1.10 */ @RequiredArgsConstructor @@ -92,8 +93,9 @@ class ProjectingMethodInterceptor implements MethodInterceptor { private Object projectCollectionElements(Collection sources, TypeInformation type) { Class rawType = type.getType(); + TypeInformation componentType = type.getComponentType(); Collection result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType, - type.getComponentType().getType(), sources.size()); + componentType != null ? componentType.getType() : null, sources.size()); for (Object source : sources) { result.add(getProjection(source, type.getRequiredComponentType().getType())); diff --git a/src/test/java/org/springframework/data/projection/ProjectingMethodInterceptorUnitTests.java b/src/test/java/org/springframework/data/projection/ProjectingMethodInterceptorUnitTests.java index ffa7495af..85b951524 100755 --- a/src/test/java/org/springframework/data/projection/ProjectingMethodInterceptorUnitTests.java +++ b/src/test/java/org/springframework/data/projection/ProjectingMethodInterceptorUnitTests.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.*; import java.util.Collection; import java.util.Collections; +import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -39,6 +40,7 @@ import org.springframework.core.convert.support.DefaultConversionService; * * @author Oliver Gierke * @author Saulo Medeiros de Araujo + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class ProjectingMethodInterceptorUnitTests { @@ -105,8 +107,7 @@ public class ProjectingMethodInterceptorUnitTests { assertThat(result).isInstanceOf(Set.class); Set projections = (Set) result; - assertThat(projections).hasSize(1); - assertThat(projections).hasOnlyElementsOfType(HelperProjection.class); + assertThat(projections).hasSize(1).hasOnlyElementsOfType(HelperProjection.class); } @Test // DATAREST-394, DATAREST-408 @@ -122,8 +123,7 @@ public class ProjectingMethodInterceptorUnitTests { List projections = (List) result; - assertThat(projections).hasSize(1); - assertThat(projections).hasOnlyElementsOfType(HelperProjection.class); + assertThat(projections).hasSize(1).hasOnlyElementsOfType(HelperProjection.class); } @Test // DATAREST-394, DATAREST-408 @@ -138,8 +138,7 @@ public class ProjectingMethodInterceptorUnitTests { Collection projections = (Collection) result; - assertThat(projections).hasSize(1); - assertThat(projections).hasOnlyElementsOfType(HelperProjection.class); + assertThat(projections).hasSize(1).hasOnlyElementsOfType(HelperProjection.class); } @Test // DATAREST-394, DATAREST-408 @@ -156,8 +155,7 @@ public class ProjectingMethodInterceptorUnitTests { Map projections = (Map) result; - assertThat(projections).hasSize(1); - assertThat(projections).matches(map -> map.get("foo") instanceof HelperProjection); + assertThat(projections).hasSize(1).matches(map -> map.get("foo") instanceof HelperProjection); } @Test @@ -173,8 +171,22 @@ public class ProjectingMethodInterceptorUnitTests { Collection collection = (Collection) result; - assertThat(collection).hasSize(1); - assertThat(collection).hasOnlyElementsOfType(HelperProjection.class); + assertThat(collection).hasSize(1).hasOnlyElementsOfType(HelperProjection.class); + } + + @Test // DATACMNS-1598 + public void returnsEnumSet() throws Throwable { + + MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor, + conversionService); + + Object result = methodInterceptor + .invoke(mockInvocationOf("getHelperEnumSet", Collections.singletonList(HelperEnum.Helpful))); + + assertThat(result).isInstanceOf(EnumSet.class); + + Collection collection = (Collection) result; + assertThat(collection).containsOnly(HelperEnum.Helpful); } /** @@ -210,6 +222,8 @@ public class ProjectingMethodInterceptorUnitTests { Map getHelperMap(); Collection getHelperArray(); + + EnumSet getHelperEnumSet(); } interface HelperProjection { @@ -217,4 +231,8 @@ public class ProjectingMethodInterceptorUnitTests { String getString(); } + + enum HelperEnum { + Helpful, NotSoMuch; + } }