renamed from spring-eai-core

This commit is contained in:
Mark Fisher
2007-12-15 18:25:49 +00:00
parent 2290442e27
commit c8259e436c
121 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1}: %m%n
log4j.category.org.springframework.integration=INFO

View File

@@ -0,0 +1,26 @@
/*
* 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.aop;
/**
* @author Mark Fisher
*/
public interface ITestBean {
String test();
}

View File

@@ -0,0 +1,101 @@
/*
* 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.aop;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class MessagePublishingInterceptorTests {
@Test
public void testNonNullReturnValuePublishedWithDefaultChannel() {
MessageChannel channel = new PointToPointChannel();
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor();
interceptor.setDefaultChannel(channel);
TestService proxy = (TestService) this.createProxy(new TestServiceImpl("hello world"), interceptor);
proxy.messageTest();
Message message = channel.receive(0);
assertNotNull(message);
assertEquals("hello world", message.getPayload());
}
@Test
public void testNullReturnValueNotPublished() {
MessageChannel channel = new PointToPointChannel();
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor();
interceptor.setDefaultChannel(channel);
TestService proxy = (TestService) this.createProxy(new TestServiceImpl(null), interceptor);
proxy.messageTest();
Message message = channel.receive(0);
assertNull(message);
}
@Test
public void testVoidReturnValueNotPublished() {
MessageChannel channel = new PointToPointChannel();
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor();
interceptor.setDefaultChannel(channel);
TestService proxy = (TestService) this.createProxy(new TestServiceImpl(null), interceptor);
proxy.voidTest();
Message message = channel.receive(0);
assertNull(message);
}
private Object createProxy(Object target, MessagePublishingInterceptor interceptor) {
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(interceptor);
return factory.getProxy();
}
private static interface TestService {
String messageTest();
void voidTest();
}
private static class TestServiceImpl implements TestService {
private String message;
public TestServiceImpl(String message) {
this.message = message;
}
public String messageTest() {
return this.message;
}
public void voidTest() {
return;
}
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.aop;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.integration.channel.ChannelMapping;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class PublisherAnnotationAdvisorTests {
@Test
public void testPublisherAnnotation() {
final MessageChannel channel = new PointToPointChannel();
ChannelMapping channelMapping = new ChannelMapping() {
public MessageChannel getChannel(String channelName) {
if (channelName.equals("testChannel")) {
return channel;
}
return null;
}
public MessageChannel getInvalidMessageChannel() {
return null;
}
};
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor(channelMapping);
TestService proxy = (TestService) this.createProxy(new TestServiceImpl("hello world"), advisor);
proxy.publisherTest();
Message message = channel.receive(0);
assertNotNull(message);
assertEquals("hello world", message.getPayload());
}
@Test
public void testNoPublisherAnnotation() {
final MessageChannel channel = new PointToPointChannel();
ChannelMapping channelMapping = new ChannelMapping() {
public MessageChannel getChannel(String channelName) {
if (channelName.equals("testChannel")) {
return channel;
}
return null;
}
public MessageChannel getInvalidMessageChannel() {
return null;
}
};
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor(channelMapping);
TestService proxy = (TestService) this.createProxy(new TestServiceImpl("hello world"), advisor);
proxy.noPublisherTest();
Message message = channel.receive(0);
assertNull(message);
}
private Object createProxy(Object target, PublisherAnnotationAdvisor advisor) {
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvisor(advisor);
return factory.getProxy();
}
private static interface TestService {
String publisherTest();
String noPublisherTest();
}
private static class TestServiceImpl implements TestService {
private String message;
public TestServiceImpl(String message) {
this.message = message;
}
@Publisher(channel="testChannel")
public String publisherTest() {
return this.message;
}
public String noPublisherTest() {
return this.message;
}
}
}

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.aop;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class PublisherAnnotationPostProcessorTests {
@Test
public void testPublisherAnnotation() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"publisherAnnotationPostProcessorTests.xml", this.getClass());
ITestBean testBean = (ITestBean) context.getBean("testBean");
testBean.test();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
Message result = channel.receive();
assertEquals("test", result.getPayload());
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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.aop;
/**
* @author Mark Fisher
*/
public class PublisherAnnotationTestBean implements ITestBean {
@Publisher(channel="testChannel")
public String test() {
return "test";
}
}

