This commit is contained in:
Oleg Zhurakousky
2010-03-27 00:48:14 +00:00
parent c5b1bc46a9
commit 7381ee28cf
10 changed files with 754 additions and 1 deletions

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel id="inputA">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="eight"/>
<ref bean="channelInterceptor"/>
</int:interceptors>
</int:channel>
<int:channel id="inputB">
<int:queue capacity="1"/>
</int:channel>
<int:publish-subscribe-channel id="foo"/>
<int:channel id="bar">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="eight"/>
<ref bean="channelInterceptor"/>
</int:interceptors>
</int:channel>
<int:channel id="baz"/>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, fooA" order="1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="one"/>
<ref bean="channelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="two"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="three"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="-1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="four"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="-5">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="five"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<bean id="channelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="seven"/>
<!-- <bean id="object" class="java.lang.Object"/>-->
</beans>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
</int:channel-interceptor-chain>
<bean id="object" class="java.lang.Object"/>
</beans>

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel id="inputA">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleOrderedInterceptor"
p:testIdentifier="eight"
p:order="5"/>
<ref bean="unorderedChannelInterceptor"/>
<ref bean="orderedChannelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="five"/>
</int:interceptors>
</int:channel>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
<ref bean="unorderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*" order="1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="one"/>
<ref bean="orderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="-1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleOrderedInterceptor"
p:testIdentifier="four"
p:order="7"/>
<ref bean="orderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<bean id="unorderedChannelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="seven"/>
<bean id="orderedChannelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleOrderedInterceptor"
p:testIdentifier="ten"
p:order="4"/>
</beans>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel id="inputA">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="eight"/>
<ref bean="unorderedChannelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="five"/>
</int:interceptors>
</int:channel>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="six"/>
<ref bean="unorderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*">
<ref bean="unorderedChannelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="one"/>
</int:channel-interceptor-chain>
<bean id="unorderedChannelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="seven"/>
</beans>

View File

@@ -0,0 +1,209 @@
/*
* 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.channel.interceptor;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.channel.ChannelInterceptor;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class GlobalChannelInterceptorTests {
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptor(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel);
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 13);
Assert.assertEquals("four", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("seven", inter[3].getTestIdentifier());
Assert.assertEquals("eight", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("one", inter[6].getTestIdentifier());
Assert.assertEquals("seven", inter[7].getTestIdentifier());
Assert.assertEquals("two", inter[8].getTestIdentifier());
Assert.assertEquals("three", inter[9].getTestIdentifier());
Assert.assertEquals("seven", inter[10].getTestIdentifier());
Assert.assertEquals("six", inter[11].getTestIdentifier());
Assert.assertEquals("seven", inter[12].getTestIdentifier());
}
else
if (channelName.equals("inputB")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 11);
Assert.assertEquals("four", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("seven", inter[3].getTestIdentifier());
Assert.assertEquals("one", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("two", inter[6].getTestIdentifier());
Assert.assertEquals("three", inter[7].getTestIdentifier());
Assert.assertEquals("seven", inter[8].getTestIdentifier());
Assert.assertEquals("six", inter[9].getTestIdentifier());
Assert.assertEquals("seven", inter[10].getTestIdentifier());
}
else
if (channelName.equals("foo")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 6);
Assert.assertEquals("four", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("seven", inter[3].getTestIdentifier());
Assert.assertEquals("six", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
}
else
if (channelName.equals("bar")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 2);
Assert.assertEquals("eight", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
}
else
if (channelName.equals("baz")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 0);
}
}
}
/**
* Will test mix of Ordered and un-Ordered ChannelInterceptors
* Individual interceptors will only be sorted within groups they are defined.
* For example: interceptors defined inside of channels will be sorted according to Ordered implementation
* If global interceptors were added BEFORE (negative order) or AFTER (ppositive order) the global stack will be sorted
* and added before/after the existing stack
*/
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptorsOrdered(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-ordered-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel);
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 10);
Assert.assertEquals("ten", inter[0].getTestIdentifier());
Assert.assertEquals("four", inter[1].getTestIdentifier());
Assert.assertEquals("ten", inter[2].getTestIdentifier());
Assert.assertEquals("eight", inter[3].getTestIdentifier());
Assert.assertEquals("seven", inter[4].getTestIdentifier());
Assert.assertEquals("five", inter[5].getTestIdentifier());
Assert.assertEquals("ten", inter[6].getTestIdentifier());
Assert.assertEquals("one", inter[7].getTestIdentifier());
Assert.assertEquals("six", inter[8].getTestIdentifier());
Assert.assertEquals("seven", inter[9].getTestIdentifier());
}
}
}
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptorsUnOrdered(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-unordered-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel);
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 7);
Assert.assertEquals("eight", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("six", inter[3].getTestIdentifier());
Assert.assertEquals("seven", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("one", inter[6].getTestIdentifier());
}
}
}
/**
* Will test failure if 'channel-name-pattern' filter points to a valid
* bean which is not an AbstractMessageChannel
*/
@Test(expected=BeanCreationException.class)
public void failGlobalInterceptorConfig(){
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-failed-context.xml", GlobalChannelInterceptorTests.class);
}
public static class SampleInterceptor implements ChannelInterceptor {
private String testIdentifier;
public String getTestIdentifier() {
return testIdentifier;
}
public void setTestIdentifier(String testIdentifier) {
this.testIdentifier = testIdentifier;
}
public Message<?> postReceive(Message<?> message, MessageChannel channel) {
return null;
}
public void postSend(Message<?> message, MessageChannel channel,
boolean sent) {
}
public boolean preReceive(MessageChannel channel) {
return false;
}
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return null;
}
}
public static class SampleOrderedInterceptor extends SampleInterceptor implements Ordered {
private int order;
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
}