    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.controller.impl;
    17
    18  import static org.easymock.EasyMock.createMock;
    19  import static org.easymock.EasyMock.expect;
    20  import static org.easymock.EasyMock.replay;
    21  import static org.easymock.EasyMock.verify;
    22  import static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.assertTrue;
    24
    25  import java.util.HashMap;
    26  import java.util.Map;
    27
    28  import javax.servlet.http.HttpServletRequest;
    29
    30  import org.junit.Test;
    31  import org.seasar.cubby.controller.impl.DefaultRequestParser;
    32
    33  public class DefaultRequestParserImplTest {
    34
    35  	public DefaultRequestParser requestParser = new DefaultRequestParser();
    36
    37  	@Test
    38  	public void getEmptyParameterMap() {
    39  		HttpServletRequest request = createMock(HttpServletRequest.class);
    40  		Map<String, String[]> requestParameterMap = new HashMap<String, String[]>();
    41  		expect(request.getParameterMap()).andReturn(requestParameterMap);
    42  		replay(request);
    43
    44  		Map<String, Object[]> parameterMap = requestParser
    45  				.getParameterMap(request);
    46  		assertEquals("parameterMap.size()", 0, parameterMap.size());
    47  		verify(request);
    48  	}
    49
    50  	@Test
    51  	public void getParameterMap() throws Throwable {
    52  		HttpServletRequest request = createMock(HttpServletRequest.class);
    53  		Map<String, String[]> requestParameterMap = new HashMap<String, String[]>();
    54  		requestParameterMap.put("a", new String[] { "12345" });
    55  		requestParameterMap.put("b", new String[] { "abc", "def" });
    56  		expect(request.getParameterMap()).andReturn(requestParameterMap);
    57  		replay(request);
    58
    59  		Map<String, Object[]> parameterMap = requestParser
    60  				.getParameterMap(request);
    61  		assertEquals("parameterMap.size()", 2, parameterMap.size());
    62  		Object[] a = parameterMap.get("a");
    63  		assertEquals("a.length", 1, a.length);
    64  		assertEquals("a[0]", "12345", a[0]);
    65  		Object[] b = parameterMap.get("b");
    66  		assertEquals("b.length", 2, b.length);
    67  		assertEquals("b[0]", "abc", b[0]);
    68  		assertEquals("b[1]", "def", b[1]);
    69  		verify(request);
    70  	}
    71
    72  	@Test
    73  	public void isParsable1() {
    74  		HttpServletRequest request = createMock(HttpServletRequest.class);
    75  		expect(request.getContentType()).andReturn(
    76  				"application/x-www-form-urlencoded").anyTimes();
    77  		replay(request);
    78  		assertTrue(requestParser.isParsable(request));
    79  		verify(request);
    80  	}
    81
    82  	@Test
    83  	public void isParsable2() {
    84  		HttpServletRequest request = createMock(HttpServletRequest.class);
    85  		expect(request.getContentType()).andReturn("multipart/form-data")
    86  				.anyTimes();
    87  		replay(request);
    88  		assertTrue(requestParser.isParsable(request));
    89  		verify(request);
    90  	}
    91
    92  	@Test
    93  	public void isParsable3() {
    94  		HttpServletRequest request = createMock(HttpServletRequest.class);
    95  		expect(request.getContentType()).andReturn("application/atom+xml")
    96  				.anyTimes();
    97  		replay(request);
    98  		assertTrue(requestParser.isParsable(request));
    99  		verify(request);
    100 	}
    101
    102 }