11package org .hamcrest .xml ;
22
3- import static org .hamcrest .MatcherAssert .assertThat ;
3+ import static org .hamcrest .AbstractMatcherTest .assertDescription ;
4+ import static org .hamcrest .AbstractMatcherTest .assertDoesNotMatch ;
5+ import static org .hamcrest .AbstractMatcherTest .assertMatches ;
6+ import static org .hamcrest .AbstractMatcherTest .assertMismatchDescription ;
7+ import static org .hamcrest .AbstractMatcherTest .assertNullSafe ;
8+ import static org .hamcrest .AbstractMatcherTest .assertUnknownTypeSafe ;
49import static org .hamcrest .core .IsEqual .equalTo ;
510import static org .hamcrest .core .IsNot .not ;
611import static org .hamcrest .core .StringContains .containsString ;
712import static org .hamcrest .xml .HasXPath .hasXPath ;
13+ import static org .junit .Assert .fail ;
814
915import java .io .ByteArrayInputStream ;
1016import java .util .HashSet ;
1420import javax .xml .parsers .DocumentBuilder ;
1521import javax .xml .parsers .DocumentBuilderFactory ;
1622
17- import org .hamcrest .AbstractMatcherTest ;
1823import org .hamcrest .Matcher ;
24+ import org .junit .Test ;
1925import org .w3c .dom .Document ;
26+ import org .w3c .dom .Node ;
2027
2128/**
2229 * @author Joe Walnes
30+ * @author Tom Denley
2331 */
24- public class HasXPathTest extends AbstractMatcherTest {
25- private Document xml ;
26- private NamespaceContext ns ;
27-
28- @ Override
29- protected void setUp () throws Exception {
30- super .setUp ();
31- xml = parse (""
32- + "<root type='food'>\n "
33- + " <something id='a'><cheese>Edam</cheese></something>\n "
34- + " <something id='b'><cheese>Cheddar</cheese></something>\n "
35- + " <f:foreignSomething xmlns:f=\" http://cheese.com\" milk=\" camel\" >Caravane</f:foreignSomething>\n "
36- + " <emptySomething />\n "
37- + " <f:emptySomething xmlns:f=\" http://cheese.com\" />"
38- + "</root>\n "
39- );
40- ns = new NamespaceContext () {
41- @ Override
42- public String getNamespaceURI (String prefix ) {
43- return ("cheese" .equals (prefix ) ? "http://cheese.com" : null );
44- }
45-
46- @ Override
47- public String getPrefix (String namespaceURI ) {
48- return ("http://cheese.com" .equals (namespaceURI ) ? "cheese" : null );
49- }
50-
51- @ Override
52- public Iterator <String > getPrefixes (String namespaceURI ) {
53- HashSet <String > prefixes = new HashSet <String >();
54- String prefix = getPrefix (namespaceURI );
55- if (prefix != null ) {
56- prefixes .add (prefix );
57- }
58- return prefixes .iterator ();
59- }
60- };
61- }
32+ public final class HasXPathTest {
33+
34+ private final Document xml = parse (""
35+ + "<root type='food'>\n "
36+ + " <something id='a'><cheese>Edam</cheese></something>\n "
37+ + " <something id='b'><cheese>Cheddar</cheese></something>\n "
38+ + " <f:foreignSomething xmlns:f=\" http://cheese.com\" milk=\" camel\" >Caravane</f:foreignSomething>\n "
39+ + " <emptySomething />\n "
40+ + " <f:emptySomething xmlns:f=\" http://cheese.com\" />"
41+ + "</root>\n "
42+ );
43+
44+ private final NamespaceContext ns = new NamespaceContext () {
45+ @ Override
46+ public String getNamespaceURI (String prefix ) {
47+ return ("cheese" .equals (prefix ) ? "http://cheese.com" : null );
48+ }
49+
50+ @ Override
51+ public String getPrefix (String namespaceURI ) {
52+ return ("http://cheese.com" .equals (namespaceURI ) ? "cheese" : null );
53+ }
6254
63- @ Override
64- protected Matcher <?> createMatcher () {
65- return hasXPath ("//irrelevant" );
55+ @ Override
56+ public Iterator <String > getPrefixes (String namespaceURI ) {
57+ HashSet <String > prefixes = new HashSet <String >();
58+ String prefix = getPrefix (namespaceURI );
59+ if (prefix != null ) {
60+ prefixes .add (prefix );
61+ }
62+ return prefixes .iterator ();
63+ }
64+ };
65+
66+ @ Test public void
67+ copesWithNullsAndUnknownTypes () {
68+ Matcher <Node > matcher = hasXPath ("//irrelevant" );
69+
70+ assertNullSafe (matcher );
71+ assertUnknownTypeSafe (matcher );
6672 }
6773
68- public void testAppliesMatcherToXPathInDocument () throws Exception {
69- assertThat (xml , hasXPath ("/root/something[2]/cheese" , equalTo ("Cheddar" )));
70- assertThat (xml , hasXPath ("//something[1]/cheese" , containsString ("dam" )));
71- assertThat (xml , hasXPath ("//something[2]/cheese" , not (containsString ("dam" ))));
72- assertThat (xml , hasXPath ("/root/@type" , equalTo ("food" )));
73- assertThat (xml , hasXPath ("//something[@id='b']/cheese" , equalTo ("Cheddar" )));
74- assertThat (xml , hasXPath ("//something[@id='b']/cheese" ));
74+ @ Test public void
75+ appliesMatcherToXPathInDocument () {
76+ assertMatches (hasXPath ("/root/something[2]/cheese" , equalTo ("Cheddar" )), xml );
77+ assertMatches (hasXPath ("//something[1]/cheese" , containsString ("dam" )), xml );
78+ assertMatches (hasXPath ("//something[2]/cheese" , not (containsString ("dam" ))), xml );
79+ assertMatches (hasXPath ("/root/@type" , equalTo ("food" )), xml );
80+ assertMatches (hasXPath ("//something[@id='b']/cheese" , equalTo ("Cheddar" )), xml );
81+ assertMatches (hasXPath ("//something[@id='b']/cheese" ), xml );
7582 }
7683
77- public void testMatchesEmptyElement () throws Exception {
78- assertThat (xml , hasXPath ("//emptySomething" ));
84+ @ Test public void
85+ matchesEmptyElement () {
86+ assertMatches (hasXPath ("//emptySomething" ), xml );
7987 }
8088
81- public void testMatchesEmptyElementInNamespace () throws Exception {
82- assertThat (xml , hasXPath ("//cheese:emptySomething" , ns ));
89+ @ Test public void
90+ matchesEmptyElementInNamespace () {
91+ assertMatches (hasXPath ("//cheese:emptySomething" , ns ), xml );
8392 }
8493
85- public void testFailsIfNodeIsMissing () throws Exception {
86- assertThat (xml , not (hasXPath ("/root/something[3]/cheese" , ns , equalTo ("Cheddar" ))));
87- assertThat (xml , not (hasXPath ("//something[@id='c']/cheese" , ns )));
94+ @ Test public void
95+ failsIfNodeIsMissing () {
96+ assertDoesNotMatch (hasXPath ("/root/something[3]/cheese" , ns , equalTo ("Cheddar" )), xml );
97+ assertDoesNotMatch (hasXPath ("//something[@id='c']/cheese" , ns ), xml );
8898 }
8999
90- public void testFailsIfNodeIsMissingInNamespace () throws Exception {
91- assertThat (xml , not (hasXPath ("//cheese:foreignSomething" , equalTo ("Badger" ))));
92- assertThat (xml , not (hasXPath ("//cheese:foreignSomething" )));
100+ @ Test public void
101+ failsIfNodeIsMissingInNamespace () {
102+ assertDoesNotMatch (hasXPath ("//cheese:foreignSomething" , equalTo ("Badger" )), xml );
103+ assertDoesNotMatch (hasXPath ("//cheese:foreignSomething" ), xml );
93104 }
94105
95- public void testMatchesWithNamespace () throws Exception {
96- assertThat (xml , hasXPath ("//cheese:foreignSomething" , ns ));
97- assertThat (xml , hasXPath ("//cheese:foreignSomething/@milk" , ns , equalTo ("camel" )));
98- assertThat (xml , hasXPath ("//cheese:foreignSomething/text()" , ns , equalTo ("Caravane" )));
106+ @ Test public void
107+ matchesWithNamespace () {
108+ assertMatches (hasXPath ("//cheese:foreignSomething" , ns ), xml );
109+ assertMatches (hasXPath ("//cheese:foreignSomething/@milk" , ns , equalTo ("camel" )), xml );
110+ assertMatches (hasXPath ("//cheese:foreignSomething/text()" , ns , equalTo ("Caravane" )), xml );
99111 }
100112
101- public void testThrowsIllegalArgumentExceptionIfGivenIllegalExpression () {
113+ @ Test public void
114+ throwsIllegalArgumentExceptionIfGivenIllegalExpression () {
102115 try {
103116 hasXPath ("\\ g:dfgd::DSgf/root/something[2]/cheese" , equalTo ("blah" ));
104117 fail ("Expected exception" );
@@ -107,25 +120,34 @@ public void testThrowsIllegalArgumentExceptionIfGivenIllegalExpression() {
107120 }
108121 }
109122
110- public void testDescribesItself () throws Exception {
123+ @ Test public void
124+ describesItself () {
111125 assertDescription ("an XML document with XPath /some/path \" Cheddar\" " ,
112- hasXPath ("/some/path" , equalTo ("Cheddar" )));
126+ hasXPath ("/some/path" , equalTo ("Cheddar" )));
127+
113128 assertDescription ("an XML document with XPath /some/path" ,
114- hasXPath ("/some/path" ));
129+ hasXPath ("/some/path" ));
115130 }
116131
117- public void testDescribesMissingNodeMismatch () {
132+ @ Test public void
133+ describesMissingNodeMismatch () {
118134 assertMismatchDescription ("xpath returned no results." , hasXPath ("//honky" ), xml );
119135 }
120136
121- public void testDescribesIncorrectNodeValueMismatch () {
137+ @ Test public void
138+ describesIncorrectNodeValueMismatch () {
122139 assertMismatchDescription ("was \" Edam\" " , hasXPath ("//something[1]/cheese" , equalTo ("parmesan" )), xml );
123140 }
124141
125- private static Document parse (String xml ) throws Exception {
126- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
127- documentBuilderFactory .setNamespaceAware (true );
128- DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder ();
129- return documentBuilder .parse (new ByteArrayInputStream (xml .getBytes ()));
142+ private static Document parse (String xml ) {
143+ try {
144+ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance ();
145+ documentBuilderFactory .setNamespaceAware (true );
146+ DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder ();
147+ return documentBuilder .parse (new ByteArrayInputStream (xml .getBytes ()));
148+ }
149+ catch (Exception e ) {
150+ throw new IllegalStateException (e );
151+ }
130152 }
131153}
0 commit comments