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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

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
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit;
21

22
import javax.xml.xpath.XPathConstants;
23
import javax.xml.xpath.XPathExpression;
24
import javax.xml.xpath.XPathExpressionException;
25
import javax.xml.xpath.XPathFactory;
26

27
import org.w3c.dom.Document;
28
import org.w3c.dom.Node;
29
import org.w3c.dom.NodeList;
30

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

39
    /** XPath which dictates matching nodes, from root. */
40
    private XPathExpression xpath;
41

42
    /** The path. */
43
    private String path;
44

45
    /** The Constant DEBUG. */
46
    // set to true for debugging
47
    public static final boolean DEBUG = false;
48

49
    /**
50
     * Constructs an HTMLElementPredicate that matches only those elements which match the provided XPath.
51
     *
52
     * @param path
53
     *            [in] XPath specification of valid/matching nodes
54
     *
55
     * @throws XPathExpressionException
56
     *             if the xpath is invalid
57
     */
58
    public XPathPredicate(String path) throws XPathExpressionException {
1✔
59
        this.path = path;
1✔
60
        this.xpath = XPathFactory.newInstance().newXPath().compile(path);
1✔
61
    }
1✔
62

63
    /**
64
     * debug Output for node structure.
65
     *
66
     * @param node
67
     *            the node
68
     * @param indent
69
     *            the indent
70
     */
71
    private void debugOut(Node node, String indent) {
72
        System.out.print(indent + node.getNodeName() + ":");
×
73
        System.out.println(indent + node.getNodeValue());
×
74
        NodeList nl = node.getChildNodes();
×
75
        for (int i = 0; i < nl.getLength(); i++) {
×
76
            debugOut(nl.item(i), indent + "\t");
×
77
        }
78
    }
×
79

80
    /**
81
     * check whether the given criteria are matched for the given element
82
     *
83
     * @param someElement
84
     *            - the element to check
85
     * @param criteria
86
     *            - the criteria to check
87
     */
88
    @Override
89
    public boolean matchesCriteria(final Object someElement, final Object criteria) {
90

91
        // this condition should normally be false
92
        if (!(someElement instanceof HTMLElement)) {
1!
93
            return false;
×
94
        }
95

96
        HTMLElement htmlElement = (HTMLElement) someElement;
1✔
97

98
        Node htmlNode = htmlElement.getNode();
1✔
99
        Document doc = htmlNode.getOwnerDocument();
1✔
100
        if (DEBUG) {
101
            debugOut(doc, "");
102
        }
103

104
        NodeList nodes;
105
        try {
106
            nodes = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET);
1✔
107
            final int nodeCount = nodes.getLength();
1✔
108
            for (int i = 0; i < nodeCount; i++) {
1✔
109
                if (nodes.item(i).equals(htmlNode)) {
1✔
110
                    return true;
1✔
111
                }
112
            }
113
        } catch (XPathExpressionException e) {
×
114
            throw new RuntimeException("unable to evaluate xpath '" + path + "'", e);
×
115
        }
1✔
116
        return false;
1✔
117
    }
118

119
}
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