View File

@@ -0,0 +1,17 @@
<?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="messageBus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="testChannel" class="org.springframework.integration.channel.PointToPointChannel"/>
<bean id="testBean" class="org.springframework.integration.aop.PublisherAnnotationTestBean"/>
<bean class="org.springframework.integration.config.PublisherAnnotationPostProcessor">
<property name="channelMapping" ref="messageBus"/>
</bean>
</beans>

View File

@@ -0,0 +1,97 @@
/*
* 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.bus;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class EventDrivenConsumerTests {
@Test
public void testDynamicConcurrency() throws Exception {
int messagesToSend = 200;
int concurrency = 1;
int maxConcurrency = 40;
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(messagesToSend);
final AtomicInteger maxActive = new AtomicInteger(0);
final AtomicInteger activeSum = new AtomicInteger(0);
final MessageBus bus = new MessageBus();
PointToPointChannel channel = new PointToPointChannel();
MessageEndpoint endpoint = new GenericMessageEndpoint() {
public void messageReceived(Message message) {
counter.incrementAndGet();
latch.countDown();
try { Thread.sleep(3); } catch (InterruptedException e) {}
activeSum.set(activeSum.addAndGet(bus.getActiveCountForEndpoint("testEndpoint")));
maxActive.set(Math.max(bus.getActiveCountForEndpoint("testEndpoint"), maxActive.get()));
}
};
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
ConsumerPolicy policy = new ConsumerPolicy();
policy.setConcurrency(concurrency);
policy.setMaxConcurrency(maxConcurrency);
policy.setMaxMessagesPerTask(1);
policy.setRejectionLimit(1);
policy.setPeriod(0);
policy.setReceiveTimeout(100);
Subscription subscription = new Subscription();
subscription.setChannel("testChannel");
subscription.setEndpoint("testEndpoint");
subscription.setPolicy(policy);
bus.activateSubscription(subscription);
bus.start();
for (int i = 0; i < messagesToSend - 110; i++) {
channel.send(new GenericMessage<String>(1, "fast-1." + (i+1)));
}
int activeCountAfterFirstBurst = bus.getActiveCountForEndpoint("testEndpoint");
for (int i = 0; i < 10; i++) {
channel.send(new GenericMessage<String>(1, "slow-1." + (i+1)));
Thread.sleep(10);
}
int activeCountAfterSlowDown = bus.getActiveCountForEndpoint("testEndpoint");
for (int i = 0; i < 100; i++) {
channel.send(new GenericMessage<String>(1, "fast-2." + (i+1)));
}
int activeCountAfterLastBurst = bus.getActiveCountForEndpoint("testEndpoint");
latch.await(100, TimeUnit.SECONDS);
int averageActive = activeSum.get() / messagesToSend;
assertTrue(activeCountAfterSlowDown < activeCountAfterFirstBurst);
assertTrue(activeCountAfterLastBurst > activeCountAfterSlowDown);
assertEquals(messagesToSend, counter.get());
assertTrue(maxActive.get() > concurrency);
assertTrue(maxActive.get() <= maxConcurrency);
assertTrue(averageActive > concurrency);
assertTrue(averageActive < maxActive.get());
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.bus;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class FixedDelayConsumerTests {
@Test
public void testAllSentMessagesAreReceivedWithinTimeLimit() throws Exception {
int messagesToSend = 20;
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(messagesToSend);
PointToPointChannel channel = new PointToPointChannel();
MessageEndpoint endpoint = new GenericMessageEndpoint() {
public void messageReceived(Message message) {
counter.incrementAndGet();
latch.countDown();
}
};
MessageBus bus = new MessageBus();
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
ConsumerPolicy policy = new ConsumerPolicy();
policy.setConcurrency(1);
policy.setMaxConcurrency(1);
policy.setMaxMessagesPerTask(1);
policy.setFixedRate(true);
policy.setPeriod(10);
Subscription subscription = new Subscription();
subscription.setChannel("testChannel");
subscription.setEndpoint("testEndpoint");
subscription.setPolicy(policy);
bus.activateSubscription(subscription);
bus.start();
for (int i = 0; i < messagesToSend; i++) {
channel.send(new GenericMessage<String>(1, "test " + (i+1)));
}
latch.await(250, TimeUnit.MILLISECONDS);
assertEquals(messagesToSend, counter.get());
}
@Test
public void testTimedOutMessagesAreNotReceived() throws Exception {
int messagesToSend = 20;
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(messagesToSend);
PointToPointChannel channel = new PointToPointChannel();
MessageEndpoint endpoint = new GenericMessageEndpoint() {
public void messageReceived(Message message) {
counter.incrementAndGet();
latch.countDown();
}
};
MessageBus bus = new MessageBus();
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
ConsumerPolicy policy = new ConsumerPolicy();
policy.setConcurrency(1);
policy.setMaxConcurrency(1);
policy.setMaxMessagesPerTask(1);
policy.setFixedRate(true);
policy.setPeriod(10);
Subscription subscription = new Subscription();
subscription.setChannel("testChannel");
subscription.setEndpoint("testEndpoint");
subscription.setPolicy(policy);
bus.activateSubscription(subscription);
bus.start();
for (int i = 0; i < messagesToSend; i++) {
channel.send(new GenericMessage<String>(1, "test " + (i+1)));
}
latch.await(80, TimeUnit.MILLISECONDS);
assertTrue(counter.get() < 15);
assertTrue(counter.get() > 5);
}
}

View File

@@ -0,0 +1,104 @@
/*
* 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.bus;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class FixedRateConsumerTests {
@Test
public void testAllSentMessagesAreReceivedWithinTimeLimit() throws Exception {
int messagesToSend = 20;
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(messagesToSend);
PointToPointChannel channel = new PointToPointChannel();
MessageEndpoint endpoint = new GenericMessageEndpoint() {
public void messageReceived(Message message) {
counter.incrementAndGet();
latch.countDown();
}
};
MessageBus bus = new MessageBus();
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
ConsumerPolicy policy = new ConsumerPolicy();
policy.setFixedRate(true);
policy.setPeriod(10);
Subscription subscription = new Subscription();
subscription.setChannel("testChannel");
subscription.setEndpoint("testEndpoint");
subscription.setPolicy(policy);
bus.activateSubscription(subscription);
bus.start();
for (int i = 0; i < messagesToSend; i++) {
channel.send(new GenericMessage<String>(1, "test " + (i+1)));
}
latch.await(250, TimeUnit.MILLISECONDS);
assertEquals(messagesToSend, counter.get());
}
@Test
public void testTimedOutMessagesAreNotReceived() throws Exception {
int messagesToSend = 20;
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(messagesToSend);
PointToPointChannel channel = new PointToPointChannel();
MessageEndpoint endpoint = new GenericMessageEndpoint() {
public void messageReceived(Message message) {
counter.incrementAndGet();
latch.countDown();
}
};
MessageBus bus = new MessageBus();
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
ConsumerPolicy policy = new ConsumerPolicy();
policy.setConcurrency(1);
policy.setMaxConcurrency(1);
policy.setMaxMessagesPerTask(1);
policy.setFixedRate(true);
policy.setPeriod(10);
Subscription subscription = new Subscription();
subscription.setChannel("testChannel");
subscription.setEndpoint("testEndpoint");
subscription.setPolicy(policy);
bus.activateSubscription(subscription);
bus.start();
for (int i = 0; i < messagesToSend; i++) {
channel.send(new GenericMessage<String>(1, "test " + (i+1)));
}
latch.await(80, TimeUnit.MILLISECONDS);
assertTrue(counter.get() < 10);
assertTrue("only " + counter.get() + " messages received", counter.get() > 7);
}
}

View File

@@ -0,0 +1,78 @@
/*
* 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.bus;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class MessageBusTests {
/*
@Test
public void testStandaloneWithEndpoint() {
MessageBus bus = new MessageBus();
MessageChannel sourceChannel = new PointToPointChannel();
MessageChannel targetChannel = new PointToPointChannel();
bus.registerChannel("sourceChannel", sourceChannel);
sourceChannel.send(new DocumentMessage("123", "test"));
bus.registerChannel("targetChannel", targetChannel);
GenericMessageEndpoint endpoint = new GenericMessageEndpoint(sourceChannel);
endpoint.setTarget(targetChannel);
bus.registerEndpoint("endpoint", endpoint);
Message result = targetChannel.receive();
assertEquals("test", result.getPayload());
}
@Test
public void testStandaloneWithoutEndpoint() {
MessageBus bus = new MessageBus();
MessageChannel sourceChannel = new PointToPointChannel();
sourceChannel.send(new DocumentMessage("123", "test"));
MessageChannel targetChannel = new PointToPointChannel();
bus.registerChannel("sourceChannel", sourceChannel);
bus.registerChannel("targetChannel", targetChannel);
Message result = targetChannel.receive(10);
assertNull(result);
}
*/
@Test
public void testAutodetectionWithApplicationContext() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("messageBusTests.xml", this.getClass());
context.start();
MessageChannel sourceChannel = (MessageChannel) context.getBean("sourceChannel");
sourceChannel.send(new GenericMessage<String>("123", "test"));
MessageChannel targetChannel = (MessageChannel) context.getBean("targetChannel");
MessageBus bus = (MessageBus) context.getBean("bus");
ConsumerPolicy policy = new ConsumerPolicy();
Subscription subscription = new Subscription();
subscription.setChannel("sourceChannel");
subscription.setEndpoint("endpoint");
subscription.setPolicy(policy);
bus.activateSubscription(subscription);
Message<String> result = targetChannel.receive(10);
assertEquals("test", result.getPayload());
}
}

