• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.5
/src/main/java/com/meterware/httpunit/XPathPredicate.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit;
9

10
import javax.xml.xpath.XPathConstants;
11
import javax.xml.xpath.XPathExpression;
12
import javax.xml.xpath.XPathExpressionException;
13
import javax.xml.xpath.XPathFactory;
14

15
import org.w3c.dom.Document;
16
import org.w3c.dom.Node;
17
import org.w3c.dom.NodeList;
18

19
/**
20
 * Provides an HTMLElement Predicate that is capable of matching based on an XPath node specification. This allows for
21
 * very advanced matching techniques. THREAD: Instances are not thread safe, each thread should create its own instance
22
 * with a specific xpath. (The same instance can be used for multiple documents, each change in document will result in
23
 * its internal caches being flushed).
24
 */
25
public class XPathPredicate implements HTMLElementPredicate {
26

27
    /** XPath which dictates matching nodes, from root. */
28
    private XPathExpression xpath;
29

30
    /** The path. */
31
    private String path;
32

33
    /** The Constant DEBUG. */
34
    // set to true for debugging
35
    public static final boolean DEBUG = false;
36

37
    /**
38
     * Constructs an HTMLElementPredicate that matches only those elements which match the provided XPath.
39
     *
40
     * @param path
41
     *            [in] XPath specification of valid/matching nodes
42
     *
43
     * @throws XPathExpressionException
44
     *             if the xpath is invalid
45
     */
46
    public XPathPredicate(String path) throws XPathExpressionException {
1✔
47
        this.path = path;
1✔
48
        this.xpath = XPathFactory.newInstance().newXPath().compile(path);
1✔
49
    }
1✔
50

51
    /**
52
     * debug Output for node structure.
53
     *
54
     * @param node
55
     *            the node
56
     * @param indent
57
     *            the indent
58
     */
59
    private void debugOut(Node node, String indent) {
60
        System.out.print(indent + node.getNodeName() + ":");
×
61
        System.out.println(indent + node.getNodeValue());
×
62
        NodeList nl = node.getChildNodes();
×
63
        for (int i = 0; i < nl.getLength(); i++) {
×
64
            debugOut(nl.item(i), indent + "\t");
×
65
        }
66
    }
×
67

68
    /**
69
     * check whether the given criteria are matched for the given element
70
     *
71
     * @param someElement
72
     *            - the element to check
73
     * @param criteria
74
     *            - the criteria to check
75
     */
76
    @Override
77
    public boolean matchesCriteria(final Object someElement, final Object criteria) {
78

79
        // this condition should normally be false
80
        if (!(someElement instanceof HTMLElement)) {
1!
81
            return false;
×
82
        }
83

84
        HTMLElement htmlElement = (HTMLElement) someElement;
1✔
85

86
        Node htmlNode = htmlElement.getNode();
1✔
87
        Document doc = htmlNode.getOwnerDocument();
1✔
88
        if (DEBUG) {
89
            debugOut(doc, "");
90
        }
91

92
        NodeList nodes;
93
        try {
94
            nodes = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET);
1✔
95
            final int nodeCount = nodes.getLength();
1✔
96
            for (int i = 0; i < nodeCount; i++) {
1✔
97
                if (nodes.item(i).equals(htmlNode)) {
1✔
98
                    return true;
1✔
99
                }
100
            }
101
        } catch (XPathExpressionException e) {
×
102
            throw new RuntimeException("unable to evaluate xpath '" + path + "'", e);
×
103
        }
1✔
104
        return false;
1✔
105
    }
106

107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc