From d65d6d7c49ed404f41bbb18958ea5647d6c40687 Mon Sep 17 00:00:00 2001 From: Jonas Partner Date: Mon, 16 Feb 2009 13:13:55 +0000 Subject: [PATCH] INT-558 XmlPayloadValidatingRouter --- .../xml/DefaultXmlPayloadConverter.java | 20 +++- .../integration/xml/XmlPayloadConverter.java | 4 + .../IntegrationXmlNamespaceHandler.java | 1 + .../XmlPayloadValidatingRouterParser.java | 103 ++++++++++++++++++ .../xml/config/spring-integration-xml-1.0.xsd | 43 ++++++++ .../xml/router/SchemaValidator.java | 52 +++++++++ .../router/XmlPayloadValidatingRouter.java | 61 +++++++++++ .../integration/xml/router/XmlValidator.java | 25 +++++ .../xml/DefaultXmlPayloadConverterTests.java | 31 ++++++ ...XmlPayloadValidatingRouterParserTests.java | 91 ++++++++++++++++ .../xml/config/validationTestsSchema.xsd | 6 + .../xml/router/SchemaValidatorTests.java | 57 ++++++++++ .../XmlPayloadValidatingRouterTests.java | 81 ++++++++++++++ .../xml/router/validationTestsSchema.xsd | 6 + 14 files changed, 579 insertions(+), 2 deletions(-) create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParser.java create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/SchemaValidator.java create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouter.java create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlValidator.java create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParserTests.java create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/validationTestsSchema.xsd create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/SchemaValidatorTests.java create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouterTests.java create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/validationTestsSchema.xsd diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java index ebaa35377e..8044d5ea0d 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java @@ -21,13 +21,15 @@ import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import org.springframework.integration.core.MessagingException; +import org.springframework.xml.transform.StringSource; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; -import org.springframework.integration.core.MessagingException; - /** * Default implementation of {@link XmlPayloadConverter}. * Supports {@link Document} and {@link String}. @@ -70,6 +72,20 @@ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { } return convertToDocument(object); } + + public Source convertToSource(Object object){ + Source source; + if(object instanceof Source){ + source = (Source)object; + } else if (object instanceof Document){ + source = new DOMSource((Document)object); + } else if (object instanceof String){ + source = new StringSource((String)object); + } else { + throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); + } + return source; + } protected synchronized DocumentBuilder getDocumentBuilder() { try { diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java index 695cdc6a0d..d9c2cd74b1 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java @@ -16,6 +16,8 @@ package org.springframework.integration.xml; +import javax.xml.transform.Source; + import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -29,5 +31,7 @@ public interface XmlPayloadConverter { public Document convertToDocument(Object object); public Node convertToNode(Object object); + + public Source convertToSource(Object object); } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java index db4be6038d..7157e9228e 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java @@ -31,6 +31,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport { registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser()); registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser()); registerBeanDefinitionParser("xpath-splitter", new XPathMessageSplitterParser()); + registerBeanDefinitionParser("validating-router", new XmlPayloadValidatingRouterParser()); } } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParser.java new file mode 100644 index 0000000000..b27bd57803 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParser.java @@ -0,0 +1,103 @@ +/* + * 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.xml.config; + +import javax.xml.XMLConstants; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.AbstractConsumerEndpointParser; +import org.springframework.integration.xml.router.SchemaValidator; +import org.springframework.integration.xml.router.XmlPayloadValidatingRouter; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * @author Jonas Partner + */ +public class XmlPayloadValidatingRouterParser extends + AbstractConsumerEndpointParser { + + private XPathExpressionParser xpathParser = new XPathExpressionParser(); + + @Override + protected boolean shouldGenerateId() { + return false; + } + + @Override + protected boolean shouldGenerateIdAsFallback() { + return true; + } + + @Override + protected BeanDefinitionBuilder parseHandler(Element element, + ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder + .genericBeanDefinition(); + builder.getBeanDefinition().setBeanClass( + XmlPayloadValidatingRouter.class); + String channelResolver = element.getAttribute("channel-resolver"); + + String validChannelName = element.getAttribute("valid-channel"); + String invalidChannelName = element.getAttribute("invalid-channel"); + String schemaType = element.getAttribute("schema-type"); + String schemaLocation = element.getAttribute("schema-location"); + + Assert.state(schemaType.equals("xml-schema") + || schemaType.equals("relax-ng"), "Unrecognised schema type " + + schemaType); + + + Assert.state(StringUtils.hasText(invalidChannelName) + && StringUtils.hasText(validChannelName), + "valid-channel and invalid-channel must both be specified"); + + builder.addConstructorArgValue(validChannelName); + builder.addConstructorArgValue(invalidChannelName); + + + + BeanDefinition validatorBeanDefinition; + if (schemaType.equals("xml-schema")) { + validatorBeanDefinition = createValidator(XMLConstants.W3C_XML_SCHEMA_NS_URI, schemaLocation); + } else { + validatorBeanDefinition = createValidator(XMLConstants.RELAXNG_NS_URI, schemaLocation); + } + builder.addConstructorArgValue(validatorBeanDefinition); + + + if (StringUtils.hasText(channelResolver)) { + builder.addPropertyReference("channelResolver", channelResolver); + } + + return builder; + } + + protected BeanDefinition createValidator(String schemaType, String schemaLocation){ + BeanDefinitionBuilder xmlValidator = BeanDefinitionBuilder + .genericBeanDefinition(); + xmlValidator.getBeanDefinition().setBeanClass(SchemaValidator.class); + xmlValidator.addConstructorArgValue(schemaLocation); + xmlValidator.addConstructorArgValue(schemaType); + + return xmlValidator.getBeanDefinition(); + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd index 1942c3eb87..9bc81e7998 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd @@ -277,6 +277,49 @@ + + + + + + Defines an validating router. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/SchemaValidator.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/SchemaValidator.java new file mode 100644 index 0000000000..81d2272d9d --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/SchemaValidator.java @@ -0,0 +1,52 @@ +/* + * 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.xml.router; + +import java.io.IOException; + +import javax.xml.transform.Source; + +import org.springframework.core.io.Resource; +import org.springframework.integration.core.MessagingException; +import org.springframework.xml.validation.XmlValidationException; +import org.springframework.xml.validation.XmlValidatorFactory; +import org.xml.sax.SAXParseException; + +public class SchemaValidator implements XmlValidator { + + private final org.springframework.xml.validation.XmlValidator xmlValidator; + + public SchemaValidator(Resource schemaResource, String schemaLanguage) + throws IOException { + super(); + this.xmlValidator = XmlValidatorFactory.createValidator(schemaResource, + schemaLanguage); + } + + public boolean isValid(Source source) { + try { + SAXParseException[] exceptions = xmlValidator.validate(source); + return exceptions.length < 1; + } catch (IOException ioE) { + throw new MessagingException( + "Exception applying schema validation", ioE); + } catch (XmlValidationException validationException){ + return false; + } + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouter.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouter.java new file mode 100644 index 0000000000..e64b127f70 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouter.java @@ -0,0 +1,61 @@ +/* + * 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.xml.router; + +import org.springframework.integration.core.Message; +import org.springframework.integration.router.AbstractSingleChannelNameRouter; +import org.springframework.integration.xml.DefaultXmlPayloadConverter; +import org.springframework.integration.xml.XmlPayloadConverter; + +public class XmlPayloadValidatingRouter extends AbstractSingleChannelNameRouter{ + + private final String validMessageChannelName; + + private final String invalidMessageChannelName; + + private final XmlValidator xmlValidator; + + private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter(); + + + public XmlPayloadValidatingRouter(String validMessageChannelName, + String invalidMessageChannelName, XmlValidator xmlValidator) { + super(); + this.validMessageChannelName = validMessageChannelName; + this.invalidMessageChannelName = invalidMessageChannelName; + this.xmlValidator = xmlValidator; + } + + /** + * Converter used to convert payloads prior to validation + * + * @param converter + */ + public void setConverter(XmlPayloadConverter converter) { + this.converter = converter; + } + + + @Override + protected String determineTargetChannelName(Message message) { + return xmlValidator.isValid(converter.convertToSource(message.getPayload())) ? validMessageChannelName : invalidMessageChannelName; + } + + + + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlValidator.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlValidator.java new file mode 100644 index 0000000000..b32ddb8816 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XmlValidator.java @@ -0,0 +1,25 @@ +/* + * 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.xml.router; + +import javax.xml.transform.Source; + +public interface XmlValidator { + + public boolean isValid(Source source) ; + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java index 4aefc87563..499d1a5095 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java @@ -15,17 +15,23 @@ */ package org.springframework.integration.xml; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.StringReader; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; import junit.framework.Assert; import org.custommonkey.xmlunit.XMLAssert; import org.junit.Before; import org.junit.Test; +import org.springframework.integration.core.MessagingException; +import org.springframework.xml.transform.StringSource; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; @@ -80,5 +86,30 @@ public class DefaultXmlPayloadConverterTests { Node n = converter.convertToNode(testDocument); XMLAssert.assertXMLEqual(testDocument, (Document) n); } + + + @Test + public void testGetSourcePassingDocumet() throws Exception{ + Source source = converter.convertToSource(testDocument); + assertEquals(DOMSource.class, source.getClass()); + } + + @Test + public void testGetSourcePassingString() throws Exception{ + Source source = converter.convertToSource(testDocumentAsString); + assertEquals(StringSource.class, source.getClass()); + } + + @Test + public void testGetSourcePassingSource() throws Exception{ + SAXSource passedInSource = new SAXSource(); + Source source = converter.convertToSource(passedInSource); + assertEquals(source, passedInSource); + } + + @Test(expected=MessagingException.class) + public void testInvalidPayload(){ + converter.convertToSource(12); + } } diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParserTests.java new file mode 100644 index 0000000000..f0fc650277 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingRouterParserTests.java @@ -0,0 +1,91 @@ +/* + * 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.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.channel.QueueChannel; +import org.springframework.integration.core.MessageChannel; +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 = " "; + + @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("hello"); + GenericMessage docMessage = new GenericMessage(doc); + buildContext(""); + inputChannel.send(docMessage); + assertEquals("Wrong number of messages", 1, validOutputChannel.getMesssageCount()); + } + + @Test + public void testInvalidMessage() throws Exception { + Document doc = XmlTestUtil.getDocumentForString(""); + GenericMessage docMessage = new GenericMessage(doc); + buildContext(""); + inputChannel.send(docMessage); + assertEquals("Wrong number of messages", 1, invalidOutputChannel.getMesssageCount()); + } + + + + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/validationTestsSchema.xsd b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/validationTestsSchema.xsd new file mode 100644 index 0000000000..8e2acedf9e --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/validationTestsSchema.xsd @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/SchemaValidatorTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/SchemaValidatorTests.java new file mode 100644 index 0000000000..92642fab79 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/SchemaValidatorTests.java @@ -0,0 +1,57 @@ +/* + * 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.xml.router; + +import static org.junit.Assert.*; + +import javax.xml.XMLConstants; +import javax.xml.transform.Source; + +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.xml.transform.StringSource; + +public class SchemaValidatorTests { + + + + + + @Test + public void testValidMessageWithXsd() throws Exception{ + SchemaValidator validator = new SchemaValidator(new ClassPathResource("validationTestsSchema.xsd", SchemaValidator.class), XMLConstants.W3C_XML_SCHEMA_NS_URI); + Source source = XmlTestUtil.getDomSourceForString("hello"); + assertTrue("Document expected to be valid " ,validator.isValid(source)) ; + } + + @Test + public void testInvalidMessageWithXsd() throws Exception{ + SchemaValidator validator = new SchemaValidator(new ClassPathResource("validationTestsSchema.xsd", SchemaValidator.class), XMLConstants.W3C_XML_SCHEMA_NS_URI); + Source source = XmlTestUtil.getDomSourceForString("hello"); + assertFalse("Document not expected to be valid " ,validator.isValid(source)) ; + } + + @Test + public void testInvalidXml() throws Exception { + SchemaValidator validator = new SchemaValidator(new ClassPathResource("validationTestsSchema.xsd", SchemaValidator.class), XMLConstants.W3C_XML_SCHEMA_NS_URI); + Source source =new StringSource("something else"); + assertFalse("Document not expected to be valid " ,validator.isValid(source)) ; + } + + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouterTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouterTests.java new file mode 100644 index 0000000000..83cb169295 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/XmlPayloadValidatingRouterTests.java @@ -0,0 +1,81 @@ +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.core.Message; +import org.springframework.integration.message.MessageBuilder; + +public class XmlPayloadValidatingRouterTests { + + String validChannelName = "VALID"; + + String invalidChannelName = "INVALID"; + + Source testSource; + + Message 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; + } + + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/validationTestsSchema.xsd b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/validationTestsSchema.xsd new file mode 100644 index 0000000000..8e2acedf9e --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/router/validationTestsSchema.xsd @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file