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

xmlunit / xmlunit / 667

pending completion
667

push

travis-ci-com

bodewig
add Cyclone DX SBOM generation to build

5824 of 6326 relevant lines covered (92.06%)

3.68 hits per line

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

91.43
/xmlunit-legacy/src/main/java/org/custommonkey/xmlunit/NodeTest.java
1
/*
2
******************************************************************
3
Copyright (c) 2001-2007,2015 Jeff Martin, Tim Bacon
4
All rights reserved.
5

6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
8
are met:
9

10
    * Redistributions of source code must retain the above copyright
11
      notice, this list of conditions and the following disclaimer.
12
    * Redistributions in binary form must reproduce the above
13
      copyright notice, this list of conditions and the following
14
      disclaimer in the documentation and/or other materials provided
15
      with the distribution.
16
    * Neither the name of the XMLUnit nor the names
17
      of its contributors may be used to endorse or promote products
18
      derived from this software without specific prior written
19
      permission.
20

21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33

34
******************************************************************
35
*/
36

37
package org.custommonkey.xmlunit;
38

39
import java.io.IOException;
40
import java.io.Reader;
41
import java.io.StringReader;
42

43
import org.w3c.dom.Document;
44
import org.w3c.dom.Node;
45
import org.w3c.dom.traversal.DocumentTraversal;
46
import org.w3c.dom.traversal.NodeFilter;
47
import org.w3c.dom.traversal.NodeIterator;
48
import org.xml.sax.InputSource;
49
import org.xml.sax.SAXException;
50

51
/**
52
 * Encapsulation of the Node-by-Node testing of a DOM Document
53
 * Uses a nodetype-specific <code>NodeFilter</code> to pass the DOM Nodes
54
 * to a NodeTester instance that performs the acual Node validation.
55
 * @see NodeTester
56
 */
