    1   /*
    2    * Copyright 2004-2009 the Seasar Foundation and the Others.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
    13   * either express or implied. See the License for the specific language
    14   * governing permissions and limitations under the License.
    15   */
    16  package org.seasar.cubby.filter;
    17
    18  import static org.easymock.EasyMock.createMock;
    19  import static org.easymock.EasyMock.expect;
    20  import static org.easymock.EasyMock.isA;
    21  import static org.easymock.EasyMock.replay;
    22  import static org.easymock.EasyMock.verify;
    23
    24  import javax.servlet.FilterChain;
    25  import javax.servlet.FilterConfig;
    26  import javax.servlet.ServletRequest;
    27  import javax.servlet.ServletResponse;
    28  import javax.servlet.http.HttpServletRequest;
    29  import javax.servlet.http.HttpServletResponse;
    30
    31  import org.junit.Test;
    32
    33  public class SendErrorFilterTest {
    34
    35  	@Test
    36  	public void doFilter1() throws Exception {
    37  		FilterConfig config = createMock(FilterConfig.class);
    38  		expect(config.getInitParameter("statusCode")).andReturn(null);
    39  		expect(config.getInitParameter("ignorePathPattern")).andReturn(null);
    40  		HttpServletRequest request = createMock(HttpServletRequest.class);
    41  		expect(request.getServletPath()).andReturn("/bar/").anyTimes();
    42  		HttpServletResponse response = createMock(HttpServletResponse.class);
    43  		response.sendError(403);
    44  		FilterChain chain = createMock(FilterChain.class);
    45  		replay(config, request, response, chain);
    46
    47  		SendErrorFilter filter = new SendErrorFilter();
    48  		filter.init(config);
    49  		filter.doFilter(request, response, chain);
    50  		filter.destroy();
    51
    52  		verify(config, request, response, chain);
    53  	}
    54
    55  	@Test
    56  	public void doFilter2() throws Exception {
    57  		FilterConfig config = createMock(FilterConfig.class);
    58  		expect(config.getInitParameter("statusCode")).andReturn("404");
    59  		expect(config.getInitParameter("ignorePathPattern")).andReturn(null);
    60  		HttpServletRequest request = createMock(HttpServletRequest.class);
    61  		expect(request.getServletPath()).andReturn("/bar/").anyTimes();
    62  		HttpServletResponse response = createMock(HttpServletResponse.class);
    63  		response.sendError(404);
    64  		FilterChain chain = createMock(FilterChain.class);
    65  		replay(config, request, response, chain);
    66
    67  		SendErrorFilter filter = new SendErrorFilter();
    68  		filter.init(config);
    69  		filter.doFilter(request, response, chain);
    70  		filter.destroy();
    71
    72  		verify(config, request, response, chain);
    73  	}
    74
    75  	@Test
    76  	public void doFilter3() throws Exception {
    77  		FilterConfig config = createMock(FilterConfig.class);
    78  		expect(config.getInitParameter("statusCode")).andReturn("404");
    79  		expect(config.getInitParameter("ignorePathPattern")).andReturn(
    80  				".*\\.jsp,.*\\.html");
    81  		HttpServletRequest request = createMock(HttpServletRequest.class);
    82  		expect(request.getServletPath()).andReturn("/foo/bar.html").anyTimes();
    83  		HttpServletResponse response = createMock(HttpServletResponse.class);
    84  		FilterChain chain = createMock(FilterChain.class);
    85  		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    86  		replay(config, request, response, chain);
    87
    88  		SendErrorFilter filter = new SendErrorFilter();
    89  		filter.init(config);
    90  		filter.doFilter(request, response, chain);
    91  		filter.destroy();
    92
    93  		verify(config, request, response, chain);
    94  	}
    95
    96  	@Test
    97  	public void doFilter4() throws Exception {
    98  		FilterConfig config = createMock(FilterConfig.class);
    99  		expect(config.getInitParameter("statusCode")).andReturn("404");
    100 		expect(config.getInitParameter("ignorePathPattern")).andReturn(
    101 				".*\\.jsp,.*\\.html");
    102 		HttpServletRequest request = createMock(HttpServletRequest.class);
    103 		expect(request.getServletPath()).andReturn("/foo/bar.html").anyTimes();
    104 		HttpServletResponse response = createMock(HttpServletResponse.class);
    105 		FilterChain chain = createMock(FilterChain.class);
    106 		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    107 		replay(config, request, response, chain);
    108
    109 		SendErrorFilter filter = new SendErrorFilter();
    110 		filter.init(config);
    111 		filter.doFilter(request, response, chain);
    112 		filter.destroy();
    113
    114 		verify(config, request, response, chain);
    115 	}
    116
    117 	@Test
    118 	public void doFilter5() throws Exception {
    119 		FilterConfig config = createMock(FilterConfig.class);
    120 		expect(config.getInitParameter("statusCode")).andReturn("404");
    121 		expect(config.getInitParameter("ignorePathPattern")).andReturn(
    122 				".*\\.jsp,.*\\.html");
    123 		HttpServletRequest request = createMock(HttpServletRequest.class);
    124 		expect(request.getServletPath()).andReturn("/bar/").anyTimes();
    125 		HttpServletResponse response = createMock(HttpServletResponse.class);
    126 		response.sendError(404);
    127 		FilterChain chain = createMock(FilterChain.class);
    128
    129 		replay(config, request, response, chain);
    130
    131 		SendErrorFilter filter = new SendErrorFilter();
    132 		filter.init(config);
    133 		filter.doFilter(request, response, chain);
    134 		filter.destroy();
    135
    136 		verify(config, request, response, chain);
    137 	}
    138
    139 }