From f65e47b41d1d1d056b3a87891a9ccc05843020f3 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 24 Dec 2009 14:23:55 +0000 Subject: [PATCH] INT-944 MethodInvokingMessageProcessor now handles Mockito mocks properly. --- ...iceActivatorOnMockitoMockTests-context.xml | 2 +- .../ServiceActivatorOnMockitoMockTests.java | 23 +++++++++++++++---- .../MethodInvokingMessageProcessor.java | 17 +++++++++++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests-context.xml b/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests-context.xml index 7ee8fdf5ee..5ed0f63b87 100644 --- a/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests-context.xml +++ b/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests-context.xml @@ -14,7 +14,7 @@ - + diff --git a/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests.java b/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests.java index 40e7832f8c..60d8a661e8 100644 --- a/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests.java +++ b/org.springframework.integration.test/src/test/java/org/springframework/integration/test/mockito/ServiceActivatorOnMockitoMockTests.java @@ -1,11 +1,26 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.integration.test.mockito; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.PollableChannel; import org.springframework.integration.core.MessageChannel; import org.springframework.integration.message.MessageBuilder; @@ -13,11 +28,10 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * + * @author Iwein Fuld */ @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) -@Ignore//remove to reproduce INT-944 public class ServiceActivatorOnMockitoMockTests { @Autowired @Qualifier("in") @@ -27,7 +41,6 @@ public class ServiceActivatorOnMockitoMockTests { PollableChannel out; public static class SingleMethod { - @ServiceActivator public String move(String s){return s;}; } diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/handler/MethodInvokingMessageProcessor.java b/org.springframework.integration/src/main/java/org/springframework/integration/handler/MethodInvokingMessageProcessor.java index d457cf08c5..6154359153 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/handler/MethodInvokingMessageProcessor.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/handler/MethodInvokingMessageProcessor.java @@ -192,7 +192,8 @@ public class MethodInvokingMessageProcessor implements MessageProcessor { final Map, HandlerMethod> candidateMethods = new HashMap, HandlerMethod>(); final Map, HandlerMethod> fallbackMethods = new HashMap, HandlerMethod>(); final AtomicReference> ambiguousFallbackType = new AtomicReference>(); - ReflectionUtils.doWithMethods(targetObject.getClass(), new MethodCallback() { + Class targetClass = this.getTargetClass(targetObject); + ReflectionUtils.doWithMethods(targetClass, new MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { boolean matchesAnnotation = false; if (method.isBridge()) { @@ -250,6 +251,20 @@ public class MethodInvokingMessageProcessor implements MessageProcessor { return fallbackMethods; } + private Class getTargetClass(Object targetObject) { + Class targetClass = targetObject.getClass(); + if (AopUtils.isAopProxy(targetObject)) { + targetClass = AopUtils.getTargetClass(targetObject); + } + else if(AopUtils.isCglibProxyClass(targetClass)) { + Class superClass = targetObject.getClass().getSuperclass(); + if (!Object.class.equals(superClass)) { + targetClass = superClass; + } + } + return targetClass; + } + private List findHandlerMethodsForMessage(Message message) { final Class payloadType = message.getPayload().getClass(); HandlerMethod closestMatch = this.findClosestMatch(payloadType);