diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/beans/CustomEnum.java b/org.springframework.web.servlet/src/test/java/org/springframework/beans/CustomEnum.java new file mode 100644 index 00000000000..1e43492191b --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/beans/CustomEnum.java @@ -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(); + } + +} \ No newline at end of file diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/beans/GenericBean.java b/org.springframework.web.servlet/src/test/java/org/springframework/beans/GenericBean.java new file mode 100644 index 00000000000..c4b85fa1f61 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/beans/GenericBean.java @@ -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 { + + private Set integerSet; + + private List resourceList; + + private List> listOfLists; + + private ArrayList listOfArrays; + + private List> listOfMaps; + + private Map plainMap; + + private Map shortMap; + + private HashMap longMap; + + private Map> collectionMap; + + private Map> mapOfMaps; + + private Map> mapOfLists; + + private CustomEnum customEnum; + + private T genericProperty; + + private List genericListProperty; + + + public GenericBean() { + } + + public GenericBean(Set integerSet) { + this.integerSet = integerSet; + } + + public GenericBean(Set integerSet, List resourceList) { + this.integerSet = integerSet; + this.resourceList = resourceList; + } + + public GenericBean(HashSet integerSet, Map shortMap) { + this.integerSet = integerSet; + this.shortMap = shortMap; + } + + public GenericBean(Map shortMap, Resource resource) { + this.shortMap = shortMap; + this.resourceList = Collections.singletonList(resource); + } + + public GenericBean(Map plainMap, Map shortMap) { + this.plainMap = plainMap; + this.shortMap = shortMap; + } + + public GenericBean(HashMap longMap) { + this.longMap = longMap; + } + + public GenericBean(boolean someFlag, Map> collectionMap) { + this.collectionMap = collectionMap; + } + + + public Set getIntegerSet() { + return integerSet; + } + + public void setIntegerSet(Set integerSet) { + this.integerSet = integerSet; + } + + public List getResourceList() { + return resourceList; + } + + public void setResourceList(List resourceList) { + this.resourceList = resourceList; + } + + public List> getListOfLists() { + return listOfLists; + } + + public ArrayList getListOfArrays() { + return listOfArrays; + } + + public void setListOfArrays(ArrayList listOfArrays) { + this.listOfArrays = listOfArrays; + } + + public void setListOfLists(List> listOfLists) { + this.listOfLists = listOfLists; + } + + public List> getListOfMaps() { + return listOfMaps; + } + + public void setListOfMaps(List> listOfMaps) { + this.listOfMaps = listOfMaps; + } + + public Map getPlainMap() { + return plainMap; + } + + public Map getShortMap() { + return shortMap; + } + + public void setShortMap(Map shortMap) { + this.shortMap = shortMap; + } + + public HashMap getLongMap() { + return longMap; + } + + public void setLongMap(HashMap longMap) { + this.longMap = longMap; + } + + public Map> getCollectionMap() { + return collectionMap; + } + + public void setCollectionMap(Map> collectionMap) { + this.collectionMap = collectionMap; + } + + public Map> getMapOfMaps() { + return mapOfMaps; + } + + public void setMapOfMaps(Map> mapOfMaps) { + this.mapOfMaps = mapOfMaps; + } + + public Map> getMapOfLists() { + return mapOfLists; + } + + public void setMapOfLists(Map> mapOfLists) { + this.mapOfLists = mapOfLists; + } + + public T getGenericProperty() { + return genericProperty; + } + + public void setGenericProperty(T genericProperty) { + this.genericProperty = genericProperty; + } + + public List getGenericListProperty() { + return genericListProperty; + } + + public void setGenericListProperty(List genericListProperty) { + this.genericListProperty = genericListProperty; + } + + public CustomEnum getCustomEnum() { + return customEnum; + } + + public void setCustomEnum(CustomEnum customEnum) { + this.customEnum = customEnum; + } + + + public static GenericBean createInstance(Set integerSet) { + return new GenericBean(integerSet); + } + + public static GenericBean createInstance(Set integerSet, List resourceList) { + return new GenericBean(integerSet, resourceList); + } + + public static GenericBean createInstance(HashSet integerSet, Map shortMap) { + return new GenericBean(integerSet, shortMap); + } + + public static GenericBean createInstance(Map shortMap, Resource resource) { + return new GenericBean(shortMap, resource); + } + + public static GenericBean createInstance(Map map, Map shortMap) { + return new GenericBean(map, shortMap); + } + + public static GenericBean createInstance(HashMap longMap) { + return new GenericBean(longMap); + } + + public static GenericBean createInstance(boolean someFlag, Map> collectionMap) { + return new GenericBean(someFlag, collectionMap); + } + +} \ No newline at end of file diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/beans/Pet.java b/org.springframework.web.servlet/src/test/java/org/springframework/beans/Pet.java new file mode 100644 index 00000000000..35d9c736c13 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/beans/Pet.java @@ -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); + } + +} \ No newline at end of file diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockBodyContent.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockBodyContent.java new file mode 100644 index 00000000000..64a65f83c34 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockBodyContent.java @@ -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. + * + *

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); + } + +} \ No newline at end of file diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockExpressionEvaluator.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockExpressionEvaluator.java new file mode 100644 index 00000000000..7ff01020fb6 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockExpressionEvaluator.java @@ -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. + * + *