View File

@@ -0,0 +1,18 @@
<?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.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="sourceChannel" class="org.springframework.integration.channel.PointToPointChannel"/>
<bean id="targetChannel" class="org.springframework.integration.channel.PointToPointChannel"/>
<bean id="endpoint" class="org.springframework.integration.endpoint.GenericMessageEndpoint">
<property name="inputChannelName" value="sourceChannel"/>
<property name="defaultOutputChannelName" value="targetChannel"/>
</bean>
</beans>

View File

@@ -0,0 +1,259 @@
/*
* 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.channel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageSelector;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class PointToPointChannelTests {
@Test
public void testSimpleSendAndReceive() throws Exception {
final AtomicBoolean messageReceived = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(1);
final PointToPointChannel channel = new PointToPointChannel();
new Thread(new Runnable() {
public void run() {
Message<String> message = channel.receive();
if (message != null) {
messageReceived.set(true);
latch.countDown();
}
}
}).start();
assertFalse(messageReceived.get());
channel.send(new GenericMessage<String>(1, "testing"));
latch.await(25, TimeUnit.MILLISECONDS);
assertTrue(messageReceived.get());
}
@Test
public void testImmediateReceive() throws Exception {
final AtomicBoolean messageReceived = new AtomicBoolean(false);
final PointToPointChannel channel = new PointToPointChannel();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
Runnable receiveTask1 = new Runnable() {
public void run() {
Message<String> message = channel.receive(0);
if (message != null) {
messageReceived.set(true);
}
latch1.countDown();
}
};
Runnable sendTask = new Runnable() {
public void run() {
channel.send(new GenericMessage<String>(1, "testing"));
}
};
singleThreadExecutor.execute(receiveTask1);
latch1.await();
singleThreadExecutor.execute(sendTask);
assertFalse(messageReceived.get());
Runnable receiveTask2 = new Runnable() {
public void run() {
Message<String> message = channel.receive(0);
if (message != null) {
messageReceived.set(true);
}
latch2.countDown();
}
};
singleThreadExecutor.execute(receiveTask2);
latch2.await();
assertTrue(messageReceived.get());
}
@Test
public void testBlockingReceiveWithNoTimeout() throws Exception{
final PointToPointChannel channel = new PointToPointChannel();
final AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
Message<String> message = channel.receive();
receiveInterrupted.set(true);
assertTrue(message == null);
latch.countDown();
}
});
t.start();
assertFalse(receiveInterrupted.get());
t.interrupt();
latch.await();
assertTrue(receiveInterrupted.get());
}
@Test
public void testBlockingReceiveWithTimeout() throws Exception{
final PointToPointChannel channel = new PointToPointChannel();
final AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
Message<String> message = channel.receive(10000);
receiveInterrupted.set(true);
assertTrue(message == null);
latch.countDown();
}
});
t.start();
assertFalse(receiveInterrupted.get());
t.interrupt();
latch.await();
assertTrue(receiveInterrupted.get());
}
@Test
public void testImmediateSend() {
PointToPointChannel channel = new PointToPointChannel(3);
boolean result1 = channel.send(new GenericMessage<String>(1, "test-1"));
assertTrue(result1);
boolean result2 = channel.send(new GenericMessage<String>(2, "test-2"), 100);
assertTrue(result2);
boolean result3 = channel.send(new GenericMessage<String>(3, "test-3"), 0);
assertTrue(result3);
boolean result4 = channel.send(new GenericMessage<String>(4, "test-4"), 0);
assertFalse(result4);
}
@Test
public void testBlockingSendWithNoTimeout() throws Exception{
final PointToPointChannel channel = new PointToPointChannel(1);
boolean result1 = channel.send(new GenericMessage<String>(1, "test-1"));
assertTrue(result1);
final AtomicBoolean sendInterrupted = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
channel.send(new GenericMessage<String>(2, "test-2"));
sendInterrupted.set(true);
latch.countDown();
}
});
t.start();
assertFalse(sendInterrupted.get());
t.interrupt();
latch.await();
assertTrue(sendInterrupted.get());
}
@Test
public void testBlockingSendWithTimeout() throws Exception{
final PointToPointChannel channel = new PointToPointChannel(1);
boolean result1 = channel.send(new GenericMessage<String>(1, "test-1"));
assertTrue(result1);
final AtomicBoolean sendInterrupted = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
channel.send(new GenericMessage<String>(2, "test-2"), 10000);
sendInterrupted.set(true);
latch.countDown();
}
});
t.start();
assertFalse(sendInterrupted.get());
t.interrupt();
latch.await();
assertTrue(sendInterrupted.get());
}
@Test
public void testSelectorMatchesWithinTimeout() throws Exception {
final PointToPointChannel channel = new PointToPointChannel();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Message<String>> messageRef = new AtomicReference<Message<String>>();
Thread receiver = new Thread(new Runnable() {
public void run() {
Message message = channel.receive(new MessageSelector() {
public boolean accept(Message message) {
return (((Integer)message.getId()).intValue() == 3);
}
}, 50);
messageRef.set(message);
latch.countDown();
}
});
receiver.start();
Thread sender = new Thread(new Runnable() {
public void run() {
channel.send(new GenericMessage<String>(1, "test-1"));
try { Thread.sleep(5); } catch (Exception e) {}
channel.send(new GenericMessage<String>(2, "test-2"));
try { Thread.sleep(5); } catch (Exception e) {}
channel.send(new GenericMessage<String>(3, "test-3"));
try { Thread.sleep(100); } catch (Exception e) {}
channel.send(new GenericMessage<String>(4, "test-4"));
}
});
sender.start();
latch.await();
assertEquals("test-3", messageRef.get().getPayload());
}
@Test
public void testSelectorDoesNotMatchWithinTimeout() throws Exception {
final PointToPointChannel channel = new PointToPointChannel();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Message<String>> messageRef = new AtomicReference<Message<String>>();
Thread receiver = new Thread(new Runnable() {
public void run() {
Message message = channel.receive(new MessageSelector() {
public boolean accept(Message message) {
return (((Integer)message.getId()).intValue() == 3);
}
}, 7);
messageRef.set(message);
latch.countDown();
}
});
receiver.start();
Thread sender = new Thread(new Runnable() {
public void run() {
channel.send(new GenericMessage<String>(1, "test-1"));
try { Thread.sleep(5); } catch (Exception e) {}
channel.send(new GenericMessage<String>(2, "test-2"));
try { Thread.sleep(5); } catch (Exception e) {}
channel.send(new GenericMessage<String>(3, "test-3"));
}
});
sender.start();
latch.await();
assertNull(messageRef.get());
}
}

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.config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class ChannelParserTests {
@Test
public void testSimpleChannelWithDefaults() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
for (int i = 0; i < 10; i++) {
boolean result = channel.send(new GenericMessage<String>(1, "test"), 10);
assertTrue(result);
}
assertFalse(channel.send(new GenericMessage<String>(1, "test"), 3));
}
}

View File

@@ -0,0 +1,100 @@
/*
* 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class ComponentConfigurerTests {
@Test
public void testServiceActivator() {
GenericApplicationContext context = new GenericApplicationContext();
ComponentConfigurer cr = new ComponentConfigurer(context, null);
context.registerBeanDefinition("tester", new RootBeanDefinition(Tester.class));
context.registerBeanDefinition("out", new RootBeanDefinition(PointToPointChannel.class));
context.registerBeanDefinition("bus", new RootBeanDefinition(MessageBus.class));
String name = cr.serviceActivator(null, "out", "tester", "test");
context.refresh();
MessageEndpoint endpoint = (MessageEndpoint) context.getBean(name);
endpoint.messageReceived(new GenericMessage<String>(1, "world"));
MessageChannel channel = (MessageChannel) context.getBean("out");
assertEquals("hello world", channel.receive().getPayload());
}
@Test
public void testInboundChannelAdapter() {
GenericApplicationContext context = new GenericApplicationContext();
ComponentConfigurer cr = new ComponentConfigurer(context, null);
context.registerBeanDefinition("tester", new RootBeanDefinition(Tester.class));
String adapter = cr.inboundChannelAdapter("tester", "foo");
MessageChannel channel = (MessageChannel) context.getBean(adapter);
Message<String> message = channel.receive();
assertEquals("bar", message.getPayload());
}
@Test
public void testOutboundChannelAdapter() {
GenericApplicationContext context = new GenericApplicationContext();
ComponentConfigurer cr = new ComponentConfigurer(context, null);
context.registerBeanDefinition("tester", new RootBeanDefinition(Tester.class));
String adapter = cr.outboundChannelAdapter("tester", "store");
Tester tester = (Tester) context.getBean("tester");
assertNull(tester.getStoredValue());
MessageChannel channel = (MessageChannel) context.getBean(adapter);
boolean result = channel.send(new GenericMessage<String>(1, "foo"));
assertTrue(result);
assertEquals("foo", tester.getStoredValue());
}
public static class Tester {
private String storedValue;
public String test(String s) {
return "hello " + s;
}
public String foo() {
return "bar";
}
public void store(String s) {
this.storedValue = s;
}
public String getStoredValue() {
return this.storedValue;
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class EndpointParserTests {
@Test
public void testSimpleEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"genericEndpointTests.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
TestHandler handler = (TestHandler) context.getBean("testHandler");
assertNull(handler.getMessageString());
channel.send(new GenericMessage<String>(1, "test"));
handler.getLatch().await(50, TimeUnit.MILLISECONDS);
assertEquals("test", handler.getMessageString());
}
@Test
public void testHandlerAdapterEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAdapterEndpointTests.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
TestBean bean = (TestBean) context.getBean("testBean");
assertNull(bean.getMessage());
channel.send(new GenericMessage<String>(1, "test"));
bean.getLatch().await(50, TimeUnit.MILLISECONDS);
assertEquals("test", bean.getMessage());
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.config;
import java.util.concurrent.CountDownLatch;
/**
* @author Mark Fisher
*/
public class TestBean {
private String message;
private CountDownLatch latch;
public TestBean(int countdown) {
this.latch = new CountDownLatch(countdown);
}
public CountDownLatch getLatch() {
return this.latch;
}
public void store(String message) {
this.message = message;
latch.countDown();
}
public String getMessage() {
return this.message;
}
}

