INT-3257 Make AggXmlMValidationEx more convenient

JIRA: https://jira.springsource.org/browse/INT-3257

Conflicts:

	spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XmlPayloadValidatingFilterParserTests.java

Resolved.
This commit is contained in:
Artem Bilan
2014-01-07 13:21:50 +02:00
committed by Gary Russell
parent 28832d2cd6
commit d4ade73cfb
2 changed files with 39 additions and 11 deletions

View File

@@ -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.<Throwable>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<Throwable> exceptionIterator() {
return exceptions.iterator();
}
public List<Throwable> getExceptions() {
return Collections.unmodifiableList(exceptions);
}
}

View File

@@ -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.integration.MessageChannel;
import org.springframework.integration.MessageRejectedException;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.AggregatedXmlMessageValidationException;
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("<greeting><other/></greeting>");
Document doc = XmlTestUtil.getDocumentForString("<greeting ping=\"pong\"><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("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 {