Assert XML without considering order in XmlExpectationsHelper
This commit changes the `assertXmlEqual` implementation to compare expected and actual XML documents without considering the order of XML nodes. Issue: SPR-15156
This commit is contained in:
parent
6f68af6860
commit
28e77fa68a
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
|
|
@ -28,7 +28,9 @@ import org.w3c.dom.Document;
|
|||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xmlunit.builder.DiffBuilder;
|
||||
import org.xmlunit.diff.DefaultNodeMatcher;
|
||||
import org.xmlunit.diff.Diff;
|
||||
import org.xmlunit.diff.ElementSelectors;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
|
||||
|
|
@ -58,7 +60,7 @@ public class XmlExpectationsHelper {
|
|||
|
||||
/**
|
||||
* Parse the content as {@link DOMSource} and apply a {@link Matcher}.
|
||||
* @see <a href="http://code.google.com/p/xml-matchers/">xml-matchers</a>
|
||||
* @see <a href="https://github.com/davidehringer/xml-matchers">xml-matchers</a>
|
||||
*/
|
||||
public void assertSource(String content, Matcher<? super Source> matcher) throws Exception {
|
||||
Document document = parseXmlString(content);
|
||||
|
|
@ -70,7 +72,7 @@ public class XmlExpectationsHelper {
|
|||
* two are "similar" -- i.e. they contain the same elements and attributes
|
||||
* regardless of order.
|
||||
* <p>Use of this method assumes the
|
||||
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
|
||||
* <a href="https://github.com/xmlunit/xmlunit">XMLUnit<a/> library is available.
|
||||
* @param expected the expected XML content
|
||||
* @param actual the actual XML content
|
||||
* @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Object...)
|
||||
|
|
@ -78,6 +80,7 @@ public class XmlExpectationsHelper {
|
|||
*/
|
||||
public void assertXmlEqual(String expected, String actual) throws Exception {
|
||||
Diff diffSimilar = DiffBuilder.compare(expected).withTest(actual)
|
||||
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
|
||||
.ignoreWhitespace().ignoreComments()
|
||||
.checkForSimilar()
|
||||
.build();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright 2002-2017 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.test.util;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link XmlExpectationsHelper}.
|
||||
*
|
||||
* @author Matthew Depue
|
||||
*/
|
||||
public class XmlExpectationsHelperTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void assertXmlEqualForEqual() throws Exception {
|
||||
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
|
||||
final String test = "<root><field1>f1</field1><field2>f2</field2></root>";
|
||||
|
||||
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
|
||||
xmlHelper.assertXmlEqual(control, test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertXmlEqualExceptionForIncorrectValue() throws Exception {
|
||||
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
|
||||
final String test = "<root><field1>notf1</field1><field2>f2</field2></root>";
|
||||
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage(Matchers.startsWith("Body content Expected child 'field1'"));
|
||||
|
||||
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
|
||||
xmlHelper.assertXmlEqual(control, test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertXmlEqualForOutOfOrder() throws Exception {
|
||||
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
|
||||
final String test = "<root><field2>f2</field2><field1>f1</field1></root>";
|
||||
|
||||
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
|
||||
xmlHelper.assertXmlEqual(control, test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertXmlEqualExceptionForMoreEntries() throws Exception {
|
||||
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
|
||||
final String test = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
|
||||
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage(Matchers.containsString("Expected child nodelist length '2' but was '3'"));
|
||||
|
||||
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
|
||||
xmlHelper.assertXmlEqual(control, test);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertXmlEqualExceptionForLessEntries() throws Exception {
|
||||
final String control = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
|
||||
final String test = "<root><field1>f1</field1><field2>f2</field2></root>";
|
||||
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage(Matchers.containsString("Expected child nodelist length '3' but was '2'"));
|
||||
|
||||
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
|
||||
xmlHelper.assertXmlEqual(control, test);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue