/
FormTester.java
905 lines (825 loc) · 25.3 KB
/
FormTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.util.tester;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.AbstractSingleSelectChoice;
import org.apache.wicket.markup.html.form.AbstractTextComponent;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.IFormSubmittingComponent;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.form.upload.MultiFileUploadField;
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.string.StringValue;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
/**
* A helper class for testing validation and submission of <code>FormComponent</code>s.
*
* @author Ingram Chen
* @author Frank Bille (frankbille)
* @since 1.2.6
*/
public class FormTester
{
/**
* An auto incrementing index used as a suffix for MultiFileUploadField's inputName
*/
private int multiFileUploadIndex = 0;
/**
* A selector template for selecting selectable <code>FormComponent</code>s with an index of
* option -- supports <code>RadioGroup</code>, <code>CheckGroup</code>, and
* <code>AbstractChoice</code> family.
*/
protected abstract class ChoiceSelector
{
/**
* TODO need Javadoc from author.
*/
private final class SearchOptionByIndexVisitor implements IVisitor<Component, Component>
{
int count = 0;
private final int index;
private SearchOptionByIndexVisitor(int index)
{
super();
this.index = index;
}
/**
* @see org.apache.wicket.util.visit.IVisitor#component(Object,
* org.apache.wicket.util.visit.IVisit)
*/
@Override
public void component(final Component component, final IVisit<Component> visit)
{
if (count == index)
{
visit.stop(component);
}
else
{
count++;
}
}
}
private final FormComponent<?> formComponent;
/**
* Constructor.
*
* @param formComponent
* a <code>FormComponent</code>
*/
protected ChoiceSelector(FormComponent<?> formComponent)
{
this.formComponent = formComponent;
}
/**
* Implements whether toggle or accumulate the selection.
*
* @param formComponent
* a <code>FormComponent</code>
* @param value
* a <code>String</code> value
*/
protected abstract void assignValueToFormComponent(FormComponent<?> formComponent,
String value);
public String getChoiceValueForIndex(int index)
{
if (formComponent instanceof RadioGroup)
{
Radio<?> foundRadio = (Radio<?>)formComponent.visitChildren(Radio.class,
new SearchOptionByIndexVisitor(index));
if (foundRadio == null)
{
fail("RadioGroup " + formComponent.getPath() + " does not have index:" + index);
return null;
}
return foundRadio.getValue();
}
else if (formComponent instanceof CheckGroup)
{
Check<?> foundCheck = (Check<?>)formComponent.visitChildren(Check.class,
new SearchOptionByIndexVisitor(index));
if (foundCheck == null)
{
fail("CheckGroup " + formComponent.getPath() + " does not have index:" + index);
return null;
}
return foundCheck.getValue();
}
else
{
String idValue = selectAbstractChoice(formComponent, index);
if (idValue == null)
{
fail(formComponent.getPath() + " is not a selectable Component.");
return null;
}
else
{
return idValue;
}
}
}
/**
* Selects a given index in a selectable <code>FormComponent</code>.
*
* @param index
*/
protected final void doSelect(final int index)
{
String value = getChoiceValueForIndex(index);
assignValueToFormComponent(formComponent, value);
}
/**
* Selects a given index in a selectable <code>FormComponent</code>.
*
* @param formComponent
* a <code>FormComponent</code>
* @param index
* the index to select
* @return the id value at the selected index
*/
@SuppressWarnings("unchecked")
private String selectAbstractChoice(final FormComponent<?> formComponent, final int index)
{
try
{
Method getChoicesMethod = formComponent.getClass().getMethod("getChoices",
(Class<?>[])null);
getChoicesMethod.setAccessible(true);
List<Object> choices = (List<Object>)getChoicesMethod.invoke(formComponent,
(Object[])null);
Method getChoiceRendererMethod = formComponent.getClass().getMethod(
"getChoiceRenderer", (Class<?>[])null);
getChoiceRendererMethod.setAccessible(true);
IChoiceRenderer<Object> choiceRenderer = (IChoiceRenderer<Object>)getChoiceRendererMethod.invoke(
formComponent, (Object[])null);
return choiceRenderer.getIdValue(choices.get(index), index);
}
catch (SecurityException e)
{
throw new WicketRuntimeException("unexpect select failure", e);
}
catch (NoSuchMethodException e)
{
// component without getChoices() or getChoiceRenderer() is not
// selectable
return null;
}
catch (IllegalAccessException e)
{
throw new WicketRuntimeException("unexpect select failure", e);
}
catch (InvocationTargetException e)
{
throw new WicketRuntimeException("unexpect select failure", e);
}
}
}
/**
* A factory that creates an appropriate <code>ChoiceSelector</code> based on type of
* <code>FormComponent</code>.
*/
private class ChoiceSelectorFactory
{
/**
* <code>MultipleChoiceSelector</code> class.
*/
private final class MultipleChoiceSelector extends ChoiceSelector
{
/**
* Constructor.
*
* @param formComponent
* a <code>FormComponent</code>
*/
protected MultipleChoiceSelector(FormComponent<?> formComponent)
{
super(formComponent);
if (!allowMultipleChoice(formComponent))
{
fail("Component:'" + formComponent.getPath() +
"' Does not support multiple selection.");
}
}
@Override
protected void assignValueToFormComponent(FormComponent<?> formComponent, String value)
{
// multiple selectable should retain selected option
addFormComponentValue(formComponent, value);
}
}
/**
* <code>SingleChoiceSelector</code> class.
*/
private final class SingleChoiceSelector extends ChoiceSelector
{
/**
* Constructor.
*
* @param formComponent
* a <code>FormComponent</code>
*/
protected SingleChoiceSelector(FormComponent<?> formComponent)
{
super(formComponent);
}
@Override
protected void assignValueToFormComponent(FormComponent<?> formComponent, String value)
{
// single selectable should overwrite already selected option
setFormComponentValue(formComponent, value);
}
}
/**
* Creates a <code>ChoiceSelector</code>.
*
* @param formComponent
* a <code>FormComponent</code>
* @return ChoiceSelector a <code>ChoiceSelector</code>
*/
protected ChoiceSelector create(FormComponent<?> formComponent)
{
if (formComponent == null)
{
fail("Trying to select on null component.");
return null;
}
else if (formComponent instanceof RadioGroup ||
formComponent instanceof AbstractSingleSelectChoice)
{
return new SingleChoiceSelector(formComponent);
}
else if (allowMultipleChoice(formComponent))
{
return new MultipleChoiceSelector(formComponent);
}
else
{
fail("Selecting on the component:'" + formComponent.getPath() +
"' is not supported.");
return null;
}
}
/**
* Creates a <code>MultipleChoiceSelector</code>.
*
* @param formComponent
* a <code>FormComponent</code>
* @return ChoiceSelector a <code>ChoiceSelector</code>
*/
protected ChoiceSelector createForMultiple(FormComponent<?> formComponent)
{
return new MultipleChoiceSelector(formComponent);
}
/**
* Tests if a given <code>FormComponent</code> allows multiple choice.
*
* @param formComponent
* a <code>FormComponent</code>
* @return <code>true</code> if the given FormComponent allows multiple choice
*/
private boolean allowMultipleChoice(FormComponent<?> formComponent)
{
return formComponent instanceof CheckGroup ||
formComponent instanceof ListMultipleChoice;
}
}
private final ChoiceSelectorFactory choiceSelectorFactory = new ChoiceSelectorFactory();
/**
* An instance of <code>FormTester</code> can only be used once. Create a new instance of each
* test.
*/
private boolean closed = false;
/** path to <code>FormComponent</code> */
private final String path;
/** <code>BaseWicketTester</code> that create <code>FormTester</code> */
private final BaseWicketTester tester;
/** <code>FormComponent</code> to be tested */
private final Form<?> workingForm;
private boolean clearFeedbackMessagesBeforeSubmit = true;
/**
* @see WicketTester#newFormTester(String)
*
* @param path
* path to <code>FormComponent</code>
* @param workingForm
* <code>FormComponent</code> to be tested
* @param wicketTester
* <code>WicketTester</code> that creates <code>FormTester</code>
* @param fillBlankString
* specifies whether to fill child <code>TextComponent</code>s with blank
* <code>String</code>s
*/
protected FormTester(final String path, final Form<?> workingForm,
final BaseWicketTester wicketTester, final boolean fillBlankString)
{
this.path = path;
this.workingForm = workingForm;
tester = wicketTester;
// fill blank String for Text Component.
workingForm.visitFormComponents(new IVisitor<FormComponent<?>, Void>()
{
@Override
public void component(final FormComponent<?> formComponent, final IVisit<Void> visit)
{
// do nothing for invisible or disabled component -- the browser would not send any
// parameter for a disabled component
if (!(formComponent.isVisibleInHierarchy() && formComponent.isEnabledInHierarchy()))
{
return;
}
String[] values = getInputValue(formComponent);
if (formComponent instanceof AbstractTextComponent<?>)
{
if (values.length == 0 && fillBlankString)
{
setFormComponentValue(formComponent, "");
}
}
for (String value : values)
{
addFormComponentValue(formComponent, value);
}
}
});
workingForm.detach();
}
/**
* Gets request parameter values for the form component that represents its current model value
*
* @param formComponent
* @return array containing parameter values
*/
public static String[] getInputValue(FormComponent<?> formComponent)
{
// the browser sends parameters for visible and enabled components only
if (formComponent.isVisibleInHierarchy() && formComponent.isEnabledInHierarchy())
{
if (formComponent instanceof IFormSubmittingComponent)
{
// buttons have to be submitted explicitely
}
else if (formComponent instanceof AbstractTextComponent)
{
return new String[] { getFormComponentValue(formComponent) };
}
else
{
// TODO is it safe to assume that all other components' values can be split?
String value = getFormComponentValue(formComponent);
if (!Strings.isEmpty(value))
{
return value.split(FormComponent.VALUE_SEPARATOR);
}
}
}
return new String[] { };
}
private static String getFormComponentValue(final FormComponent<?> formComponent)
{
boolean oldEscape = formComponent.getEscapeModelStrings();
formComponent.setEscapeModelStrings(false);
String val = formComponent.getValue();
formComponent.setEscapeModelStrings(oldEscape);
return val;
}
/**
* Retrieves the current <code>Form</code> object.
*
* @return the working <code>Form</code>
*/
public Form<?> getForm()
{
return workingForm;
}
/**
* Gets the value for an <code>AbstractTextComponent</code> with the provided id.
*
* @param id
* <code>Component</code> id
* @return the value of the text component
*/
public String getTextComponentValue(final String id)
{
Component c = getForm().get(id);
if (c instanceof AbstractTextComponent)
{
return ((AbstractTextComponent<?>)c).getValue();
}
return null;
}
/**
* Simulates selecting an option of a <code>FormComponent</code>. Supports
* <code>RadioGroup</code>, <code>CheckGroup</code>, and <code>AbstractChoice</code> family
* currently. The behavior is similar to interacting on the browser: For a single choice, such
* as <code>Radio</code> or <code>DropDownList</code>, the selection will toggle each other. For
* multiple choice, such as <code>Checkbox</code> or <code>ListMultipleChoice</code>, the
* selection will accumulate.
*
* @param formComponentId
* relative path (from <code>Form</code>) to the selectable
* <code>FormComponent</code>
* @param index
* index of the selectable option, starting from 0
* @return This
*/
public FormTester select(final String formComponentId, int index)
{
checkClosed();
FormComponent<?> component = (FormComponent<?>)workingForm.get(formComponentId);
ChoiceSelector choiceSelector = choiceSelectorFactory.create(component);
choiceSelector.doSelect(index);
for (FormComponentUpdatingBehavior updater : component.getBehaviors(FormComponentUpdatingBehavior.class)) {
tester.invokeListener(component, updater);
}
return this;
}
/**
* A convenience method to select multiple options for the <code>FormComponent</code>. The
* method only support multiple selectable <code>FormComponent</code>s.
*
* @see #select(String, int)
*
* @param formComponentId
* relative path (from <code>Form</code>) to the selectable
* <code>FormComponent</code>
* @param indexes
* index of the selectable option, starting from 0
* @return This
*/
public FormTester selectMultiple(String formComponentId, int[] indexes)
{
return selectMultiple(formComponentId, indexes, false);
}
/**
* A convenience method to select multiple options for the <code>FormComponent</code>. The
* method only support multiple selectable <code>FormComponent</code>s.
*
* @see #select(String, int)
*
* @param formComponentId
* relative path (from <code>Form</code>) to the selectable
* <code>FormComponent</code>
* @param indexes
* index of the selectable option, starting from 0
* @param replace
* If true, than all previous selects are first reset, thus existing selects are
* replaced. If false, than the new indexes will be added.
* @return This
*/
public FormTester selectMultiple(String formComponentId, int[] indexes, final boolean replace)
{
checkClosed();
if (replace)
{
// Reset first
setValue(formComponentId, "");
}
ChoiceSelector choiceSelector = choiceSelectorFactory.createForMultiple((FormComponent<?>)workingForm.get(formComponentId));
for (int index : indexes)
{
choiceSelector.doSelect(index);
}
return this;
}
/**
* Simulates filling in a field on a <code>Form</code>.
*
* @param formComponentId
* relative path (from <code>Form</code>) to the selectable
* <code>FormComponent</code> or <code>IFormSubmittingComponent</code>
* @param value
* the field value
* @return This
*/
public FormTester setValue(final String formComponentId, final String value)
{
Component component = workingForm.get(formComponentId);
assertNotNull(component, "Unable to set value. Couldn't find component with name: " +
formComponentId);
return setValue(component, value);
}
/**
* Simulates filling in a field on a <code>Form</code>.
*
* @param formComponent
* relative path (from <code>Form</code>) to the selectable
* <code>FormComponent</code> or <code>IFormSubmittingComponent</code>
* @param value
* the field value
* @return This
*/
public FormTester setValue(final Component formComponent, final String value)
{
Args.notNull(formComponent, "formComponent");
checkClosed();
if (formComponent instanceof IFormSubmittingComponent)
{
setFormSubmittingComponentValue((IFormSubmittingComponent)formComponent, value);
}
else if (formComponent instanceof FormComponent)
{
setFormComponentValue((FormComponent<?>)formComponent, value);
}
else
{
fail("Component with id: " + formComponent.getId() + " is not a FormComponent");
}
return this;
}
/**
* @param checkBoxId
* @param value
* @return This
*/
public FormTester setValue(String checkBoxId, boolean value)
{
return setValue(checkBoxId, Boolean.toString(value));
}
/**
* Sets the <code>File</code> on a {@link FileUploadField}.
*
* @param formComponentId
* relative path (from <code>Form</code>) to the selectable
* <code>FormComponent</code>. The <code>FormComponent</code> must be of a type
* <code>FileUploadField</code>.
* @param file
* the <code>File</code> to upload or {@code null} for an empty input
* @param contentType
* the content type of the file. Must be a valid mime type.
* @return This
*/
public FormTester setFile(final String formComponentId, final File file,
final String contentType)
{
checkClosed();
FormComponent<?> formComponent = (FormComponent<?>)workingForm.get(formComponentId);
MockHttpServletRequest servletRequest = tester.getRequest();
if (formComponent instanceof FileUploadField)
{
servletRequest.addFile(formComponent.getInputName(), file, contentType);
}
else if (formComponent instanceof MultiFileUploadField)
{
String inputName = formComponent.getInputName() + MultiFileUploadField.MAGIC_SEPARATOR + multiFileUploadIndex++;
servletRequest.addFile(inputName, file, contentType);
}
else
{
fail("'" + formComponentId + "' is not " +
"a FileUploadField. You can only attach a file to form " +
"component of this type.");
}
return this;
}
/**
* Submits the <code>Form</code>. Note that <code>submit</code> can be executed only once.
*
* @return This
*/
public FormTester submit()
{
checkClosed();
try
{
if (clearFeedbackMessagesBeforeSubmit)
{
tester.clearFeedbackMessages();
}
tester.getRequest().setUseMultiPartContentType(workingForm.isMultiPart());
tester.submitForm(path);
}
finally
{
closed = true;
}
return this;
}
public boolean isClearFeedbackMessagesBeforeSubmit()
{
return clearFeedbackMessagesBeforeSubmit;
}
public FormTester setClearFeedbackMessagesBeforeSubmit(boolean clearFeedbackMessagesBeforeSubmit)
{
this.clearFeedbackMessagesBeforeSubmit = clearFeedbackMessagesBeforeSubmit;
return this;
}
/**
* A convenience method for submitting the <code>Form</code> with an alternate button.
* <p>
* Note that if the button is associated with a model, it's better to use the
* <code>setValue</code> method instead:
*
* <pre>
* formTester.setValue("to:my:button", "value on the button");
* formTester.submit();
* </pre>
*
* @param buttonComponentId
* relative path (from <code>Form</code>) to the button
* @return This
*/
public FormTester submit(final String buttonComponentId)
{
Component submitter = getForm().get(buttonComponentId);
if (submitter == null)
{
fail("Cannot submit the form because there is no submitting component with id: " + buttonComponentId);
}
return submit(submitter);
}
/**
* A convenience method for submitting the <code>Form</code> with an alternate button.
* <p>
* Note that if the button is associated with a model, it's better to use the
* <code>setValue</code> method instead:
*
* <pre>
* formTester.setValue(myButton, "value on the button");
* formTester.submit();
* </pre>
*
* @param buttonComponent
* relative path (from <code>Form</code>) to the button
* @return This
*/
public FormTester submit(final Component buttonComponent)
{
Args.notNull(buttonComponent, "buttonComponent");
setValue(buttonComponent, "marked");
if (buttonComponent instanceof AjaxButton || buttonComponent instanceof AjaxSubmitLink)
{
if (clearFeedbackMessagesBeforeSubmit)
{
tester.clearFeedbackMessages();
}
tester.getRequest().setUseMultiPartContentType(workingForm.isMultiPart());
tester.executeAjaxEvent(buttonComponent, "click");
return this;
}
else
{
return submit();
}
}
/**
* A convenience method to submit the Form via a SubmitLink which may inside or outside of the
* Form.
*
* @param path
* The path to the SubmitLink
* @param pageRelative
* if true, than the 'path' to the SubmitLink is relative to the page. Thus the link
* can be outside the form. If false, the path is relative to the form and thus the
* link is inside the form.
* @return This
*/
public FormTester submitLink(String path, final boolean pageRelative)
{
if (pageRelative)
{
tester.clickLink(path, false);
}
else
{
path = this.path + ":" + path;
tester.clickLink(path, false);
}
return this;
}
/**
* Adds an additional <code>FormComponent</code>'s value into request parameter -- this method
* retains existing parameters but removes any duplicate parameters.
*
* @param formComponent
* a <code>FormComponent</code>
* @param value
* a value to add
* @return This
*/
private FormTester addFormComponentValue(FormComponent<?> formComponent, String value)
{
if (parameterExist(formComponent))
{
List<StringValue> values = tester.getRequest()
.getPostParameters()
.getParameterValues(formComponent.getInputName());
// remove duplicated
HashSet<String> all = new HashSet<String>();
for (StringValue val : values)
{
all.add(val.toString());
}
all.add(value);
values = new ArrayList<StringValue>();
for (String val : all)
{
values.add(StringValue.valueOf(val));
}
tester.getRequest()
.getPostParameters()
.setParameterValues(formComponent.getInputName(), values);
}
else
{
setFormComponentValue(formComponent, value);
}
return this;
}
/**
* <code>FormTester</code> must only be used once. Create a new instance of
* <code>FormTester</code> for each test.
*/
private void checkClosed()
{
if (closed)
{
fail("'" + path + "' already submitted. Note that FormTester " +
"is allowed to submit only once");
}
}
/**
* Returns <code>true</code> if the parameter exists in the <code>FormComponent</code>.
*
* @param formComponent
* a <code>FormComponent</code>
* @return <code>true</code> if the parameter exists in the <code>FormComponent</code>
*/
private boolean parameterExist(final FormComponent<?> formComponent)
{
String parameter = tester.getRequest()
.getPostParameters()
.getParameterValue(formComponent.getInputName())
.toString();
return parameter != null && parameter.trim().length() > 0;
}
/**
* Set formComponent's value into request parameter, this method overwrites existing parameters.
*
* @param formComponent
* a <code>FormComponent</code>
* @param value
* a value to add
*/
private void setFormComponentValue(final FormComponent<?> formComponent, final String value)
{
tester.getRequest()
.getPostParameters()
.setParameterValue(formComponent.getInputName(), value);
}
/**
* Set component's value into request parameter, this method overwrites existing parameters.
*
* @param component
* an {@link IFormSubmittingComponent}
* @param value
* a value to add
*/
private void setFormSubmittingComponentValue(IFormSubmittingComponent component, String value)
{
tester.getRequest().getPostParameters().setParameterValue(component.getInputName(), value);
}
/**
*
* @param message
*/
private void fail(String message)
{
throw new WicketRuntimeException(message);
}
}