57
public class NodeTest {
58
    private final DocumentTraversal documentTraversal;
59
    private final Node rootNode;
60

61
    /**
62
     * Construct a NodeTest for the DOM built using the String and JAXP
63
     */
64
    public NodeTest(String xmlString)
65
        throws SAXException, IOException {
66
        this(new StringReader(xmlString));
4✔
67
    }
4✔
68

69
    /**
70
     * Construct a NodeTest for the DOM built using the Reader and JAXP
71
     */
72
    public NodeTest(Reader reader) throws SAXException,
73
                                          IOException {
74
        this(XMLUnit.buildDocument(XMLUnit.newControlParser(), reader));
4✔
75
    }
4✔
76

77
    /**
78
     * Construct a NodeTest for the DOM built using the InputSource.
79
     */
80
    public NodeTest(InputSource src) throws SAXException,
81
                                            IOException {
82
        this(XMLUnit.buildDocument(XMLUnit.newControlParser(), src));
4✔
83
    }
4✔
84

85
    /**
86
     * Construct a NodeTest for the specified Document
87
     * @exception IllegalArgumentException if the Document does not support the DOM
88
     * DocumentTraversal interface (most DOM implementations should provide this
89
     * support)
90
     */
91
    public NodeTest(Document document) {
92
        this(getDocumentTraversal(document),
4✔
93
             document.getDocumentElement());
4✔
94
    }
4✔
95

96
    /**
97
     * Try to cast a Document into a DocumentTraversal
98
     * @param document
99
     * @return DocumentTraversal interface if the DOM implementation supports it
100
     */
101
    private static DocumentTraversal getDocumentTraversal(Document document) {
102
        try {
103
            return (DocumentTraversal) document;
4✔
104
        } catch (ClassCastException e) {
×
105
            throw new IllegalArgumentException("DOM Traversal not supported by "
×
106
                                               + document.getImplementation().getClass().getName()
×
107
                                               + ". To use this class you will need to switch to a DOM implementation that supports Traversal.");
108
        }
109
    }
110

111
    /**
112
     * Construct a NodeTest using the specified DocumentTraversal, starting at
113
     * the specified root node
114
     */
115
    public NodeTest(DocumentTraversal documentTraversal, Node rootNode) {
4✔
116
        this.documentTraversal = documentTraversal;
4✔
117
        this.rootNode = rootNode;
4✔
118
    }
4✔
119

120
    /**
121
     * Does this NodeTest pass using the specified NodeTester instance?
122
     * @param tester
123
     * @param singleNodeType note <code>Node.ATTRIBUTE_NODE</code> is not
124
     *  exposed by the DocumentTraversal node iterator unless the root node
125
     *  is itself an attribute - so a NodeTester that needs to test attributes
126
     *  should obtain those attributes from <code>Node.ELEMENT_NODE</code>
127
     *  nodes
128
     * @exception NodeTestException if test fails
129
     */
130
    public void performTest(NodeTester tester, short singleNodeType)
131
        throws NodeTestException {
132
        performTest(tester, new short[] {singleNodeType});
4✔
133
    }
4✔
134

135
    /**
136
     * Does this NodeTest pass using the specified NodeTester instance?
137
     * @param tester
138
     * @param nodeTypes note <code>Node.ATTRIBUTE_NODE</code> is not
139
     *  exposed by the DocumentTraversal node iterator unless the root node
140
     *  is itself an attribute - so a NodeTester that needs to test attributes
141
     *  should obtain those attributes from <code>Node.ELEMENT_NODE</code>
142
     *  nodes instead
143
     * @exception NodeTestException if test fails
144
     */
145
    public void performTest(NodeTester tester, short[] nodeTypes)
146
        throws NodeTestException {
147
        NodeIterator iter = documentTraversal.createNodeIterator(rootNode,
4✔
148
                                                                 NodeFilter.SHOW_ALL, new NodeTypeNodeFilter(nodeTypes), true);
149

150
        for (Node nextNode = iter.nextNode(); nextNode != null;
4✔
151
             nextNode = iter.nextNode()) {
4✔
152
            tester.testNode(nextNode, this);
4✔
153
        }
154
        tester.noMoreNodes(this);
4✔
155
    }
4✔
156

157
    /**
158
     * Node type specific Node Filter: accepts Nodes of those types specified
159
     * in constructor, rejects all others
160
     */
161
    private static class NodeTypeNodeFilter implements NodeFilter {
162
        private final short[] nodeTypes;
163

164
        /**
165
         * Construct filter for specific node types
166
         * @param nodeTypes note <code>Node.ATTRIBUTE_NODE</code> is not
167
         *  exposed by the DocumentTraversal node iterator unless the root node
168
         *  is itself an attribute - so a NodeTester that needs to test attributes
169
         *  should obtain those attributes from <code>Node.ELEMENT_NODE</code>
170
         *  nodes
171
         */
172
        public NodeTypeNodeFilter(short[] nodeTypes) {
4✔
173
            this.nodeTypes = nodeTypes;
4✔
174
        }
4✔
175

176
        /**
177
         * NodeFilter method.
178
         * @param aNode
179
         * @return
180
         */
181
        public short acceptNode(Node aNode) {
182
            if (acceptNodeType(aNode.getNodeType())) {
4✔
183
                return NodeFilter.FILTER_ACCEPT;
4✔
184
            }
185
            return NodeFilter.FILTER_REJECT;
4✔
186
        }
187

188
        /**
189
         * Does this instance accept nodes with the node type value
190
         * @param shortVal
191
         * @return
192
         */
193
        private boolean acceptNodeType(short shortVal) {
194
            for (int i=0; i < nodeTypes.length; ++i) {
4✔
195
                if (nodeTypes[i] == shortVal) {
4✔
196
                    return true;
4✔
197
                }
198
            }
199
            return false;
4✔
200
        }
201
    }
202
}
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

© 2025 Coveralls, Inc