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

📄 formactiontests.java

📁 spring的WEB开发插件,支持多状态WEB开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2004 the original author or authors.
 *
 * Licensed 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.springframework.webflow.action;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.webflow.Event;
import org.springframework.webflow.RequestContext;
import org.springframework.webflow.ScopeType;
import org.springframework.webflow.test.MockRequestContext;

/**
 * Unit test for the FormAction class.
 * 
 * @see org.springframework.webflow.action.FormAction
 * 
 * @author Erwin Vervaet
 */
public class FormActionTests extends TestCase {
	
	private static class TestBean {
		
		private String prop;
		
		public TestBean() {
		}
		
		public TestBean(String prop) {
			this.prop = prop;
		}
		
		public String getProp() {
			return prop;
		}
		
		public void setProp(String prop) {
			this.prop = prop;
		}
	}
	
	private static class OtherTestBean {
		
		private String otherProp;
		
		public String getOtherProp() {
			return otherProp;
		}
		
		public void setOtherProp(String otherProp) {
			this.otherProp = otherProp;
		}
	}
	
	private static class TestBeanValidator implements Validator {
		public boolean supports(Class clazz) {
			return TestBean.class.equals(clazz);
		}
		
		public void validate(Object formObject, Errors errors) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "prop", "Prop cannot be empty");
		}
	}
	
	private FormAction action;
	
	protected void setUp() throws Exception {
		action = createFormAction("test");
	}
	
	public void testSetupForm() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "value")));
		
		// setupForm() should initialize the form object and the Errors
		// instance, but no bind & validate should happen since bindOnSetupForm
		// is not set
		
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());
		
		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertFalse(getErrors(context).hasErrors());
		assertNull(getFormObject(context).getProp());
	}
	
	public void testSetupFormWithBinding() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "value")));

		action.setBindOnSetupForm(true);
		
		// setupForm() should initialize the form object and the Errors
		// instance and do a bind & validate (bindOnSetupForm == true)
		
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());
		
		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertFalse(getErrors(context).hasErrors());
		assertEquals("value", getFormObject(context).getProp());
	}
	
	public void testSetupFormWithExistingFormObject() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "value")));
		
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());
		
		Errors errors = getErrors(context);
		errors.reject("dummy");
		TestBean formObject = getFormObject(context);
		formObject.setProp("bla");
		
		// setupForm() should leave the existing form object and Errors instance
		// untouched, at least when no bind & validate is done (bindOnSetupForm == false)

		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());

		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertSame(errors, getErrors(context));
		assertSame(formObject, getFormObject(context));
		assertTrue(getErrors(context).hasErrors());
		assertEquals("bla", getFormObject(context).getProp());
	}
	
	public void testSetupFormWithExistingFormObjectAndWithBinding() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "value")));
		
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());
		
		Errors errors = getErrors(context);
		errors.reject("dummy");
		TestBean formObject = getFormObject(context);
		formObject.setProp("bla");
		
		action.setBindOnSetupForm(true);
		
		// setupForm() should leave the existing form object untouched but should
		// generate a new errors instance since we did binding (bindOnSetupForm == true)

		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());

		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertNotSame(errors, getErrors(context));
		assertSame(formObject, getFormObject(context));
		assertFalse(getErrors(context).hasErrors());
		assertEquals("value", getFormObject(context).getProp());
	}
	
	public void testBindAndValidate() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "value")));
		
		// bindAndValidate() should setup a new form object and errors instance
		// and do a bind & validate
		
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.bindAndValidate(context).getId());
		
		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertFalse(getErrors(context).hasErrors());
		assertEquals("value", getFormObject(context).getProp());
	}
	
	public void testBindAndValidateFailure() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this));
		
		// bindAndValidate() should setup a new form object and errors instance
		// and do a bind & validate, which fails because the provided value is empty
		
		assertEquals(AbstractAction.ERROR_EVENT_ID, action.bindAndValidate(context).getId());
		
		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertTrue(getErrors(context).hasErrors());
		assertNull(getFormObject(context).getProp());
	}
	
	public void testBindAndValidateWithExistingFormObject() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "value")));
		
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());
		
		Errors errors = getErrors(context);
		errors.reject("dummy");
		TestBean formObject = getFormObject(context);
		formObject.setProp("bla");
		
		// bindAndValidate() should leave the existing form object untouched
		// but should setup a new Errors instance during bind & validate

		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.bindAndValidate(context).getId());
		
		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertNotSame(errors, getErrors(context));
		assertSame(formObject, getFormObject(context));
		assertFalse(getErrors(context).hasErrors());
		assertEquals("value", getFormObject(context).getProp());
	}
	
	// this is what happens in a 'form state'
	public void testBindAndValidateFailureThenSetupForm() throws Exception {
		MockRequestContext context = new MockRequestContext();
		context.setLastEvent(new Event(this, "test", params("prop", "")));
		
		// setup existing form object & errors
		assertEquals(AbstractAction.SUCCESS_EVENT_ID, action.setupForm(context).getId());		
		TestBean formObject = getFormObject(context);
		formObject.setProp("bla");
		
		assertEquals(AbstractAction.ERROR_EVENT_ID, action.bindAndValidate(context).getId());

		assertEquals(2, context.getRequestScope().size());
		assertEquals(2, context.getFlowScope().size());
		assertSame(formObject, getFormObject(context));
		assertTrue(getErrors(context).hasErrors());
		assertEquals("", getFormObject(context).getProp());

⌨️ 快捷键说明

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