Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Last active December 16, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garyrussell/5375131 to your computer and use it in GitHub Desktop.
Save garyrussell/5375131 to your computer and use it in GitHub Desktop.
Intercepting Channels in Tests
<?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>
/*
* 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"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment