public

Intercepting Channels in Tests

  • Download Gist
HeaderExpectationsTests-context.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
<int:channel id="input" />
<int:header-enricher input-channel="input" output-channel="next">
<int:header name="bar" value="baz"/>
</int:header-enricher>
 
<int:channel id="next" />
 
<int:transformer input-channel="next" output-channel="end" expression="'qux'" />
<int:channel id="end">
<int:queue/>
</int:channel>
 
</beans>
HeaderExpectationsTests.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*
* Copyright 2002-2013 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;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
 
import java.util.concurrent.atomic.AtomicReference;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
/**
* @author Gary Russell
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class HeaderExpectationsTests {
 
@Autowired
private MessageChannel input;
 
@Autowired
private AbstractMessageChannel next;
 
@Autowired
private PollableChannel end;
 
@Test
public void test() {
final AtomicReference<Message<?>> interceptedMessage = new AtomicReference<Message<?>>();
 
// Intercept the channel where we want to test the header(s)
this.next.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
interceptedMessage.set(message);
return super.preSend(message, channel);
}
});
 
input.send(new GenericMessage<String>("foo"));
Message<?> out = end.receive(1000);
assertNotNull(out);
assertEquals("qux", out.getPayload());
assertNotNull(interceptedMessage.get());
assertEquals("baz", interceptedMessage.get().getHeaders().get("bar"));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.