Source code: com/meterware/httpunit/FormParameter.java
1 package com.meterware.httpunit;
2 /********************************************************************************************************************
3 * $Id: FormParameter.java,v 1.7 2004/03/15 02:42:16 russgold Exp $
4 *
5 * Copyright (c) 2002-2003, Russell Gold
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
16 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *******************************************************************************************************************/
22
23 import com.meterware.httpunit.scripting.ScriptableDelegate;
24
25 import java.lang.String;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28
29 /**
30 * Represents the aggregate of all form controls with a particular name. This permits us to abstract setting
31 * values so that changing a control type does not break a test.
32 *
33 * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
34 **/
35 class FormParameter {
36
37
38 private FormControl[] _controls;
39 private ArrayList _controlList = new ArrayList();
40 private RadioGroupFormControl _group;
41 private String _name;
42
43
44 void addControl( FormControl control ) {
45 _controls = null;
46 if (_name == null) _name = control.getName();
47 if (!_name.equalsIgnoreCase( control.getName() )) throw new RuntimeException( "all controls should have the same name" );
48 if (control.isExclusive()) {
49 getRadioGroup( control.getForm() ).addRadioButton( (RadioButtonFormControl) control );
50 } else {
51 _controlList.add( control );
52 }
53 }
54
55
56 private FormControl[] getControls() {
57 if (_controls == null) _controls = (FormControl[]) _controlList.toArray( new FormControl[ _controlList.size() ] );
58 return _controls;
59 }
60
61
62 Object getScriptableObject() {
63 if (getControls().length == 1) {
64 return getControls()[0].getDelegate();
65 } else {
66 ArrayList list = new ArrayList();
67 for (int i = 0; i < _controls.length; i++) {
68 FormControl control = _controls[i];
69 list.add( control.getScriptableDelegate() );
70 }
71 return list.toArray( new ScriptableDelegate[ list.size() ] );
72 }
73 }
74
75
76 String[] getValues() {
77 ArrayList valueList = new ArrayList();
78 FormControl[] controls = getControls();
79 for (int i = 0; i < controls.length; i++) {
80 valueList.addAll( Arrays.asList( controls[i].getValues() ) );
81 }
82 return (String[]) valueList.toArray( new String[ valueList.size() ] );
83 }
84
85
86 void setValues( String[] values ) {
87 ArrayList list = new ArrayList( values.length );
88 list.addAll( Arrays.asList( values ) );
89 for (int i = 0; i < getControls().length; i++) getControls()[i].claimRequiredValues( list );
90 for (int i = 0; i < getControls().length; i++) getControls()[i].claimUniqueValue( list );
91 for (int i = 0; i < getControls().length; i++) getControls()[i].claimValue( list );
92 if (!list.isEmpty()) throw new UnusedParameterValueException( _name, (String) list.get(0) );
93 }
94
95
96 public void toggleCheckbox() {
97 FormControl[] controls = getControls();
98 if (controls.length != 1) throw new IllegalCheckboxParameterException( _name, "toggleCheckbox" );
99 controls[0].toggle();
100 }
101
102
103 public void toggleCheckbox( String value ) {
104 FormControl[] controls = getControls();
105 for (int i = 0; i < controls.length; i++) {
106 FormControl control = controls[i];
107 if (value.equals( control.getValueAttribute())) {
108 control.toggle();
109 return;
110 }
111 }
112 throw new IllegalCheckboxParameterException( _name + "/" + value , "toggleCheckbox" );
113 }
114
115
116 public void setValue( boolean state ) {
117 FormControl[] controls = getControls();
118 if (controls.length != 1) throw new IllegalCheckboxParameterException( _name, "setCheckbox" );
119 controls[0].setState( state );
120 }
121
122
123 public void setValue( String value, boolean state ) {
124 FormControl[] controls = getControls();
125 for (int i = 0; i < controls.length; i++) {
126 FormControl control = controls[i];
127 if (value.equals( control.getValueAttribute())) {
128 control.setState( state );
129 return;
130 }
131 }
132 throw new IllegalCheckboxParameterException( _name + "/" + value , "setCheckbox" );
133 }
134
135
136 void setFiles( UploadFileSpec[] fileArray ) {
137 ArrayList list = new ArrayList( fileArray.length );
138 list.addAll( Arrays.asList( fileArray ) );
139 for (int i = 0; i < getControls().length; i++) getControls()[i].claimUploadSpecification( list );
140 if (!list.isEmpty()) throw new UnusedUploadFileException( _name, fileArray.length - list.size(), fileArray.length );
141 }
142
143
144 String[] getOptions() {
145 ArrayList optionList = new ArrayList();
146 FormControl[] controls = getControls();
147 for (int i = 0; i < controls.length; i++) {
148 optionList.addAll( Arrays.asList( controls[i].getDisplayedOptions() ) );
149 }
150 return (String[]) optionList.toArray( new String[ optionList.size() ] );
151 }
152
153
154 String[] getOptionValues() {
155 ArrayList valueList = new ArrayList();
156 for (int i = 0; i < getControls().length; i++) {
157 valueList.addAll( Arrays.asList( getControls()[i].getOptionValues() ) );
158 }
159 return (String[]) valueList.toArray( new String[ valueList.size() ] );
160 }
161
162
163 boolean isMultiValuedParameter() {
164 FormControl[] controls = getControls();
165 for (int i = 0; i < controls.length; i++) {
166 if (controls[i].isMultiValued()) return true;
167 if (!controls[i].isExclusive() && controls.length > 1) return true;
168 }
169 return false;
170 }
171
172
173 int getNumTextParameters() {
174 int result = 0;
175 FormControl[] controls = getControls();
176 for (int i = 0; i < controls.length; i++) {
177 if (controls[i].isTextControl()) result++;
178 }
179 return result;
180 }
181
182
183 boolean isTextParameter() {
184 FormControl[] controls = getControls();
185 for (int i = 0; i < controls.length; i++) {
186 if (controls[i].isTextControl()) return true;
187 }
188 return false;
189 }
190
191
192 boolean isFileParameter() {
193 FormControl[] controls = getControls();
194 for (int i = 0; i < controls.length; i++) {
195 if (controls[i].isFileParameter()) return true;
196 }
197 return false;
198 }
199
200
201 boolean isDisabledParameter() {
202 FormControl[] controls = getControls();
203 for (int i = 0; i < controls.length; i++) {
204 if (!controls[i].isDisabled()) return false;
205 }
206 return true;
207 }
208
209
210 boolean isReadOnlyParameter() {
211 FormControl[] controls = getControls();
212 for (int i = 0; i < controls.length; i++) {
213 if (!controls[i].isReadOnly()) return false;
214 }
215 return true;
216 }
217
218
219 public boolean isHiddenParameter() {
220 FormControl[] controls = getControls();
221 for (int i = 0; i < controls.length; i++) {
222 if (!controls[i].isHidden()) return false;
223 }
224 return true;
225 }
226
227
228 private RadioGroupFormControl getRadioGroup( WebForm form ) {
229 if (_group == null) {
230 _group = new RadioGroupFormControl( form );
231 _controlList.add( _group );
232 }
233 return _group;
234 }
235
236
237 //============================= exception class UnusedParameterValueException ======================================
238
239
240 /**
241 * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form.
242 **/
243 class UnusedParameterValueException extends IllegalRequestParameterException {
244
245
246 UnusedParameterValueException( String parameterName, String badValue ) {
247 _parameterName = parameterName;
248 _badValue = badValue;
249 }
250
251
252 public String getMessage() {
253 StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
254 sb.append( "Attempted to assign to parameter '" ).append( _parameterName );
255 sb.append( "' the extraneous value '" ).append( _badValue ).append( "'." );
256 return sb.toString();
257 }
258
259
260 private String _parameterName;
261 private String _badValue;
262 }
263
264
265 //============================= exception class UnusedUploadFileException ======================================
266
267
268 /**
269 * This exception is thrown on an attempt to upload more files than permitted by the form.
270 **/
271 class UnusedUploadFileException extends IllegalRequestParameterException {
272
273
274 UnusedUploadFileException( String parameterName, int numFilesExpected, int numFilesSupplied ) {
275 _parameterName = parameterName;
276 _numExpected = numFilesExpected;
277 _numSupplied = numFilesSupplied;
278 }
279
280
281 public String getMessage() {
282 StringBuffer sb = new StringBuffer( HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE );
283 sb.append( "Attempted to upload " ).append( _numSupplied ).append( " files using parameter '" ).append( _parameterName );
284 if (_numExpected == 0) {
285 sb.append( "' which is not a file parameter." );
286 } else {
287 sb.append( "' which only has room for " ).append( _numExpected ).append( '.' );
288 }
289 return sb.toString();
290 }
291
292
293 private String _parameterName;
294 private int _numExpected;
295 private int _numSupplied;
296 }
297
298
299 //============================= exception class IllegalCheckboxParameterException ======================================
300
301
302 /**
303 * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form.
304 **/
305 static class IllegalCheckboxParameterException extends IllegalRequestParameterException {
306
307
308 IllegalCheckboxParameterException( String parameterName, String methodName ) {
309 _parameterName = parameterName;
310 _methodName = methodName;
311 }
312
313
314 public String getMessage() {
315 StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
316 sb.append( "Attempted to invoke method '" ).append( _methodName );
317 sb.append( "' for parameter '" ).append( _parameterName ).append( "', which is not a unique checkbox control." );
318 return sb.toString();
319 }
320
321
322 private String _parameterName;
323 private String _methodName;
324 }
325
326
327
328 }
329