• 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

95.27
/src/main/java/com/meterware/httpunit/FrameHolder.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 java.io.IOException;
23
import java.net.MalformedURLException;
24
import java.util.ArrayList;
25
import java.util.Enumeration;
26
import java.util.Hashtable;
27
import java.util.List;
28

29
import org.xml.sax.SAXException;
30

31
/**
32
 * The Class FrameHolder.
33
 */
34
class FrameHolder {
35

36
    /** Map from a frame selector to its corresponding web response. **/
37
    private Hashtable _contents = new Hashtable<>();
1✔
38

39
    /** Map from a frame selector to its subframe selectors. **/
40
    private Hashtable _subframes = new Hashtable<>();
1✔
41

42
    /** The window which owns this frame holder. **/
43
    private WebWindow _window;
44

45
    /** The topmost frame in this frameholder. **/
46
    private FrameSelector _topFrame;
47

48
    /**
49
     * Instantiates a new frame holder.
50
     *
51
     * @param window
52
     *            the window
53
     */
54
    FrameHolder(WebWindow window) {
1✔
55
        _window = window;
1✔
56
        _topFrame = FrameSelector.newTopFrame(window);
1✔
57
        DefaultWebResponse blankResponse = new DefaultWebResponse(window.getClient(), null, WebResponse.BLANK_HTML);
1✔
58
        _contents.put(_topFrame, blankResponse);
1✔
59
        HttpUnitOptions.getScriptingEngine().associate(blankResponse);
1✔
60
    }
1✔
61

62
    /**
63
     * Gets the top frame.
64
     *
65
     * @return the top frame
66
     */
67
    FrameSelector getTopFrame() {
68
        return _topFrame;
1✔
69
    }
70

71
    /**
72
     * Gets the frame contents.
73
     *
74
     * @param targetFrame
75
     *            the target frame
76
     *
77
     * @return the frame contents
78
     */
79
    WebResponse getFrameContents(FrameSelector targetFrame) {
80
        if (targetFrame == FrameSelector.TOP_FRAME) {
1✔
81
            targetFrame = getTopFrame();
1✔
82
        }
83
        WebResponse response = get(targetFrame);
1✔
84
        if (response == null) {
1!
85
            throw new NoSuchFrameException(targetFrame.getName());
×
86
        }
87
        return response;
1✔
88
    }
89

90
    /**
91
     * Gets the subframe contents.
92
     *
93
     * @param frame
94
     *            the frame
95
     * @param subFrameName
96
     *            the sub frame name
97
     *
98
     * @return the subframe contents
99
     */
100
    WebResponse getSubframeContents(FrameSelector frame, String subFrameName) {
101
        FrameSelector[] subframes = (FrameSelector[]) _subframes.get(frame);
1✔
102
        if (subframes == null) {
1✔
103
            throw new NoSuchFrameException(subFrameName);
1✔
104
        }
105

106
        for (FrameSelector subframe : subframes) {
1✔
107
            if (subframe.getName().equalsIgnoreCase(subFrameName)) {
1✔
108
                return get(subframe);
1✔
109
            }
110
        }
111
        throw new NoSuchFrameException(subFrameName);
1✔
112
    }
113

114
    /**
115
     * Gets the parent frame contents.
116
     *
117
     * @param frame
118
     *            the frame
119
     *
120
     * @return the parent frame contents
121
     */
122
    WebResponse getParentFrameContents(FrameSelector frame) {
123
        return get(frame.getParent() == null ? _topFrame : frame.getParent());
1✔
124
    }
125

126
    /**
127
     * Gets the.
128
     *
129
     * @param targetFrame
130
     *            the target frame
131
     *
132
     * @return the web response
133
     */
134
    WebResponse get(FrameSelector targetFrame) {
135
        return (WebResponse) _contents.get(targetFrame);
1✔
136
    }
137

138
    /**
139
     * Gets the.
140
     *
141
     * @param target
142
     *            the target
143
     *
144
     * @return the web response
145
     */
146
    WebResponse get(String target) {
147
        FrameSelector frame = getFrame(_topFrame, target);
1✔
148
        return frame == null ? null : (WebResponse) _contents.get(frame);
1!
149
    }
150

151
    /**
152
     * Gets the frame.
153
     *
154
     * @param target
155
     *            the target
156
     *
157
     * @return the frame
158
     */
159
    FrameSelector getFrame(String target) {
160
        return target.equals(_window.getName()) ? _topFrame : getFrame(_topFrame, target);
1✔
161
    }
162

163
    /**
164
     * Gets the frame.
165
     *
166
     * @param rootFrame
167
     *            the root frame
168
     * @param target
169
     *            the target
170
     *
171
     * @return the frame
172
     */
173
    private FrameSelector getFrame(FrameSelector rootFrame, String target) {
174
        if (target.equalsIgnoreCase(WebRequest.TOP_FRAME)) {
1✔
175
            return _topFrame;
1✔
176
        }
177
        if (target.equalsIgnoreCase(rootFrame.getName())) {
1!
178
            return rootFrame;
×
179
        }
180

181
        return lookupFrame(rootFrame, target);
1✔
182
    }
183

184
    /**
185
     * Lookup frame.
186
     *
187
     * @param rootFrame
188
     *            the root frame
189
     * @param target
190
     *            the target
191
     *
192
     * @return the frame selector
193
     */
194
    private FrameSelector lookupFrame(FrameSelector rootFrame, String target) {
195
        FrameSelector result = getFromSubframe(rootFrame, target);
1✔
196
        if (result != null) {
1✔
197
            return result;
1✔
198
        }
199
        if (rootFrame.getName().equals(target)) {
1!
200
            return rootFrame;
×
201
        }
202
        if (rootFrame.getParent() != null) {
1✔
203
            return lookupFrame(rootFrame.getParent(), target);
1✔
204
        }
205
        return null;
1✔
206
    }
207

208
    /**
209
     * Gets the from subframe.
210
     *
211
     * @param rootFrame
212
     *            the root frame
213
     * @param target
214
     *            the target
215
     *
216
     * @return the from subframe
217
     */
218
    private FrameSelector getFromSubframe(FrameSelector rootFrame, String target) {
219
        FrameSelector[] subframes = (FrameSelector[]) _subframes.get(rootFrame);
1✔
220
        if (subframes == null) {
1✔
221
            return null;
1✔
222
        }
223

224
        for (FrameSelector subframe : subframes) {
1✔
225
            if (subframe.getName().equalsIgnoreCase(target)) {
1✔
226
                return subframe;
1✔
227
            }
228
        }
229
        for (FrameSelector subframe : subframes) {
1✔
230
            FrameSelector result = getFromSubframe(subframe, target);
1✔
231
            if (result != null) {
1✔
232
                return result;
1✔
233
            }
234
        }
235
        return null;
1✔
236
    }
237

238
    /**
239
     * Gets the active frame names.
240
     *
241
     * @return the active frame names
242
     */
243
    List<String> getActiveFrameNames() {
244
        List<String> result = new ArrayList<>();
1✔
245
        for (Enumeration e = _contents.keys(); e.hasMoreElements();) {
1✔
246
            result.add(((FrameSelector) e.nextElement()).getName());
1✔
247
        }
248

249
        return result;
1✔
250
    }
251

252
    /**
253
     * Determines the frame in which the reply to a request will be stored.
254
     *
255
     * @param request
256
     *            the request
257
     *
258
     * @return the target frame
259
     */
260
    FrameSelector getTargetFrame(WebRequest request) {
261
        if (WebRequest.NEW_WINDOW.equalsIgnoreCase(request.getTarget())) {
1✔
262
            return FrameSelector.NEW_FRAME;
1✔
263
        }
264
        if (WebRequest.TOP_FRAME.equalsIgnoreCase(request.getTarget())) {
1✔
265
            return _topFrame;
1✔
266
        }
267
        if (WebRequest.SAME_FRAME.equalsIgnoreCase(request.getTarget())) {
1✔
268
            return request.getSourceFrame();
1✔
269
        }
270
        if (WebRequest.PARENT_FRAME.equalsIgnoreCase(request.getTarget())) {
1✔
271
            return request.getSourceFrame().getParent() == null ? _topFrame : request.getSourceFrame().getParent();
1✔
272
        }
273
        if (request.getSourceFrame().getName().equalsIgnoreCase(request.getTarget())) {
1✔
274
            return request.getSourceFrame();
1✔
275
        }
276
        FrameSelector targetFrame = getFrame(request.getSourceFrame(), request.getTarget());
1✔
277
        if (targetFrame == null) {
1✔
278
            targetFrame = _window.getClient().findFrame(request.getTarget());
1✔
279
        }
280
        return targetFrame != null ? targetFrame : FrameSelector.NEW_FRAME;
1✔
281
    }
282

283
    /**
284
     * Update frames.
285
     *
286
     * @param response
287
     *            the response
288
     * @param frame
289
     *            the frame
290
     * @param requestContext
291
     *            the request context
292
     *
293
     * @throws MalformedURLException
294
     *             the malformed URL exception
295
     * @throws IOException
296
     *             Signals that an I/O exception has occurred.
297
     * @throws SAXException
298
     *             the SAX exception
299
     */
300
    void updateFrames(WebResponse response, FrameSelector frame, RequestContext requestContext)
301
            throws MalformedURLException, IOException, SAXException {
302
        removeSubFrames(frame);
1✔
303
        _contents.put(frame, response);
1✔
304

305
        if (response.isHTML()) {
1✔
306
            HttpUnitOptions.getScriptingEngine().associate(response);
1✔
307
            requestContext.addNewResponse(response);
1✔
308
            WebRequest[] requests = response.getFrameRequests();
1✔
309
            if (requests.length > 0) {
1✔
310
                createSubFrames(frame, response.getFrameSelectors());
1✔
311
                for (WebRequest request : requests) {
1✔
312
                    if (request.getURLString().length() != 0) {
1✔
313
                        response.getWindow().getSubframeResponse(request, requestContext);
1✔
314
                    }
315
                }
316
            }
317
        }
318
    }
1✔
319

320
    /**
321
     * Removes the sub frames.
322
     *
323
     * @param frame
324
     *            the frame
325
     */
326
    private void removeSubFrames(FrameSelector frame) {
327
        FrameSelector[] subframes = (FrameSelector[]) _subframes.get(frame);
1✔
328
        if (subframes == null) {
1✔
329
            return;
1✔
330
        }
331

332
        _subframes.remove(frame);
1✔
333
        for (FrameSelector subframe : subframes) {
1✔
334
            removeSubFrames(subframe);
1✔
335
            _contents.remove(subframe);
1✔
336
        }
337
    }
1✔
338

339
    /**
340
     * Creates the sub frames.
341
     *
342
     * @param frame
343
     *            the frame
344
     * @param subframes
345
     *            the subframes
346
     */
347
    private void createSubFrames(FrameSelector frame, FrameSelector[] subframes) {
348
        _subframes.put(frame, subframes);
1✔
349
        for (FrameSelector subframe : subframes) {
1✔
350
            _contents.put(subframe, WebResponse.createBlankResponse());
1✔
351
        }
352
    }
1✔
353

354
    /**
355
     * Given the qualified name of a frame and the name of a nested frame, returns the qualified name of the nested
356
     * frame.
357
     *
358
     * @param parentFrame
359
     *            the parent frame
360
     * @param relativeName
361
     *            the relative name
362
     *
363
     * @return the frame selector
364
     */
365
    static FrameSelector newNestedFrame(FrameSelector parentFrame, final String relativeName) {
366
        if (relativeName == null || relativeName.isEmpty()) {
1!
367
            return new FrameSelector();
1✔
368
        }
369
        return new FrameSelector(relativeName, parentFrame);
1✔
370
    }
371

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