@ -16,9 +16,13 @@
@@ -16,9 +16,13 @@
package org.springframework.test.util ;
import java.io.StringReader ;
import static org.hamcrest.MatcherAssert.* ;
import static org.springframework.test.util.AssertionErrors.* ;
import java.io.ByteArrayInputStream ;
import java.util.Collections ;
import java.util.Map ;
import javax.xml.namespace.QName ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
@ -35,11 +39,9 @@ import org.w3c.dom.NodeList;
@@ -35,11 +39,9 @@ import org.w3c.dom.NodeList;
import org.xml.sax.InputSource ;
import org.springframework.util.CollectionUtils ;
import org.springframework.util.StringUtils ;
import org.springframework.util.xml.SimpleNamespaceContext ;
import static org.hamcrest.MatcherAssert.* ;
import static org.springframework.test.util.AssertionErrors.* ;
/ * *
* A helper class for applying assertions via XPath expressions .
*
@ -93,8 +95,8 @@ public class XpathExpectationsHelper {
@@ -93,8 +95,8 @@ public class XpathExpectationsHelper {
* Parse the content , evaluate the XPath expression as a { @link Node } , and
* assert it with the given { @code Matcher < Node > } .
* /
public void assertNode ( String content , final Matcher < ? super Node > matcher ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertNode ( byte [ ] content , String encoding , final Matcher < ? super Node > matcher ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
Node node = evaluateXpath ( document , XPathConstants . NODE , Node . class ) ;
assertThat ( "XPath " + this . expression , node , matcher ) ;
}
@ -102,14 +104,18 @@ public class XpathExpectationsHelper {
@@ -102,14 +104,18 @@ public class XpathExpectationsHelper {
/ * *
* Parse the given XML content to a { @link Document } .
* @param xml the content to parse
* @param encoding optional content encoding , if provided as metadata ( e . g . in HTTP headers )
* @return the parsed document
* @throws Exception in case of errors
* @throws Exception
* /
protected Document parseXmlString ( String xml ) throws Exception {
protected Document parseXmlByteArray ( byte [ ] xml , String encoding ) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory . newInstance ( ) ;
factory . setNamespaceAware ( this . hasNamespaces ) ;
DocumentBuilder documentBuilder = factory . newDocumentBuilder ( ) ;
InputSource inputSource = new InputSource ( new StringReader ( xml ) ) ;
InputSource inputSource = new InputSource ( new ByteArrayInputStream ( xml ) ) ;
if ( StringUtils . hasText ( encoding ) ) {
inputSource . setEncoding ( encoding ) ;
}
return documentBuilder . parse ( inputSource ) ;
}
@ -128,8 +134,8 @@ public class XpathExpectationsHelper {
@@ -128,8 +134,8 @@ public class XpathExpectationsHelper {
* Apply the XPath expression and assert the resulting content exists .
* @throws Exception if content parsing or expression evaluation fails
* /
public void exists ( String content ) throws Exception {
Document document = parseXmlString ( content ) ;
public void exists ( byte [ ] content , String encoding ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
Node node = evaluateXpath ( document , XPathConstants . NODE , Node . class ) ;
assertTrue ( "XPath " + this . expression + " does not exist" , node ! = null ) ;
}
@ -138,8 +144,8 @@ public class XpathExpectationsHelper {
@@ -138,8 +144,8 @@ public class XpathExpectationsHelper {
* Apply the XPath expression and assert the resulting content does not exist .
* @throws Exception if content parsing or expression evaluation fails
* /
public void doesNotExist ( String content ) throws Exception {
Document document = parseXmlString ( content ) ;
public void doesNotExist ( byte [ ] content , String encoding ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
Node node = evaluateXpath ( document , XPathConstants . NODE , Node . class ) ;
assertTrue ( "XPath " + this . expression + " exists" , node = = null ) ;
}
@ -149,8 +155,8 @@ public class XpathExpectationsHelper {
@@ -149,8 +155,8 @@ public class XpathExpectationsHelper {
* given Hamcrest matcher .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertNodeCount ( String content , Matcher < Integer > matcher ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertNodeCount ( byte [ ] content , String encoding , Matcher < Integer > matcher ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
NodeList nodeList = evaluateXpath ( document , XPathConstants . NODESET , NodeList . class ) ;
assertThat ( "nodeCount for XPath " + this . expression , nodeList . getLength ( ) , matcher ) ;
}
@ -159,8 +165,8 @@ public class XpathExpectationsHelper {
@@ -159,8 +165,8 @@ public class XpathExpectationsHelper {
* Apply the XPath expression and assert the resulting content as an integer .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertNodeCount ( String content , int expectedCount ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertNodeCount ( byte [ ] content , String encoding , int expectedCount ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
NodeList nodeList = evaluateXpath ( document , XPathConstants . NODESET , NodeList . class ) ;
assertEquals ( "nodeCount for XPath " + this . expression , expectedCount , nodeList . getLength ( ) ) ;
}
@ -170,8 +176,8 @@ public class XpathExpectationsHelper {
@@ -170,8 +176,8 @@ public class XpathExpectationsHelper {
* given Hamcrest matcher .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertString ( String content , Matcher < ? super String > matcher ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertString ( byte [ ] content , String encoding , Matcher < ? super String > matcher ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
String result = evaluateXpath ( document , XPathConstants . STRING , String . class ) ;
assertThat ( "XPath " + this . expression , result , matcher ) ;
}
@ -180,8 +186,8 @@ public class XpathExpectationsHelper {
@@ -180,8 +186,8 @@ public class XpathExpectationsHelper {
* Apply the XPath expression and assert the resulting content as a String .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertString ( String content , String expectedValue ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertString ( byte [ ] content , String encoding , String expectedValue ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
String actual = evaluateXpath ( document , XPathConstants . STRING , String . class ) ;
assertEquals ( "XPath " + this . expression , expectedValue , actual ) ;
}
@ -191,8 +197,8 @@ public class XpathExpectationsHelper {
@@ -191,8 +197,8 @@ public class XpathExpectationsHelper {
* given Hamcrest matcher .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertNumber ( String content , Matcher < ? super Double > matcher ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertNumber ( byte [ ] content , String encoding , Matcher < ? super Double > matcher ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
Double result = evaluateXpath ( document , XPathConstants . NUMBER , Double . class ) ;
assertThat ( "XPath " + this . expression , result , matcher ) ;
}
@ -201,8 +207,8 @@ public class XpathExpectationsHelper {
@@ -201,8 +207,8 @@ public class XpathExpectationsHelper {
* Apply the XPath expression and assert the resulting content as a Double .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertNumber ( String content , Double expectedValue ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertNumber ( byte [ ] content , String encoding , Double expectedValue ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
Double actual = evaluateXpath ( document , XPathConstants . NUMBER , Double . class ) ;
assertEquals ( "XPath " + this . expression , expectedValue , actual ) ;
}
@ -211,8 +217,8 @@ public class XpathExpectationsHelper {
@@ -211,8 +217,8 @@ public class XpathExpectationsHelper {
* Apply the XPath expression and assert the resulting content as a Boolean .
* @throws Exception if content parsing or expression evaluation fails
* /
public void assertBoolean ( String content , boolean expectedValue ) throws Exception {
Document document = parseXmlString ( content ) ;
public void assertBoolean ( byte [ ] content , String encoding , boolean expectedValue ) throws Exception {
Document document = parseXmlByteArray ( content , encoding ) ;
String actual = evaluateXpath ( document , XPathConstants . STRING , String . class ) ;
assertEquals ( "XPath " + this . expression , expectedValue , Boolean . parseBoolean ( actual ) ) ;
}