|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 the original author or authors. |
| 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, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.test.web.mock.servlet.samples.standalone; |
| 17 | + |
| 18 | +import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.fileUpload; |
| 19 | +import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.model; |
| 20 | +import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | +import org.springframework.mock.web.MockMultipartFile; |
| 26 | +import org.springframework.stereotype.Controller; |
| 27 | +import org.springframework.ui.Model; |
| 28 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 29 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 30 | +import org.springframework.web.bind.annotation.RequestParam; |
| 31 | +import org.springframework.web.multipart.MultipartFile; |
| 32 | + |
| 33 | +public class FileUploadControllerTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void readString() throws Exception { |
| 37 | + |
| 38 | + MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); |
| 39 | + |
| 40 | + standaloneSetup(new FileUploadController()).build() |
| 41 | + .perform(fileUpload("/fileupload").file(file)) |
| 42 | + .andExpect(model().attribute("message", "File 'orig' uploaded successfully")); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Controller |
| 47 | + private static class FileUploadController { |
| 48 | + |
| 49 | + @RequestMapping(value="/fileupload", method=RequestMethod.POST) |
| 50 | + public String processUpload(@RequestParam MultipartFile file, Model model) throws IOException { |
| 51 | + model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully"); |
| 52 | + return "redirect:/index"; |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments