From 66efb34342fa9b4d21afb3d7c86d0e5cb4ce17c4 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 7 Jan 2014 13:21:50 +0200 Subject: [PATCH] INT-3257 Make AggXmlMValidationEx more convenient JIRA: https://jira.springsource.org/browse/INT-3257 --- ...gregatedXmlMessageValidationException.java | 18 ++++++++-- ...XmlPayloadValidatingFilterParserTests.java | 36 +++++++++++++------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/AggregatedXmlMessageValidationException.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/AggregatedXmlMessageValidationException.java index 9649dc8f8b..8f9913f133 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/AggregatedXmlMessageValidationException.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/AggregatedXmlMessageValidationException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2014 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. @@ -22,6 +22,7 @@ import java.util.List; /** * @author Oleg Zhurakousky + * @author Artem Bilan * @since 2.0 */ @SuppressWarnings("serial") @@ -34,12 +35,25 @@ public class AggregatedXmlMessageValidationException extends RuntimeException { this.exceptions = (exceptions != null) ? exceptions : Collections.emptyList(); } + @Override + public String getMessage() { + StringBuilder message = new StringBuilder("Multiple causes:\n"); + for (Throwable exception : this.exceptions) { + message.append(" " + exception.getMessage() + "\n"); + } + return message.toString(); + } /** - * Returns an Iterator for the aggregated Exceptions. + * @deprecated in favor of #getExceptions */ + @Deprecated public Iterator exceptionIterator() { return exceptions.iterator(); } + public List getExceptions() { + return Collections.unmodifiableList(exceptions); + } + } diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingFilterParserTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingFilterParserTests.java index 1be26e5c0b..4f3de94ec6 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingFilterParserTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingFilterParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -18,22 +18,28 @@ package org.springframework.integration.xml.config; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import org.hamcrest.Matchers; import org.junit.Test; +import org.w3c.dom.Document; + import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.messaging.MessageChannel; import org.springframework.integration.MessageRejectedException; +import org.springframework.integration.xml.AggregatedXmlMessageValidationException; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.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 * @author Gunnar Hillert + * @author Artem Bilan */ @ContextConfiguration public class XmlPayloadValidatingFilterParserTests { @@ -60,17 +66,25 @@ public class XmlPayloadValidatingFilterParserTests { assertNotNull(invalidChannel.receive(100)); assertNull(validChannel.receive(100)); } - @Test(expected=MessageRejectedException.class) + @Test public void testInvalidMessageWithThrowException() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("XmlPayloadValidatingFilterParserTests-context.xml", this.getClass()); - Document doc = XmlTestUtil.getDocumentForString(""); + Document doc = XmlTestUtil.getDocumentForString(""); GenericMessage docMessage = new GenericMessage(doc); - PollableChannel validChannel = ac.getBean("validOutputChannel", PollableChannel.class); - PollableChannel invalidChannel = ac.getBean("invalidOutputChannel", PollableChannel.class); MessageChannel inputChannel = ac.getBean("inputChannelB", MessageChannel.class); - inputChannel.send(docMessage); - assertNotNull(invalidChannel.receive(100)); - assertNull(validChannel.receive(100)); + try { + inputChannel.send(docMessage); + fail("MessageRejectedException expected"); + } + catch (Exception e) { + assertThat(e, Matchers.instanceOf(MessageRejectedException.class)); + Throwable cause = e.getCause(); + assertThat(cause, Matchers.instanceOf(AggregatedXmlMessageValidationException.class)); + assertThat(cause.getMessage(), + Matchers.containsString("Element 'greeting' is a simple type, so it must have no element information item [children].")); + assertThat(cause.getMessage(), + Matchers.containsString("Element 'greeting' is a simple type, so it cannot have attributes,")); + } } @Test public void testValidMessageWithValidator() throws Exception {