    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.anyObject;
    19  import static org.easymock.EasyMock.createMock;
    20  import static org.easymock.EasyMock.expect;
    21  import static org.easymock.EasyMock.expectLastCall;
    22  import static org.easymock.EasyMock.getCurrentArguments;
    23  import static org.easymock.EasyMock.isA;
    24  import static org.easymock.EasyMock.replay;
    25  import static org.easymock.EasyMock.verify;
    26  import static org.junit.Assert.assertEquals;
    27
    28  import java.io.IOException;
    29  import java.util.HashMap;
    30  import java.util.Map;
    31
    32  import javax.servlet.FilterChain;
    33  import javax.servlet.FilterConfig;
    34  import javax.servlet.ServletException;
    35  import javax.servlet.ServletRequest;
    36  import javax.servlet.ServletResponse;
    37  import javax.servlet.http.HttpServletRequest;
    38  import javax.servlet.http.HttpServletResponse;
    39
    40  import org.easymock.IAnswer;
    41  import org.junit.Test;
    42
    43  public class EncodingFilterTest {
    44
    45  	@Test
    46  	public void setEncoding1() throws ServletException, IOException {
    47  		FilterConfig config = createMock(FilterConfig.class);
    48  		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    49  		expect(config.getInitParameter("forceEncoding")).andReturn(null);
    50  		expect(config.getInitParameter("URIEncoding")).andReturn(null);
    51  		expect(config.getInitParameter("URIBytesEncoding")).andReturn(null);
    52
    53  		HttpServletRequest request = createMockHttpServletRequest();
    54  		expect(request.getCharacterEncoding()).andReturn(null);
    55  		request.setCharacterEncoding("UTF-8");
    56
    57  		HttpServletResponse response = createMock(HttpServletResponse.class);
    58
    59  		FilterChain chain = createMock(FilterChain.class);
    60  		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    61
    62  		replay(config, request, response, chain);
    63
    64  		EncodingFilter filter = new EncodingFilter();
    65  		filter.init(config);
    66  		filter.doFilter(request, response, chain);
    67  		filter.destroy();
    68
    69  		verify(config, request, response, chain);
    70  	}
    71
    72  	@Test
    73  	public void setEncoding2() throws ServletException, IOException {
    74  		FilterConfig config = createMock(FilterConfig.class);
    75  		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    76  		expect(config.getInitParameter("forceEncoding")).andReturn(null);
    77  		expect(config.getInitParameter("URIEncoding")).andReturn(null);
    78  		expect(config.getInitParameter("URIBytesEncoding")).andReturn(null);
    79
    80  		HttpServletRequest request = createMockHttpServletRequest();
    81  		expect(request.getCharacterEncoding()).andReturn("Shift_JIS");
    82
    83  		HttpServletResponse response = createMock(HttpServletResponse.class);
    84
    85  		FilterChain chain = createMock(FilterChain.class);
    86  		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    87
    88  		replay(config, request, response, chain);
    89
    90  		EncodingFilter filter = new EncodingFilter();
    91  		filter.init(config);
    92  		filter.doFilter(request, response, chain);
    93  		filter.destroy();
    94
    95  		verify(config, request, response, chain);
    96  	}
    97
    98  	@Test
    99  	public void setForceEncoding1() throws ServletException, IOException {
    100 		FilterConfig config = createMock(FilterConfig.class);
    101 		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    102 		expect(config.getInitParameter("forceEncoding")).andReturn("true");
    103 		expect(config.getInitParameter("URIEncoding")).andReturn(null);
    104 		expect(config.getInitParameter("URIBytesEncoding")).andReturn(null);
    105
    106 		HttpServletRequest request = createMockHttpServletRequest();
    107 		expect(request.getCharacterEncoding()).andReturn("Shift_JIS");
    108 		request.setCharacterEncoding("UTF-8");
    109
    110 		HttpServletResponse response = createMock(HttpServletResponse.class);
    111
    112 		FilterChain chain = createMock(FilterChain.class);
    113 		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    114
    115 		replay(config, request, response, chain);
    116
    117 		EncodingFilter filter = new EncodingFilter();
    118 		filter.init(config);
    119 		filter.doFilter(request, response, chain);
    120 		filter.destroy();
    121
    122 		verify(config, request, response, chain);
    123 	}
    124
    125 	@Test
    126 	public void setForceEncoding2() throws ServletException, IOException {
    127 		FilterConfig config = createMock(FilterConfig.class);
    128 		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    129 		expect(config.getInitParameter("forceEncoding")).andReturn("true");
    130 		expect(config.getInitParameter("URIEncoding")).andReturn(null);
    131 		expect(config.getInitParameter("URIBytesEncoding")).andReturn(null);
    132
    133 		HttpServletRequest request = createMockHttpServletRequest();
    134 		expect(request.getCharacterEncoding()).andReturn("Shift_JIS");
    135 		request.setCharacterEncoding("UTF-8");
    136
    137 		HttpServletResponse response = createMock(HttpServletResponse.class);
    138
    139 		FilterChain chain = createMock(FilterChain.class);
    140 		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    141
    142 		replay(config, request, response, chain);
    143
    144 		EncodingFilter filter = new EncodingFilter();
    145 		filter.init(config);
    146 		filter.doFilter(request, response, chain);
    147 		filter.destroy();
    148
    149 		verify(config, request, response, chain);
    150 	}
    151
    152 	@Test
    153 	public void setForceEncoding3() throws ServletException, IOException {
    154 		FilterConfig config = createMock(FilterConfig.class);
    155 		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    156 		expect(config.getInitParameter("forceEncoding")).andReturn("false");
    157 		expect(config.getInitParameter("URIEncoding")).andReturn(null);
    158 		expect(config.getInitParameter("URIBytesEncoding")).andReturn(null);
    159
    160 		HttpServletRequest request = createMockHttpServletRequest();
    161 		expect(request.getCharacterEncoding()).andReturn("Shift_JIS");
    162
    163 		HttpServletResponse response = createMock(HttpServletResponse.class);
    164
    165 		FilterChain chain = createMock(FilterChain.class);
    166 		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    167
    168 		replay(config, request, response, chain);
    169
    170 		EncodingFilter filter = new EncodingFilter();
    171 		filter.init(config);
    172 		filter.doFilter(request, response, chain);
    173 		filter.destroy();
    174
    175 		verify(config, request, response, chain);
    176 	}
    177
    178 	@Test
    179 	public void doURIEncoding1() throws ServletException, IOException {
    180 		FilterConfig config = createMock(FilterConfig.class);
    181 		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    182 		expect(config.getInitParameter("forceEncoding")).andReturn("false");
    183 		expect(config.getInitParameter("URIEncoding")).andReturn("UTF-8");
    184 		expect(config.getInitParameter("URIBytesEncoding")).andReturn(null);
    185
    186 		HttpServletRequest request = createMockHttpServletRequest();
    187 		expect(request.getCharacterEncoding()).andReturn(null);
    188 		request.setCharacterEncoding("UTF-8");
    189 		expect(request.getServletPath()).andReturn(
    190 				new String("あいうえおABC123".getBytes("UTF-8"), "ISO-8859-1"));
    191 		expect(request.getPathInfo()).andReturn(
    192 				new String("かきくけこXYZ987".getBytes("UTF-8"), "ISO-8859-1"));
    193
    194 		HttpServletResponse response = createMock(HttpServletResponse.class);
    195
    196 		FilterChain chain = createMock(FilterChain.class);
    197 		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    198 		expectLastCall().andAnswer(new IAnswer<Object>() {
    199
    200 			public Object answer() throws Throwable {
    201 				HttpServletRequest request = (HttpServletRequest) getCurrentArguments()[0];
    202 				assertEquals("あいうえおABC123", request.getServletPath());
    203 				assertEquals("かきくけこXYZ987", request.getPathInfo());
    204 				return null;
    205 			}
    206
    207 		});
    208
    209 		replay(config, request, response, chain);
    210
    211 		EncodingFilter filter = new EncodingFilter();
    212 		filter.init(config);
    213 		filter.doFilter(request, response, chain);
    214 		filter.destroy();
    215
    216 		verify(config, request, response, chain);
    217 	}
    218
    219 	@Test
    220 	public void doURIEncoding2() throws ServletException, IOException {
    221 		FilterConfig config = createMock(FilterConfig.class);
    222 		expect(config.getInitParameter("encoding")).andReturn("UTF-8");
    223 		expect(config.getInitParameter("forceEncoding")).andReturn("false");
    224 		expect(config.getInitParameter("URIEncoding")).andReturn("UTF-8");
    225 		expect(config.getInitParameter("URIBytesEncoding")).andReturn(
    226 				"ISO-8859-1");
    227
    228 		HttpServletRequest request = createMockHttpServletRequest();
    229 		expect(request.getCharacterEncoding()).andReturn(null);
    230 		request.setCharacterEncoding("UTF-8");
    231 		expect(request.getServletPath()).andReturn(
    232 				new String("あいうえおABC123".getBytes("UTF-8"), "ISO-8859-1"));
    233 		expect(request.getPathInfo()).andReturn(
    234 				new String("かきくけこXYZ987".getBytes("UTF-8"), "ISO-8859-1"));
    235
    236 		HttpServletResponse response = createMock(HttpServletResponse.class);
    237
    238 		FilterChain chain = createMock(FilterChain.class);
    239 		chain.doFilter(isA(ServletRequest.class), isA(ServletResponse.class));
    240 		expectLastCall().andAnswer(new IAnswer<Object>() {
    241
    242 			public Object answer() throws Throwable {
    243 				HttpServletRequest request = (HttpServletRequest) getCurrentArguments()[0];
    244 				assertEquals("あいうえおABC123", request.getServletPath());
    245 				assertEquals("かきくけこXYZ987", request.getPathInfo());
    246 				return null;
    247 			}
    248
    249 		});
    250
    251 		replay(config, request, response, chain);
    252
    253 		EncodingFilter filter = new EncodingFilter();
    254 		filter.init(config);
    255 		filter.doFilter(request, response, chain);
    256 		filter.destroy();
    257
    258 		verify(config, request, response, chain);
    259 	}
    260
    261 	private HttpServletRequest createMockHttpServletRequest() {
    262 		final Map<String, Object> requestAttributes = new HashMap<String, Object>();
    263
    264 		HttpServletRequest request = createMock(HttpServletRequest.class);
    265
    266 		expect(request.getAttribute((String) anyObject())).andStubAnswer(
    267 				new IAnswer<Object>() {
    268
    269 					public Object answer() throws Throwable {
    270 						return requestAttributes.get(getCurrentArguments()[0]);
    271 					}
    272
    273 				});
    274
    275 		request.setAttribute((String) anyObject(), anyObject());
    276 		expectLastCall().andStubAnswer(new IAnswer<Object>() {
    277
    278 			public Object answer() throws Throwable {
    279 				requestAttributes.put((String) getCurrentArguments()[0],
    280 						getCurrentArguments()[1]);
    281 				return null;
    282 			}
    283
    284 		});
    285
    286 		request.removeAttribute((String) anyObject());
    287 		expectLastCall().andAnswer(new IAnswer<Object>() {
    288
    289 			public Object answer() throws Throwable {
    290 				requestAttributes.remove((String) getCurrentArguments()[0]);
    291 				return null;
    292 			}
    293
    294 		});
    295
    296 		return request;
    297 	}
    298
    299 }