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

📄 multiactioncontrollertests.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.sql.SQLException;
import java.util.HashMap;
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.FatalBeanException;
import org.springframework.beans.TestBean;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver;
import org.springframework.web.servlet.support.SessionRequiredException;

/**
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Colin Sampaleanu
 */
public class MultiActionControllerTests extends TestCase {

	public void testDefaultInternalPathMethodNameResolver() throws Exception {
		doDefaultTestInternalPathMethodNameResolver("/foo.html", "foo");
		doDefaultTestInternalPathMethodNameResolver("/foo/bar.html", "bar");
		doDefaultTestInternalPathMethodNameResolver("/bugal.xyz", "bugal");
		doDefaultTestInternalPathMethodNameResolver("/x/y/z/q/foo.html", "foo");
		doDefaultTestInternalPathMethodNameResolver("qqq.q", "qqq");
	}

	private void doDefaultTestInternalPathMethodNameResolver(String in, String expected) throws Exception {
		MultiActionController rc = new MultiActionController();
		HttpServletRequest request = new MockHttpServletRequest("GET", in);
		String actual = rc.getMethodNameResolver().getHandlerMethodName(request);
		assertEquals("Wrong method name resolved", expected, actual);
	}

	public void testCustomizedInternalPathMethodNameResolver() throws Exception {
		doTestCustomizedInternalPathMethodNameResolver("/foo.html", "my", null, "myfoo");
		doTestCustomizedInternalPathMethodNameResolver("/foo/bar.html", null, "Handler", "barHandler");
		doTestCustomizedInternalPathMethodNameResolver("/Bugal.xyz", "your", "Method", "yourBugalMethod");
	}

	private void doTestCustomizedInternalPathMethodNameResolver(
			String in, String prefix, String suffix, String expected) throws Exception {
		MultiActionController rc = new MultiActionController();
		InternalPathMethodNameResolver resolver = new InternalPathMethodNameResolver();
		if (prefix != null) {
			resolver.setPrefix(prefix);
		}
		if (suffix != null) {
			resolver.setSuffix(suffix);
		}
		rc.setMethodNameResolver(resolver);
		HttpServletRequest request = new MockHttpServletRequest("GET", in);
		String actual = rc.getMethodNameResolver().getHandlerMethodName(request);
		assertEquals("Wrong method name resolved", expected, actual);
	}

	public void testParameterMethodNameResolver() throws NoSuchRequestHandlingMethodException {
		ParameterMethodNameResolver mnr = new ParameterMethodNameResolver();
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
		request.addParameter("action", "bar");
		assertEquals("bar", mnr.getHandlerMethodName(request));
		request = new MockHttpServletRequest("GET", "/foo.html");
		try {
			mnr.getHandlerMethodName(request);
			// should have thrown NoSuchRequestHandlingMethodException
		}
		catch (NoSuchRequestHandlingMethodException ex) {
			// expected
		}
	}

	public void testParameterMethodNameResolverWithCustomParamName() throws NoSuchRequestHandlingMethodException {
		ParameterMethodNameResolver mnr = new ParameterMethodNameResolver();
		mnr.setParamName("myparam");
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
		request.addParameter("myparam", "bar");
		assertEquals("bar", mnr.getHandlerMethodName(request));
	}

	public void testParameterMethodNameResolverWithParamNames() throws NoSuchRequestHandlingMethodException {
		ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
		resolver.setDefaultMethodName("default");
		resolver.setMethodParamNames(new String[] {"hello", "spring", "colin"});
		Properties logicalMappings = new Properties();
		logicalMappings.setProperty("hello", "goodbye");
		logicalMappings.setProperty("nina", "colin");
		resolver.setLogicalMappings(logicalMappings);

		MockHttpServletRequest request = new MockHttpServletRequest();
		request.addParameter("nomatch", "whatever");
		try {
			resolver.getHandlerMethodName(request);
		}
		catch (NoSuchRequestHandlingMethodException ex) {
			//expected
		}

		// verify default handler
		request = new MockHttpServletRequest();
		request.addParameter("this will not match anything", "whatever");
		assertEquals("default", resolver.getHandlerMethodName(request));

		// verify first resolution strategy (action=method)
		request = new MockHttpServletRequest();
		request.addParameter("action", "reset");
		assertEquals("reset", resolver.getHandlerMethodName(request));
		// this one also tests logical mapping
		request = new MockHttpServletRequest();
		request.addParameter("action", "nina");
		assertEquals("colin", resolver.getHandlerMethodName(request));

		// now validate second resolution strategy (parameter existence)
		// this also tests logical mapping
		request = new MockHttpServletRequest();
		request.addParameter("hello", "whatever");
		assertEquals("goodbye", resolver.getHandlerMethodName(request));

		request = new MockHttpServletRequest();
		request.addParameter("spring", "whatever");
		assertEquals("spring", resolver.getHandlerMethodName(request));

		request = new MockHttpServletRequest();
		request.addParameter("hello", "whatever");
		request.addParameter("spring", "whatever");
		assertEquals("goodbye", resolver.getHandlerMethodName(request));

		request = new MockHttpServletRequest();
		request.addParameter("colin", "whatever");
		request.addParameter("spring", "whatever");
		assertEquals("spring", resolver.getHandlerMethodName(request));
		
		// validate image button handling
		request = new MockHttpServletRequest();
		request.addParameter("spring.x", "whatever");
		assertEquals("spring", resolver.getHandlerMethodName(request));

		request = new MockHttpServletRequest();
		request.addParameter("hello.x", "whatever");
		request.addParameter("spring", "whatever");
		assertEquals("goodbye", resolver.getHandlerMethodName(request));
	}

