Spring JSP tags do not use their own expression support on Servlet 3.0 containers by default; added support for web.xml context-param "springJspExpressionSupport" (explicit "true"/"false")

This commit is contained in:
Juergen Hoeller
2011-07-28 20:25:18 +00:00
parent bbcf358a9d
commit c864908779
3 changed files with 134 additions and 24 deletions

View File

@@ -76,8 +76,8 @@ import org.springframework.web.util.WebUtils;
* and XmlWebApplicationContext with an underlying MockServletContext (as long as
* the MockServletContext has been configured with a FileSystemResourceLoader).
*
* Supports the Servlet 3.0 API level, but throws {@link UnsupportedOperationException}
* for all methods introduced in Servlet 3.0.
* <p>Supports Servlet 3.0 API level, but throws {@link UnsupportedOperationException}
* for most methods introduced in Servlet 3.0.
*
* @author Rod Johnson
* @author Juergen Hoeller
@@ -102,8 +102,14 @@ public class MockServletContext implements ServletContext {
private String contextPath = "";
private int majorVersion = 2;
private int minorVersion = 5;
private int effectiveMajorVersion = 2;
private int effectiveMinorVersion = 5;
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
@@ -191,14 +197,15 @@ public class MockServletContext implements ServletContext {
return this.contexts.get(contextPath);
}
public void setMajorVersion(int majorVersion) {
this.majorVersion = majorVersion;
}
public int getMajorVersion() {
return 2;
return this.majorVersion;
}
public void setMinorVersion(int minorVersion) {
if (minorVersion < 3 || minorVersion > 5) {
throw new IllegalArgumentException("Only Servlet minor versions between 3 and 5 are supported");
}
this.minorVersion = minorVersion;
}
@@ -206,6 +213,22 @@ public class MockServletContext implements ServletContext {
return this.minorVersion;
}
public void setEffectiveMajorVersion(int effectiveMajorVersion) {
this.effectiveMajorVersion = effectiveMajorVersion;
}
public int getEffectiveMajorVersion() {
return this.effectiveMajorVersion;
}
public void setEffectiveMinorVersion(int effectiveMinorVersion) {
this.effectiveMinorVersion = effectiveMinorVersion;
}
public int getEffectiveMinorVersion() {
return this.effectiveMinorVersion;
}
public String getMimeType(String filePath) {
return MimeTypeResolver.getMimeType(filePath);
}
@@ -444,14 +467,6 @@ public class MockServletContext implements ServletContext {
throw new UnsupportedOperationException();
}
public int getEffectiveMajorVersion() {
throw new UnsupportedOperationException();
}
public int getEffectiveMinorVersion() {
throw new UnsupportedOperationException();
}
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
throw new UnsupportedOperationException();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2011 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.
@@ -24,18 +24,55 @@ import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.FunctionMapper;
import javax.servlet.jsp.el.VariableResolver;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.mock.web.MockExpressionEvaluator;
import org.springframework.mock.web.MockPageContext;
import org.springframework.mock.web.MockServletContext;
import static org.junit.Assert.*;
/**
* @author Aled Arendsen
* @author Juergen Hoeller
* @since 16.09.2003
*/
public class ExpressionEvaluationUtilsTests extends TestCase {
public class ExpressionEvaluationUtilsTests {
@Test
public void testIsSpringJspExpressionSupportActive() {
MockServletContext sc = new MockServletContext();
PageContext pc = new MockPageContext(sc);
assertTrue(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
sc.addInitParameter("springJspExpressionSupport", "false");
assertFalse(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
}
@Test
public void testIsSpringJspExpressionSupportActiveOnServlet30() {
MockServletContext sc = new MockServletContext();
sc.setMajorVersion(3);
sc.setMinorVersion(0);
PageContext pc = new MockPageContext(sc);
assertFalse(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
sc.addInitParameter("springJspExpressionSupport", "true");
assertTrue(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
}
@Test
public void testIsSpringJspExpressionSupportActiveOnServlet30WithServlet23Application() {
MockServletContext sc = new MockServletContext();
sc.setMajorVersion(3);
sc.setMinorVersion(0);
sc.setEffectiveMajorVersion(2);
sc.setEffectiveMinorVersion(3);
PageContext pc = new MockPageContext(sc);
assertTrue(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
sc.addInitParameter("springJspExpressionSupport", "false");
assertFalse(ExpressionEvaluationUtils.isSpringJspExpressionSupportActive(pc));
}
@Test
public void testIsExpressionLanguage() {
assertTrue(ExpressionEvaluationUtils.isExpressionLanguage("${bla}"));
assertTrue(ExpressionEvaluationUtils.isExpressionLanguage("bla${bla}"));
@@ -43,6 +80,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertFalse(ExpressionEvaluationUtils.isExpressionLanguage("bla$b{"));
}
@Test
public void testEvaluate() throws Exception {
PageContext ctx = new MockPageContext();
ctx.setAttribute("bla", "blie");
@@ -59,6 +97,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
}
}
@Test
public void testEvaluateWithConcatenation() throws Exception {
PageContext ctx = new MockPageContext();
ctx.setAttribute("bla", "blie");
@@ -98,6 +137,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
}
}
@Test
public void testEvaluateString() throws Exception {
PageContext ctx = new MockPageContext();
ctx.setAttribute("bla", "blie");
@@ -106,6 +146,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertEquals("blie", ExpressionEvaluationUtils.evaluateString("test", "blie", ctx));
}
@Test
public void testEvaluateStringWithConcatenation() throws Exception {
PageContext ctx = new MockPageContext();
ctx.setAttribute("bla", "blie");
@@ -137,6 +178,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
}
@Test
public void testEvaluateInteger() throws Exception {
PageContext ctx = new MockPageContext();
ctx.setAttribute("bla", new Integer(1));
@@ -145,6 +187,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertEquals(21, ExpressionEvaluationUtils.evaluateInteger("test", "21", ctx));
}
@Test
public void testEvaluateBoolean() throws Exception {
PageContext ctx = new MockPageContext();
ctx.setAttribute("bla", new Boolean(true));
@@ -153,6 +196,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertTrue(ExpressionEvaluationUtils.evaluateBoolean("test", "true", ctx));
}
@Test
public void testRepeatedEvaluate() throws Exception {
PageContext ctx = new CountingMockPageContext();
CountingMockExpressionEvaluator eval = (CountingMockExpressionEvaluator) ctx.getExpressionEvaluator();
@@ -172,6 +216,7 @@ public class ExpressionEvaluationUtilsTests extends TestCase {
assertEquals(4, eval.evaluateCount);
}
@Test
public void testEvaluateWithComplexConcatenation() throws Exception {
PageContext ctx = new CountingMockPageContext();
CountingMockExpressionEvaluator eval = (CountingMockExpressionEvaluator) ctx.getExpressionEvaluator();