Added MethodInvokingSelector.

This commit is contained in:
Mark Fisher
2008-09-24 23:44:33 +00:00
parent 2feb20e022
commit 784c732389
2 changed files with 162 additions and 0 deletions

View File

@@ -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<Integer>(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<Integer>(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;
}
}
}