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

📄 wizardformcontrollertests.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2005 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.web.servlet.mvc;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import junit.framework.TestCase;

import org.springframework.beans.TestBean;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Juergen Hoeller
 * @since 29.04.2003
 */
public class WizardFormControllerTests extends TestCase {

	public void testNoDirtyPageChange() throws Exception {
		AbstractWizardFormController wizard = new TestWizardController();
		wizard.setAllowDirtyBack(false);
		wizard.setAllowDirtyForward(false);
		wizard.setPageAttribute("currentPage");

		assertTrue(wizard.getFormSessionAttributeName() != wizard.getPageSessionAttributeName());
		HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");

		Properties params = new Properties();
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, null, 0, null, 0, "currentPage");
		// not allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.x", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.y", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty("date", "not a date");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.y", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// not allowed to go to 0

		params.clear();
		params.setProperty("age", "32");
		params.setProperty(AbstractWizardFormController.PARAM_PAGE, "1");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
		performRequest(wizard, session, params, 0, "myname", 32, "currentPage");
		// age set -> now allowed to go to 0

		params.clear();
		params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
		performRequest(wizard, session, params, -1, "myname", 32, null);
	}

	public void testCustomSessionAttributes() throws Exception {
		AbstractWizardFormController wizard = new TestWizardController() {
			protected String getFormSessionAttributeName() {
				return "myFormAttr";
			}
			protected String getPageSessionAttributeName() {
				return "myPageAttr";
			}
		};
		wizard.setAllowDirtyBack(false);
		wizard.setAllowDirtyForward(false);
		wizard.setPageAttribute("currentPage");

		HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
		assertTrue(session.getAttribute("myFormAttr") instanceof TestBean);
		assertEquals(new Integer(0), session.getAttribute("myPageAttr"));

		Properties params = new Properties();
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, null, 0, null, 0, "currentPage");
		// not allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty("age", "32");
		params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
		performRequest(wizard, session, params, -1, "myname", 32, "currentPage");
	}

	public void testCustomRequestDependentSessionAttributes() throws Exception {
		AbstractWizardFormController wizard = new TestWizardController() {
			protected String getFormSessionAttributeName(HttpServletRequest request) {
				return "myFormAttr" + request.getParameter("formAttr");
			}
			protected String getPageSessionAttributeName(HttpServletRequest request) {
				return "myPageAttr" + request.getParameter("pageAttr");
			}
		};
		wizard.setAllowDirtyBack(false);
		wizard.setAllowDirtyForward(false);
		wizard.setPageAttribute("currentPage");

		HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
		assertTrue(session.getAttribute("myFormAttr1") instanceof TestBean);
		assertEquals(new Integer(0), session.getAttribute("myPageAttr2"));

		Properties params = new Properties();
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, null, 0, null, 0, "currentPage");
		// not allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty("age", "32");
		params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
		performRequest(wizard, session, params, -1, "myname", 32, "currentPage");
	}

	public void testDirtyBack() throws Exception {
		AbstractWizardFormController wizard = new TestWizardController();
		wizard.setAllowDirtyBack(true);
		wizard.setAllowDirtyForward(false);
		HttpSession session = performRequest(wizard, null, null, 0, null, 0, null);

		Properties params = new Properties();
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, params, 0, null, 0, null);
		// not allowed to go to 1

		params.clear();
		params.setProperty("name", "myname");
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
		performRequest(wizard, session, params, 1, "myname", 0, null);
		// name set -> now allowed to go to 1

		params.clear();
		params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
		performRequest(wizard, session, params, 0, "myname", 0, null);
		// dirty back -> allowed to go to 0

		params.clear();
		params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
		performRequest(wizard, session, params, 1, "myname", 0, null);
		// finish while dirty -> show dirty page (1)

		params.clear();
		params.setProperty("age", "32");
		params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
		performRequest(wizard, session, params, -1, "myname", 32, null);
		// age set -> now allowed to finish
	}

	public void testDirtyForward() throws Exception {
		AbstractWizardFormController wizard = new TestWizardController();
		wizard.setAllowDirtyBack(false);
		wizard.setAllowDirtyForward(true);
		HttpSession session = performRequest(wizard, null, null, 0, null, 0, null);

⌨️ 快捷键说明

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