DATAREST-394, DATAREST-408 - Projections now consider collections and maps.
The ProjectingMethodInterceptor now inspects collections and maps to apply projections to collection elements and map values. Test cases inspired by a contribution of Saulo Medeiros de Araujo. Related pull request: #152.
This commit is contained in:
committed by
Oliver Gierke
parent
42b906ca04
commit
e6a19a366d
@@ -20,8 +20,16 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -31,6 +39,7 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
* Unit tests for {@link ProjectingMethodInterceptor}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Saulo Medeiros de Araujo
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ProjectingMethodInterceptorUnitTests {
|
||||
@@ -95,6 +104,96 @@ public class ProjectingMethodInterceptorUnitTests {
|
||||
verify(factory, times(0)).createProjection(anyObject(), (Class<?>) anyObject());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-394, DATAREST-408
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void appliesProjectionToNonEmptySets() throws Throwable {
|
||||
|
||||
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(null), interceptor);
|
||||
Object result = methodInterceptor.invoke(mockInvocationOf("getHelperCollection",
|
||||
Collections.singleton(mock(Helper.class))));
|
||||
|
||||
assertThat(result, is(instanceOf(Set.class)));
|
||||
|
||||
Set<Object> projections = (Set<Object>) result;
|
||||
assertThat(projections, hasSize(1));
|
||||
assertThat(projections, hasItem(instanceOf(HelperProjection.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-394, DATAREST-408
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void appliesProjectionToNonEmptyLists() throws Throwable {
|
||||
|
||||
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(null), interceptor);
|
||||
Object result = methodInterceptor.invoke(mockInvocationOf("getHelperList",
|
||||
Collections.singletonList(mock(Helper.class))));
|
||||
|
||||
assertThat(result, is(instanceOf(List.class)));
|
||||
|
||||
List<Object> projections = (List<Object>) result;
|
||||
|
||||
assertThat(projections, hasSize(1));
|
||||
assertThat(projections, hasItem(instanceOf(HelperProjection.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-394, DATAREST-408
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void allowsMaskingAnArrayIntoACollection() throws Throwable {
|
||||
|
||||
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(null), interceptor);
|
||||
Object result = methodInterceptor.invoke(mockInvocationOf("getHelperArray", new Helper[] { mock(Helper.class) }));
|
||||
|
||||
assertThat(result, is(instanceOf(Collection.class)));
|
||||
|
||||
Collection<Object> projections = (Collection<Object>) result;
|
||||
|
||||
assertThat(projections, hasSize(1));
|
||||
assertThat(projections, hasItem(instanceOf(HelperProjection.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-394, DATAREST-408
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void appliesProjectionToNonEmptyMap() throws Throwable {
|
||||
|
||||
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(null), interceptor);
|
||||
|
||||
Object result = methodInterceptor.invoke(mockInvocationOf("getHelperMap",
|
||||
Collections.singletonMap("foo", mock(Helper.class))));
|
||||
|
||||
assertThat(result, is(instanceOf(Map.class)));
|
||||
|
||||
Map<String, Object> projections = (Map<String, Object>) result;
|
||||
assertThat(projections.entrySet(), is(Matchers.<Entry<String, Object>> iterableWithSize(1)));
|
||||
assertThat(projections, hasEntry(is("foo"), instanceOf(HelperProjection.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mocks the {@link Helper} method of the given name to return the given value.
|
||||
*
|
||||
* @param methodName
|
||||
* @param returnValue
|
||||
* @return
|
||||
* @throws Throwable
|
||||
*/
|
||||
private MethodInvocation mockInvocationOf(String methodName, Object returnValue) throws Throwable {
|
||||
|
||||
when(invocation.getMethod()).thenReturn(Helper.class.getMethod(methodName));
|
||||
when(interceptor.invoke(invocation)).thenReturn(returnValue);
|
||||
|
||||
return invocation;
|
||||
}
|
||||
|
||||
interface Helper {
|
||||
|
||||
Helper getHelper();
|
||||
@@ -102,5 +201,21 @@ public class ProjectingMethodInterceptorUnitTests {
|
||||
String getString();
|
||||
|
||||
long getPrimitive();
|
||||
|
||||
Collection<HelperProjection> getHelperCollection();
|
||||
|
||||
List<HelperProjection> getHelperList();
|
||||
|
||||
Set<HelperProjection> getHelperSet();
|
||||
|
||||
Map<String, HelperProjection> getHelperMap();
|
||||
|
||||
Collection<HelperProjection> getHelperArray();
|
||||
}
|
||||
|
||||
interface HelperProjection {
|
||||
Helper getHelper();
|
||||
|
||||
String getString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user