Added tests for adapters.

This commit is contained in:
Mark Fisher
2007-12-10 05:14:41 +00:00
parent 39d31e087e
commit 96a14151eb
6 changed files with 323 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2002-2007 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.endpoint;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.IOException;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Mark Fisher
*/
public class AdapterBusTests {
@Test
public void testAdapters() throws IOException, InterruptedException {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("adapterBusTests.xml", this.getClass());
TestSink sink = (TestSink) context.getBean("sink");
assertNull(sink.get());
context.start();
String result = null;
int attempts = 0;
while (result == null && attempts++ < 10) {
Thread.sleep(5);
result = sink.get();
}
assertNotNull(result);
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2007 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.endpoint;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class InboundChannelAdapterTests {
@Test
public void testValidMethod() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("validMethod");
adapter.afterPropertiesSet();
Message message = adapter.receive();
assertEquals("valid", message.getPayload());
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithArg() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("invalidMethodWithArg");
adapter.receive();
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithNoReturnValue() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("invalidMethodWithNoReturnValue");
adapter.receive();
}
@Test(expected=MessageHandlingException.class)
public void testNoMatchingMethodName() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("noSuchMethod");
adapter.receive();
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2007 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.endpoint;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter;
import org.springframework.integration.message.DocumentMessage;
/**
* @author Mark Fisher
*/
public class OutboundChannelAdapterTests {
@Test
public void testValidMethod() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("validMethod");
adapter.afterPropertiesSet();
boolean result = adapter.send(new DocumentMessage(1, "test"));
assertTrue(result);
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithNoArgs() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("invalidMethodWithNoArgs");
adapter.afterPropertiesSet();
adapter.send(new DocumentMessage(1, "test"));
}
@Test
public void testValidMethodWithIgnoredReturnValue() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("validMethodWithIgnoredReturnValue");
adapter.afterPropertiesSet();
boolean result = adapter.send(new DocumentMessage(1, "test"));
assertTrue(result);
}
@Test(expected=MessageHandlingException.class)
public void testNoMatchingMethodName() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("noSuchMethod");
adapter.send(new DocumentMessage(1, "test"));
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2007 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.endpoint;
/**
* @author Mark Fisher
*/
public class TestSink {
private String result;
public void validMethod(String s) {
}
public void invalidMethodWithNoArgs() {
}
public String validMethodWithIgnoredReturnValue(String s) {
return "ignored";
}
public void store(String s) {
this.result = s;
}
public String get() {
return this.result;
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2002-2007 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.endpoint;
/**
* @author Mark Fisher
*/
public class TestSource {
private boolean fooCalled;
public String validMethod() {
return "valid";
}
public String invalidMethodWithArg(String arg) {
return "invalid";
}
public void invalidMethodWithNoReturnValue() {
}
public String foo() {
if (this.fooCalled) {
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
this.fooCalled = true;
return "bar";
}
}

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="inboundAdapter" class="org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter">
<property name="object">
<bean class="org.springframework.integration.adapter.TestSource"/>
</property>
<property name="method" value="foo"/>
</bean>
<bean id="outboundAdapter" class="org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter">
<property name="object" ref="sink"/>
<property name="method" value="store"/>
</bean>
<bean id="sink" class="org.springframework.integration.adapter.TestSink"/>
<bean id="endpoint" class="org.springframework.integration.endpoint.GenericMessageEndpoint">
<property name="defaultOutputChannelName" value="outboundAdapter"/>
</bean>
<bean class="org.springframework.integration.bus.Subscription">
<property name="channel" value="inboundAdapter"/>
<property name="endpoint" value="endpoint"/>
<property name="policy">
<bean class="org.springframework.integration.bus.ConsumerPolicy">
<property name="receiveTimeout" value="100"/>
</bean>
</property>
</bean>
</beans>