diff --git a/webapp/test/stubs/javax/servlet/ServletConfigStub.java b/webapp/test/stubs/javax/servlet/ServletConfigStub.java new file mode 100644 index 000000000..12af5bef2 --- /dev/null +++ b/webapp/test/stubs/javax/servlet/ServletConfigStub.java @@ -0,0 +1,56 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package stubs.javax.servlet; + +import java.util.Enumeration; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; + +/** + * A simple stub for testing servlets. + */ +public class ServletConfigStub implements ServletConfig { + // ---------------------------------------------------------------------- + // Stub infrastructure + // ---------------------------------------------------------------------- + + private ServletContext servletContext; + + public void setServletContext(ServletContext servletContext) { + this.servletContext = servletContext; + } + + // ---------------------------------------------------------------------- + // Stub methods + // ---------------------------------------------------------------------- + + @Override + public ServletContext getServletContext() { + return servletContext; + } + + // ---------------------------------------------------------------------- + // Un-implemented methods + // ---------------------------------------------------------------------- + + @Override + public String getInitParameter(String arg0) { + throw new RuntimeException( + "ServletConfigStub.getInitParameter() not implemented."); + } + + @Override + @SuppressWarnings("rawtypes") + public Enumeration getInitParameterNames() { + throw new RuntimeException( + "ServletConfigStub.getInitParameterNames() not implemented."); + } + + @Override + public String getServletName() { + throw new RuntimeException( + "ServletConfigStub.getServletName() not implemented."); + } + +} diff --git a/webapp/test/stubs/javax/servlet/http/HttpServletRequestStub.java b/webapp/test/stubs/javax/servlet/http/HttpServletRequestStub.java index b00577ec0..f6f52d746 100644 --- a/webapp/test/stubs/javax/servlet/http/HttpServletRequestStub.java +++ b/webapp/test/stubs/javax/servlet/http/HttpServletRequestStub.java @@ -5,8 +5,10 @@ package stubs.javax.servlet.http; import java.io.BufferedReader; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.URL; import java.security.Principal; import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; @@ -20,18 +22,26 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** - * - * @author jeb228 + * A simple stub for HttpServletRequest */ -@SuppressWarnings("deprecation") public class HttpServletRequestStub implements HttpServletRequest { - // ---------------------------------------------------------------------- - // Stub infrastructure - // ---------------------------------------------------------------------- + // ---------------------------------------------------------------------- + // Stub infrastructure + // ---------------------------------------------------------------------- + private URL requestUrl; + private String httpMethodType = "GET"; + private String remoteAddr = "127.0.0.1"; + + private HttpSession session; private final Map> parameters; private final Map attributes; + public HttpServletRequestStub() { + parameters = new HashMap>(); + attributes = new HashMap(); + } + public HttpServletRequestStub(Map> parameters, Map attributes) { this(); @@ -39,9 +49,17 @@ public class HttpServletRequestStub implements HttpServletRequest { this.attributes.putAll(attributes); } - public HttpServletRequestStub() { - parameters = new HashMap>(); - attributes = new HashMap(); + public void setRequestUrl(URL url) { + this.requestUrl = url; + } + + /** Set to "GET" or "POST", etc. */ + public void setMethod(String method) { + this.httpMethodType = method; + } + + public void setRemoteAddr(String remoteAddr) { + this.remoteAddr = remoteAddr; } public void addParameter(String name, String value) { @@ -56,17 +74,52 @@ public class HttpServletRequestStub implements HttpServletRequest { parameters.remove(name); } - // ---------------------------------------------------------------------- - // Stub methods - // ---------------------------------------------------------------------- - - public String getParameter(String name) { - if (!parameters.containsKey(name)) { - return null; - } - return parameters.get(name).get(0); + public void setSession(HttpSession session) { + this.session = session; } + // ---------------------------------------------------------------------- + // Stub methods + // ---------------------------------------------------------------------- + + public HttpSession getSession() { + return getSession(true); + } + + public HttpSession getSession(boolean create) { + if (create && (session == null)) { + session = new HttpSessionStub(); + } + return session; + } + + public String getContextPath() { + String path = requestUrl.getPath(); + if (path.isEmpty()) { + return ""; + } + int secondSlash = path.indexOf("/", 1); + if (secondSlash == -1) { + return ""; + } else { + return path.substring(0, secondSlash); + } + } + + public String getMethod() { + return httpMethodType; + } + + public String getRemoteAddr() { + return remoteAddr; + } + + @SuppressWarnings("rawtypes") + public Enumeration getParameterNames() { + return Collections.enumeration(parameters.keySet()); + } + + @SuppressWarnings("rawtypes") public Map getParameterMap() { Map map = new HashMap(); for (String key : parameters.keySet()) { @@ -75,20 +128,50 @@ public class HttpServletRequestStub implements HttpServletRequest { return map; } - // ---------------------------------------------------------------------- - // Un-implemented methods - // ---------------------------------------------------------------------- + public String getParameter(String name) { + if (!parameters.containsKey(name)) { + return null; + } + return parameters.get(name).get(0); + } + + public String[] getParameterValues(String name) { + if (!parameters.containsKey(name)) { + return null; + } + List list = parameters.get(name); + return list.toArray(new String[list.size()]); + } + + public Object getAttribute(String name) { + return attributes.get(name); + } + + @SuppressWarnings("rawtypes") + public Enumeration getAttributeNames() { + return Collections.enumeration(attributes.keySet()); + } + + public void removeAttribute(String name) { + attributes.remove(name); + } + + public void setAttribute(String name, Object value) { + if (value == null) { + removeAttribute(name); + } + attributes.put(name, value); + } + + // ---------------------------------------------------------------------- + // Un-implemented methods + // ---------------------------------------------------------------------- public String getAuthType() { throw new RuntimeException( "HttpServletRequestStub.getAuthType() not implemented."); } - public String getContextPath() { - throw new RuntimeException( - "HttpServletRequestStub.getContextPath() not implemented."); - } - public Cookie[] getCookies() { throw new RuntimeException( "HttpServletRequestStub.getCookies() not implemented."); @@ -104,11 +187,13 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.getHeader() not implemented."); } + @SuppressWarnings("rawtypes") public Enumeration getHeaderNames() { throw new RuntimeException( "HttpServletRequestStub.getHeaderNames() not implemented."); } + @SuppressWarnings("rawtypes") public Enumeration getHeaders(String arg0) { throw new RuntimeException( "HttpServletRequestStub.getHeaders() not implemented."); @@ -119,11 +204,6 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.getIntHeader() not implemented."); } - public String getMethod() { - throw new RuntimeException( - "HttpServletRequestStub.getMethod() not implemented."); - } - public String getPathInfo() { throw new RuntimeException( "HttpServletRequestStub.getPathInfo() not implemented."); @@ -164,16 +244,6 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.getServletPath() not implemented."); } - public HttpSession getSession() { - throw new RuntimeException( - "HttpServletRequestStub.getSession() not implemented."); - } - - public HttpSession getSession(boolean arg0) { - throw new RuntimeException( - "HttpServletRequestStub.getSession() not implemented."); - } - public Principal getUserPrincipal() { throw new RuntimeException( "HttpServletRequestStub.getUserPrincipal() not implemented."); @@ -204,16 +274,6 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.isUserInRole() not implemented."); } - public Object getAttribute(String arg0) { - throw new RuntimeException( - "HttpServletRequestStub.getAttribute() not implemented."); - } - - public Enumeration getAttributeNames() { - throw new RuntimeException( - "HttpServletRequestStub.getAttributeNames() not implemented."); - } - public String getCharacterEncoding() { throw new RuntimeException( "HttpServletRequestStub.getCharacterEncoding() not implemented."); @@ -254,21 +314,12 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.getLocale() not implemented."); } + @SuppressWarnings("rawtypes") public Enumeration getLocales() { throw new RuntimeException( "HttpServletRequestStub.getLocales() not implemented."); } - public Enumeration getParameterNames() { - throw new RuntimeException( - "HttpServletRequestStub.getParameterNames() not implemented."); - } - - public String[] getParameterValues(String arg0) { - throw new RuntimeException( - "HttpServletRequestStub.getParameterValues() not implemented."); - } - public String getProtocol() { throw new RuntimeException( "HttpServletRequestStub.getProtocol() not implemented."); @@ -284,11 +335,6 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.getRealPath() not implemented."); } - public String getRemoteAddr() { - throw new RuntimeException( - "HttpServletRequestStub.getRemoteAddr() not implemented."); - } - public String getRemoteHost() { throw new RuntimeException( "HttpServletRequestStub.getRemoteHost() not implemented."); @@ -324,16 +370,6 @@ public class HttpServletRequestStub implements HttpServletRequest { "HttpServletRequestStub.isSecure() not implemented."); } - public void removeAttribute(String arg0) { - throw new RuntimeException( - "HttpServletRequestStub.removeAttribute() not implemented."); - } - - public void setAttribute(String arg0, Object arg1) { - throw new RuntimeException( - "HttpServletRequestStub.setAttribute() not implemented."); - } - public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException { throw new RuntimeException( diff --git a/webapp/test/stubs/javax/servlet/http/HttpServletResponseStub.java b/webapp/test/stubs/javax/servlet/http/HttpServletResponseStub.java new file mode 100644 index 000000000..53b24c5ca --- /dev/null +++ b/webapp/test/stubs/javax/servlet/http/HttpServletResponseStub.java @@ -0,0 +1,226 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package stubs.javax.servlet.http; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Locale; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; + +/** + * A simple stub for HttpServletResponse + */ +public class HttpServletResponseStub implements HttpServletResponse { + // ---------------------------------------------------------------------- + // Stub infrastructure + // ---------------------------------------------------------------------- + + private String redirectLocation; + + public String getRedirectLocation() { + return redirectLocation; + } + + // ---------------------------------------------------------------------- + // Stub methods + // ---------------------------------------------------------------------- + + @Override + public void sendRedirect(String location) throws IOException { + this.redirectLocation = location; + } + + // ---------------------------------------------------------------------- + // Un-implemented methods + // ---------------------------------------------------------------------- + + @Override + public void flushBuffer() throws IOException { + throw new RuntimeException( + "HttpServletResponseStub.flushBuffer() not implemented."); + } + + @Override + public int getBufferSize() { + throw new RuntimeException( + "HttpServletResponseStub.getBufferSize() not implemented."); + } + + @Override + public String getCharacterEncoding() { + throw new RuntimeException( + "HttpServletResponseStub.getCharacterEncoding() not implemented."); + } + + @Override + public String getContentType() { + throw new RuntimeException( + "HttpServletResponseStub.getContentType() not implemented."); + } + + @Override + public Locale getLocale() { + throw new RuntimeException( + "HttpServletResponseStub.getLocale() not implemented."); + } + + @Override + public ServletOutputStream getOutputStream() throws IOException { + throw new RuntimeException( + "HttpServletResponseStub.getOutputStream() not implemented."); + } + + @Override + public PrintWriter getWriter() throws IOException { + throw new RuntimeException( + "HttpServletResponseStub.getWriter() not implemented."); + } + + @Override + public boolean isCommitted() { + throw new RuntimeException( + "HttpServletResponseStub.isCommitted() not implemented."); + } + + @Override + public void reset() { + throw new RuntimeException( + "HttpServletResponseStub.reset() not implemented."); + } + + @Override + public void resetBuffer() { + throw new RuntimeException( + "HttpServletResponseStub.resetBuffer() not implemented."); + } + + @Override + public void setBufferSize(int arg0) { + throw new RuntimeException( + "HttpServletResponseStub.setBufferSize() not implemented."); + } + + @Override + public void setCharacterEncoding(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.setCharacterEncoding() not implemented."); + } + + @Override + public void setContentLength(int arg0) { + throw new RuntimeException( + "HttpServletResponseStub.setContentLength() not implemented."); + } + + @Override + public void setContentType(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.setContentType() not implemented."); + } + + @Override + public void setLocale(Locale arg0) { + throw new RuntimeException( + "HttpServletResponseStub.setLocale() not implemented."); + } + + @Override + public void addCookie(Cookie arg0) { + throw new RuntimeException( + "HttpServletResponseStub.addCookie() not implemented."); + } + + @Override + public void addDateHeader(String arg0, long arg1) { + throw new RuntimeException( + "HttpServletResponseStub.addDateHeader() not implemented."); + } + + @Override + public void addHeader(String arg0, String arg1) { + throw new RuntimeException( + "HttpServletResponseStub.addHeader() not implemented."); + } + + @Override + public void addIntHeader(String arg0, int arg1) { + throw new RuntimeException( + "HttpServletResponseStub.addIntHeader() not implemented."); + } + + @Override + public boolean containsHeader(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.containsHeader() not implemented."); + } + + @Override + public String encodeRedirectURL(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.encodeRedirectURL() not implemented."); + } + + @Override + public String encodeRedirectUrl(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.encodeRedirectUrl() not implemented."); + } + + @Override + public String encodeURL(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.encodeURL() not implemented."); + } + + @Override + public String encodeUrl(String arg0) { + throw new RuntimeException( + "HttpServletResponseStub.encodeUrl() not implemented."); + } + + @Override + public void sendError(int arg0) throws IOException { + throw new RuntimeException( + "HttpServletResponseStub.sendError() not implemented."); + } + + @Override + public void sendError(int arg0, String arg1) throws IOException { + throw new RuntimeException( + "HttpServletResponseStub.sendError() not implemented."); + } + + @Override + public void setDateHeader(String arg0, long arg1) { + throw new RuntimeException( + "HttpServletResponseStub.setDateHeader() not implemented."); + } + + @Override + public void setHeader(String arg0, String arg1) { + throw new RuntimeException( + "HttpServletResponseStub.setHeader() not implemented."); + } + + @Override + public void setIntHeader(String arg0, int arg1) { + throw new RuntimeException( + "HttpServletResponseStub.setIntHeader() not implemented."); + } + + @Override + public void setStatus(int arg0) { + throw new RuntimeException( + "HttpServletResponseStub.setStatus() not implemented."); + } + + @Override + public void setStatus(int arg0, String arg1) { + throw new RuntimeException( + "HttpServletResponseStub.setStatus() not implemented."); + } + +} diff --git a/webapp/test/stubs/javax/servlet/http/HttpSessionStub.java b/webapp/test/stubs/javax/servlet/http/HttpSessionStub.java index fce158631..cf5478b69 100644 --- a/webapp/test/stubs/javax/servlet/http/HttpSessionStub.java +++ b/webapp/test/stubs/javax/servlet/http/HttpSessionStub.java @@ -3,20 +3,36 @@ package stubs.javax.servlet.http; import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + /** * A simple stand-in for the HttpSession, for use in unit tests. */ @SuppressWarnings("deprecation") public class HttpSessionStub implements HttpSession { + private static final Log log = LogFactory.getLog(HttpSessionStub.class); + // ---------------------------------------------------------------------- // Stub infrastructure // ---------------------------------------------------------------------- + private String id = "arbitraryId"; private ServletContext context; + private final Map attributes = new HashMap(); + + @SuppressWarnings("unused") + private int maxInactiveInterval; + + public void setId(String id) { + this.id = id; + } public void setServletContext(ServletContext context) { this.context = context; @@ -26,21 +42,52 @@ public class HttpSessionStub implements HttpSession { // Stub methods // ---------------------------------------------------------------------- + @Override + public String getId() { + return this.id; + } + @Override public ServletContext getServletContext() { return this.context; } + @Override + public void setAttribute(String name, Object value) { + if (name == null) { + throw new NullPointerException("name may not be null."); + } + if (value == null) { + removeAttribute(name); + } + attributes.put(name, value); + log.debug("setAttribute: " + name + "=" + value); + } + + @Override + public void removeAttribute(String name) { + attributes.remove(name); + log.debug("removeAttribute: " + name); + } + + @Override + public Object getAttribute(String name) { + return attributes.get(name); + } + + /** + * So far, we don't do anything with this, or even confirm that it has been + * set. We just don't throw an exception if someone wants to set it. + */ + @Override + public void setMaxInactiveInterval(int seconds) { + this.maxInactiveInterval = seconds; + } + // ---------------------------------------------------------------------- // Un-implemented methods // ---------------------------------------------------------------------- - @Override - public Object getAttribute(String arg0) { - throw new RuntimeException( - "HttpSessionStub.getAttribute() not implemented."); - } - @Override @SuppressWarnings("rawtypes") public Enumeration getAttributeNames() { @@ -54,11 +101,6 @@ public class HttpSessionStub implements HttpSession { "HttpSessionStub.getCreationTime() not implemented."); } - @Override - public String getId() { - throw new RuntimeException("HttpSessionStub.getId() not implemented."); - } - @Override public long getLastAccessedTime() { throw new RuntimeException( @@ -106,27 +148,9 @@ public class HttpSessionStub implements HttpSession { "HttpSessionStub.putValue() not implemented."); } - @Override - public void removeAttribute(String arg0) { - throw new RuntimeException( - "HttpSessionStub.removeAttribute() not implemented."); - } - public void removeValue(String arg0) { throw new RuntimeException( "HttpSessionStub.removeValue() not implemented."); } - @Override - public void setAttribute(String arg0, Object arg1) { - throw new RuntimeException( - "HttpSessionStub.setAttribute() not implemented."); - } - - @Override - public void setMaxInactiveInterval(int arg0) { - throw new RuntimeException( - "HttpSessionStub.setMaxInactiveInterval() not implemented."); - } - }