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

xmlunit / xmlunit / cd160610-9b67-4752-a2c4-e3a0d82d991e

21 Apr 2025 11:55AM UTC coverage: 91.756% (-0.02%) from 91.78%
cd160610-9b67-4752-a2c4-e3a0d82d991e

push

circleci

web-flow
Merge pull request #289 from xmlunit/circleci-project-setup

CircleCI project setup

3996 of 4698 branches covered (85.06%)

11754 of 12810 relevant lines covered (91.76%)

2.35 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
     * @param xmlString the XML to test
64
     * @throws SAXException if the parser says so
65
     * @throws IOException on I/O errors
66
     */
67
    public NodeTest(String xmlString)
68
        throws SAXException, IOException {
69
        this(new StringReader(xmlString));
1✔
70
    }
1✔
71

72
    /**
73
     * Construct a NodeTest for the DOM built using the Reader and JAXP
74
     * @param reader the XML to test
75
     * @throws SAXException if the parser says so
76
     * @throws IOException on I/O errors
77
     */
78
    public NodeTest(Reader reader) throws SAXException,
79
                                          IOException {
80
        this(XMLUnit.buildDocument(XMLUnit.newControlParser(), reader));
1✔
81
    }
1✔
82

83
    /**
84
     * Construct a NodeTest for the DOM built using the InputSource.
85
     * @param src the XML to test
86
     * @throws SAXException if the parser says so
87
     * @throws IOException on I/O errors
88
     */
89
    public NodeTest(InputSource src) throws SAXException,
90
                                            IOException {
91
        this(XMLUnit.buildDocument(XMLUnit.newControlParser(), src));
1✔
92
    }
1✔
93

94
    /**
95
     * Construct a NodeTest for the specified Document
96
     * @exception IllegalArgumentException if the Document does not support the DOM
97
     * DocumentTraversal interface (most DOM implementations should provide this
98
     * support)
99
     * @param document the XML to test
100
     */
101
    public NodeTest(Document document) {
102
        this(getDocumentTraversal(document),
1✔
103
             document.getDocumentElement());
1✔
104
    }
1✔
105

106
    /**
107
     * Try to cast a Document into a DocumentTraversal
108
     * @return DocumentTraversal interface if the DOM implementation supports it
109
     */
110
    private static DocumentTraversal getDocumentTraversal(Document document) {
111
        try {
112
            return (DocumentTraversal) document;
1✔
113
        } catch (ClassCastException e) {
×
114
            throw new IllegalArgumentException("DOM Traversal not supported by "
×
115
                                               + document.getImplementation().getClass().getName()
×
116
                                               + ". To use this class you will need to switch to a DOM implementation that supports Traversal.");
117
        }
118
    }
119

120
    /**
121
     * Construct a NodeTest using the specified DocumentTraversal, starting at
122
     * the specified root node
123
     * @param documentTraversal traversal
124
     * @param rootNode starting node for test
125
     */
126
    public NodeTest(DocumentTraversal documentTraversal, Node rootNode) {
1✔
127
        this.documentTraversal = documentTraversal;
1✔
128
        this.rootNode = rootNode;
1✔
129
    }
1✔
130

131
    /**
132
     * Does this NodeTest pass using the specified NodeTester instance?
133
     * @param tester actually performs the tests
134
     * @param singleNodeType note <code>Node.ATTRIBUTE_NODE</code> is not
135
     *  exposed by the DocumentTraversal node iterator unless the root node
136
     *  is itself an attribute - so a NodeTester that needs to test attributes
137
     *  should obtain those attributes from <code>Node.ELEMENT_NODE</code>
138
     *  nodes
139
     * @exception NodeTestException if test fails
140
     */
141
    public void performTest(NodeTester tester, short singleNodeType)
142
        throws NodeTestException {
143
        performTest(tester, new short[] {singleNodeType});
1✔
144
    }
1✔
145

146
    /**
147
     * Does this NodeTest pass using the specified NodeTester instance?
148
     * @param tester actually performs the tests
149
     * @param nodeTypes note <code>Node.ATTRIBUTE_NODE</code> is not
150
     *  exposed by the DocumentTraversal node iterator unless the root node
151
     *  is itself an attribute - so a NodeTester that needs to test attributes
152
     *  should obtain those attributes from <code>Node.ELEMENT_NODE</code>
153
     *  nodes instead
154
     * @exception NodeTestException if test fails
155
     */
156
    public void performTest(NodeTester tester, short[] nodeTypes)
157
        throws NodeTestException {
158
        NodeIterator iter = documentTraversal.createNodeIterator(rootNode,
1✔
159
                                                                 NodeFilter.SHOW_ALL, new NodeTypeNodeFilter(nodeTypes), true);
160

161
        for (Node nextNode = iter.nextNode(); nextNode != null;
1✔
162
             nextNode = iter.nextNode()) {
1✔
163
            tester.testNode(nextNode, this);
1✔
164
        }
165
        tester.noMoreNodes(this);
1✔
166
    }
1✔
167

168
    /**
169
     * Node type specific Node Filter: accepts Nodes of those types specified
170
     * in constructor, rejects all others
171
     */
172
    private static class NodeTypeNodeFilter implements NodeFilter {
173
        private final short[] nodeTypes;
174

175
        /**
176
         * Construct filter for specific node types
177
         * @param nodeTypes note <code>Node.ATTRIBUTE_NODE</code> is not
178
         *  exposed by the DocumentTraversal node iterator unless the root node
179
         *  is itself an attribute - so a NodeTester that needs to test attributes
180
         *  should obtain those attributes from <code>Node.ELEMENT_NODE</code>
181
         *  nodes
182
         */
183
        public NodeTypeNodeFilter(short[] nodeTypes) {
1✔
184
            this.nodeTypes = nodeTypes;
1✔
185
        }
1✔
186

187
        /**
188
         * NodeFilter method.
189
         * @param aNode
190
         * @return
191
         */
192
        public short acceptNode(Node aNode) {
193
            if (acceptNodeType(aNode.getNodeType())) {
1✔
194
                return NodeFilter.FILTER_ACCEPT;
1✔
195
            }
196
            return NodeFilter.FILTER_REJECT;
1✔
197
        }
198

199
        /**
200
         * Does this instance accept nodes with the node type value
201
         * @param shortVal
202
         * @return
203
         */
204
        private boolean acceptNodeType(short shortVal) {
205
            for (int i=0; i < nodeTypes.length; ++i) {
1✔
206
                if (nodeTypes[i] == shortVal) {
1✔
207
                    return true;
1✔
208
                }
209
            }
210
            return false;
1✔
211
        }
212
    }
213
}
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