IN PROGRESS - issue INT-642: Add DefaultAggregator

http://jira.springframework.org/browse/INT-642

Polished Alex' patch
This commit is contained in:
Iwein Fuld
2009-05-20 14:56:36 +00:00
parent 2b93373323
commit 399eee83b1
7 changed files with 297 additions and 48 deletions

View File

@@ -0,0 +1,59 @@
/*
* 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.aggregator;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.awt.Button;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.core.Message;
/**
* @author Alex Peters
*
*/
public class DefaultMessageAggregatorTests {
DefaultMessageAggregator aggregator;
@Before
public void setUp() {
aggregator = new DefaultMessageAggregator();
}
@SuppressWarnings("unchecked")
@Test
public void aggregateMessages_withMultiplePayloads_allAsListInResultMsg() {
List<Serializable> anyPayloads = Arrays.asList("foo", "bar", 123L, new Button());
List<Message<?>> messageGroup = new ArrayList<Message<?>>(anyPayloads.size());
for (Serializable payload : anyPayloads) {
Message<Serializable> mock = mock(Message.class);
when(mock.getPayload()).thenReturn(payload);
messageGroup.add(mock);
}
Message<?> result = aggregator.aggregateMessages(messageGroup);
assertThat((List<Serializable>) result.getPayload(), is(anyPayloads));
}
}

View File

@@ -6,22 +6,21 @@
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<annotation-config/>
<annotation-config />
<channel id="input">
<queue capacity="5"/>
<queue capacity="5" />
</channel>
<aggregator ref="summer" method="sum" input-channel="input" output-channel="output">
<aggregator ref="summer" method="sum" input-channel="input"
output-channel="output">
<poller task-executor="executor" max-messages-per-poll="5">
<interval-trigger interval="20"/>
<interval-trigger interval="20" />
</poller>
</aggregator>
<beans:bean id="executor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<beans:property name="corePoolSize" value="5" />
</beans:bean>
<thread-pool-task-executor id="executor"
core-size="5" />
<channel id="output">
<queue capacity="5" />

View File

@@ -16,7 +16,9 @@
package org.springframework.integration.aggregator.integration;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.HashMap;
import java.util.List;
@@ -24,9 +26,10 @@ import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.aggregator.AbstractMessageAggregator;
import org.springframework.integration.aggregator.MethodInvokingAggregator;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.core.MessageHeaders;
@@ -36,6 +39,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Iwein Fuld
* @author Alex Peters
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@@ -49,10 +53,12 @@ public class ConcurrentAggregatorIntegrationTests {
@Qualifier("output")
private PollableChannel output;
@Autowired
AbstractMessageAggregator aggregator;
@Test
public void configOk() throws Exception {
// nothing to assert
assertThat(aggregator, is(MethodInvokingAggregator.class));
}
@Test(timeout = 1000)
@@ -61,11 +67,10 @@ public class ConcurrentAggregatorIntegrationTests {
Map<String, Object> headers = stubHeaders(i, 5, 1);
input.send(new GenericMessage<Integer>(i, headers));
}
assertEquals(0+1+2+3+4, output.receive().getPayload());
assertEquals(0 + 1 + 2 + 3 + 4, output.receive().getPayload());
}
//configured in context associated with this test
// configured in context associated with this test
public static class SummingAggregator {
public Integer sum(List<Integer> numbers) {
int result = 0;
@@ -76,7 +81,6 @@ public class ConcurrentAggregatorIntegrationTests {
}
}
private Map<String, Object> stubHeaders(int sequenceNumber, int sequenceSize, int correllationId) {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(MessageHeaders.SEQUENCE_NUMBER, sequenceNumber);

View File

@@ -0,0 +1,29 @@
<?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">
<annotation-config />
<channel id="input">
<queue capacity="5" />
</channel>
<aggregator input-channel="input" output-channel="output">
<poller task-executor="executor" max-messages-per-poll="5">
<interval-trigger interval="20" />
</poller>
</aggregator>
<thread-pool-task-executor id="executor"
core-size="5" />
<channel id="output">
<queue capacity="5" />
</channel>
</beans:beans>

View File

@@ -0,0 +1,84 @@
/*
* 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.aggregator.integration;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.aggregator.AbstractMessageAggregator;
import org.springframework.integration.aggregator.DefaultMessageAggregator;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Alex Peters
* @author Iwein Fuld
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DefaultMessageAggregatorIntegrationTests {
@Autowired
@Qualifier("input")
MessageChannel input;
@Autowired
@Qualifier("output")
PollableChannel output;
@Autowired
AbstractMessageAggregator aggregator;
@Test
public void configOk() throws Exception {
assertThat(aggregator, is(DefaultMessageAggregator.class));
}
@SuppressWarnings("unchecked")
@Test(timeout = 1000)
public void aggregate() throws Exception {
for (int i = 0; i < 5; i++) {
Map<String, Object> headers = stubHeaders(i, 5, 1);
input.send(new GenericMessage<Integer>(i, headers));
}
Object payload = output.receive().getPayload();
assertThat(payload, is(List.class));
assertThat((List) payload, is(Arrays.asList(0, 1, 2, 3, 4)));
}
Map<String, Object> stubHeaders(int sequenceNumber, int sequenceSize, int correllationId) {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(MessageHeaders.SEQUENCE_NUMBER, sequenceNumber);
headers.put(MessageHeaders.SEQUENCE_SIZE, sequenceSize);
headers.put(MessageHeaders.CORRELATION_ID, correllationId);
return headers;
}
}