diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/filter/MethodInvokingSelector.java b/org.springframework.integration/src/main/java/org/springframework/integration/filter/MethodInvokingSelector.java new file mode 100644 index 0000000000..e87ff02518 --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/filter/MethodInvokingSelector.java @@ -0,0 +1,56 @@ +/* + * Copyright 2002-2008 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.filter; + +import java.lang.reflect.Method; + +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMappingMethodInvoker; +import org.springframework.integration.message.selector.MessageSelector; +import org.springframework.util.Assert; + +/** + * A method-invoking implementation of {@link MessageSelector}. + * + * @author Mark Fisher + */ +public class MethodInvokingSelector implements MessageSelector { + + private final MessageMappingMethodInvoker invoker; + + + public MethodInvokingSelector(Object object, Method method) { + this.invoker = new MessageMappingMethodInvoker(object, method); + Class returnType = method.getReturnType(); + Assert.isTrue(boolean.class.isAssignableFrom(returnType) + || Boolean.class.isAssignableFrom(returnType), + "MethodInvokingSelector method must return a boolean result."); + } + + public MethodInvokingSelector(Object object, String methodName) { + this.invoker = new MessageMappingMethodInvoker(object, methodName); + } + + + public boolean accept(Message message) { + Object result = this.invoker.invokeMethod(message); + Assert.notNull(result, "result must not be null"); + Assert.isAssignable(Boolean.class, result.getClass(), "a boolean result is required"); + return (Boolean) result; + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/filter/MethodInvokingSelectorTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/filter/MethodInvokingSelectorTests.java new file mode 100644 index 0000000000..0ea48fd759 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/filter/MethodInvokingSelectorTests.java @@ -0,0 +1,106 @@ +/* + * Copyright 2002-2008 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.filter; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; + +import org.junit.Test; + +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; + +/** + * @author Mark Fisher + */ +public class MethodInvokingSelectorTests { + + @Test + public void acceptedWithMethodName() { + MethodInvokingSelector selector = new MethodInvokingSelector(new TestBean(), "acceptString"); + assertTrue(selector.accept(new StringMessage("should accept"))); + } + + @Test + public void acceptedWithMethodReference() throws Exception { + TestBean testBean = new TestBean(); + Method method = testBean.getClass().getMethod("acceptString", new Class[] { Message.class }); + MethodInvokingSelector selector = new MethodInvokingSelector(testBean, method); + assertTrue(selector.accept(new StringMessage("should accept"))); + } + + @Test + public void rejectedWithMethodName() { + MethodInvokingSelector selector = new MethodInvokingSelector(new TestBean(), "acceptString"); + assertFalse(selector.accept(new GenericMessage(99))); + } + + @Test + public void rejectedWithMethodReference() throws Exception { + TestBean testBean = new TestBean(); + Method method = testBean.getClass().getMethod("acceptString", new Class[] { Message.class }); + MethodInvokingSelector selector = new MethodInvokingSelector(testBean, method); + assertFalse(selector.accept(new GenericMessage(99))); + } + + @Test(expected = IllegalArgumentException.class) + public void noArgMethodWithMethodName() { + MethodInvokingSelector selector = new MethodInvokingSelector(new TestBean(), "noArgs"); + selector.accept(new StringMessage("test")); + } + + @Test(expected = IllegalArgumentException.class) + public void noArgMethodWithMethodReference() throws Exception { + TestBean testBean = new TestBean(); + Method method = testBean.getClass().getMethod("noArgs", new Class[] {}); + MethodInvokingSelector selector = new MethodInvokingSelector(testBean, method); + selector.accept(new StringMessage("test")); + } + + @Test(expected = IllegalArgumentException.class) + public void voidReturningMethodWithMethodName() { + MethodInvokingSelector selector = new MethodInvokingSelector(new TestBean(), "returnVoid"); + selector.accept(new StringMessage("test")); + } + + @Test(expected = IllegalArgumentException.class) + public void voidReturningMethodWithMethodReference() throws Exception { + TestBean testBean = new TestBean(); + Method method = testBean.getClass().getMethod("returnVoid", new Class[] { Message.class }); + MethodInvokingSelector selector = new MethodInvokingSelector(testBean, method); + selector.accept(new StringMessage("test")); + } + + + private static class TestBean { + + public boolean acceptString(Message message) { + return (message.getPayload() instanceof String); + } + + public void returnVoid(Message message) { + } + + public boolean noArgs() { + return true; + } + } + +}