Thursday, July 9, 2009

MockingSpring - Testing ServletFiltes

mocking spring,

well it's hot in davis and I have no AC, so what's better than to run into the nice and cold office and playing with some junit tests.

Basically how to junit test servlet filters. Like always somebody smart wrote some great tools for me to use.

An example of a simple servlet filter which forwads all access to a cutom report factory

package edu.ucdavis.genomics.metabolomics.binbase.filter;

import edu.ucdavis.genomics.metabolomics.util.status.Report;
import edu.ucdavis.genomics.metabolomics.util.status.ReportFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

* forwards all requests to the reporter which deals with it

* User: wohlgemuth
* Date: Jun 28, 2009
* Time: 3:33:00 PM
public class IPLogger implements Filter {

* forwards everything to the server
private Report report;

* logging nstances
private Logger logger = Logger.getLogger(getClass().getName());

* initialize the connection to the report method
* @param filterConfig
* @throws ServletException
public void init(FilterConfig filterConfig) throws ServletException {"initialize filter");

//access the factory we need for the reporter
String factory = filterConfig.getInitParameter("factory");

if (factory == null) {"no factory provided, so we use the default factory");

report = ReportFactory.newInstance().create(IPLogger.class.getSimpleName());
} else {"using factory: " + factory);
report = ReportFactory.newInstance(factory).create(IPLogger.class.getSimpleName());

* does the actual filtering
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
String host = servletRequest.getRemoteHost();
String address = servletRequest.getRemoteAddr();
int port = servletRequest.getRemotePort();

String uri = "unknown";
if (servletRequest instanceof HttpServletRequest) {
HttpServletRequest req = (HttpServletRequest) servletRequest;

uri = req.getRequestURI();

//send it out so that we can deal with these event in a different class or so IPRequest(host, uri, address, port), FilterReports.FITLER_URI_ACCESS, FilterReports.FILTER_REQUEST);

public void destroy() {

and the junit test to test it, without a web server or so.

public class IPLoggerTest {

private IPLogger filter;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private MockFilterChain chain;

public void setUp() throws Exception {
filter = new IPLogger();
MockFilterConfig config = new MockFilterConfig();
config.addInitParameter("factory", TestFactory.class.getName());
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
chain = new MockFilterChain();

public void tearDown() throws Exception {

public void testDoFilter() throws IOException, ServletException {
filter.doFilter(request, response, chain);


and our little factory which does the acutal testing of the received object

public class TestFactory extends ReportFactory{

private Logger logger = Logger.getLogger(getClass());
* checks if all the result's fit the exspectations
* @param properties
* @param s
* @return
public Report create(Properties properties, String s) {
return new Report() {
public void report(String s, Serializable serializable, ReportEvent reportEvent, ReportType reportType, String s1, Date date, Exception e) {"received: " + serializable);

assertTrue(serializable instanceof IPRequest);
IPRequest request = (IPRequest) serializable;

assertTrue(request.getPort() == 80);

public String getOwner() {

return IPLogger.class.getSimpleName();

No comments:

Post a Comment