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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

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

95.27
/src/main/java/com/meterware/httpunit/FrameHolder.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit;
9

10
import java.io.IOException;
11
import java.net.MalformedURLException;
12
import java.util.ArrayList;
13
import java.util.Enumeration;
14
import java.util.Hashtable;
15
import java.util.List;
16

17
import org.xml.sax.SAXException;
18

19
/**
20
 * The Class FrameHolder.
21
 */
22
class FrameHolder {
23

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

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

30
    /** The window which owns this frame holder. **/
31
    private WebWindow _window;
32

33
    /** The topmost frame in this frameholder. **/
34
    private FrameSelector _topFrame;
35

36
    /**
37
     * Instantiates a new frame holder.
38
     *
39
     * @param window
40
     *            the window
41
     */
42
    FrameHolder(WebWindow window) {
1✔
43
        _window = window;
1✔
44
        _topFrame = FrameSelector.newTopFrame(window);
1✔
45
        DefaultWebResponse blankResponse = new DefaultWebResponse(window.getClient(), null, WebResponse.BLANK_HTML);
1✔
46
        _contents.put(_topFrame, blankResponse);
1✔
47
        HttpUnitOptions.getScriptingEngine().associate(blankResponse);
1✔
48
    }
1✔
49

50
    /**
51
     * Gets the top frame.
52
     *
53
     * @return the top frame
54
     */
55
    FrameSelector getTopFrame() {
56
        return _topFrame;
1✔
57
    }
58

59
    /**
60
     * Gets the frame contents.
61
     *
62
     * @param targetFrame
63
     *            the target frame
64
     *
65
     * @return the frame contents
66
     */
67
    WebResponse getFrameContents(FrameSelector targetFrame) {
68
        if (targetFrame == FrameSelector.TOP_FRAME) {
1✔
69
            targetFrame = getTopFrame();
1✔
70
        }
71
        WebResponse response = get(targetFrame);
1✔
72
        if (response == null) {
1!
73
            throw new NoSuchFrameException(targetFrame.getName());
×
74
        }
75
        return response;
1✔
76
    }
77

78
    /**
79
     * Gets the subframe contents.
80
     *
81
     * @param frame
82
     *            the frame
83
     * @param subFrameName
84
     *            the sub frame name
85
     *
86
     * @return the subframe contents
87
     */
88
    WebResponse getSubframeContents(FrameSelector frame, String subFrameName) {
89
        FrameSelector[] subframes = (FrameSelector[]) _subframes.get(frame);
1✔
90
        if (subframes == null) {
1✔
91
            throw new NoSuchFrameException(subFrameName);
1✔
92
        }
93

94
        for (FrameSelector subframe : subframes) {
1✔
95
            if (subframe.getName().equalsIgnoreCase(subFrameName)) {
1✔
96
                return get(subframe);
1✔
97
            }
98
        }
99
        throw new NoSuchFrameException(subFrameName);
1✔
100
    }
101

102
    /**
103
     * Gets the parent frame contents.
104
     *
105
     * @param frame
106
     *            the frame
107
     *
108
     * @return the parent frame contents
109
     */
110
    WebResponse getParentFrameContents(FrameSelector frame) {
111
        return get(frame.getParent() == null ? _topFrame : frame.getParent());
1✔
112
    }
113

114
    /**
115
     * Gets the.
116
     *
117
     * @param targetFrame
118
     *            the target frame
119
     *
120
     * @return the web response
121
     */
122
    WebResponse get(FrameSelector targetFrame) {
123
        return (WebResponse) _contents.get(targetFrame);
1✔
124
    }
125

126
    /**
127
     * Gets the.
128
     *
129
     * @param target
130
     *            the target
131
     *
132
     * @return the web response
133
     */
134
    WebResponse get(String target) {
135
        FrameSelector frame = getFrame(_topFrame, target);
1✔
136
        return frame == null ? null : (WebResponse) _contents.get(frame);
1!
137
    }
138

139
    /**
140
     * Gets the frame.
141
     *
142
     * @param target
143
     *            the target
144
     *
145
     * @return the frame
146
     */
147
    FrameSelector getFrame(String target) {
148
        return target.equals(_window.getName()) ? _topFrame : getFrame(_topFrame, target);
1✔
149
    }
150

151
    /**
152
     * Gets the frame.
153
     *
154
     * @param rootFrame
155
     *            the root frame
156
     * @param target
157
     *            the target
158
     *
159
     * @return the frame
160
     */
161
    private FrameSelector getFrame(FrameSelector rootFrame, String target) {
162
        if (target.equalsIgnoreCase(WebRequest.TOP_FRAME)) {
1✔
163
            return _topFrame;
1✔
164
        }
165
        if (target.equalsIgnoreCase(rootFrame.getName())) {
1!
166
            return rootFrame;
×
167
        }
168

169
        return lookupFrame(rootFrame, target);
1✔
170
    }
171

172
    /**
173
     * Lookup frame.
174
     *
175
     * @param rootFrame
176
     *            the root frame
177
     * @param target
178
     *            the target
179
     *
180
     * @return the frame selector
181
     */
182
    private FrameSelector lookupFrame(FrameSelector rootFrame, String target) {
183
        FrameSelector result = getFromSubframe(rootFrame, target);
1✔
184
        if (result != null) {
1✔
185
            return result;
1✔
186
        }
187
        if (rootFrame.getName().equals(target)) {
1!
188
            return rootFrame;
×
189
        }
190
        if (rootFrame.getParent() != null) {
1✔
191
            return lookupFrame(rootFrame.getParent(), target);
1✔
192
        }
193
        return null;
1✔
194
    }
195

196
    /**
197
     * Gets the from subframe.
198
     *
199
     * @param rootFrame
200
     *            the root frame
201
     * @param target
202
     *            the target
203
     *
204
     * @return the from subframe
205
     */
206
    private FrameSelector getFromSubframe(FrameSelector rootFrame, String target) {
207
        FrameSelector[] subframes = (FrameSelector[]) _subframes.get(rootFrame);
1✔
208
        if (subframes == null) {
1✔
209
            return null;
1✔
210
        }
211

212
        for (FrameSelector subframe : subframes) {
1✔
213
            if (subframe.getName().equalsIgnoreCase(target)) {
1✔
214
                return subframe;
1✔
215
            }
216
        }
217
        for (FrameSelector subframe : subframes) {
1✔
218
            FrameSelector result = getFromSubframe(subframe, target);
1✔
219
            if (result != null) {
1✔
220
                return result;
1✔
221
            }
222
        }
223
        return null;
1✔
224
    }
225

226
    /**
227
     * Gets the active frame names.
228
     *
229
     * @return the active frame names
230
     */
231
    List<String> getActiveFrameNames() {
232
        List<String> result = new ArrayList<>();
1✔
233
        for (Enumeration e = _contents.keys(); e.hasMoreElements();) {
1✔
234
            result.add(((FrameSelector) e.nextElement()).getName());
1✔
235
        }
236

237
        return result;
1✔
238
    }
239

240
    /**
241
     * Determines the frame in which the reply to a request will be stored.
242
     *
243
     * @param request
244
     *            the request
245
     *
246
     * @return the target frame
247
     */
248
    FrameSelector getTargetFrame(WebRequest request) {
249
        if (WebRequest.NEW_WINDOW.equalsIgnoreCase(request.getTarget())) {
1✔
250
            return FrameSelector.NEW_FRAME;
1✔
251
        }
252
        if (WebRequest.TOP_FRAME.equalsIgnoreCase(request.getTarget())) {
1✔
253
            return _topFrame;
1✔
254
        }
255
        if (WebRequest.SAME_FRAME.equalsIgnoreCase(request.getTarget())) {
1✔
256
            return request.getSourceFrame();
1✔
257
        }
258
        if (WebRequest.PARENT_FRAME.equalsIgnoreCase(request.getTarget())) {
1✔
259
            return request.getSourceFrame().getParent() == null ? _topFrame : request.getSourceFrame().getParent();
1✔
260
        }
261
        if (request.getSourceFrame().getName().equalsIgnoreCase(request.getTarget())) {
1✔
262
            return request.getSourceFrame();
1✔
263
        }
264
        FrameSelector targetFrame = getFrame(request.getSourceFrame(), request.getTarget());
1✔
265
        if (targetFrame == null) {
1✔
266
            targetFrame = _window.getClient().findFrame(request.getTarget());
1✔
267
        }
268
        return targetFrame != null ? targetFrame : FrameSelector.NEW_FRAME;
1✔
269
    }
270

271
    /**
272
     * Update frames.
273
     *
274
     * @param response
275
     *            the response
276
     * @param frame
277
     *            the frame
278
     * @param requestContext
279
     *            the request context
280
     *
281
     * @throws MalformedURLException
282
     *             the malformed URL exception
283
     * @throws IOException
284
     *             Signals that an I/O exception has occurred.
285
     * @throws SAXException
286
     *             the SAX exception
287
     */
288
    void updateFrames(WebResponse response, FrameSelector frame, RequestContext requestContext)
289
            throws MalformedURLException, IOException, SAXException {
290
        removeSubFrames(frame);
1✔
291
        _contents.put(frame, response);
1✔
292

293
        if (response.isHTML()) {
1✔
294
            HttpUnitOptions.getScriptingEngine().associate(response);
1✔
295
            requestContext.addNewResponse(response);
1✔
296
            WebRequest[] requests = response.getFrameRequests();
1✔
297
            if (requests.length > 0) {
1✔
298
                createSubFrames(frame, response.getFrameSelectors());
1✔
299
                for (WebRequest request : requests) {
1✔
300
                    if (request.getURLString().length() != 0) {
1✔
301
                        response.getWindow().getSubframeResponse(request, requestContext);
1✔
302
                    }
303
                }
304
            }
305
        }
306
    }
1✔
307

308
    /**
309
     * Removes the sub frames.
310
     *
311
     * @param frame
312
     *            the frame
313
     */
314
    private void removeSubFrames(FrameSelector frame) {
315
        FrameSelector[] subframes = (FrameSelector[]) _subframes.get(frame);
1✔
316
        if (subframes == null) {
1✔
317
            return;
1✔
318
        }
319

320
        _subframes.remove(frame);
1✔
321
        for (FrameSelector subframe : subframes) {
1✔
322
            removeSubFrames(subframe);
1✔
323
            _contents.remove(subframe);
1✔
324
        }
325
    }
1✔
326

327
    /**
328
     * Creates the sub frames.
329
     *
330
     * @param frame
331
     *            the frame
332
     * @param subframes
333
     *            the subframes
334
     */
335
    private void createSubFrames(FrameSelector frame, FrameSelector[] subframes) {
336
        _subframes.put(frame, subframes);
1✔
337
        for (FrameSelector subframe : subframes) {
1✔
338
            _contents.put(subframe, WebResponse.createBlankResponse());
1✔
339
        }
340
    }
1✔
341

342
    /**
343
     * Given the qualified name of a frame and the name of a nested frame, returns the qualified name of the nested
344
     * frame.
345
     *
346
     * @param parentFrame
347
     *            the parent frame
348
     * @param relativeName
349
     *            the relative name
350
     *
351
     * @return the frame selector
352
     */
353
    static FrameSelector newNestedFrame(FrameSelector parentFrame, final String relativeName) {
354
        if (relativeName == null || relativeName.isEmpty()) {
1!
355
            return new FrameSelector();
1✔
356
        }
357
        return new FrameSelector(relativeName, parentFrame);
1✔
358
    }
359

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