From 28e77fa68ae60df22bf339ff4fc22036e2090d3d Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 13 Feb 2017 20:58:31 +0100 Subject: [PATCH] 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 --- .../test/util/XmlExpectationsHelper.java | 9 +- .../test/util/XmlExpectationsHelperTests.java | 89 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 spring-test/src/test/java/org/springframework/test/util/XmlExpectationsHelperTests.java diff --git a/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java index ac1ea9fb93c..a9caf379a66 100644 --- a/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java @@ -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 xml-matchers + * @see xml-matchers */ public void assertSource(String content, Matcher 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. *

Use of this method assumes the - * XMLUnit library is available. + * XMLUnit 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(); diff --git a/spring-test/src/test/java/org/springframework/test/util/XmlExpectationsHelperTests.java b/spring-test/src/test/java/org/springframework/test/util/XmlExpectationsHelperTests.java new file mode 100644 index 00000000000..f4fe6f1da73 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/util/XmlExpectationsHelperTests.java @@ -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 = "f1f2"; + final String test = "f1f2"; + + final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper(); + xmlHelper.assertXmlEqual(control, test); + } + + @Test + public void assertXmlEqualExceptionForIncorrectValue() throws Exception { + final String control = "f1f2"; + final String test = "notf1f2"; + + 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 = "f1f2"; + final String test = "f2f1"; + + final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper(); + xmlHelper.assertXmlEqual(control, test); + } + + @Test + public void assertXmlEqualExceptionForMoreEntries() throws Exception { + final String control = "f1f2"; + final String test = "f1f2f3"; + + 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 = "f1f2f3"; + final String test = "f1f2"; + + 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); + } + +} \ No newline at end of file