001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.util;
018
019import java.io.StringReader;
020import java.util.Map;
021import javax.xml.parsers.DocumentBuilder;
022import javax.xml.parsers.DocumentBuilderFactory;
023import javax.xml.transform.Source;
024import javax.xml.transform.dom.DOMSource;
025
026import org.custommonkey.xmlunit.Diff;
027import org.custommonkey.xmlunit.XMLUnit;
028import org.hamcrest.Matcher;
029import org.w3c.dom.Document;
030import org.w3c.dom.Node;
031import org.xml.sax.InputSource;
032
033import static org.hamcrest.MatcherAssert.*;
034
035/**
036 * A helper class for assertions on XML content.
037 *
038 * @author Rossen Stoyanchev
039 * @since 3.2
040 */
041public class XmlExpectationsHelper {
042
043        /**
044         * Parse the content as {@link Node} and apply a {@link Matcher}.
045         */
046        public void assertNode(String content, Matcher<? super Node> matcher) throws Exception {
047                Document document = parseXmlString(content);
048                assertThat("Body content", document, matcher);
049        }
050
051        private Document parseXmlString(String xml) throws Exception  {
052                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
053                factory.setNamespaceAware(true);
054                DocumentBuilder documentBuilder = factory.newDocumentBuilder();
055                InputSource inputSource = new InputSource(new StringReader(xml));
056                return documentBuilder.parse(inputSource);
057        }
058
059        /**
060         * Parse the content as {@link DOMSource} and apply a {@link Matcher}.
061         * @see <a href="https://code.google.com/p/xml-matchers/">xml-matchers</a>
062         */
063        public void assertSource(String content, Matcher<? super Source> matcher) throws Exception {
064                Document document = parseXmlString(content);
065                assertThat("Body content", new DOMSource(document), matcher);
066        }
067
068        /**
069         * Parse the expected and actual content strings as XML and assert that the
070         * two are "similar" -- i.e. they contain the same elements and attributes
071         * regardless of order.
072         * <p>Use of this method assumes the
073         * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
074         * @param expected the expected XML content
075         * @param actual the actual XML content
076         * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Object...)
077         * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Map, Object...)
078         */
079        public void assertXmlEqual(String expected, String actual) throws Exception {
080                XMLUnit.setIgnoreWhitespace(true);
081                XMLUnit.setIgnoreComments(true);
082                XMLUnit.setIgnoreAttributeOrder(true);
083
084                Document control = XMLUnit.buildControlDocument(expected);
085                Document test = XMLUnit.buildTestDocument(actual);
086                Diff diff = new Diff(control, test);
087                if (!diff.similar()) {
088                        AssertionErrors.fail("Body content " + diff.toString());
089                }
090        }
091
092}