	public void testParameterMethodNameResolverWithDefaultMethodName() throws NoSuchRequestHandlingMethodException {
		ParameterMethodNameResolver mnr = new ParameterMethodNameResolver();
		mnr.setDefaultMethodName("foo");
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html");
		request.addParameter("action", "bar");
		assertEquals("bar", mnr.getHandlerMethodName(request));
		request = new MockHttpServletRequest("GET", "/foo.html");
		assertEquals("foo", mnr.getHandlerMethodName(request));
	}

	public void testInvokesCorrectMethod() throws Exception {
		TestMaController mc = new TestMaController();
		HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		HttpServletResponse response = new MockHttpServletResponse();
		Properties p = new Properties();
		p.put("/welcome.html", "welcome");
		PropertiesMethodNameResolver mnr = new PropertiesMethodNameResolver();
		mnr.setMappings(p);
		mc.setMethodNameResolver(mnr);

		ModelAndView mv = mc.handleRequest(request, response);
		assertTrue("Invoked welcome method", mc.wasInvoked("welcome"));
		assertTrue("view name is welcome", mv.getViewName().equals("welcome"));
		assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);

		mc = new TestMaController();
		request = new MockHttpServletRequest("GET", "/subdir/test.html");
		response = new MockHttpServletResponse();
		mv = mc.handleRequest(request, response);
		assertTrue("Invoked test method", mc.wasInvoked("test"));
		assertTrue("view name is subdir_test", mv.getViewName().equals("test"));
		assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);
	}

	public void testPathMatching() throws Exception {
		TestMaController mc = new TestMaController();
		HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
		HttpServletResponse response = new MockHttpServletResponse();
		Properties p = new Properties();
		p.put("/welc*.html", "welcome");
		PropertiesMethodNameResolver mn = new PropertiesMethodNameResolver();
		mn.setMappings(p);
		mc.setMethodNameResolver(mn);

		ModelAndView mv = mc.handleRequest(request, response);
		assertTrue("Invoked welcome method", mc.wasInvoked("welcome"));
		assertTrue("view name is welcome", mv.getViewName().equals("welcome"));
		assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);

		mc = new TestMaController();
		mc.setMethodNameResolver(mn);
		request = new MockHttpServletRequest("GET", "/nomatch");
		response = new MockHttpServletResponse();
		try {
			mv = mc.handleRequest(request, response);
		}
		catch (Exception e) {
			// expected
		}
		assertFalse("Not invoking welcome method", mc.wasInvoked("welcome"));
		assertTrue("No method invoked", mc.getInvokedMethods() == 0);
	}

	public static class TestDelegate {

		boolean invoked;

		public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
			invoked = true;
			return new ModelAndView("test");
		}
	}

	public void testInvokesCorrectMethodOnDelegate() throws Exception {
		MultiActionController mac = new MultiActionController();
		TestDelegate d = new TestDelegate();
		mac.setDelegate(d);
		HttpServletRequest request = new MockHttpServletRequest("GET", "/test.html");
		HttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mac.handleRequest(request, response);
		assertTrue("view name is test", mv.getViewName().equals("test"));
		assertTrue("Delegate was invoked", d.invoked);
	}

	public void testInvokesCorrectMethodWithSession() throws Exception {
		TestMaController mc = new TestMaController();
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/inSession.html");
		request.setSession(new MockHttpSession(null));
		HttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);
		assertTrue("Invoked inSession method", mc.wasInvoked("inSession"));
		assertTrue("view name is welcome", mv.getViewName().equals("inSession"));
		assertTrue("Only one method invoked", mc.getInvokedMethods() == 1);

		request = new MockHttpServletRequest("GET", "/inSession.html");
		response = new MockHttpServletResponse();
		try {

			mc.handleRequest(request, response);
			fail("Should have rejected request without session");
		}
		catch (ServletException ex) {
			//OK
		}
	}

	public void testInvokesCommandMethodNoSession() throws Exception {
		TestMaController mc = new TestMaController();
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandNoSession.html");
		request.addParameter("name", "rod");
		request.addParameter("age", "32");
		HttpServletResponse response = new MockHttpServletResponse();
		ModelAndView mv = mc.handleRequest(request, response);

⌨️ 快捷键说明

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