home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (MultiModuleDispatchIntegrationTest.java)

This example Java source code file (MultiModuleDispatchIntegrationTest.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

filterchain, filterpipeline, http, httpservletrequest, injector, ioexception, multimoduledispatchintegrationtest, override, request, response, servlet, servletexception, servletmodule, servletresponse, singleton, testcase, testfilter

The MultiModuleDispatchIntegrationTest.java Java example source code

package com.google.inject.servlet;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Singleton;

import junit.framework.TestCase;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

 * This tests that filter stage of the pipeline dispatches
 * correctly to guice-managed filters with multiple modules.
 * WARNING(dhanji): Non-parallelizable test =(
 * @author dhanji@gmail.com (Dhanji R. Prasanna)
public class MultiModuleDispatchIntegrationTest extends TestCase {
    private static int inits, doFilters, destroys;

  public final void setUp() {
    inits = 0;
    doFilters = 0;
    destroys = 0;


  public final void testDispatchRequestToManagedPipeline() throws ServletException, IOException {
    final Injector injector = Guice.createInjector(new ServletModule() {

      protected void configureServlets() {

        // These filters should never fire

    }, new ServletModule() {

      protected void configureServlets() {

        // These filters should never fire


    final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);

    //create ourselves a mock request with test URI
    HttpServletRequest requestMock = createMock(HttpServletRequest.class);


    //dispatch request
    pipeline.dispatch(requestMock, null, createMock(FilterChain.class));


    assertTrue("lifecycle states did not"
        + " fire correct number of times-- inits: " + inits + "; dos: " + doFilters
        + "; destroys: " + destroys,
        inits == 1 && doFilters == 3 && destroys == 1);

  public static class TestFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain filterChain) throws IOException, ServletException {
      filterChain.doFilter(servletRequest, servletResponse);

    public void destroy() {

Other Java examples (source code examples)

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

my book on functional programming


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.