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

hazendaz / httpunit / 646

06 Dec 2025 08:11PM UTC coverage: 80.526% (+0.02%) from 80.509%
646

push

github

hazendaz
Cleanup array usage

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

4 of 4 new or added lines in 3 files covered. (100.0%)

532 existing lines in 26 files now uncovered.

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

75.89
/src/main/java/com/meterware/httpunit/dom/ElementImpl.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.dom;
21

22
import java.util.ArrayList;
23
import java.util.Hashtable;
24
import java.util.Iterator;
25

26
import org.w3c.dom.Attr;
27
import org.w3c.dom.DOMException;
28
import org.w3c.dom.Element;
29
import org.w3c.dom.NamedNodeMap;
30
import org.w3c.dom.NodeList;
31
import org.w3c.dom.TypeInfo;
32

33
/**
34
 * The Class ElementImpl.
35
 */
36
public class ElementImpl extends NamespaceAwareNodeImpl implements Element {
1✔
37

38
    /** The Constant serialVersionUID. */
39
    private static final long serialVersionUID = 1L;
40

41
    /** The attributes. */
42
    private Hashtable _attributes = new Hashtable<>();
1✔
43

44
    /** The listeners. */
45
    private ArrayList _listeners = new ArrayList<>();
1✔
46

47
    /**
48
     * Creates the element.
49
     *
50
     * @param owner
51
     *            the owner
52
     * @param tagName
53
     *            the tag name
54
     *
55
     * @return the element impl
56
     */
57
    static ElementImpl createElement(DocumentImpl owner, String tagName) {
58
        ElementImpl element = new ElementImpl();
1✔
59
        element.initialize(owner, tagName);
1✔
60
        return element;
1✔
61
    }
62

63
    /**
64
     * Creates the element.
65
     *
66
     * @param owner
67
     *            the owner
68
     * @param namespaceURI
69
     *            the namespace URI
70
     * @param qualifiedName
71
     *            the qualified name
72
     *
73
     * @return the element
74
     */
75
    public static Element createElement(DocumentImpl owner, String namespaceURI, String qualifiedName) {
76
        ElementImpl element = new ElementImpl();
1✔
77
        element.initialize(owner, namespaceURI, qualifiedName);
1✔
78
        return element;
1✔
79
    }
80

81
    /**
82
     * Adds the dom listener.
83
     *
84
     * @param listener
85
     *            the listener
86
     */
87
    public void addDomListener(DomListener listener) {
88
        synchronized (_listeners) {
1✔
89
            _listeners.add(listener);
1✔
90
        }
1✔
91
    }
1✔
92

93
    /**
94
     * Report property changed.
95
     *
96
     * @param propertyName
97
     *            the property name
98
     */
99
    protected void reportPropertyChanged(String propertyName) {
100
        ArrayList listeners;
101
        synchronized (_listeners) {
1✔
102
            listeners = (ArrayList) _listeners.clone();
1✔
103
        }
1✔
104

105
        for (Iterator each = listeners.iterator(); each.hasNext();) {
1✔
106
            ((DomListener) each.next()).propertyChanged(this, propertyName);
1✔
107
        }
108
    }
1✔
109

110
    // ---------------------------------------- Element methods
111
    // -------------------------------------------------------------
112

113
    @Override
114
    public short getNodeType() {
115
        return ELEMENT_NODE;
1✔
116
    }
117

118
    @Override
119
    public String getNodeValue() throws DOMException {
120
        return null;
1✔
121
    }
122

123
    @Override
124
    public void setNodeValue(String nodeValue) throws DOMException {
125
    }
1✔
126

127
    @Override
128
    public boolean hasAttributes() {
129
        return !_attributes.isEmpty();
1✔
130
    }
131

132
    @Override
133
    public NamedNodeMap getAttributes() {
134
        return new NamedNodeMapImpl(_attributes);
1✔
135
    }
136

137
    /**
138
     * get the attribute with the given name
139
     *
140
     * @param name
141
     *            - the name of the attribute to get
142
     */
143
    @Override
144
    public String getAttribute(String name) {
145
        Attr attr = getAttributeNode(name);
1✔
146
        return attr == null ? "" : attr.getValue();
1✔
147
    }
148

149
    @Override
150
    public void setAttribute(String name, String value) throws DOMException {
151
        if (value.equals(getAttribute(name))) {
1!
152
            return;
×
153
        }
154

155
        Attr attribute = getOwnerDocument().createAttribute(name);
1✔
156
        attribute.setValue(value);
1✔
157
        setAttributeNode(attribute);
1✔
158
        reportPropertyChanged(name);
1✔
159
    }
1✔
160

161
    /**
162
     * get the event Handler script for the event e.g. onchange, onmousedown, onclick, onmouseup execute the script if
163
     * it's assigned by calling doEvent for the script
164
     *
165
     * @param eventName
166
     *            the event name
167
     *
168
     * @return true, if successful
169
     */
170
    @Override
171
    public boolean handleEvent(String eventName) {
172
        // check whether onclick is activated
173
        if (eventName.equalsIgnoreCase("onclick")) {
×
UNCOV
174
            handleEvent("onmousedown");
×
175
        }
176
        String eventScript = getAttribute(eventName);
×
177
        boolean result = doEventScript(eventScript);
×
178
        if (eventName.equalsIgnoreCase("onclick")) {
×
UNCOV
179
            handleEvent("onmouseup");
×
180
        }
UNCOV
181
        return result;
×
182
    }
183

184
    @Override
185
    public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
186
        Attr attribute = getOwnerDocument().createAttributeNS(namespaceURI, qualifiedName);
1✔
187
        attribute.setValue(value);
1✔
188
        setAttributeNodeNS(attribute);
1✔
189
    }
