View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.test.util;
18  
19  import java.io.StringReader;
20  import java.util.Map;
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.transform.Source;
24  import javax.xml.transform.dom.DOMSource;
25  
26  import org.custommonkey.xmlunit.Diff;
27  import org.custommonkey.xmlunit.XMLUnit;
28  import org.hamcrest.Matcher;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Node;
31  import org.xml.sax.InputSource;
32  
33  import static org.springframework.test.util.MatcherAssertionErrors.*;
34  
35  /**
36   * A helper class for assertions on XML content.
37   *
38   * @author Rossen Stoyanchev
39   * @since 3.2
40   */
41  public class XmlExpectationsHelper {
42  
43  	/**
44  	 * Parse the content as {@link Node} and apply a {@link Matcher}.
45  	 */
46  	public void assertNode(String content, Matcher<? super Node> matcher) throws Exception {
47  		Document document = parseXmlString(content);
48  		assertThat("Body content", document, matcher);
49  	}
50  
51  	private Document parseXmlString(String xml) throws Exception  {
52  		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
53  		factory.setNamespaceAware(true);
54  		DocumentBuilder documentBuilder = factory.newDocumentBuilder();
55  		InputSource inputSource = new InputSource(new StringReader(xml));
56  		return documentBuilder.parse(inputSource);
57  	}
58  
59  	/**
60  	 * Parse the content as {@link DOMSource} and apply a {@link Matcher}.
61  	 * @see <a href="http://code.google.com/p/xml-matchers/">xml-matchers</a>
62  	 */
63  	public void assertSource(String content, Matcher<? super Source> matcher) throws Exception {
64  		Document document = parseXmlString(content);
65  		assertThat("Body content", new DOMSource(document), matcher);
66  	}
67  
68  	/**
69  	 * Parse the expected and actual content strings as XML and assert that the
70  	 * two are "similar" -- i.e. they contain the same elements and attributes
71  	 * regardless of order.
72  	 * <p>Use of this method assumes the
73  	 * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
74  	 * @param expected the expected XML content
75  	 * @param actual the actual XML content
76  	 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Object...)
77  	 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Map, Object...)
78  	 */
79  	public void assertXmlEqual(String expected, String actual) throws Exception {
80  		XMLUnit.setIgnoreWhitespace(true);
81  		XMLUnit.setIgnoreComments(true);
82  		XMLUnit.setIgnoreAttributeOrder(true);
83  
84  		Document control = XMLUnit.buildControlDocument(expected);
85  		Document test = XMLUnit.buildTestDocument(actual);
86  		Diff diff = new Diff(control, test);
87  		if (!diff.similar()) {
88  			AssertionErrors.fail("Body content " + diff.toString());
89  		}
90  	}
91  
92  }