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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

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
    private String path;
42
    // set to true for debugging
43
    public static final boolean DEBUG = false;
44

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

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

74
    /**
75
     * check whether the given criteria are matched for the given element
76
     *
77
     * @param someElement
78
     *            - the element to check
79
     * @param criteria
80
     *            - the criteria to check
81
     */
82
    @Override
83
    public boolean matchesCriteria(final Object someElement, final Object criteria) {
84

85
        // this condition should normally be false
86
        if (!(someElement instanceof HTMLElement)) {
1!
87
            return false;
×
88
        }
89

90
        HTMLElement htmlElement = (HTMLElement) someElement;
1✔
91

92
        Node htmlNode = htmlElement.getNode();
1✔
93
        Document doc = htmlNode.getOwnerDocument();
1✔
94
        if (DEBUG) {
95
            debugOut(doc, "");
96
        }
97

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

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