1✔
190

191
    @Override
192
    public void removeAttribute(String name) throws DOMException {
193
        _attributes.remove(name);
1✔
194
    }
1✔
195

196
    @Override
197
    public Attr getAttributeNode(String name) {
198
        return (Attr) _attributes.get(name);
1✔
199
    }
200

201
    @Override
202
    public Attr setAttributeNode(Attr newAttr) throws DOMException {
203
        if (newAttr.getOwnerDocument() != getOwnerDocument()) {
1!
UNCOV
204
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
×
205
                    "attribute must be from the same document as the element");
206
        }
207

208
        ((AttrImpl) newAttr).setOwnerElement(this);
1✔
209
        AttrImpl oldAttr = (AttrImpl) _attributes.put(newAttr.getName(), newAttr);
1✔
210
        if (oldAttr != null) {
1✔
211
            oldAttr.setOwnerElement(null);
1✔
212
        }
213
        return oldAttr;
1✔
214
    }
215

216
    @Override
217
    public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
218
        if (newAttr.getOwnerDocument() != getOwnerDocument()) {
1!
UNCOV
219
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
×
220
                    "attribute must be from the same document as the element");
221
        }
222

223
        ((AttrImpl) newAttr).setOwnerElement(this);
1✔
224
        AttrImpl oldAttr = (AttrImpl) _attributes.put(newAttr.getName(), newAttr);
1✔
225
        if (oldAttr != null) {
1✔
226
            oldAttr.setOwnerElement(null);
1✔
227
        }
228
        return oldAttr;
1✔
229
    }
230

231
    @Override
232
    public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
233
        if (!_attributes.containsValue(oldAttr)) {
1✔
234
            throw new DOMException(DOMException.NOT_FOUND_ERR, "Specified attribute is not defined for this element");
1✔
235
        }
236

237
        AttrImpl removedAttr = (AttrImpl) _attributes.remove(oldAttr.getName());
1✔
238
        if (removedAttr != null) {
1!
239
            removedAttr.setOwnerElement(null);
1✔
240
        }
241
        return removedAttr;
1✔
242
    }
243

244
    @Override
245
    public boolean hasAttribute(String name) {
246
        return _attributes.containsKey(name);
1✔
247
    }
248

249
    // ----------------------- namespaces are not supported at present --------------------------------
250

251
    @Override
252
    public String getAttributeNS(String namespaceURI, String localName) {
UNCOV
253
        return null;
×
254
    }
255

256
    @Override
257
    public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
UNCOV
258
    }
×
259

260
    @Override
261
    public Attr getAttributeNodeNS(String namespaceURI, String localName) {
UNCOV
262
        return null;
×
263
    }
264

265
    @Override
266
    public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
UNCOV
267
        return null;
×
268
    }
269

270
    @Override
271
    public boolean hasAttributeNS(String namespaceURI, String localName) {
UNCOV
272
        return false;
×
273
    }
274

275
    /**
276
     * Import node.
277
     *
278
     * @param document
279
     *            the document
280
     * @param original
281
     *            the original
282
     * @param deep
283
     *            the deep
284
     *
285
     * @return the element
286
     */
287
    public static Element importNode(DocumentImpl document, Element original, boolean deep) {
288
        Element copy = document.createElementNS(original.getNamespaceURI(), original.getTagName());
1✔
289
        NamedNodeMap attributes = original.getAttributes();
1✔
290
        for (int i = 0; i < attributes.getLength(); i++) {
1✔
291
            copy.setAttributeNode((Attr) document.importNode(attributes.item(i), false));
1✔
292
        }
293
        if (deep) {
1✔
294
            document.importChildren(original, copy);
1✔
295
        }
296
        return copy;
1✔
297
    }
298

299
    // ------------------------------------- DOM level 3 methods
300
    // ------------------------------------------------------------
301

302
    @Override
303
    public TypeInfo getSchemaTypeInfo() {
UNCOV
304
        return null; // To change body of implemented methods use File | Settings | File Templates.
×
305
    }
306

307
    @Override
308
    public void setIdAttribute(String name, boolean isId) throws DOMException {
309
        // To change body of implemented methods use File | Settings | File Templates.
UNCOV
310
    }
×
311

312
    @Override
313
    public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
314
        // To change body of implemented methods use File | Settings | File Templates.
UNCOV
315
    }
×
316

317
    @Override
318
    public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
319
        // To change body of implemented methods use File | Settings | File Templates.
UNCOV
320
    }
×
321
}
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