MockServletRequest.java :  » MVC » jurfa » com » jurfa » test » Java Open Source

Java Open Source » MVC » jurfa 
jurfa » com » jurfa » test » MockServletRequest.java
package com.jurfa.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jurfa.common.JurfaConstants;

public class MockServletRequest implements HttpServletRequest 
{
  public static final String METHOD_GET = "GET";
  public static final String METHOD_POST = "POST";
  
  private HttpSession session = new MockSession();  
  private Map<String, Object> headerMap = new HashMap<String, Object>();
  private Map<String, Object> attributeMap = new HashMap<String, Object>();
  private RequestDispatcher requestDispatcher;
  
  protected String encoding = "utf-8";
  protected String contentType = "text/html";
  protected Locale locale = Locale.getDefault();
  
  private String contextPath = "/jurfa_test";
  private String servletPath = null;
  private String queryString = "";
  private String method = METHOD_GET;
  private String protocol = "HTTP/1.1"; 
  private String scheme = "http";
  
  private String localAddr = "127.0.0.1";
  private String localHost = "localhost";
  private int localPort = 8080;
  private String remoteAddr = "10.0.0.1";
  private String remoteHost = "remotehost";
  private int remotePort = 8080;
  private String serverName = "jurfa-server";
  private int serverPort = 8080;

  public MockServletRequest(String method, String path) {
    this.method = method;
    this.servletPath = path;
  }
    
  // helper methods  
  @SuppressWarnings("unchecked")
  public Map<String, Object> getModel() {
    return (Map<String, Object>) getAttribute(JurfaConstants.PARAM_MODEL);
  }
  
  public String getLocalHost() {
    return localHost;
  }

  public void setLocalHost(String localHost) {
    this.localHost = localHost;
  }

  public void setContextPath(String contextPath) {
    this.contextPath = contextPath;
  }

  public void setServletPath(String servletPath) {
    this.servletPath = servletPath;
  }

  public void setQueryString(String queryString) {
    this.queryString = queryString;
    // TODO inject parameters
  }

  public void setMethod(String method) {
    this.method = method;
  }

  public void setProtocol(String protocol) {
    this.protocol = protocol;
  }

  public void setScheme(String scheme) {
    this.scheme = scheme;
  }

  public void setLocalAddr(String localAddr) {
    this.localAddr = localAddr;
  }

  public void setLocalPort(int localPort) {
    this.localPort = localPort;
  }

  public void setRemoteAddr(String remoteAddr) {
    this.remoteAddr = remoteAddr;
  }

  public void setRemoteHost(String remoteHost) {
    this.remoteHost = remoteHost;
  }

  public void setRemotePort(int remotePort) {
    this.remotePort = remotePort;
  }

  public void setServerName(String serverName) {
    this.serverName = serverName;
  }

  public void setServerPort(int serverPort) {
    this.serverPort = serverPort;
  }
  
  // overrided methods
  public String getContextPath() {
    return contextPath;
  }

  public String getPathInfo() {
    return null;
  }

  public String getPathTranslated() {
    return null;
  }

  public String getRequestURI() {
    return contextPath + servletPath;
  }

  public StringBuffer getRequestURL() {
    StringBuffer buffer = new StringBuffer();
    buffer.append(scheme);
    buffer.append("//");
    buffer.append(serverName);
    buffer.append(":");
    buffer.append(serverPort);
    buffer.append(contextPath);
    buffer.append(servletPath);
    return buffer;
  }

  public String getRealPath(String path) {
    return getClass().getResource(".").getPath();
  }  
  
  public String getServletPath() {
    return servletPath;
  }
  
  public ServletInputStream getInputStream() throws IOException {
    return null;
  }

  public RequestDispatcher getRequestDispatcher(String path) {
    if (requestDispatcher == null) {
      synchronized (this) {
        if (requestDispatcher == null) {
          requestDispatcher = new MockRequestDispatcher();
        }
      }
    }
    return requestDispatcher;
  }

  public BufferedReader getReader() throws IOException {
    return null;
  }

  public String getAuthType() {    
    return null;
  }
  
  public Cookie[] getCookies() {
    return MockCookieHandler.getInstance().getCookies();
  }

  public long getDateHeader(String name) {
    return (Long) headerMap.get(name);
  }

  public String getHeader(String name) {
    return (String) headerMap.get(name);
  }

  @SuppressWarnings("unchecked")
  public Enumeration getHeaderNames() {
    return Collections.enumeration(headerMap.keySet());
  }

  @SuppressWarnings("unchecked")
  public Enumeration getHeaders(String name) {
    return Collections.enumeration((Collection) headerMap.get(name));
  }

  public int getIntHeader(String name) {
    return (Integer) headerMap.get(name);
  }

  public String getMethod() {
    return method;
  }

  public String getQueryString() {
    return queryString;
  }

  public String getRemoteUser() {
    return null;
  }

  public String getRequestedSessionId() {
    return session.getId();
  }

  public HttpSession getSession() {
    return session;
  }

  public HttpSession getSession(boolean create) {
    return session;
  }

  public Principal getUserPrincipal() {
    return null;
  }

  public boolean isRequestedSessionIdFromCookie() {
    return false;
  }

  public boolean isRequestedSessionIdFromURL() {
    return false;
  }

  public boolean isRequestedSessionIdFromUrl() {
    return false;
  }

  public boolean isRequestedSessionIdValid() {
    return false;
  }

  public boolean isUserInRole(String role) {
    return false;
  }

  public Object getAttribute(String name) {
    return attributeMap.get(name);
  }

  @SuppressWarnings("unchecked")
  public Enumeration getAttributeNames() {
    return Collections.enumeration(attributeMap.keySet());
  }

  public String getCharacterEncoding() {
    return encoding;
  }

  public int getContentLength() {
    return (Integer) headerMap.get("Content-Lenght");
  }

  public String getContentType() {
    return contentType;
  }

  public String getLocalAddr() {
    return localAddr;
  }

  public String getLocalName() {
    return localHost;
  }

  public int getLocalPort() {
    return localPort;
  }

  public Locale getLocale() {
    return locale;
  }

  @SuppressWarnings("unchecked")
  public Enumeration getLocales() {
    return null;
  }

  public String getParameter(String name) {  
    if (attributeMap.get(name) == null) {
      return null;
    }
    return attributeMap.get(name).toString();
  }

  @SuppressWarnings("unchecked")
  public Map getParameterMap() {
    return attributeMap;
  }

  @SuppressWarnings("unchecked")
  public Enumeration getParameterNames() {
    return Collections.enumeration(attributeMap.keySet());
  }

  public String[] getParameterValues(String name) {
    return attributeMap.values().toArray(new String[attributeMap.size()]);
  }

  public String getProtocol() {
    return protocol;
  }

  public String getRemoteAddr() {
    return remoteAddr;
  }

  public String getRemoteHost() {
    return remoteHost;
  }

  public int getRemotePort() {
    return remotePort;
  }

  public String getScheme() {
    return scheme;
  }

  public String getServerName() {
    return serverName;
  }

  public int getServerPort() {
    return serverPort;
  }

  public boolean isSecure() {
    return false;
  }

  public void removeAttribute(String name) {
    attributeMap.remove(name);
  }

  public void setAttribute(String name, Object o) {
    attributeMap.put(name, o);
  }

  public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
    encoding = env;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.