Moved tests from testsuite to web.servlet

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@275 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Arjen Poutsma 2008-11-03 12:03:31 +00:00
parent d9e1188eb7
commit 9a0ed51d84
38 changed files with 1155 additions and 23 deletions

View File

@ -0,0 +1,30 @@
/*
* Copyright 2002-2007 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.beans;
/**
* @author Juergen Hoeller
*/
public enum CustomEnum {
VALUE_1, VALUE_2;
public String toString() {
return "CustomEnum: " + name();
}
}

View File

@ -0,0 +1,237 @@
/*
* Copyright 2002-2008 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.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.core.io.Resource;
/**
* @author Juergen Hoeller
*/
public class GenericBean<T> {
private Set<Integer> integerSet;
private List<Resource> resourceList;
private List<List<Integer>> listOfLists;
private ArrayList<String[]> listOfArrays;
private List<Map<Integer, Long>> listOfMaps;
private Map plainMap;
private Map<Short, Integer> shortMap;
private HashMap<Long, ?> longMap;
private Map<Number, Collection<? extends Object>> collectionMap;
private Map<String, Map<Integer, Long>> mapOfMaps;
private Map<Integer, List<Integer>> mapOfLists;
private CustomEnum customEnum;
private T genericProperty;
private List<T> genericListProperty;
public GenericBean() {
}
public GenericBean(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
public GenericBean(Set<Integer> integerSet, List<Resource> resourceList) {
this.integerSet = integerSet;
this.resourceList = resourceList;
}
public GenericBean(HashSet<Integer> integerSet, Map<Short, Integer> shortMap) {
this.integerSet = integerSet;
this.shortMap = shortMap;
}
public GenericBean(Map<Short, Integer> shortMap, Resource resource) {
this.shortMap = shortMap;
this.resourceList = Collections.singletonList(resource);
}
public GenericBean(Map plainMap, Map<Short, Integer> shortMap) {
this.plainMap = plainMap;
this.shortMap = shortMap;
}
public GenericBean(HashMap<Long, ?> longMap) {
this.longMap = longMap;
}
public GenericBean(boolean someFlag, Map<Number, Collection<? extends Object>> collectionMap) {
this.collectionMap = collectionMap;
}
public Set<Integer> getIntegerSet() {
return integerSet;
}
public void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
public List<Resource> getResourceList() {
return resourceList;
}
public void setResourceList(List<Resource> resourceList) {
this.resourceList = resourceList;
}
public List<List<Integer>> getListOfLists() {
return listOfLists;
}
public ArrayList<String[]> getListOfArrays() {
return listOfArrays;
}
public void setListOfArrays(ArrayList<String[]> listOfArrays) {
this.listOfArrays = listOfArrays;
}
public void setListOfLists(List<List<Integer>> listOfLists) {
this.listOfLists = listOfLists;
}
public List<Map<Integer, Long>> getListOfMaps() {
return listOfMaps;
}
public void setListOfMaps(List<Map<Integer, Long>> listOfMaps) {
this.listOfMaps = listOfMaps;
}
public Map getPlainMap() {
return plainMap;
}
public Map<Short, Integer> getShortMap() {
return shortMap;
}
public void setShortMap(Map<Short, Integer> shortMap) {
this.shortMap = shortMap;
}
public HashMap<Long, ?> getLongMap() {
return longMap;
}
public void setLongMap(HashMap<Long, ?> longMap) {
this.longMap = longMap;
}
public Map<Number, Collection<? extends Object>> getCollectionMap() {
return collectionMap;
}
public void setCollectionMap(Map<Number, Collection<? extends Object>> collectionMap) {
this.collectionMap = collectionMap;
}
public Map<String, Map<Integer, Long>> getMapOfMaps() {
return mapOfMaps;
}
public void setMapOfMaps(Map<String, Map<Integer, Long>> mapOfMaps) {
this.mapOfMaps = mapOfMaps;
}
public Map<Integer, List<Integer>> getMapOfLists() {
return mapOfLists;
}
public void setMapOfLists(Map<Integer, List<Integer>> mapOfLists) {
this.mapOfLists = mapOfLists;
}
public T getGenericProperty() {
return genericProperty;
}
public void setGenericProperty(T genericProperty) {
this.genericProperty = genericProperty;
}
public List<T> getGenericListProperty() {
return genericListProperty;
}
public void setGenericListProperty(List<T> genericListProperty) {
this.genericListProperty = genericListProperty;
}
public CustomEnum getCustomEnum() {
return customEnum;
}
public void setCustomEnum(CustomEnum customEnum) {
this.customEnum = customEnum;
}
public static GenericBean createInstance(Set<Integer> integerSet) {
return new GenericBean(integerSet);
}
public static GenericBean createInstance(Set<Integer> integerSet, List<Resource> resourceList) {
return new GenericBean(integerSet, resourceList);
}
public static GenericBean createInstance(HashSet<Integer> integerSet, Map<Short, Integer> shortMap) {
return new GenericBean(integerSet, shortMap);
}
public static GenericBean createInstance(Map<Short, Integer> shortMap, Resource resource) {
return new GenericBean(shortMap, resource);
}
public static GenericBean createInstance(Map map, Map<Short, Integer> shortMap) {
return new GenericBean(map, shortMap);
}
public static GenericBean createInstance(HashMap<Long, ?> longMap) {
return new GenericBean(longMap);
}
public static GenericBean createInstance(boolean someFlag, Map<Number, Collection<? extends Object>> collectionMap) {
return new GenericBean(someFlag, collectionMap);
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2002-2006 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.beans;
/**
* @author Rob Harrop
* @since 2.0
*/
public class Pet {
private String name;
public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return getName();
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Pet pet = (Pet) o;
if (name != null ? !name.equals(pet.name) : pet.name != null) return false;
return true;
}
public int hashCode() {
return (name != null ? name.hashCode() : 0);
}
}

View File

@ -0,0 +1,198 @@
/*
* Copyright 2002-2007 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.mock.web;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
/**
* Mock implementation of the {@link javax.servlet.jsp.tagext.BodyContent} class.
*
* <p>Used for testing the web framework; only necessary for testing
* applications when testing custom JSP tags.
*
* @author Juergen Hoeller
* @since 2.5
*/
public class MockBodyContent extends BodyContent {
private final String content;
/**
* Create a MockBodyContent for the given response.
* @param content the body content to expose
* @param response the servlet response to wrap
*/
public MockBodyContent(String content, HttpServletResponse response) {
this(content, response, null);
}
/**
* Create a MockBodyContent for the given response.
* @param content the body content to expose
* @param targetWriter the target Writer to wrap
*/
public MockBodyContent(String content, Writer targetWriter) {
this(content, null, targetWriter);
}
/**
* Create a MockBodyContent for the given response.
* @param content the body content to expose
* @param response the servlet response to wrap
* @param targetWriter the target Writer to wrap
*/
public MockBodyContent(String content, HttpServletResponse response, Writer targetWriter) {
super(adaptJspWriter(targetWriter, response));
this.content = content;
}
private static JspWriter adaptJspWriter(Writer targetWriter, HttpServletResponse response) {
if (targetWriter instanceof JspWriter) {
return (JspWriter) targetWriter;
}
else {
return new MockJspWriter(response, targetWriter);
}
}
public Reader getReader() {
return new StringReader(this.content);
}
public String getString() {
return this.content;
}
public void writeOut(Writer writer) throws IOException {
writer.write(this.content);
}
//---------------------------------------------------------------------
// Delegating implementations of JspWriter's abstract methods
//---------------------------------------------------------------------
public void clear() throws IOException {
getEnclosingWriter().clear();
}
public void clearBuffer() throws IOException {
getEnclosingWriter().clearBuffer();
}
public void close() throws IOException {
getEnclosingWriter().close();
}
public int getRemaining() {
return getEnclosingWriter().getRemaining();
}
public void newLine() throws IOException {
getEnclosingWriter().println();
}
public void write(char value[], int offset, int length) throws IOException {
getEnclosingWriter().write(value, offset, length);
}
public void print(boolean value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(char value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(char[] value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(double value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(float value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(int value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(long value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(Object value) throws IOException {
getEnclosingWriter().print(value);
}
public void print(String value) throws IOException {
getEnclosingWriter().print(value);
}
public void println() throws IOException {
getEnclosingWriter().println();
}
public void println(boolean value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(char value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(char[] value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(double value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(float value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(int value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(long value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(Object value) throws IOException {
getEnclosingWriter().println(value);
}
public void println(String value) throws IOException {
getEnclosingWriter().println(value);
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2002-2006 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.mock.web;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.Expression;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.FunctionMapper;
import javax.servlet.jsp.el.VariableResolver;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
/**
* Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator}
* interface, delegating to the Jakarta JSTL ExpressionEvaluatorManager.
*
* <p>Used for testing the web framework; only necessary for testing
* applications when testing custom JSP tags.
*
* <p>Note that the Jakarta JSTL implementation (jstl.jar, standard.jar)
* has to be available on the class path to use this expression evaluator.
*
* @author Juergen Hoeller
* @since 1.1.5
* @see org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager
*/
public class MockExpressionEvaluator extends ExpressionEvaluator {
private final PageContext pageContext;
/**
* Create a new MockExpressionEvaluator for the given PageContext.
* @param pageContext the JSP PageContext to run in
*/
public MockExpressionEvaluator(PageContext pageContext) {
this.pageContext = pageContext;
}
public Expression parseExpression(
final String expression, final Class expectedType, final FunctionMapper functionMapper)
throws ELException {
return new Expression() {
public Object evaluate(VariableResolver variableResolver) throws ELException {
return doEvaluate(expression, expectedType, functionMapper);
}
};
}
public Object evaluate(
String expression, Class expectedType, VariableResolver variableResolver, FunctionMapper functionMapper)
throws ELException {
if (variableResolver != null) {
throw new IllegalArgumentException("Custom VariableResolver not supported");
}
return doEvaluate(expression, expectedType, functionMapper);
}
protected Object doEvaluate(
String expression, Class expectedType, FunctionMapper functionMapper)
throws ELException {
if (functionMapper != null) {
throw new IllegalArgumentException("Custom FunctionMapper not supported");
}
try {
return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
}
catch (JspException ex) {
throw new ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex);
}
}
}

View File

@ -0,0 +1,192 @@
/*
* Copyright 2002-2007 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.mock.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
/**
* Mock implementation of the {@link javax.servlet.jsp.JspWriter} class.
*
* <p>Used for testing the web framework; only necessary for testing
* applications when testing custom JSP tags.
*
* @author Juergen Hoeller
* @since 2.5
*/
public class MockJspWriter extends JspWriter {
private final HttpServletResponse response;
private PrintWriter targetWriter;
/**
* Create a MockJspWriter for the given response,
* using the response's default Writer.
* @param response the servlet response to wrap
*/
public MockJspWriter(HttpServletResponse response) {
this(response, null);
}
/**
* Create a MockJspWriter for the given plain Writer.
* @param targetWriter the target Writer to wrap
*/
public MockJspWriter(Writer targetWriter) {
this(null, targetWriter);
}
/**
* Create a MockJspWriter for the given response.
* @param response the servlet response to wrap
* @param targetWriter the target Writer to wrap
*/
public MockJspWriter(HttpServletResponse response, Writer targetWriter) {
super(DEFAULT_BUFFER, true);
this.response = (response != null ? response : new MockHttpServletResponse());
if (targetWriter instanceof PrintWriter) {
this.targetWriter = (PrintWriter) targetWriter;
}
else if (targetWriter != null) {
this.targetWriter = new PrintWriter(targetWriter);
}
}
/**
* Lazily initialize the target Writer.
*/
protected PrintWriter getTargetWriter() throws IOException {
if (this.targetWriter == null) {
this.targetWriter = this.response.getWriter();
}
return this.targetWriter;
}
public void clear() throws IOException {
if (this.response.isCommitted()) {
throw new IOException("Response already committed");
}
this.response.resetBuffer();
}
public void clearBuffer() throws IOException {
}
public void flush() throws IOException {
this.response.flushBuffer();
}
public void close() throws IOException {
flush();
}
public int getRemaining() {
return Integer.MAX_VALUE;
}
public void newLine() throws IOException {
getTargetWriter().println();
}
public void write(char value[], int offset, int length) throws IOException {
getTargetWriter().write(value, offset, length);
}
public void print(boolean value) throws IOException {
getTargetWriter().print(value);
}
public void print(char value) throws IOException {
getTargetWriter().print(value);
}
public void print(char[] value) throws IOException {
getTargetWriter().print(value);
}
public void print(double value) throws IOException {
getTargetWriter().print(value);
}
public void print(float value) throws IOException {
getTargetWriter().print(value);
}
public void print(int value) throws IOException {
getTargetWriter().print(value);
}
public void print(long value) throws IOException {
getTargetWriter().print(value);
}
public void print(Object value) throws IOException {
getTargetWriter().print(value);
}
public void print(String value) throws IOException {
getTargetWriter().print(value);
}
public void println() throws IOException {
getTargetWriter().println();
}
public void println(boolean value) throws IOException {
getTargetWriter().println(value);
}
public void println(char value) throws IOException {
getTargetWriter().println(value);
}
public void println(char[] value) throws IOException {
getTargetWriter().println(value);
}
public void println(double value) throws IOException {
getTargetWriter().println(value);
}
public void println(float value) throws IOException {
getTargetWriter().println(value);
}
public void println(int value) throws IOException {
getTargetWriter().println(value);
}
public void println(long value) throws IOException {
getTargetWriter().println(value);
}
public void println(Object value) throws IOException {
getTargetWriter().println(value);
}
public void println(String value) throws IOException {
getTargetWriter().println(value);
}
}

View File

@ -0,0 +1,330 @@
/*
* Copyright 2002-2007 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.mock.web;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;
import org.springframework.util.Assert;
/**
* Mock implementation of the {@link javax.servlet.jsp.PageContext} interface.
*
* <p>Used for testing the web framework; only necessary for testing
* applications when testing custom JSP tags.
*
* <p>Note: Expects initialization via the constructor rather than via the
* <code>PageContext.initialize</code> method. Does not support writing to
* a JspWriter, request dispatching, and <code>handlePageException</code> calls.
*
* @author Juergen Hoeller
* @since 1.0.2
*/
public class MockPageContext extends PageContext {
private final ServletContext servletContext;
private final HttpServletRequest request;
private final HttpServletResponse response;
private final ServletConfig servletConfig;
private final Hashtable attributes = new Hashtable();
private JspWriter out;
/**
* Create new MockPageContext with a default {@link org.springframework.mock.web.MockServletContext},
* {@link org.springframework.mock.web.MockHttpServletRequest}, {@link org.springframework.mock.web.MockHttpServletResponse},
* {@link org.springframework.mock.web.MockServletConfig}.
*/
public MockPageContext() {
this(null, null, null, null);
}
/**
* Create new MockPageContext with a default {@link org.springframework.mock.web.MockHttpServletRequest},
* {@link org.springframework.mock.web.MockHttpServletResponse}, {@link org.springframework.mock.web.MockServletConfig}.
* @param servletContext the ServletContext that the JSP page runs in
* (only necessary when actually accessing the ServletContext)
*/
public MockPageContext(ServletContext servletContext) {
this(servletContext, null, null, null);
}
/**
* Create new MockPageContext with a MockHttpServletResponse,
* MockServletConfig.
* @param servletContext the ServletContext that the JSP page runs in
* @param request the current HttpServletRequest
* (only necessary when actually accessing the request)
*/
public MockPageContext(ServletContext servletContext, HttpServletRequest request) {
this(servletContext, request, null, null);
}
/**
* Create new MockPageContext with a MockServletConfig.
* @param servletContext the ServletContext that the JSP page runs in
* @param request the current HttpServletRequest
* @param response the current HttpServletResponse
* (only necessary when actually writing to the response)
*/
public MockPageContext(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) {
this(servletContext, request, response, null);
}
/**
* Create new MockServletConfig.
* @param servletContext the ServletContext that the JSP page runs in
* @param request the current HttpServletRequest
* @param response the current HttpServletResponse
* @param servletConfig the ServletConfig (hardly ever accessed from within a tag)
*/
public MockPageContext(ServletContext servletContext, HttpServletRequest request,
HttpServletResponse response, ServletConfig servletConfig) {
this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
this.request = (request != null ? request : new MockHttpServletRequest(servletContext));
this.response = (response != null ? response : new MockHttpServletResponse());
this.servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig(servletContext));
}
public void initialize(
Servlet servlet, ServletRequest request, ServletResponse response,
String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) {
throw new UnsupportedOperationException("Use appropriate constructor");
}
public void release() {
}
public void setAttribute(String name, Object value) {
Assert.notNull(name, "Attribute name must not be null");
if (value != null) {
this.attributes.put(name, value);
}
else {
this.attributes.remove(name);
}
}
public void setAttribute(String name, Object value, int scope) {
Assert.notNull(name, "Attribute name must not be null");
switch (scope) {
case PAGE_SCOPE:
setAttribute(name, value);
break;
case REQUEST_SCOPE:
this.request.setAttribute(name, value);
break;
case SESSION_SCOPE:
this.request.getSession().setAttribute(name, value);
break;
case APPLICATION_SCOPE:
this.servletContext.setAttribute(name, value);
break;
default:
throw new IllegalArgumentException("Invalid scope: " + scope);
}
}
public Object getAttribute(String name) {
Assert.notNull(name, "Attribute name must not be null");
return this.attributes.get(name);
}
public Object getAttribute(String name, int scope) {
Assert.notNull(name, "Attribute name must not be null");
switch (scope) {
case PAGE_SCOPE:
return getAttribute(name);
case REQUEST_SCOPE:
return this.request.getAttribute(name);
case SESSION_SCOPE:
HttpSession session = this.request.getSession(false);
return (session != null ? session.getAttribute(name) : null);
case APPLICATION_SCOPE:
return this.servletContext.getAttribute(name);
default:
throw new IllegalArgumentException("Invalid scope: " + scope);
}
}
public Object findAttribute(String name) {
Object value = getAttribute(name);
if (value == null) {
value = getAttribute(name, REQUEST_SCOPE);
if (value == null) {
value = getAttribute(name, SESSION_SCOPE);
if (value == null) {
value = getAttribute(name, APPLICATION_SCOPE);
}
}
}
return value;
}
public void removeAttribute(String name) {
Assert.notNull(name, "Attribute name must not be null");
this.removeAttribute(name, PageContext.PAGE_SCOPE);
this.removeAttribute(name, PageContext.REQUEST_SCOPE);
this.removeAttribute(name, PageContext.SESSION_SCOPE);
this.removeAttribute(name, PageContext.APPLICATION_SCOPE);
}
public void removeAttribute(String name, int scope) {
Assert.notNull(name, "Attribute name must not be null");
switch (scope) {
case PAGE_SCOPE:
this.attributes.remove(name);
break;
case REQUEST_SCOPE:
this.request.removeAttribute(name);
break;
case SESSION_SCOPE:
this.request.getSession().removeAttribute(name);
break;
case APPLICATION_SCOPE:
this.servletContext.removeAttribute(name);
break;
default:
throw new IllegalArgumentException("Invalid scope: " + scope);
}
}
public int getAttributesScope(String name) {
if (getAttribute(name) != null) {
return PAGE_SCOPE;
}
else if (getAttribute(name, REQUEST_SCOPE) != null) {
return REQUEST_SCOPE;
}
else if (getAttribute(name, SESSION_SCOPE) != null) {
return SESSION_SCOPE;
}
else if (getAttribute(name, APPLICATION_SCOPE) != null) {
return APPLICATION_SCOPE;
}
else {
return 0;
}
}
public Enumeration getAttributeNames() {
return this.attributes.keys();
}
public Enumeration getAttributeNamesInScope(int scope) {
switch (scope) {
case PAGE_SCOPE:
return getAttributeNames();
case REQUEST_SCOPE:
return this.request.getAttributeNames();
case SESSION_SCOPE:
HttpSession session = this.request.getSession(false);
return (session != null ? session.getAttributeNames() : null);
case APPLICATION_SCOPE:
return this.servletContext.getAttributeNames();
default:
throw new IllegalArgumentException("Invalid scope: " + scope);
}
}
public JspWriter getOut() {
if (this.out == null) {
this.out = new MockJspWriter(this.response);
}
return this.out;
}
public ExpressionEvaluator getExpressionEvaluator() {
return new MockExpressionEvaluator(this);
}
public VariableResolver getVariableResolver() {
return null;
}
public HttpSession getSession() {
return this.request.getSession();
}
public Object getPage() {
throw new UnsupportedOperationException("getPage");
}
public ServletRequest getRequest() {
return this.request;
}
public ServletResponse getResponse() {
return this.response;
}
public Exception getException() {
throw new UnsupportedOperationException("getException");
}
public ServletConfig getServletConfig() {
return this.servletConfig;
}
public ServletContext getServletContext() {
return this.servletContext;
}
public void forward(String url) throws ServletException, IOException {
throw new UnsupportedOperationException("forward");
}
public void include(String url) throws ServletException, IOException {
throw new UnsupportedOperationException("include");
}
public void include(String url, boolean flush) throws ServletException, IOException {
throw new UnsupportedOperationException("include");
}
public void handlePageException(Exception ex) throws ServletException, IOException {
throw new UnsupportedOperationException("handlePageException");
}
public void handlePageException(Throwable ex) throws ServletException, IOException {
throw new UnsupportedOperationException("handlePageException");
}
}

View File

@ -20,7 +20,6 @@ import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.AssertThrows;
/**
* @author Rob Harrop
@ -150,13 +149,14 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
}
public void testWithNullResolvedCommand() throws Exception {
new AssertThrows(IllegalArgumentException.class,
"Must not be able to have a command name that resolves to null") {
public void test() throws Exception {
tag.setCommandName("${null}");
tag.doStartTag();
}
}.runTest();
try {
tag.setCommandName("${null}");
tag.doStartTag();
fail("Must not be able to have a command name that resolves to null");
}
catch (IllegalArgumentException ex) {
// expected
}
}
/*

View File

@ -30,7 +30,6 @@ import org.springframework.beans.TestBean;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.mock.web.MockBodyContent;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.AssertThrows;
import org.springframework.util.StringUtils;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.web.servlet.support.BindStatus;
@ -141,12 +140,12 @@ public class OptionTagTests extends AbstractHtmlElementTagTests {
this.tag.setParent(null);
this.tag.setValue("foo");
this.tag.setLabel("Foo");
new AssertThrows(IllegalStateException.class,
"Must not be able to use <option> tag without exposed context.") {
public void test() throws Exception {
tag.doStartTag();
}
}.runTest();
try {
tag.doStartTag();
fail("Must not be able to use <option> tag without exposed context.");
} catch (IllegalStateException ex) {
// expected
}
}
public void testWithEnum() throws Exception {
@ -429,14 +428,14 @@ public class OptionTagTests extends AbstractHtmlElementTagTests {
}
public void testOptionTagNotNestedWithinSelectTag() throws Exception {
new AssertThrows(IllegalStateException.class,
"Must throw an IllegalStateException when not nested within a <select/> tag.") {
public void test() throws Exception {
tag.setParent(null);
tag.setValue("foo");
tag.doStartTag();
}
}.runTest();
try {
tag.setParent(null);
tag.setValue("foo");
tag.doStartTag();
fail("Must throw an IllegalStateException when not nested within a <select/> tag.");
} catch (IllegalStateException ex) {
// expected
}
}