📄 formtester.java
字号:
/* * 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 java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Arrays;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import org.apache.wicket.Component;import org.apache.wicket.WicketRuntimeException;import org.apache.wicket.Component.IVisitor;import org.apache.wicket.markup.html.form.AbstractTextComponent;import org.apache.wicket.markup.html.form.Check;import org.apache.wicket.markup.html.form.CheckBox;import org.apache.wicket.markup.html.form.CheckGroup;import org.apache.wicket.markup.html.form.DropDownChoice;import org.apache.wicket.markup.html.form.Form;import org.apache.wicket.markup.html.form.FormComponent;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.RadioChoice;import org.apache.wicket.markup.html.form.RadioGroup;import org.apache.wicket.markup.html.form.upload.FileUploadField;import org.apache.wicket.protocol.http.MockHttpServletRequest;import org.apache.wicket.protocol.http.WebRequestCycle;import org.apache.wicket.util.file.File;import org.apache.wicket.util.string.Strings;/** * 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{ /** * 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 { int count = 0; private final int index; private SearchOptionByIndexVisitor(int index) { super(); this.index = index; } /** * @see org.apache.wicket.Component.IVisitor#component(org.apache.wicket.Component) */ public Object component(Component component) { if (count == index) { return component; } else { count++; return CONTINUE_TRAVERSAL; } } } 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); /** * Selects a given index in a selectable <code>FormComponent</code>. * * @param index */ protected final void doSelect(final 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); } assignValueToFormComponent(formComponent, 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); } assignValueToFormComponent(formComponent, foundCheck.getValue()); } else { String idValue = selectAbstractChoice(formComponent, index); if (idValue == null) { fail(formComponent.getPath() + " is not a selectable Component."); } else { assignValueToFormComponent(formComponent, idValue); } } } /** * 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 */ private String selectAbstractChoice(FormComponent formComponent, final int index) { try { Method getChoicesMethod = formComponent.getClass().getMethod("getChoices", (Class[])null); getChoicesMethod.setAccessible(true); List choices = (List)getChoicesMethod.invoke(formComponent, (Object[])null); Method getChoiceRendererMethod = formComponent.getClass().getMethod( "getChoiceRenderer", (Class[])null); getChoiceRendererMethod.setAccessible(true); IChoiceRenderer choiceRenderer = (IChoiceRenderer)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."); } } /** * * @see org.apache.wicket.util.tester.FormTester.ChoiceSelector#assignValueToFormComponent(org.apache.wicket.markup.html.form.FormComponent, * java.lang.String) */ 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); } /** * @see org.apache.wicket.util.tester.FormTester.ChoiceSelector#assignValueToFormComponent(org.apache.wicket.markup.html.form.FormComponent, * java.lang.String) */ 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."); } if (formComponent instanceof RadioGroup || formComponent instanceof DropDownChoice || formComponent instanceof RadioChoice) { 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 baseWicketTester; /** <code>FormComponent</code> to be tested */ private final Form workingForm; /** * @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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -