INT-1505 added support for a 'payload-expression' attribute on the inbound ApplicationEvent Channel Adapter

This commit is contained in:
Mark Fisher
2010-10-08 14:42:11 -04:00
parent 7cfd0f4a55
commit b3c79358e1
7 changed files with 87 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -53,8 +53,8 @@ public class ApplicationEventInboundChannelAdapterTests {
assertEquals("event2", ((ApplicationEvent) message3.getPayload()).getSource());
}
@SuppressWarnings("unchecked")
@Test
@SuppressWarnings("unchecked")
public void onlyConfiguredEventTypesAreSent() {
QueueChannel channel = new QueueChannel();
ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter();
@@ -93,6 +93,24 @@ public class ApplicationEventInboundChannelAdapterTests {
assertEquals(ContextClosedEvent.class, closedEventMessage.getPayload().getClass());
}
@Test
public void payloadExpressionEvaluatedAgainstApplicationEvent() {
QueueChannel channel = new QueueChannel();
ApplicationEventInboundChannelAdapter adapter = new ApplicationEventInboundChannelAdapter();
adapter.setPayloadExpression("'received: ' + source");
adapter.setOutputChannel(channel);
Message<?> message1 = channel.receive(0);
assertNull(message1);
adapter.onApplicationEvent(new TestApplicationEvent1());
adapter.onApplicationEvent(new TestApplicationEvent2());
Message<?> message2 = channel.receive(20);
assertNotNull(message2);
assertEquals("received: event1", message2.getPayload());
Message<?> message3 = channel.receive(20);
assertNotNull(message3);
assertEquals("received: event2", message3.getPayload());
}
@SuppressWarnings("serial")
private static class TestApplicationEvent1 extends ApplicationEvent {
@@ -100,6 +118,8 @@ public class ApplicationEventInboundChannelAdapterTests {
public TestApplicationEvent1() {
super("event1");
}
}

View File

@@ -30,7 +30,13 @@
<int:channel id="inputFilteredPlaceHolder">
<int:queue/>
</int:channel>
<int-event:inbound-channel-adapter id="eventAdapterSpel" channel="inputSpel" payload-expression="source + '-test'"/>
<int:channel id="inputSpel">
<int:queue/>
</int:channel>
<context:property-placeholder location="classpath:org/springframework/integration/event/config/inbound-adapter.properties"/>
</beans>

View File

@@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.expression.Expression;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.event.ApplicationEventInboundChannelAdapter;
@@ -62,9 +63,9 @@ public class EventInboundChannelAdapterParserTests {
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
Assert.assertEquals(context.getBean("input"), adapterAccessor.getPropertyValue("outputChannel"));
}
@SuppressWarnings("unchecked")
@Test
@SuppressWarnings("unchecked")
public void validateEventParserWithEventTypes() {
Object adapter = context.getBean("eventAdapterFiltered");
Assert.assertNotNull(adapter);
@@ -77,9 +78,9 @@ public class EventInboundChannelAdapterParserTests {
assertTrue(eventTypes.contains(SampleEvent.class));
assertTrue(eventTypes.contains(AnotherSampleEvent.class));
}
@SuppressWarnings("unchecked")
@Test
@SuppressWarnings("unchecked")
public void validateEventParserWithEventTypesAndPlaceholder() {
Object adapter = context.getBean("eventAdapterFilteredPlaceHolder");
Assert.assertNotNull(adapter);
@@ -108,6 +109,16 @@ public class EventInboundChannelAdapterParserTests {
assertEquals(SampleEvent.class, message.getPayload().getClass());
}
@Test
public void validatePayloadExpression() {
Object adapter = context.getBean("eventAdapterSpel");
Assert.assertNotNull(adapter);
Assert.assertTrue(adapter instanceof ApplicationEventInboundChannelAdapter);
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
Expression expression = (Expression) adapterAccessor.getPropertyValue("payloadExpression");
Assert.assertEquals("source + '-test'", expression.getExpressionString());
}
@SuppressWarnings("serial")
public static class SampleEvent extends ApplicationEvent {