|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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 | + |
| 17 | +package org.springframework.web.bind.support; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.servlet.ServletException; |
| 23 | +import javax.servlet.http.HttpServlet; |
| 24 | +import javax.servlet.http.HttpServletRequest; |
| 25 | +import javax.servlet.http.HttpServletResponse; |
| 26 | +import javax.servlet.http.Part; |
| 27 | + |
| 28 | +import org.eclipse.jetty.server.Server; |
| 29 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 30 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 31 | +import org.junit.AfterClass; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.Test; |
| 35 | +import org.springframework.core.io.ClassPathResource; |
| 36 | +import org.springframework.core.io.Resource; |
| 37 | +import org.springframework.http.MediaType; |
| 38 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 39 | +import org.springframework.mock.web.test.MockMultipartFile; |
| 40 | +import org.springframework.util.LinkedMultiValueMap; |
| 41 | +import org.springframework.util.MultiValueMap; |
| 42 | +import org.springframework.util.SocketUtils; |
| 43 | +import org.springframework.web.client.RestTemplate; |
| 44 | +import org.springframework.web.context.request.ServletWebRequest; |
| 45 | + |
| 46 | +import static org.junit.Assert.*; |
| 47 | + |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Brian Clozel |
| 51 | + */ |
| 52 | +public class WebRequestDataBinderIntegrationTests { |
| 53 | + |
| 54 | + protected static String baseUrl; |
| 55 | + |
| 56 | + protected static MediaType contentType; |
| 57 | + |
| 58 | + private static Server jettyServer; |
| 59 | + |
| 60 | + private RestTemplate template; |
| 61 | + |
| 62 | + private static PartsServlet partsServlet; |
| 63 | + |
| 64 | + private static PartListServlet partListServlet; |
| 65 | + |
| 66 | + |
| 67 | + @Before |
| 68 | + public void createTemplate() { |
| 69 | + template = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); |
| 70 | + } |
| 71 | + |
| 72 | + @BeforeClass |
| 73 | + public static void startJettyServer() throws Exception { |
| 74 | + int port = SocketUtils.findAvailableTcpPort(); |
| 75 | + jettyServer = new Server(port); |
| 76 | + baseUrl = "http://localhost:" + port; |
| 77 | + ServletContextHandler handler = new ServletContextHandler(); |
| 78 | + |
| 79 | + partsServlet = new PartsServlet(); |
| 80 | + partListServlet = new PartListServlet(); |
| 81 | + |
| 82 | + handler.addServlet(new ServletHolder(partsServlet), "/parts"); |
| 83 | + handler.addServlet(new ServletHolder(partListServlet), "/partlist"); |
| 84 | + jettyServer.setHandler(handler); |
| 85 | + jettyServer.start(); |
| 86 | + } |
| 87 | + |
| 88 | + @AfterClass |
| 89 | + public static void stopJettyServer() throws Exception { |
| 90 | + if (jettyServer != null) { |
| 91 | + jettyServer.stop(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testPartsBinding() { |
| 98 | + |
| 99 | + PartsBean bean = new PartsBean(); |
| 100 | + partsServlet.setBean(bean); |
| 101 | + |
| 102 | + MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>(); |
| 103 | + MockMultipartFile firstPart = new MockMultipartFile("fileName", "aValue".getBytes()); |
| 104 | + parts.add("firstPart", firstPart); |
| 105 | + parts.add("secondPart", "secondValue"); |
| 106 | + |
| 107 | + template.postForLocation(baseUrl + "/parts", parts); |
| 108 | + |
| 109 | + assertNotNull(bean.getFirstPart()); |
| 110 | + assertNotNull(bean.getSecondPart()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testPartListBinding() { |
| 115 | + |
| 116 | + PartListBean bean = new PartListBean(); |
| 117 | + partListServlet.setBean(bean); |
| 118 | + |
| 119 | + MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>(); |
| 120 | + parts.add("partList", "first value"); |
| 121 | + parts.add("partList", "second value"); |
| 122 | + Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); |
| 123 | + parts.add("partList", logo); |
| 124 | + |
| 125 | + template.postForLocation(baseUrl + "/partlist", parts); |
| 126 | + |
| 127 | + assertNotNull(bean.getPartList()); |
| 128 | + assertEquals(parts.size(), bean.getPartList().size()); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + @SuppressWarnings("serial") |
| 133 | + private abstract static class AbstractStandardMultipartServlet<T> extends HttpServlet { |
| 134 | + |
| 135 | + private T bean; |
| 136 | + |
| 137 | + @Override |
| 138 | + public void service(HttpServletRequest request, HttpServletResponse response) throws |
| 139 | + ServletException, IOException { |
| 140 | + |
| 141 | + WebRequestDataBinder binder = new WebRequestDataBinder(bean); |
| 142 | + ServletWebRequest webRequest = new ServletWebRequest(request, response); |
| 143 | + |
| 144 | + binder.bind(webRequest); |
| 145 | + |
| 146 | + response.setStatus(HttpServletResponse.SC_OK); |
| 147 | + } |
| 148 | + |
| 149 | + public void setBean(T bean) { |
| 150 | + this.bean = bean; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static class PartsBean { |
| 155 | + |
| 156 | + public Part firstPart; |
| 157 | + |
| 158 | + public Part secondPart; |
| 159 | + |
| 160 | + public Part getFirstPart() { |
| 161 | + return firstPart; |
| 162 | + } |
| 163 | + |
| 164 | + @SuppressWarnings("unused") |
| 165 | + public void setFirstPart(Part firstPart) { |
| 166 | + this.firstPart = firstPart; |
| 167 | + } |
| 168 | + |
| 169 | + public Part getSecondPart() { |
| 170 | + return secondPart; |
| 171 | + } |
| 172 | + |
| 173 | + @SuppressWarnings("unused") |
| 174 | + public void setSecondPart(Part secondPart) { |
| 175 | + this.secondPart = secondPart; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @SuppressWarnings("serial") |
| 180 | + private static class PartsServlet extends AbstractStandardMultipartServlet<PartsBean> { |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + private static class PartListBean { |
| 185 | + |
| 186 | + public List<Part> partList; |
| 187 | + |
| 188 | + public List<Part> getPartList() { |
| 189 | + return partList; |
| 190 | + } |
| 191 | + |
| 192 | + @SuppressWarnings("unused") |
| 193 | + public void setPartList(List<Part> partList) { |
| 194 | + this.partList = partList; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @SuppressWarnings("serial") |
| 199 | + private static class PartListServlet extends AbstractStandardMultipartServlet<PartListBean> { |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments