jocarreira / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

java hasXpath doesn't work correctly when element does not contain text #39

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The following test case fails which I think it shouldn't:
    @Before
    public void setUp() throws Exception {
    xml = parse("<root><something/></root>");
    }

    @Test
    public void testAppliesMatcherToXPathInDocument() throws Exception {
        assertThat(xml, hasXPath("//something"));
    }

This can be solved with followinf change to HasXPath.matchesSafely

try {
            if (valueMatcher == null) {
                Boolean result = (Boolean) compiledXPath.evaluate(item,
                        XPathConstants.BOOLEAN);
                return result == null ? false : result.booleanValue();
            } else {
                String result = (String) compiledXPath.evaluate(item,
                        XPathConstants.STRING);
                if (result == null) {
                    return false;
                } else {
                    return valueMatcher.matches(result);
                }
            }
        } catch (XPathExpressionException e) {
            return false;
        } 

Original issue reported on code.google.com by jepp...@gmail.com on 29 Jul 2008 at 10:26

GoogleCodeExporter commented 8 years ago
Appears to work already. See testMatchesEmptyElement(). And I tried your test 
directly.

Original comment by smgfree...@gmail.com on 20 Nov 2008 at 8:40