From 9466893b7b261b56deb3d722b083da426d400e43 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 28 Dec 2007 15:04:04 +0000 Subject: [PATCH] Added tests for PollingSourceAdapter. --- .../adapter/PollingSourceAdapterTests.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/adapter/PollingSourceAdapterTests.java diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/PollingSourceAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/PollingSourceAdapterTests.java new file mode 100644 index 0000000000..0e1a18f605 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/PollingSourceAdapterTests.java @@ -0,0 +1,121 @@ +/* + * 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.adapter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Test; + +import org.springframework.integration.MessageHandlingException; +import org.springframework.integration.channel.PointToPointChannel; +import org.springframework.integration.message.Message; + +/** + * @author Mark Fisher + */ +public class PollingSourceAdapterTests { + + @Test + public void testPolledSourceSendsToChannel() { + TestSource source = new TestSource("testing", 1); + PointToPointChannel channel = new PointToPointChannel(); + PollingSourceAdapter adapter = new PollingSourceAdapter(source, channel, 100); + adapter.receiveAndDispatch(); + Message message = channel.receive(); + assertNotNull("message should not be null", message); + assertEquals("testing.1", message.getPayload()); + } + + @Test + public void testSendTimeout() { + TestSource source = new TestSource("testing", 1); + PointToPointChannel channel = new PointToPointChannel(1); + PollingSourceAdapter adapter = new PollingSourceAdapter(source, channel, 500); + adapter.setSendTimeout(10); + adapter.receiveAndDispatch(); + adapter.receiveAndDispatch(); + Message message1 = channel.receive(); + assertNotNull("message should not be null", message1); + assertEquals("testing.1", message1.getPayload()); + Message message2 = channel.receive(0); + assertNull("second message should be null", message2); + adapter.receiveAndDispatch(); + Message message3 = channel.receive(0); + assertNotNull("third message should not be null", message3); + assertEquals("testing.3", message3.getPayload()); + } + + @Test + public void testMultipleMessagesPerPoll() { + TestSource source = new TestSource("testing", 3); + PointToPointChannel channel = new PointToPointChannel(); + PollingSourceAdapter adapter = new PollingSourceAdapter(source, channel, 1000); + adapter.setLimit(5); + adapter.receiveAndDispatch(); + Message message1 = channel.receive(0); + assertNotNull("message should not be null", message1); + assertEquals("testing.1", message1.getPayload()); + Message message2 = channel.receive(0); + assertNotNull("message should not be null", message2); + assertEquals("testing.2", message2.getPayload()); + Message message3 = channel.receive(0); + assertNotNull("message should not be null", message3); + assertEquals("testing.3", message3.getPayload()); + Message message4 = channel.receive(0); + assertNull("message should be null", message4); + } + + @Test(expected=MessageHandlingException.class) + public void testResultSizeExceedsLimit() { + TestSource source = new TestSource("testing", 3); + PointToPointChannel channel = new PointToPointChannel(); + PollingSourceAdapter adapter = new PollingSourceAdapter(source, channel, 1000); + adapter.setLimit(2); + adapter.receiveAndDispatch(); + } + + + private static class TestSource implements PollableSource { + + private String message; + + private int messagesPerPoll; + + private AtomicInteger count = new AtomicInteger(); + + public TestSource(String message, int messagesPerPoll) { + this.message = message; + this.messagesPerPoll = messagesPerPoll; + } + + public Collection poll(int limit) { + List results = new ArrayList(this.messagesPerPoll); + for (int i = 0; i < this.messagesPerPoll; i++) { + results.add(message + "." + count.incrementAndGet()); + } + return results; + } + } + +}