    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.spi.impl;
    17
    18  import static org.easymock.EasyMock.createMock;
    19  import static org.easymock.EasyMock.expect;
    20  import static org.easymock.EasyMock.*;
    21  import static org.junit.Assert.*;
    22
    23  import java.util.Arrays;
    24  import java.util.Collection;
    25  import java.util.HashMap;
    26  import java.util.List;
    27  import java.util.Map;
    28
    29  import javax.servlet.http.HttpServletRequest;
    30
    31  import org.junit.Test;
    32  import org.seasar.cubby.controller.RequestParser;
    33  import org.seasar.cubby.controller.impl.DefaultRequestParser;
    34  import org.seasar.cubby.spi.RequestParserProvider;
    35  import org.seasar.cubby.spi.impl.AbstractRequestParserProvider;
    36
    37  public class AbstractRequestParserProviderTest {
    38
    39  	private RequestParserProvider provider = new AbstractRequestParserProvider() {
    40
    41  		private List<RequestParser> parsers = Arrays
    42  				.asList(new RequestParser[] { new MyRequestParserImpl(),
    43  						new DefaultRequestParser() });
    44
    45  		@Override
    46  		protected Collection<RequestParser> getRequestParsers() {
    47  			return parsers;
    48  		}
    49
    50  	};
    51
    52  	@Test
    53  	public void testSelect1() {
    54  		Map<String, Object[]> parameterMap = new HashMap<String, Object[]>();
    55  		HttpServletRequest request = createMock(HttpServletRequest.class);
    56  		expect(request.getContentType()).andStubReturn(null);
    57  		expect(request.getParameterMap()).andStubReturn(parameterMap);
    58  		replay(request);
    59  		assertEquals(parameterMap, provider.getParameterMap(request));
    60  		verify(request);
    61  	}
    62
    63  	@Test
    64  	public void testSelect2() {
    65  		Map<String, Object[]> parameterMap = new HashMap<String, Object[]>();
    66  		HttpServletRequest request = createMock(HttpServletRequest.class);
    67  		expect(request.getContentType()).andStubReturn(
    68  				"application/x-www-form-urlencoded");
    69  		expect(request.getParameterMap()).andStubReturn(parameterMap);
    70  		replay(request);
    71  		assertEquals(parameterMap, provider.getParameterMap(request));
    72  		verify(request);
    73  	}
    74
    75  	@Test
    76  	public void testSelect4() {
    77  		Map<String, Object[]> parameterMap = new HashMap<String, Object[]>();
    78  		HttpServletRequest request = createMock(HttpServletRequest.class);
    79  		expect(request.getContentType()).andStubReturn("application/atom+xml");
    80  		expect(request.getParameterMap()).andStubReturn(parameterMap);
    81  		replay(request);
    82  		assertEquals(parameterMap, provider.getParameterMap(request));
    83  		verify(request);
    84  	}
    85
    86  	@Test
    87  	public void testSelect5() {
    88  		HttpServletRequest request = createMock(HttpServletRequest.class);
    89  		expect(request.getContentType()).andStubReturn("foo/bar");
    90  		replay(request);
    91  		assertEquals(MY_REQUEST_PARSER_PARAMETER_MAP, provider
    92  				.getParameterMap(request));
    93  		verify(request);
    94  	}
    95
    96  	private static Map<String, Object[]> MY_REQUEST_PARSER_PARAMETER_MAP;
    97  	static {
    98  		Map<String, Object[]> map = new HashMap<String, Object[]>();
    99  		map.put("abc", new Object[] { "def" });
    100 		MY_REQUEST_PARSER_PARAMETER_MAP = map;
    101 	}
    102
    103 	private static class MyRequestParserImpl implements RequestParser {
    104
    105 		public Map<String, Object[]> getParameterMap(HttpServletRequest request) {
    106 			return MY_REQUEST_PARSER_PARAMETER_MAP;
    107 		}
    108
    109 		public boolean isParsable(HttpServletRequest request) {
    110 			String contentType = request.getContentType();
    111 			return contentType != null && contentType.startsWith("foo/");
    112 		}
    113
    114 	}
    115 }