Used for testing the web framework; only necessary for testing + * applications when testing custom JSP tags. + * + *

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); + } + } + +} \ No newline at end of file diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockJspWriter.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockJspWriter.java new file mode 100644 index 00000000000..d59471e24ff --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockJspWriter.java @@ -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. + * + *

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); + } + +} \ No newline at end of file diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockPageContext.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockPageContext.java new file mode 100644 index 00000000000..7c4fb632ee1 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockPageContext.java @@ -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. + * + *

Used for testing the web framework; only necessary for testing + * applications when testing custom JSP tags. + * + *

Note: Expects initialization via the constructor rather than via the + * PageContext.initialize method. Does not support writing to + * a JspWriter, request dispatching, and handlePageException 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"); + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/BindTagOutsideDispatcherServletTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/BindTagOutsideDispatcherServletTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/BindTagOutsideDispatcherServletTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/BindTagOutsideDispatcherServletTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/BindTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/BindTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/BindTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/BindTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagOutsideDispatcherServletTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagOutsideDispatcherServletTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagOutsideDispatcherServletTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagOutsideDispatcherServletTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/HtmlEscapeTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/MessageTagOutsideDispatcherServletTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/MessageTagOutsideDispatcherServletTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/MessageTagOutsideDispatcherServletTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/MessageTagOutsideDispatcherServletTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/MessageTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/MessageTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/MessageTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/MessageTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/ThemeTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/ThemeTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/ThemeTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/ThemeTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/AbstractFormTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/AbstractFormTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/AbstractFormTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/AbstractFormTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/AbstractHtmlElementTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/AbstractHtmlElementTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/AbstractHtmlElementTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/AbstractHtmlElementTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/CheckboxTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/CheckboxTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/CheckboxTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/CheckboxTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/CheckboxesTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/CheckboxesTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/CheckboxesTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/CheckboxesTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/Country.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/Country.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/Country.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/Country.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java similarity index 95% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java index 24b22bc25ef..10c4c5d8ac4 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/FormTagTests.java @@ -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 + } } /* diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/HiddenInputTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/HiddenInputTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/HiddenInputTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/HiddenInputTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/ItemPet.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ItemPet.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/ItemPet.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ItemPet.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/LabelTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/LabelTagTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/LabelTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/LabelTagTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/OptionTagEnumTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/OptionTagEnumTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/OptionTagEnumTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/OptionTagEnumTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/OptionTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/OptionTagTests.java similarity index 97% rename from org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/OptionTagTests.java rename to org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/OptionTagTests.java index b32fb893e2b..31ccb36fb4b 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/tags/form/OptionTagTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/OptionTagTests.java @@ -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