alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ServletTestUtils.java)

This example Java source code file (ServletTestUtils.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

class, fakehttpsessionhandler, filterchain, http, httpservletrequest, httpservletresponse, httpsession, map, method, object, override, reflection, request, response, servlet, servlettestutils, string, throwinginvocationhandler, unsupportedoperationexception, util

The ServletTestUtils.java Java example source code

// Copyright 2011 Google Inc. All Rights Reserved.

package com.google.inject.servlet;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Utilities for servlet tests.
 * 
 * @author sameb@google.com (Sam Berlin)
 */
public class ServletTestUtils {
  
  private ServletTestUtils() {}

  private static class ThrowingInvocationHandler implements InvocationHandler {
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      throw new UnsupportedOperationException("No methods are supported on this object");
    }
  }
  
  /**
   * Returns a FilterChain that does nothing.
   */
  public static FilterChain newNoOpFilterChain() {
    return new FilterChain() {
      public void doFilter(ServletRequest request, ServletResponse response) {
      }
    };
  }
  
  /**
   * Returns a fake, HttpServletRequest which stores attributes in a HashMap.
   */
  public static HttpServletRequest newFakeHttpServletRequest() {
    HttpServletRequest delegate = (HttpServletRequest) Proxy.newProxyInstance(
        HttpServletRequest.class.getClassLoader(),
        new Class[] { HttpServletRequest.class }, new ThrowingInvocationHandler());
    
    return new HttpServletRequestWrapper(delegate) {
      final Map<String, Object> attributes = Maps.newHashMap(); 
      final HttpSession session = newFakeHttpSession();

      @Override public String getMethod() {
        return "GET";
      }

      @Override public Object getAttribute(String name) {
        return attributes.get(name);
      }
      
      @Override public void setAttribute(String name, Object value) {
        attributes.put(name, value);
      }
      
      @Override public Map getParameterMap() {
        return ImmutableMap.of();
      }
      
      @Override public String getRequestURI() {
        return "/";
      }
      
      @Override public String getContextPath() {
        return "";
      }
      
      @Override public HttpSession getSession() {
        return session;
      }
    };
  }
  
  /**
   * Returns a fake, HttpServletResponse which throws an exception if any of its
   * methods are called.
   */
  public static HttpServletResponse newFakeHttpServletResponse() {
    return (HttpServletResponse) Proxy.newProxyInstance(
        HttpServletResponse.class.getClassLoader(),
        new Class[] { HttpServletResponse.class }, new ThrowingInvocationHandler());
  }  
  
  private static class FakeHttpSessionHandler implements InvocationHandler, Serializable {
    final Map<String, Object> attributes = Maps.newHashMap();

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      String name = method.getName();
      if ("setAttribute".equals(name)) {
        attributes.put((String) args[0], args[1]);
        return null;
      } else if ("getAttribute".equals(name)) {
        return attributes.get(args[0]);
      } else {
        throw new UnsupportedOperationException();
      }
    }
  }

  /**
   * Returns a fake, serializable HttpSession which stores attributes in a HashMap.
   */
  public static HttpSession newFakeHttpSession() {
    return (HttpSession) Proxy.newProxyInstance(HttpSession.class.getClassLoader(),
        new Class[] { HttpSession.class }, new FakeHttpSessionHandler());
  }

}

Other Java examples (source code examples)

Here is a short list of links related to this Java ServletTestUtils.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.