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