Added tests for PollingSourceAdapter.
This commit is contained in:
@@ -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<String> adapter = new PollingSourceAdapter<String>(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<String> adapter = new PollingSourceAdapter<String>(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<String> adapter = new PollingSourceAdapter<String>(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<String> adapter = new PollingSourceAdapter<String>(source, channel, 1000);
|
||||
adapter.setLimit(2);
|
||||
adapter.receiveAndDispatch();
|
||||
}
|
||||
|
||||
|
||||
private static class TestSource implements PollableSource<String> {
|
||||
|
||||
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<String> poll(int limit) {
|
||||
List<String> results = new ArrayList<String>(this.messagesPerPoll);
|
||||
for (int i = 0; i < this.messagesPerPoll; i++) {
|
||||
results.add(message + "." + count.incrementAndGet());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user