1616
1717package org .springframework .test .util ;
1818
19- import java .io .StringReader ;
19+ import static org .hamcrest .MatcherAssert .*;
20+ import static org .springframework .test .util .AssertionErrors .*;
21+
22+ import java .io .ByteArrayInputStream ;
2023import java .util .Collections ;
2124import java .util .Map ;
25+
2226import javax .xml .namespace .QName ;
2327import javax .xml .parsers .DocumentBuilder ;
2428import javax .xml .parsers .DocumentBuilderFactory ;
3539import org .xml .sax .InputSource ;
3640
3741import org .springframework .util .CollectionUtils ;
42+ import org .springframework .util .StringUtils ;
3843import org .springframework .util .xml .SimpleNamespaceContext ;
3944
40- import static org .hamcrest .MatcherAssert .*;
41- import static org .springframework .test .util .AssertionErrors .*;
42-
4345/**
4446 * A helper class for applying assertions via XPath expressions.
4547 *
@@ -93,23 +95,27 @@ protected XPathExpression getXpathExpression() {
9395 * Parse the content, evaluate the XPath expression as a {@link Node}, and
9496 * assert it with the given {@code Matcher<Node>}.
9597 */
96- public void assertNode (String content , final Matcher <? super Node > matcher ) throws Exception {
97- Document document = parseXmlString (content );
98+ public void assertNode (byte [] content , String encoding , final Matcher <? super Node > matcher ) throws Exception {
99+ Document document = parseXmlByteArray (content , encoding );
98100 Node node = evaluateXpath (document , XPathConstants .NODE , Node .class );
99101 assertThat ("XPath " + this .expression , node , matcher );
100102 }
101103
102104 /**
103105 * Parse the given XML content to a {@link Document}.
104106 * @param xml the content to parse
107+ * @param encoding optional content encoding, if provided as metadata (e.g. in HTTP headers)
105108 * @return the parsed document
106- * @throws Exception in case of errors
109+ * @throws Exception
107110 */
108- protected Document parseXmlString ( String xml ) throws Exception {
111+ protected Document parseXmlByteArray ( byte [] xml , String encoding ) throws Exception {
109112 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
110113 factory .setNamespaceAware (this .hasNamespaces );
111114 DocumentBuilder documentBuilder = factory .newDocumentBuilder ();
112- InputSource inputSource = new InputSource (new StringReader (xml ));
115+ InputSource inputSource = new InputSource (new ByteArrayInputStream (xml ));
116+ if (StringUtils .hasText (encoding )) {
117+ inputSource .setEncoding (encoding );
118+ }
113119 return documentBuilder .parse (inputSource );
114120 }
115121
@@ -128,8 +134,8 @@ protected <T> T evaluateXpath(Document document, QName evaluationType, Class<T>
128134 * Apply the XPath expression and assert the resulting content exists.
129135 * @throws Exception if content parsing or expression evaluation fails
130136 */
131- public void exists (String content ) throws Exception {
132- Document document = parseXmlString (content );
137+ public void exists (byte [] content , String encoding ) throws Exception {
138+ Document document = parseXmlByteArray (content , encoding );
133139 Node node = evaluateXpath (document , XPathConstants .NODE , Node .class );
134140 assertTrue ("XPath " + this .expression + " does not exist" , node != null );
135141 }
@@ -138,8 +144,8 @@ public void exists(String content) throws Exception {
138144 * Apply the XPath expression and assert the resulting content does not exist.
139145 * @throws Exception if content parsing or expression evaluation fails
140146 */
141- public void doesNotExist (String content ) throws Exception {
142- Document document = parseXmlString (content );
147+ public void doesNotExist (byte [] content , String encoding ) throws Exception {
148+ Document document = parseXmlByteArray (content , encoding );
143149 Node node = evaluateXpath (document , XPathConstants .NODE , Node .class );
144150 assertTrue ("XPath " + this .expression + " exists" , node == null );
145151 }
@@ -149,8 +155,8 @@ public void doesNotExist(String content) throws Exception {
149155 * given Hamcrest matcher.
150156 * @throws Exception if content parsing or expression evaluation fails
151157 */
152- public void assertNodeCount (String content , Matcher <Integer > matcher ) throws Exception {
153- Document document = parseXmlString (content );
158+ public void assertNodeCount (byte [] content , String encoding , Matcher <Integer > matcher ) throws Exception {
159+ Document document = parseXmlByteArray (content , encoding );
154160 NodeList nodeList = evaluateXpath (document , XPathConstants .NODESET , NodeList .class );
155161 assertThat ("nodeCount for XPath " + this .expression , nodeList .getLength (), matcher );
156162 }
@@ -159,8 +165,8 @@ public void assertNodeCount(String content, Matcher<Integer> matcher) throws Exc
159165 * Apply the XPath expression and assert the resulting content as an integer.
160166 * @throws Exception if content parsing or expression evaluation fails
161167 */
162- public void assertNodeCount (String content , int expectedCount ) throws Exception {
163- Document document = parseXmlString (content );
168+ public void assertNodeCount (byte [] content , String encoding , int expectedCount ) throws Exception {
169+ Document document = parseXmlByteArray (content , encoding );
164170 NodeList nodeList = evaluateXpath (document , XPathConstants .NODESET , NodeList .class );
165171 assertEquals ("nodeCount for XPath " + this .expression , expectedCount , nodeList .getLength ());
166172 }
@@ -170,8 +176,8 @@ public void assertNodeCount(String content, int expectedCount) throws Exception
170176 * given Hamcrest matcher.
171177 * @throws Exception if content parsing or expression evaluation fails
172178 */
173- public void assertString (String content , Matcher <? super String > matcher ) throws Exception {
174- Document document = parseXmlString (content );
179+ public void assertString (byte [] content , String encoding , Matcher <? super String > matcher ) throws Exception {
180+ Document document = parseXmlByteArray (content , encoding );
175181 String result = evaluateXpath (document , XPathConstants .STRING , String .class );
176182 assertThat ("XPath " + this .expression , result , matcher );
177183 }
@@ -180,8 +186,8 @@ public void assertString(String content, Matcher<? super String> matcher) throws
180186 * Apply the XPath expression and assert the resulting content as a String.
181187 * @throws Exception if content parsing or expression evaluation fails
182188 */
183- public void assertString (String content , String expectedValue ) throws Exception {
184- Document document = parseXmlString (content );
189+ public void assertString (byte [] content , String encoding , String expectedValue ) throws Exception {
190+ Document document = parseXmlByteArray (content , encoding );
185191 String actual = evaluateXpath (document , XPathConstants .STRING , String .class );
186192 assertEquals ("XPath " + this .expression , expectedValue , actual );
187193 }
@@ -191,8 +197,8 @@ public void assertString(String content, String expectedValue) throws Exception
191197 * given Hamcrest matcher.
192198 * @throws Exception if content parsing or expression evaluation fails
193199 */
194- public void assertNumber (String content , Matcher <? super Double > matcher ) throws Exception {
195- Document document = parseXmlString (content );
200+ public void assertNumber (byte [] content , String encoding , Matcher <? super Double > matcher ) throws Exception {
201+ Document document = parseXmlByteArray (content , encoding );
196202 Double result = evaluateXpath (document , XPathConstants .NUMBER , Double .class );
197203 assertThat ("XPath " + this .expression , result , matcher );
198204 }
@@ -201,8 +207,8 @@ public void assertNumber(String content, Matcher<? super Double> matcher) throws
201207 * Apply the XPath expression and assert the resulting content as a Double.
202208 * @throws Exception if content parsing or expression evaluation fails
203209 */
204- public void assertNumber (String content , Double expectedValue ) throws Exception {
205- Document document = parseXmlString (content );
210+ public void assertNumber (byte [] content , String encoding , Double expectedValue ) throws Exception {
211+ Document document = parseXmlByteArray (content , encoding );
206212 Double actual = evaluateXpath (document , XPathConstants .NUMBER , Double .class );
207213 assertEquals ("XPath " + this .expression , expectedValue , actual );
208214 }
@@ -211,8 +217,8 @@ public void assertNumber(String content, Double expectedValue) throws Exception
211217 * Apply the XPath expression and assert the resulting content as a Boolean.
212218 * @throws Exception if content parsing or expression evaluation fails
213219 */
214- public void assertBoolean (String content , boolean expectedValue ) throws Exception {
215- Document document = parseXmlString (content );
220+ public void assertBoolean (byte [] content , String encoding , boolean expectedValue ) throws Exception {
221+ Document document = parseXmlByteArray (content , encoding );
216222 String actual = evaluateXpath (document , XPathConstants .STRING , String .class );
217223 assertEquals ("XPath " + this .expression , expectedValue , Boolean .parseBoolean (actual ));
218224 }
0 commit comments