FileDocCategorySizeDatePackage
TestLoginFilter.javaAPI DocExample3619Thu Dec 15 22:18:04 GMT 2005com.oreilly.jent.people.servlet

TestLoginFilter.java

package com.oreilly.jent.people.servlet;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import junit.framework.Test;

import org.apache.cactus.FilterTestCase;
import org.apache.cactus.WebResponse;

public class TestLoginFilter extends FilterTestCase {
    /** An instance of the filter under test */
    private LoginFilter mFilter = null;

    /** Default constructor */
    public TestLoginFilter() {
        super();
    }

    /** Constructor with test name */
    public TestLoginFilter(String arg0) {
        super(arg0);
    }

    /** Constructor used to wrap a test with a Cactus test */
    public TestLoginFilter(String arg0, Test arg1) {
        super(arg0, arg1);
    }
    
    public void setUp() {
        mFilter = new LoginFilter();
        try {
            mFilter.init(this.config);
        }
        catch (ServletException se) {
            fail("Unexpected servlet exception: " + se.getMessage());
        }
    }
    
    public void tearDown() {
        mFilter.destroy();
        mFilter = null;
    }
    
    /** Ensure that the filter performs a redirect when no authentication
     *  credentials are present in the user session, and no login username or
     *  password are provided as request parameters. */
    public void testLoginRedirect() {
        // Ensure that the user session is clear of our authentication
        // credentials
        HttpSession session = this.request.getSession();
        if (session != null) {
            session.removeAttribute(LoginFilter.AUTHN_ID_VAR);
        }
        try {
            // Invoke the filter
            mFilter.doFilter(this.request, this.response, this.filterChain);
        }
        catch (IOException ioe) {
            fail("Unexpected I/O exception while invoking filter: "
                 + ioe.getMessage());
        }
        catch (ServletException se) {
            fail("Unexpected servlet exception while invoking filter: "
                 + se.getMessage());
        }
    }
    
    public void endLoginRedirect(WebResponse response) {
        // The response should contain an HTTP "MOVED TEMPORARILY" 
        // status with a redirect
        assertEquals("Unexpected status code seen in response",
                     response.getStatusCode(),
                     HttpServletResponse.SC_MOVED_TEMPORARILY);
    }
}