INT-1263, added more changes and tests related valdating MessageHistory in every module

This commit is contained in:
Oleg Zhurakousky
2010-09-22 18:44:51 -04:00
parent dee91f6871
commit b6e058c59b
46 changed files with 367 additions and 114 deletions

View File

@@ -23,6 +23,10 @@ package org.springframework.integration;
*/
@SuppressWarnings("serial")
public class MessageDeliveryException extends MessagingException {
public MessageDeliveryException(String description) {
super(description);
}
public MessageDeliveryException(Message<?> undeliveredMessage) {
super(undeliveredMessage);

View File

@@ -21,7 +21,11 @@ package org.springframework.integration;
* @author Mark Fisher
*/
@SuppressWarnings("serial")
public class MessageTimeoutException extends MessageHandlingException {
public class MessageTimeoutException extends MessageDeliveryException {
public MessageTimeoutException(String description) {
super(description);
}
public MessageTimeoutException(Message<?> failedMessage, String description, Throwable cause) {
super(failedMessage, description, cause);
@@ -31,10 +35,6 @@ public class MessageTimeoutException extends MessageHandlingException {
super(failedMessage, description);
}
public MessageTimeoutException(Message<?> failedMessage, Throwable cause) {
super(failedMessage, cause);
}
public MessageTimeoutException(Message<?> failedMessage) {
super(failedMessage);
}

View File

@@ -17,16 +17,17 @@
package org.springframework.integration.transformer;
import org.springframework.integration.Message;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.support.MessageBuilder;
/**
* A base class for {@link Transformer} implementations.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public abstract class AbstractTransformer implements Transformer {
public abstract class AbstractTransformer extends IntegrationObjectSupport implements Transformer {
@SuppressWarnings("unchecked")
public final Message<?> transform(Message<?> message) {
try {
Object result = this.doTransform(message);

View File

@@ -19,11 +19,7 @@ package org.springframework.integration.transformer;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.util.Assert;
@@ -41,15 +37,11 @@ import org.springframework.validation.DataBinder;
* @author Oleg Zhurakousky
* @since 2.0
*/
public class MapToObjectTransformer extends AbstractPayloadTransformer<Map<?,?>, Object> implements BeanFactoryAware{
public class MapToObjectTransformer extends AbstractPayloadTransformer<Map<?,?>, Object>{
private final Class<?> targetClass;
private final String targetBeanName;
private volatile ConfigurableBeanFactory beanFactory;
/**
* @param targetClass
*/
@@ -73,25 +65,17 @@ public class MapToObjectTransformer extends AbstractPayloadTransformer<Map<?,?>,
protected Object transformPayload(Map<?,?> payload) throws Exception {
Object target = (this.targetClass != null)
? BeanUtils.instantiate(this.targetClass)
: this.beanFactory.getBean(this.targetBeanName);
: this.getBeanFactory().getBean(this.targetBeanName);
DataBinder binder = new DataBinder(target);
binder.setConversionService(this.beanFactory.getConversionService());
binder.setConversionService(((ConfigurableListableBeanFactory)this.getBeanFactory()).getConversionService());
binder.bind(new MutablePropertyValues(payload));
return target;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
*/
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory,
"A ConfigurableListableBeanFactory is required.");
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
protected void onInit(){
if (StringUtils.hasText(this.targetBeanName)) {
Assert.isTrue(this.beanFactory.isPrototype(this.targetBeanName),
Assert.isTrue(this.getBeanFactory().isPrototype(this.targetBeanName),
"target bean [" + targetBeanName + "] must have 'prototype' scope");
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.transformer;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.integration.Message;
import org.springframework.integration.context.NamedComponent;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.util.Assert;
@@ -48,7 +49,8 @@ public class MessageTransformingHandler extends AbstractReplyProducingMessageHan
@Override
public String getComponentType() {
return "transformer";
return (this.transformer instanceof NamedComponent) ?
((NamedComponent) this.transformer).getComponentType() : "transformer";
}
@Override

View File

@@ -2,8 +2,10 @@
<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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
@@ -11,9 +13,24 @@
default-request-channel="requestChannel"
default-reply-timeout="3000"
service-interface="org.springframework.integration.gateway.GatewayRequiresReplyTests$TestService" />
<service-activator input-channel="requestChannel"
expression="payload == 'foo' ? 'bar' : null"
requires-reply="true"/>
<gateway id="timeoutGateway"
default-request-channel="timeoutChannel"
default-reply-timeout="1000"
service-interface="org.springframework.integration.gateway.GatewayRequiresReplyTests$TestService" />
<channel id="timeoutChannel">
<dispatcher task-executor="executor"/>
</channel>
<service-activator input-channel="timeoutChannel">
<beans:bean class="org.springframework.integration.gateway.GatewayRequiresReplyTests.LongRunningService"/>
</service-activator>
<task:executor id="executor" pool-size="5"/>
</beans:beans>

View File

@@ -51,10 +51,24 @@ public class GatewayRequiresReplyTests {
TestService gateway = (TestService) applicationContext.getBean("gateway");
gateway.test("bad");
}
@Test
public void timedOutGateway() {
TestService gateway = (TestService) applicationContext.getBean("timeoutGateway");
String result = gateway.test("hello");
System.out.println("Result: " + result);
}
public static interface TestService {
public String test(String s);
}
public static class LongRunningService{
public String echo(String value) throws Exception{
Thread.sleep(5000);
return value;
}
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.integration.test.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Properties;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import org.hamcrest.Matcher;
@@ -41,15 +42,18 @@ import org.springframework.integration.context.NamedComponent;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.AbstractPollingEndpoint;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.util.Assert;
import org.springframework.util.ErrorHandler;
import org.springframework.util.StringUtils;
/**
* @author Mark Fisher
* @author Iwein Fuld
* @author Oleg Zhurakousky
*/
public abstract class TestUtils {
@@ -159,4 +163,19 @@ public abstract class TestUtils {
}
};
}
public static Properties locateComponentInHistory(MessageHistory history, String componentName, int startingIndex){
Assert.notNull(history, "'history' must not be null");
Assert.isTrue(StringUtils.hasText(componentName), "'componentName' must be provided");
Assert.isTrue(startingIndex < history.size(), "'startingIndex' can not be greater then size of history");
Properties component = null;
for (int i = startingIndex; i < history.size(); i++) {
Properties properties = history.get(i);
if (componentName.equals(properties.get("name"))){
component = properties;
break;
}
}
return component;
}
}

View File

@@ -64,25 +64,6 @@ public class MapToObjectTransformerTests {
assertNotNull(person.getAddress());
assertEquals("1123 Main st", person.getAddress().getStreet());
}
@SuppressWarnings("unchecked")
@Test(expected=IllegalArgumentException.class)
public void testMapToObjectTransformationNonPrototype(){
Map map = new HashMap();
map.put("fname", "Justin");
map.put("lname", "Case");
Address address = new Address();
address.setStreet("1123 Main st");
map.put("address", address);
Message message = MessageBuilder.withPayload(map).build();
GenericApplicationContext context = new GenericApplicationContext();
RootBeanDefinition personDef = new RootBeanDefinition(Person.class);
context.registerBeanDefinition("person", personDef);
MapToObjectTransformer transformer = new MapToObjectTransformer("person");
transformer.setBeanFactory(context.getBeanFactory());
transformer.transform(message);
}
@SuppressWarnings("unchecked")
@Test