⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formtester.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	{		this.path = path;		this.workingForm = workingForm;		baseWicketTester = wicketTester;		baseWicketTester.setupRequestAndResponse();		// fill blank String for Text Component.		workingForm.visitFormComponents(new FormComponent.AbstractVisitor()		{			public void onFormComponent(final FormComponent formComponent)			{				// do nothing for invisible component				if (!formComponent.isVisibleInHierarchy())				{					return;				}				// if component is text field and do not have exist value, fill				// blank String if required				if (formComponent instanceof AbstractTextComponent)				{					if (Strings.isEmpty(formComponent.getValue()))					{						if (fillBlankString)						{							setFormComponentValue(formComponent, "");						}					}					else					{						setFormComponentValue(formComponent, formComponent.getValue());					}				}				else if ((formComponent instanceof DropDownChoice) ||					(formComponent instanceof RadioChoice) || (formComponent instanceof CheckBox))				{					setFormComponentValue(formComponent, formComponent.getValue());				}				else if (formComponent instanceof ListMultipleChoice)				{					final String[] modelValues = formComponent.getValue().split(						FormComponent.VALUE_SEPARATOR);					for (int i = 0; i < modelValues.length; i++)					{						addFormComponentValue(formComponent, modelValues[i]);					}				}				else if (formComponent instanceof CheckGroup)				{					final Collection checkGroupValues = (Collection)formComponent.getModelObject();					formComponent.visitChildren(Check.class, new IVisitor()					{						public Object component(Component component)						{							if (checkGroupValues.contains(component.getModelObject()))							{								addFormComponentValue(formComponent, ((Check)component).getValue());							}							return CONTINUE_TRAVERSAL;						}					});				}			}		});	}	/**	 * 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(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	 */	public void select(String formComponentId, int index)	{		checkClosed();		FormComponent component = (FormComponent)workingForm.get(formComponentId);		ChoiceSelector choiceSelector = choiceSelectorFactory.create(component);		choiceSelector.doSelect(index);		if (component instanceof DropDownChoice)		{			try			{				Method wantOnSelectionChangedNotificationsMethod = DropDownChoice.class.getDeclaredMethod(					"wantOnSelectionChangedNotifications", new Class[0]);				wantOnSelectionChangedNotificationsMethod.setAccessible(true);				boolean wantOnSelectionChangedNotifications = ((Boolean)wantOnSelectionChangedNotificationsMethod.invoke(					component, new Object[0])).booleanValue();				if (wantOnSelectionChangedNotifications)				{					((DropDownChoice)component).onSelectionChanged();				}			}			catch (Exception e)			{				throw new RuntimeException(e);			}		}	}	/**	 * 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	 */	public void selectMultiple(String formComponentId, int[] indexes)	{		checkClosed();		ChoiceSelector choiceSelector = choiceSelectorFactory.createForMultiple((FormComponent)workingForm.get(formComponentId));		for (int i = 0; i < indexes.length; i++)		{			choiceSelector.doSelect(indexes[i]);		}	}	/**	 * 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	 */	public void setValue(final String formComponentId, final String value)	{		checkClosed();		Component component = workingForm.get(formComponentId);		if (component instanceof IFormSubmittingComponent)		{			setFormSubmittingComponentValue((IFormSubmittingComponent)component, value);		}		else if (component instanceof FormComponent)		{			setFormComponentValue((FormComponent)component, 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.	 * @param contentType	 *            the content type of the file. Must be a valid mime type.	 */	public void setFile(final String formComponentId, final File file, final String contentType)	{		checkClosed();		FormComponent formComponent = (FormComponent)workingForm.get(formComponentId);		if (formComponent instanceof FileUploadField == false)		{			throw new IllegalArgumentException("'" + formComponentId + "' is not " +				"a FileUploadField. You can only attach a file to form " +				"component of this type.");		}		MockHttpServletRequest servletRequest = baseWicketTester.getServletRequest();		servletRequest.addFile(formComponent.getInputName(), file, contentType);	}	/**	 * Submits the <code>Form</code>. Note that <code>submit</code> can be executed only once.	 */	public void submit()	{		checkClosed();		try		{			MockHttpServletRequest servletRequest = baseWicketTester.getServletRequest();			WebRequestCycle requestCycle = baseWicketTester.createRequestCycle();			servletRequest.setRequestToComponent(workingForm);			servletRequest.setUseMultiPartContentType(isMultiPart());			baseWicketTester.processRequestCycle(requestCycle);		}		finally		{			closed = true;		}	}	private boolean isMultiPart()	{		try		{			Field multiPart = Form.class.getDeclaredField("multiPart");			multiPart.setAccessible(true);			return multiPart.getBoolean(workingForm);		}		catch (SecurityException e)		{			throw new RuntimeException(e);		}		catch (NoSuchFieldException e)		{			throw new RuntimeException(e);		}		catch (IllegalAccessException e)		{			throw new RuntimeException(e);		}	}	/**	 * 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(&quot;to:my:button&quot;, &quot;value on the button&quot;);	 * formTester.submit();	 * </pre>	 * 	 * @param buttonComponentId	 *            relative path (from <code>Form</code>) to the button	 */	public void submit(String buttonComponentId)	{		setValue(buttonComponentId, "marked");		submit();	}	/**	 * 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	 */	private void addFormComponentValue(FormComponent formComponent, String value)	{		if (parameterExist(formComponent))		{			String[] values = baseWicketTester.getServletRequest().getParameterValues(				formComponent.getInputName());			// remove duplicated			HashSet all = new HashSet(Arrays.asList(values));			all.add(value);			Map newParameters = new HashMap();			newParameters.put(formComponent.getInputName(), all.toArray(new String[all.size()]));			baseWicketTester.getServletRequest().setParameters(newParameters);		}		else		{			setFormComponentValue(formComponent, value);		}	}	/**	 * <code>FormTester</code> must only be used once. Create a new instance of	 * <code>FormTester</code> for each test.	 */	private void checkClosed()	{		if (closed)		{			throw new IllegalStateException("'" + path +				"' already sumbitted. 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(FormComponent formComponent)	{		String parameter = baseWicketTester.getServletRequest().getParameter(			formComponent.getInputName());		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(FormComponent formComponent, String value)	{		baseWicketTester.getServletRequest().setParameter(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)	{		baseWicketTester.getServletRequest().setParameter(component.getInputName(), value);	}	private void fail(String message)	{		throw new WicketRuntimeException(message);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -