|
| 1 | +package org.hamcrest.xml; |
| 2 | + |
| 3 | +import org.hamcrest.AbstractMatcherTest; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.core.IsEqual.eq; |
| 6 | +import static org.hamcrest.core.IsNot.not; |
| 7 | +import static org.hamcrest.text.StringContains.containsString; |
| 8 | +import static org.hamcrest.xml.HasXPath.hasXPath; |
| 9 | +import org.w3c.dom.Document; |
| 10 | + |
| 11 | +import javax.xml.parsers.DocumentBuilder; |
| 12 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 13 | +import java.io.ByteArrayInputStream; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Joe Walnes |
| 17 | + */ |
| 18 | +public class HasXPathTest extends AbstractMatcherTest { |
| 19 | + private Document xml; |
| 20 | + |
| 21 | + protected void setUp() throws Exception { |
| 22 | + super.setUp(); |
| 23 | + xml = parse("" |
| 24 | + + "<root type='food'>\n" |
| 25 | + + " <something id='a'><cheese>Edam</cheese></something>\n" |
| 26 | + + " <something id='b'><cheese>Cheddar</cheese></something>\n" |
| 27 | + + "</root>\n" |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + public void testAppliesMatcherToXPathInDocument() throws Exception { |
| 32 | + assertThat(xml, hasXPath("/root/something[2]/cheese", eq("Cheddar"))); |
| 33 | + assertThat(xml, hasXPath("//something[1]/cheese", containsString("dam"))); |
| 34 | + assertThat(xml, hasXPath("//something[2]/cheese", not(containsString("dam")))); |
| 35 | + assertThat(xml, hasXPath("/root/@type", eq("food"))); |
| 36 | + assertThat(xml, hasXPath("//something[@id='b']/cheese", eq("Cheddar"))); |
| 37 | + assertThat(xml, hasXPath("//something[@id='b']/cheese")); |
| 38 | + } |
| 39 | + |
| 40 | + public void testFailsIfNodeIsMissing() throws Exception { |
| 41 | + assertThat(xml, not(hasXPath("/root/something[3]/cheese", eq("Cheddar")))); |
| 42 | + assertThat(xml, not(hasXPath("//something[@id='c']/cheese"))); |
| 43 | + } |
| 44 | + |
| 45 | + public void testThrowsIllegalArgumentExceptionIfGivenIllegalExpression() { |
| 46 | + try { |
| 47 | + hasXPath("\\g:dfgd::DSgf/root/something[2]/cheese", eq("blah")); |
| 48 | + fail("Expected exception"); |
| 49 | + } catch (IllegalArgumentException expectedException) { |
| 50 | + // expected exception |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void testDescribesItself() throws Exception { |
| 55 | + assertDescription("an XML document with XPath /some/path eq(\"Cheddar\")", |
| 56 | + hasXPath("/some/path", eq("Cheddar"))); |
| 57 | + assertDescription("an XML document with XPath /some/path", |
| 58 | + hasXPath("/some/path")); |
| 59 | + } |
| 60 | + |
| 61 | + private Document parse(String xml) throws Exception { |
| 62 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 63 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 64 | + return documentBuilder.parse(new ByteArrayInputStream(xml.getBytes())); |
| 65 | + } |
| 66 | +} |
0 commit comments