DATACMNS-1598 - Polishing.

Add unit tests to back change. Guard invocations against null dereference.
Join assertions.

Original pull request: #409.
This commit is contained in:
Mark Paluch
2019-10-28 08:44:24 +01:00
parent 52469e544a
commit 8d219a53cd
2 changed files with 31 additions and 11 deletions

View File

@@ -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<Object> 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()));

View File

@@ -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<Object> projections = (Set<Object>) 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<Object> projections = (List<Object>) 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<Object> projections = (Collection<Object>) 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<String, Object> projections = (Map<String, Object>) 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<HelperEnum> collection = (Collection<HelperEnum>) result;
assertThat(collection).containsOnly(HelperEnum.Helpful);
}
/**
@@ -210,6 +222,8 @@ public class ProjectingMethodInterceptorUnitTests {
Map<String, HelperProjection> getHelperMap();
Collection<HelperProjection> getHelperArray();
EnumSet<HelperEnum> getHelperEnumSet();
}
interface HelperProjection {
@@ -217,4 +231,8 @@ public class ProjectingMethodInterceptorUnitTests {
String getString();
}
enum HelperEnum {
Helpful, NotSoMuch;
}
}