Added namespace support for the WireTap (INT-323).

This commit is contained in:
Mark Fisher
2008-08-12 22:49:27 +00:00
parent 1e8fa31e9d
commit 744db9e5a1
9 changed files with 333 additions and 47 deletions

View File

@@ -20,9 +20,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.channel.QueueChannel;
@@ -38,7 +35,7 @@ import org.springframework.integration.message.selector.MessageSelector;
public class WireTapTests {
@Test
public void wireTapWithNoSelectors() {
public void wireTapWithNoSelector() {
QueueChannel mainChannel = new QueueChannel();
QueueChannel secondaryChannel = new QueueChannel();
mainChannel.addInterceptor(new WireTap(secondaryChannel));
@@ -54,10 +51,7 @@ public class WireTapTests {
public void wireTapWithRejectingSelector() {
QueueChannel mainChannel = new QueueChannel();
QueueChannel secondaryChannel = new QueueChannel();
List<MessageSelector> selectors = new ArrayList<MessageSelector>();
selectors.add(new TestSelector(true));
selectors.add(new TestSelector(false));
mainChannel.addInterceptor(new WireTap(secondaryChannel, selectors));
mainChannel.addInterceptor(new WireTap(secondaryChannel, new TestSelector(false)));
mainChannel.send(new StringMessage("testing"));
Message<?> original = mainChannel.receive(0);
assertNotNull(original);
@@ -66,13 +60,10 @@ public class WireTapTests {
}
@Test
public void wireTapWithAcceptingSelectors() {
public void wireTapWithAcceptingSelector() {
QueueChannel mainChannel = new QueueChannel();
QueueChannel secondaryChannel = new QueueChannel();
List<MessageSelector> selectors = new ArrayList<MessageSelector>();
selectors.add(new TestSelector(true));
selectors.add(new TestSelector(true));
mainChannel.addInterceptor(new WireTap(secondaryChannel, selectors));
mainChannel.addInterceptor(new WireTap(secondaryChannel, new TestSelector(true)));
mainChannel.send(new StringMessage("testing"));
Message<?> original = mainChannel.receive(0);
assertNotNull(original);

View File

@@ -0,0 +1,37 @@
/*
* 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.config;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
/**
* @author Mark Fisher
*/
public class TestSelector implements MessageSelector {
private final boolean accept;
public TestSelector(boolean accept) {
this.accept = accept;
}
public boolean accept(Message<?> message) {
return this.accept;
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.Map;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.interceptor.WireTap;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class WireTapParserTests {
@Test
public void simpleWireTap() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"wireTapParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("simple");
WireTapTestTarget target = (WireTapTestTarget) context.getBean("testTarget");
assertNull(target.getLastMessage());
Message<?> original = new StringMessage("test");
channel.send(original);
Message<?> intercepted = target.getLastMessage();
assertNotNull(intercepted);
assertEquals(original, intercepted);
}
@Test
public void wireTapWithAcceptingSelector() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"wireTapParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("accepting");
WireTapTestTarget target = (WireTapTestTarget) context.getBean("testTarget");
assertNull(target.getLastMessage());
Message<?> original = new StringMessage("test");
channel.send(original);
Message<?> intercepted = target.getLastMessage();
assertNotNull(intercepted);
assertEquals(original, intercepted);
}
@Test
public void wireTapWithRejectingSelector() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"wireTapParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("rejecting");
WireTapTestTarget target = (WireTapTestTarget) context.getBean("testTarget");
assertNull(target.getLastMessage());
Message<?> original = new StringMessage("test");
channel.send(original);
Message<?> intercepted = target.getLastMessage();
assertNull(intercepted);
}
@Test
@SuppressWarnings("unchecked")
public void wireTapTimeouts() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"wireTapParserTests.xml", this.getClass());
Map<String, WireTap> beans = (Map<String, WireTap>) context.getBeansOfType(WireTap.class);
int defaultTimeoutCount = 0;
int expectedTimeoutCount = 0;
int otherTimeoutCount = 0;
for (WireTap wireTap : beans.values()) {
long timeout = ((Long) new DirectFieldAccessor(wireTap).getPropertyValue("timeout")).longValue();
if (timeout == 0) {
defaultTimeoutCount++;
}
else if (timeout == 1234) {
expectedTimeoutCount++;
}
else {
otherTimeoutCount++;
}
}
assertEquals(3, defaultTimeoutCount);
assertEquals(1, expectedTimeoutCount);
assertEquals(0, otherTimeoutCount);
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.config;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageTarget;
/**
* @author Mark Fisher
*/
public class WireTapTestTarget implements MessageTarget {
private volatile Message<?> lastMessage;
public Message<?> getLastMessage() {
return this.lastMessage;
}
public boolean send(Message<?> message) {
this.lastMessage= message;
return true;
}
}

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<channel id="simple">
<interceptors>
<wire-tap target="testTarget"/>
</interceptors>
</channel>
<channel id="accepting">
<interceptors>
<wire-tap target="testTarget" selector="acceptingSelector"/>
</interceptors>
</channel>
<channel id="rejecting">
<interceptors>
<wire-tap target="testTarget" selector="rejectingSelector"/>
</interceptors>
</channel>
<channel id="timeout">
<interceptors>
<wire-tap target="testTarget" timeout="1234"/>
</interceptors>
</channel>
<beans:bean id="testTarget" class="org.springframework.integration.config.WireTapTestTarget"/>
<beans:bean id="acceptingSelector" class="org.springframework.integration.config.TestSelector">
<beans:constructor-arg value="true"/>
</beans:bean>
<beans:bean id="rejectingSelector" class="org.springframework.integration.config.TestSelector">
<beans:constructor-arg value="false"/>
</beans:bean>
</beans:beans>