001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: FormParameter.java,v 1.7 2004/03/15 02:42:16 russgold Exp $
005: *
006: * Copyright (c) 2002-2003, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023:
024: import com.meterware.httpunit.scripting.ScriptableDelegate;
025:
026: import java.lang.String;
027: import java.util.ArrayList;
028: import java.util.Arrays;
029:
030: /**
031: * Represents the aggregate of all form controls with a particular name. This permits us to abstract setting
032: * values so that changing a control type does not break a test.
033: *
034: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
035: **/
036: class FormParameter {
037:
038: private FormControl[] _controls;
039: private ArrayList _controlList = new ArrayList();
040: private RadioGroupFormControl _group;
041: private String _name;
042:
043: void addControl(FormControl control) {
044: _controls = null;
045: if (_name == null)
046: _name = control.getName();
047: if (!_name.equalsIgnoreCase(control.getName()))
048: throw new RuntimeException(
049: "all controls should have the same name");
050: if (control.isExclusive()) {
051: getRadioGroup(control.getForm()).addRadioButton(
052: (RadioButtonFormControl) control);
053: } else {
054: _controlList.add(control);
055: }
056: }
057:
058: private FormControl[] getControls() {
059: if (_controls == null)
060: _controls = (FormControl[]) _controlList
061: .toArray(new FormControl[_controlList.size()]);
062: return _controls;
063: }
064:
065: Object getScriptableObject() {
066: if (getControls().length == 1) {
067: return getControls()[0].getDelegate();
068: } else {
069: ArrayList list = new ArrayList();
070: for (int i = 0; i < _controls.length; i++) {
071: FormControl control = _controls[i];
072: list.add(control.getScriptableDelegate());
073: }
074: return list.toArray(new ScriptableDelegate[list.size()]);
075: }
076: }
077:
078: String[] getValues() {
079: ArrayList valueList = new ArrayList();
080: FormControl[] controls = getControls();
081: for (int i = 0; i < controls.length; i++) {
082: valueList.addAll(Arrays.asList(controls[i].getValues()));
083: }
084: return (String[]) valueList
085: .toArray(new String[valueList.size()]);
086: }
087:
088: void setValues(String[] values) {
089: ArrayList list = new ArrayList(values.length);
090: list.addAll(Arrays.asList(values));
091: for (int i = 0; i < getControls().length; i++)
092: getControls()[i].claimRequiredValues(list);
093: for (int i = 0; i < getControls().length; i++)
094: getControls()[i].claimUniqueValue(list);
095: for (int i = 0; i < getControls().length; i++)
096: getControls()[i].claimValue(list);
097: if (!list.isEmpty())
098: throw new UnusedParameterValueException(_name,
099: (String) list.get(0));
100: }
101:
102: public void toggleCheckbox() {
103: FormControl[] controls = getControls();
104: if (controls.length != 1)
105: throw new IllegalCheckboxParameterException(_name,
106: "toggleCheckbox");
107: controls[0].toggle();
108: }
109:
110: public void toggleCheckbox(String value) {
111: FormControl[] controls = getControls();
112: for (int i = 0; i < controls.length; i++) {
113: FormControl control = controls[i];
114: if (value.equals(control.getValueAttribute())) {
115: control.toggle();
116: return;
117: }
118: }
119: throw new IllegalCheckboxParameterException(
120: _name + "/" + value, "toggleCheckbox");
121: }
122:
123: public void setValue(boolean state) {
124: FormControl[] controls = getControls();
125: if (controls.length != 1)
126: throw new IllegalCheckboxParameterException(_name,
127: "setCheckbox");
128: controls[0].setState(state);
129: }
130:
131: public void setValue(String value, boolean state) {
132: FormControl[] controls = getControls();
133: for (int i = 0; i < controls.length; i++) {
134: FormControl control = controls[i];
135: if (value.equals(control.getValueAttribute())) {
136: control.setState(state);
137: return;
138: }
139: }
140: throw new IllegalCheckboxParameterException(
141: _name + "/" + value, "setCheckbox");
142: }
143:
144: void setFiles(UploadFileSpec[] fileArray) {
145: ArrayList list = new ArrayList(fileArray.length);
146: list.addAll(Arrays.asList(fileArray));
147: for (int i = 0; i < getControls().length; i++)
148: getControls()[i].claimUploadSpecification(list);
149: if (!list.isEmpty())
150: throw new UnusedUploadFileException(_name, fileArray.length
151: - list.size(), fileArray.length);
152: }
153:
154: String[] getOptions() {
155: ArrayList optionList = new ArrayList();
156: FormControl[] controls = getControls();
157: for (int i = 0; i < controls.length; i++) {
158: optionList.addAll(Arrays.asList(controls[i]
159: .getDisplayedOptions()));
160: }
161: return (String[]) optionList.toArray(new String[optionList
162: .size()]);
163: }
164:
165: String[] getOptionValues() {
166: ArrayList valueList = new ArrayList();
167: for (int i = 0; i < getControls().length; i++) {
168: valueList.addAll(Arrays.asList(getControls()[i]
169: .getOptionValues()));
170: }
171: return (String[]) valueList
172: .toArray(new String[valueList.size()]);
173: }
174:
175: boolean isMultiValuedParameter() {
176: FormControl[] controls = getControls();
177: for (int i = 0; i < controls.length; i++) {
178: if (controls[i].isMultiValued())
179: return true;
180: if (!controls[i].isExclusive() && controls.length > 1)
181: return true;
182: }
183: return false;
184: }
185:
186: int getNumTextParameters() {
187: int result = 0;
188: FormControl[] controls = getControls();
189: for (int i = 0; i < controls.length; i++) {
190: if (controls[i].isTextControl())
191: result++;
192: }
193: return result;
194: }
195:
196: boolean isTextParameter() {
197: FormControl[] controls = getControls();
198: for (int i = 0; i < controls.length; i++) {
199: if (controls[i].isTextControl())
200: return true;
201: }
202: return false;
203: }
204:
205: boolean isFileParameter() {
206: FormControl[] controls = getControls();
207: for (int i = 0; i < controls.length; i++) {
208: if (controls[i].isFileParameter())
209: return true;
210: }
211: return false;
212: }
213:
214: boolean isDisabledParameter() {
215: FormControl[] controls = getControls();
216: for (int i = 0; i < controls.length; i++) {
217: if (!controls[i].isDisabled())
218: return false;
219: }
220: return true;
221: }
222:
223: boolean isReadOnlyParameter() {
224: FormControl[] controls = getControls();
225: for (int i = 0; i < controls.length; i++) {
226: if (!controls[i].isReadOnly())
227: return false;
228: }
229: return true;
230: }
231:
232: public boolean isHiddenParameter() {
233: FormControl[] controls = getControls();
234: for (int i = 0; i < controls.length; i++) {
235: if (!controls[i].isHidden())
236: return false;
237: }
238: return true;
239: }
240:
241: private RadioGroupFormControl getRadioGroup(WebForm form) {
242: if (_group == null) {
243: _group = new RadioGroupFormControl(form);
244: _controlList.add(_group);
245: }
246: return _group;
247: }
248:
249: //============================= exception class UnusedParameterValueException ======================================
250:
251: /**
252: * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form.
253: **/
254: class UnusedParameterValueException extends
255: IllegalRequestParameterException {
256:
257: UnusedParameterValueException(String parameterName,
258: String badValue) {
259: _parameterName = parameterName;
260: _badValue = badValue;
261: }
262:
263: public String getMessage() {
264: StringBuffer sb = new StringBuffer(
265: HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
266: sb.append("Attempted to assign to parameter '").append(
267: _parameterName);
268: sb.append("' the extraneous value '").append(_badValue)
269: .append("'.");
270: return sb.toString();
271: }
272:
273: private String _parameterName;
274: private String _badValue;
275: }
276:
277: //============================= exception class UnusedUploadFileException ======================================
278:
279: /**
280: * This exception is thrown on an attempt to upload more files than permitted by the form.
281: **/
282: class UnusedUploadFileException extends
283: IllegalRequestParameterException {
284:
285: UnusedUploadFileException(String parameterName,
286: int numFilesExpected, int numFilesSupplied) {
287: _parameterName = parameterName;
288: _numExpected = numFilesExpected;
289: _numSupplied = numFilesSupplied;
290: }
291:
292: public String getMessage() {
293: StringBuffer sb = new StringBuffer(
294: HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
295: sb.append("Attempted to upload ").append(_numSupplied)
296: .append(" files using parameter '").append(
297: _parameterName);
298: if (_numExpected == 0) {
299: sb.append("' which is not a file parameter.");
300: } else {
301: sb.append("' which only has room for ").append(
302: _numExpected).append('.');
303: }
304: return sb.toString();
305: }
306:
307: private String _parameterName;
308: private int _numExpected;
309: private int _numSupplied;
310: }
311:
312: //============================= exception class IllegalCheckboxParameterException ======================================
313:
314: /**
315: * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form.
316: **/
317: static class IllegalCheckboxParameterException extends
318: IllegalRequestParameterException {
319:
320: IllegalCheckboxParameterException(String parameterName,
321: String methodName) {
322: _parameterName = parameterName;
323: _methodName = methodName;
324: }
325:
326: public String getMessage() {
327: StringBuffer sb = new StringBuffer(
328: HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
329: sb.append("Attempted to invoke method '").append(
330: _methodName);
331: sb
332: .append("' for parameter '")
333: .append(_parameterName)
334: .append(
335: "', which is not a unique checkbox control.");
336: return sb.toString();
337: }
338:
339: private String _parameterName;
340: private String _methodName;
341: }
342:
343: }
|