    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.junit.Assert.assertFalse;
    22  import static org.junit.Assert.assertTrue;
    23  import static org.junit.Assert.fail;
    24
    25  import java.util.HashMap;
    26  import java.util.Map;
    27
    28  import javax.servlet.http.HttpServletRequest;
    29
    30  import org.apache.commons.fileupload.FileUpload;
    31  import org.apache.commons.fileupload.RequestContext;
    32  import org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException;
    33  import org.apache.commons.fileupload.servlet.ServletFileUpload;
    34  import org.apache.commons.fileupload.servlet.ServletRequestContext;
    35  import org.easymock.IAnswer;
    36  import org.junit.After;
    37  import org.junit.Before;
    38  import org.junit.Test;
    39  import org.seasar.cubby.controller.RequestParseException;
    40  import org.seasar.cubby.controller.RequestParser;
    41  import org.seasar.cubby.mock.MockContainerProvider;
    42  import org.seasar.cubby.plugin.PluginRegistry;
    43  import org.seasar.cubby.plugins.BinderPlugin;
    44  import org.seasar.cubby.spi.ContainerProvider;
    45  import org.seasar.cubby.spi.container.Container;
    46
    47  public class MultipartRequestParserImplTest {
    48
    49  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    50
    51  	private RequestParser requestParser = new MultipartRequestParser();
    52
    53  	private HttpServletRequest request;
    54
    55  	private String contentType;
    56
    57  	@Before
    58  	public void setupRequest() throws Exception {
    59  		request = createMock(HttpServletRequest.class);
    60  		expect(request.getCharacterEncoding()).andStubReturn("UTF-8");
    61  		expect(request.getParameterMap()).andStubReturn(
    62  				new HashMap<String, String[]>());
    63  		expect(request.getContentType()).andStubAnswer(new IAnswer<String>() {
    64
    65  			public String answer() throws Throwable {
    66  				return contentType;
    67  			}
    68
    69  		});
    70  		replay(request);
    71
    72  		final FileUpload fileUpload = new ServletFileUpload();
    73  		final RequestContext requestContext = new ServletRequestContext(request);
    74  		BinderPlugin binderPlugin = new BinderPlugin();
    75  		binderPlugin.bind(ContainerProvider.class).toInstance(
    76  				new MockContainerProvider(new Container() {
    77
    78  					public <T> T lookup(final Class<T> type) {
    79  						if (FileUpload.class.equals(type)) {
    80  							return type.cast(fileUpload);
    81  						}
    82
    83  						if (RequestContext.class.equals(type)) {
    84  							return type.cast(requestContext);
    85  						}
    86
    87  						return null;
    88  					}
    89
    90  				}));
    91  		pluginRegistry.register(binderPlugin);
    92  	}
    93
    94  	@After
    95  	public void tearDownProvider() {
    96  		pluginRegistry.clear();
    97  	}
    98
    99  	@Test
    100 	public void invalidCotntentType() {
    101 		contentType = "application/x-www-form-urlencoded";
    102 		try {
    103 			@SuppressWarnings("unused")
    104 			Map<String, Object[]> parameterMap = requestParser
    105 					.getParameterMap(request);
    106 			fail();
    107 		} catch (RequestParseException e) {
    108 			assertTrue(e.getCause() instanceof InvalidContentTypeException);
    109 		}
    110 	}
    111
    112 	@Test
    113 	public void isParsable() {
    114 		contentType = "application/x-www-form-urlencoded";
    115 		assertFalse(requestParser.isParsable(request));
    116
    117 		contentType = "multipart/form-data";
    118 		assertTrue(requestParser.isParsable(request));
    119
    120 		contentType = "application/atom+xml";
    121 		assertFalse(requestParser.isParsable(request));
    122 	}
    123
    124 }