INT-1515 initial round of refactoring to turn validating-router into validating-filter

This commit is contained in:
Oleg Zhurakousky
2010-10-14 05:56:19 -04:00
parent f5fe76d715
commit a389fa9795
10 changed files with 223 additions and 345 deletions

View File

@@ -0,0 +1,24 @@
<?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"
xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
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-2.0.xsd
http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml-2.0.xsd">
<int-xml:validating-filter id="filter"
input-channel="inputChannel"
output-channel="validOutputChannel"
discard-channel="invalidOutputChannel"
schema-location="org/springframework/integration/xml/config/validationTestsSchema.xsd"/>
<int:channel id="validOutputChannel">
<int:queue/>
</int:channel>
<int:channel id="invalidOutputChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,62 @@
/*
* 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.
* 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.xml.config;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.w3c.dom.Document;
/**
* @author Jonas Partner
* @author Oleg Zhurakousky
*/
@ContextConfiguration
public class XmlPayloadValidatingFilterParserTests {
@Test
public void testValidMessage() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("XmlPayloadValidatingFilterParserTests-context.xml", this.getClass());
Document doc = XmlTestUtil.getDocumentForString("<greeting>hello</greeting>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
PollableChannel validChannel = ac.getBean("validOutputChannel", PollableChannel.class);
MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
inputChannel.send(docMessage);
assertNotNull(validChannel.receive(100));
}
@Test
public void testInvalidMessage() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("XmlPayloadValidatingFilterParserTests-context.xml", this.getClass());
Document doc = XmlTestUtil.getDocumentForString("<greeting><other/></greeting>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
PollableChannel validChannel = ac.getBean("validOutputChannel", PollableChannel.class);
PollableChannel invalidChannel = ac.getBean("invalidOutputChannel", PollableChannel.class);
MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
inputChannel.send(docMessage);
assertNotNull(invalidChannel.receive(100));
assertNull(validChannel.receive(100));
}
}

View File

@@ -1,88 +0,0 @@
/*
* 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.
* 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.xml.config;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.w3c.dom.Document;
/**
* @author Jonas Partner
*/
@ContextConfiguration
public class XmlPayloadValidatingRouterParserTests {
String channelConfig = "<si:channel id='test-input'/> <si:channel id='validOutputChannel'><si:queue capacity='10'/></si:channel> <si:channel id='invalidOutputChannel'><si:queue capacity='10'/></si:channel>";
@Autowired @Qualifier("test-input")
MessageChannel inputChannel;
@Autowired @Qualifier("validOutputChannel")
QueueChannel validOutputChannel;
@Autowired @Qualifier("invalidOutputChannel")
QueueChannel invalidOutputChannel;
ConfigurableApplicationContext appContext;
public EventDrivenConsumer buildContext(String routerDef){
appContext = TestXmlApplicationContextHelper.getTestAppContext( channelConfig + routerDef);
appContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
EventDrivenConsumer consumer = (EventDrivenConsumer) appContext.getBean("router");
consumer.start();
return consumer;
}
@After
public void tearDown(){
if(appContext != null){
appContext.close();
}
}
@Test
public void testValidMessage() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<greeting>hello</greeting>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
buildContext("<si-xml:validating-router id='router' input-channel='test-input' valid-channel='validOutputChannel' invalid-channel='invalidOutputChannel' schema-location='org/springframework/integration/xml/config/validationTestsSchema.xsd' />");
inputChannel.send(docMessage);
assertEquals("Wrong number of messages", 1, validOutputChannel.getQueueSize());
}
@Test
public void testInvalidMessage() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<greeting><other/></greeting>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
buildContext("<si-xml:validating-router id='router' input-channel='test-input' valid-channel='validOutputChannel' invalid-channel='invalidOutputChannel' schema-location='org/springframework/integration/xml/config/validationTestsSchema.xsd' />");
inputChannel.send(docMessage);
assertEquals("Wrong number of messages", 1, invalidOutputChannel.getQueueSize());
}
}

View File

@@ -1,81 +0,0 @@
package org.springframework.integration.xml.router;
import static org.junit.Assert.*;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import org.junit.Before;
/*
* 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.
*/
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.support.MessageBuilder;
public class XmlPayloadValidatingRouterTests {
String validChannelName = "VALID";
String invalidChannelName = "INVALID";
Source testSource;
Message<Source> testMessage;
@Before
public void setUp(){
testSource = new SAXSource();
testMessage = MessageBuilder.withPayload(testSource).build();
}
@Test
public void testValidMessage(){
StubValidator validator = new StubValidator(true);
XmlPayloadValidatingRouter router = new XmlPayloadValidatingRouter(validChannelName, invalidChannelName, validator);
String returnedChannelName = router.determineTargetChannelName(testMessage);
assertEquals("Wrong channel name", validChannelName, returnedChannelName);
assertEquals("Source not passed to validator ", testSource, validator.passedIn);
}
@Test
public void testInvalidMessage(){
StubValidator validator = new StubValidator(false);
XmlPayloadValidatingRouter router = new XmlPayloadValidatingRouter(validChannelName, invalidChannelName, validator);
String returnedChannelName = router.determineTargetChannelName(testMessage);
assertEquals("Wrong channel name", invalidChannelName, returnedChannelName);
assertEquals("Source not passed to validator ", testSource, validator.passedIn);
}
static class StubValidator implements XmlValidator {
private final boolean validationResult;
Source passedIn;
public StubValidator(boolean validationResult) {
this.validationResult = validationResult;
}
public boolean isValid(Source source) {
passedIn = source;
return validationResult;
}
}
}