View File

@@ -0,0 +1,52 @@
/*
* 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.config;
import java.util.concurrent.CountDownLatch;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class TestHandler implements MessageHandler {
private String messageString;
private CountDownLatch latch;
public TestHandler(int countdown) {
this.latch = new CountDownLatch(countdown);
}
public Message handle(Message message) {
this.messageString = (String) message.getPayload();
this.latch.countDown();
return null;
}
public String getMessageString() {
return this.messageString;
}
public CountDownLatch getLatch() {
return this.latch;
}
}

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<channel id="testChannel" capacity="10"/>
</beans:beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<beans:bean class="org.springframework.integration.bus.MessageBus"/>
<channel id="testChannel" capacity="50"/>
<endpoint input-channel="testChannel" handler-ref="testHandler">
<consumer period="100"/>
</endpoint>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<beans:bean class="org.springframework.integration.bus.MessageBus"/>
<channel id="testChannel" capacity="50"/>
<endpoint input-channel="testChannel" handler-ref="testBean" handler-method="store">
<consumer period="100"/>
</endpoint>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
</beans:bean>
</beans:beans>

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,85 @@
/*
* 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 static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class GenericMessageEndpointTests {
@Test
public void testDefaultReplyChannel() throws Exception {
MessageChannel channel = new PointToPointChannel();
MessageChannel replyChannel = new PointToPointChannel();
MessageHandler handler = new MessageHandler() {
public Message<String> handle(Message message) {
return new StringMessage("123", "hello " + message.getPayload());
}
};
GenericMessageEndpoint endpoint = new GenericMessageEndpoint();
endpoint.setInputChannelName("testChannel");
endpoint.setHandler(handler);
endpoint.setDefaultOutputChannelName("replyChannel");
MessageBus bus = new MessageBus();
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
bus.registerChannel("replyChannel", replyChannel);
bus.start();
StringMessage testMessage = new StringMessage(1, "test");
channel.send(testMessage);
Message<String> reply = replyChannel.receive(10);
assertNotNull(reply);
assertEquals("hello test", reply.getPayload());
}
@Test
public void testExplicitReplyChannel() throws Exception {
MessageChannel channel = new PointToPointChannel();
final MessageChannel replyChannel = new PointToPointChannel();
MessageHandler handler = new MessageHandler() {
public Message handle(Message message) {
return new StringMessage("123", "hello " + message.getPayload());
}
};
GenericMessageEndpoint endpoint = new GenericMessageEndpoint();
endpoint.setInputChannelName("testChannel");
endpoint.setHandler(handler);
MessageBus bus = new MessageBus();
bus.registerChannel("testChannel", channel);
bus.registerEndpoint("testEndpoint", endpoint);
bus.registerChannel("replyChannel", replyChannel);
bus.start();
StringMessage testMessage = new StringMessage(1, "test");
testMessage.getHeader().setReplyChannelName("replyChannel");
channel.send(testMessage);
Message reply = replyChannel.receive(10);
assertNotNull(reply);
assertEquals("hello test", reply.getPayload());
}
}

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.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.afterPropertiesSet();
adapter.receive();
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithNoReturnValue() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("invalidMethodWithNoReturnValue");
adapter.afterPropertiesSet();
adapter.receive();
}
@Test(expected=MessageHandlingException.class)
public void testNoMatchingMethodName() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("noSuchMethod");
adapter.afterPropertiesSet();
adapter.receive();
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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.StringMessage;
/**
* @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 StringMessage(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 StringMessage(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 StringMessage(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.afterPropertiesSet();
adapter.send(new StringMessage(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,45 @@
<?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.endpoint.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.endpoint.TestSink"/>
<bean id="endpoint" class="org.springframework.integration.endpoint.GenericMessageEndpoint">
<property name="inputChannelName" value="inboundAdapter"/>
<property name="defaultOutputChannelName" value="outboundAdapter"/>
<property name="consumerPolicy">
<bean class="org.springframework.integration.bus.ConsumerPolicy">
<property name="receiveTimeout" value="100"/>
</bean>
</property>
</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>

View File

@@ -0,0 +1,35 @@
/*
* 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.annotation;
/**
* @author Mark Fisher
*/
@MessageEndpoint(defaultOutput="outputChannel")
public class InboundChannelAdapterTestBean {
@Polled(period=100)
public String getName() {
return "world";
}
@Handler
public String sayHello(String name) {
return "hello " + name;
}
}

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.annotation;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class MessageEndpointAnnotationPostProcessorTests {
@Test
public void testSimpleHandler() throws InterruptedException {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("simpleAnnotatedEndpointTests.xml", this.getClass());
context.start();
MessageChannel inputChannel = (MessageChannel) context.getBean("inputChannel");
MessageChannel outputChannel = (MessageChannel) context.getBean("outputChannel");
inputChannel.send(new GenericMessage<String>(1, "world"));
Message<String> message = outputChannel.receive();
assertEquals("hello world", message.getPayload());
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.annotation;
/**
* @author Mark Fisher
*/
@MessageEndpoint(input="inputChannel")
public class OutboundChannelAdapterTestBean {
@Handler
public String sayHello(String name) {
return "hello " + name;
}
@DefaultOutput
public void sendGreeting(String greeting) {
System.out.println(greeting);
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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.annotation;
/**
* @author Mark Fisher
*/
@MessageEndpoint(input="inputChannel", defaultOutput="outputChannel", pollPeriod=10)
public class SimpleAnnotatedEndpoint {
@Handler
public String sayHello(String name) {
return "hello " + name;
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:integration="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<integration:channel id="inputChannel"/>
<integration:channel id="outputChannel"/>
<bean id="endpoint" class="org.springframework.integration.endpoint.annotation.SimpleAnnotatedEndpoint"/>
<bean class="org.springframework.integration.endpoint.annotation.MessageEndpointAnnotationPostProcessor">
<property name="messageBus" ref="bus"/>
</bean>
</beans>

View File

@@ -0,0 +1,19 @@
<?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">
<property name="autoCreateChannels" value="true"/>
</bean>
<bean id="endpoint" class="org.springframework.integration.endpoint.annotation.SimpleAnnotatedEndpoint"/>
<bean id="outputChannel" class="org.springframework.integration.channel.PointToPointChannel"/>
<bean class="org.springframework.integration.endpoint.annotation.MessageEndpointAnnotationPostProcessor">
<property name="messageBus" ref="bus"/>
</bean>
</beans>

View File

@@ -0,0 +1,86 @@
/*
* 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.handler;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class MessageHandlerChainTests {
@Test
public void testSimpleChain() {
MessageHandlerChain chain = new MessageHandlerChain();
chain.add(new TestHandler("a"));
chain.add(new TestHandler("b"));
chain.add(new TestHandler("c"));
chain.add(new TestHandler("d"));
Message result = chain.handle(new StringMessage(1, "!"));
assertEquals("!abcd", result.getPayload());
}
@Test
public void testChainWithInterceptors() {
MessageHandler handler1 = new TestHandler("*");
MessageHandler handler2 = new TestInterceptingHandler("2", handler1);
MessageHandler handler3 = new TestInterceptingHandler("3", handler2);
MessageHandler handler4 = new TestInterceptingHandler("4", handler3);
MessageHandlerChain chain = new MessageHandlerChain();
chain.add(new TestHandler("a"));
chain.add(handler4);
chain.add(new TestHandler("b"));
Message result = chain.handle(new StringMessage(1, "!"));
assertEquals("234!a*234b", result.getPayload());
}
private static class TestHandler implements MessageHandler {
private String text;
TestHandler(String text) {
this.text = text;
}
public Message handle(Message message) {
return new StringMessage(1, message.getPayload() + text);
}
}
private static class TestInterceptingHandler extends InterceptingMessageHandler {
private String text;
TestInterceptingHandler(String text, MessageHandler target) {
super(target);
this.text = text;
}
public Message handle(Message message, MessageHandler target) {
message = target.handle(new StringMessage(1, text + message.getPayload()));
return new StringMessage(1, message.getPayload() + text);
}
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.util;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
/**
* @author Mark Fisher
*/
public class RandomGuidUidGeneratorTests {
@Test
public void testGeneratedIdIsNotNull() {
UidGenerator generator = new RandomGuidUidGenerator();
Object id = generator.generateUid();
assertNotNull(id);
}
}