use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.
the class ProjectingMethodInterceptorUnitTests method wrapsDelegateResultInProxyIfTypesDontMatch.
// DATAREST-221
@Test
public void wrapsDelegateResultInProxyIfTypesDontMatch() throws Throwable {
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor, conversionService);
when(invocation.getMethod()).thenReturn(Helper.class.getMethod("getHelper"));
when(interceptor.invoke(invocation)).thenReturn("Foo");
assertThat(methodInterceptor.invoke(invocation)).isInstanceOf(Helper.class);
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.
the class ProjectingMethodInterceptorUnitTests method returnsNullAsIs.
// DATAREST-221
@Test
public void returnsNullAsIs() throws Throwable {
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(factory, interceptor, conversionService);
when(interceptor.invoke(invocation)).thenReturn(null);
assertThat(methodInterceptor.invoke(invocation)).isNull();
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.
the class ProjectingMethodInterceptorUnitTests method returnsSingleElementCollectionForTargetThatReturnsNonCollection.
@Test
public void returnsSingleElementCollectionForTargetThatReturnsNonCollection() throws Throwable {
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor, conversionService);
Helper reference = mock(Helper.class);
Object result = methodInterceptor.invoke(mockInvocationOf("getHelperCollection", reference));
assertThat(result).isInstanceOf(Collection.class);
Collection<?> collection = (Collection<?>) result;
assertThat(collection).hasSize(1);
assertThat(collection).hasOnlyElementsOfType(HelperProjection.class);
}
use of org.aopalliance.intercept.MethodInterceptor in project guice by google.
the class MethodInterceptionTest method testGetElements_interceptorBindings.
@Test
public void testGetElements_interceptorBindings() throws Exception {
@SuppressWarnings("rawtypes") Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
Matcher<Method> methodMatcher = Matchers.returns(Matchers.identicalTo(int.class));
MethodInterceptor interceptor = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
return null;
}
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(classMatcher, methodMatcher, interceptor);
}
});
final List<InterceptorBinding> interceptorBindings = new ArrayList<>();
for (Element element : injector.getElements()) {
element.acceptVisitor(new DefaultElementVisitor<Void>() {
@Override
public Void visit(InterceptorBinding interceptorBinding) {
interceptorBindings.add(interceptorBinding);
return null;
}
});
}
assertThat(interceptorBindings).hasSize(1);
InterceptorBinding extractedBinding = interceptorBindings.get(0);
assertSame(classMatcher, extractedBinding.getClassMatcher());
assertSame(methodMatcher, extractedBinding.getMethodMatcher());
assertSame(interceptor, extractedBinding.getInterceptors().get(0));
}
use of org.aopalliance.intercept.MethodInterceptor in project roboguice by roboguice.
the class BindingTest method testToConstructorAndMethodInterceptors.
/*if[AOP]*/
public void testToConstructorAndMethodInterceptors() throws NoSuchMethodException {
final Constructor<D> constructor = D.class.getConstructor(Stage.class);
final AtomicInteger count = new AtomicInteger();
final MethodInterceptor countingInterceptor = new MethodInterceptor() {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
count.incrementAndGet();
return methodInvocation.proceed();
}
};
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(Object.class).toConstructor(constructor);
bindInterceptor(Matchers.any(), Matchers.any(), countingInterceptor);
}
});
D d = (D) injector.getInstance(Object.class);
d.hashCode();
d.hashCode();
assertEquals(2, count.get());
}
Aggregations