INT-3257 Make AggXmlMValidationEx more convenient

JIRA: https://jira.springsource.org/browse/INT-3257
This commit is contained in:
Artem Bilan
2014-01-07 13:21:50 +02:00
committed by Gary Russell
parent cca630e882
commit 66efb34342
2 changed files with 41 additions and 13 deletions

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.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("<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 {