15
15
*/
16
16
package org .springframework .bootstrap .autoconfigure .web ;
17
17
18
- import static org .junit .Assert .assertEquals ;
19
- import static org .junit .Assert .assertNull ;
20
- import static org .junit .Assert .assertSame ;
21
- import static org .junit .Assert .fail ;
22
-
23
18
import javax .servlet .MultipartConfigElement ;
24
19
20
+ import org .junit .After ;
21
+ import org .junit .Rule ;
25
22
import org .junit .Test ;
26
- import org .springframework . beans . factory . NoSuchBeanDefinitionException ;
23
+ import org .junit . rules . ExpectedException ;
27
24
import org .springframework .bootstrap .context .embedded .AnnotationConfigEmbeddedWebApplicationContext ;
28
25
import org .springframework .bootstrap .context .embedded .jetty .JettyEmbeddedServletContainerFactory ;
29
26
import org .springframework .bootstrap .context .embedded .tomcat .TomcatEmbeddedServletContainerFactory ;
38
35
import org .springframework .web .servlet .DispatcherServlet ;
39
36
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
40
37
38
+ import static org .junit .Assert .assertEquals ;
39
+ import static org .junit .Assert .assertNull ;
40
+ import static org .junit .Assert .assertSame ;
41
+
41
42
/**
42
43
* A series of embedded unit tests, based on an empty configuration, no multipart
43
44
* configuration, and a multipart configuration, with both Jetty and Tomcat.
44
45
*
45
46
* @author Greg Turnquist
47
+ * @author Dave Syer
46
48
*/
47
49
public class MultipartAutoConfigurationTests {
48
-
50
+
49
51
private AnnotationConfigEmbeddedWebApplicationContext context ;
50
-
52
+
53
+ @ Rule
54
+ public ExpectedException exception = ExpectedException .none ();
55
+
56
+ @ After
57
+ public void close () {
58
+ if (this .context != null ) {
59
+ this .context .close ();
60
+ }
61
+ }
62
+
51
63
@ Test
52
64
public void containerWithNothing () {
53
65
this .context = new AnnotationConfigEmbeddedWebApplicationContext (
54
66
ContainerWithNothing .class ,
55
67
EmbeddedServletContainerAutoConfiguration .class ,
56
68
MultipartAutoConfiguration .class );
57
- try {
58
- DispatcherServlet servlet = this .context .getBean (DispatcherServlet .class );
59
- assertNull (servlet .getMultipartResolver ());
60
- try {
61
- this .context .getBean (StandardServletMultipartResolver .class );
62
- fail ("Expected to receive a " + NoSuchBeanDefinitionException .class );
63
- } catch (NoSuchBeanDefinitionException e ) {
64
- }
65
- try {
66
- this .context .getBean (MultipartResolver .class );
67
- fail ("Expected to receive a " + NoSuchBeanDefinitionException .class );
68
- } catch (NoSuchBeanDefinitionException e ) {
69
- }
70
- } finally {
71
- this .context .close ();
72
- }
69
+ DispatcherServlet servlet = this .context .getBean (DispatcherServlet .class );
70
+ assertNull (servlet .getMultipartResolver ());
71
+ assertEquals (0 ,
72
+ this .context .getBeansOfType (StandardServletMultipartResolver .class )
73
+ .size ());
74
+ assertEquals (0 , this .context .getBeansOfType (MultipartResolver .class ).size ());
73
75
}
74
-
76
+
75
77
@ Configuration
76
78
public static class ContainerWithNothing {
77
79
}
@@ -82,23 +84,13 @@ public void containerWithNoMultipartJettyConfiguration() {
82
84
ContainerWithNoMultipartJetty .class ,
83
85
EmbeddedServletContainerAutoConfiguration .class ,
84
86
MultipartAutoConfiguration .class );
85
- try {
86
- DispatcherServlet servlet = this .context .getBean (DispatcherServlet .class );
87
- assertNull (servlet .getMultipartResolver ());
88
- try {
89
- this .context .getBean (StandardServletMultipartResolver .class );
90
- fail ("Expected to receive a " + NoSuchBeanDefinitionException .class );
91
- } catch (NoSuchBeanDefinitionException e ) {
92
- }
93
- try {
94
- this .context .getBean (MultipartResolver .class );
95
- fail ("Expected to receive a " + NoSuchBeanDefinitionException .class );
96
- } catch (NoSuchBeanDefinitionException e ) {
97
- }
98
- verifyServletWorks ();
99
- } finally {
100
- this .context .close ();
101
- }
87
+ DispatcherServlet servlet = this .context .getBean (DispatcherServlet .class );
88
+ assertNull (servlet .getMultipartResolver ());
89
+ assertEquals (0 ,
90
+ this .context .getBeansOfType (StandardServletMultipartResolver .class )
91
+ .size ());
92
+ assertEquals (0 , this .context .getBeansOfType (MultipartResolver .class ).size ());
93
+ verifyServletWorks ();
102
94
}
103
95
104
96
@ Configuration
@@ -107,35 +99,26 @@ public static class ContainerWithNoMultipartJetty {
107
99
JettyEmbeddedServletContainerFactory containerFactory () {
108
100
return new JettyEmbeddedServletContainerFactory ();
109
101
}
102
+
110
103
@ Bean
111
104
WebController controller () {
112
105
return new WebController ();
113
106
}
114
- }
107
+ }
115
108
116
109
@ Test
117
110
public void containerWithNoMultipartTomcatConfiguration () {
118
111
this .context = new AnnotationConfigEmbeddedWebApplicationContext (
119
112
ContainerWithNoMultipartTomcat .class ,
120
113
EmbeddedServletContainerAutoConfiguration .class ,
121
114
MultipartAutoConfiguration .class );
122
- try {
123
- DispatcherServlet servlet = this .context .getBean (DispatcherServlet .class );
124
- assertNull (servlet .getMultipartResolver ());
125
- try {
126
- this .context .getBean (StandardServletMultipartResolver .class );
127
- fail ("Expected to receive a " + NoSuchBeanDefinitionException .class );
128
- } catch (NoSuchBeanDefinitionException e ) {
129
- }
130
- try {
131
- this .context .getBean (MultipartResolver .class );
132
- fail ("Expected to receive a " + NoSuchBeanDefinitionException .class );
133
- } catch (NoSuchBeanDefinitionException e ) {
134
- }
135
- verifyServletWorks ();
136
- } finally {
137
- this .context .close ();
138
- }
115
+ DispatcherServlet servlet = this .context .getBean (DispatcherServlet .class );
116
+ assertNull (servlet .getMultipartResolver ());
117
+ assertEquals (0 ,
118
+ this .context .getBeansOfType (StandardServletMultipartResolver .class )
119
+ .size ());
120
+ assertEquals (0 , this .context .getBeansOfType (MultipartResolver .class ).size ());
121
+ verifyServletWorks ();
139
122
}
140
123
141
124
@ Configuration
@@ -144,27 +127,23 @@ public static class ContainerWithNoMultipartTomcat {
144
127
TomcatEmbeddedServletContainerFactory containerFactory () {
145
128
return new TomcatEmbeddedServletContainerFactory ();
146
129
}
130
+
147
131
@ Bean
148
132
WebController controller () {
149
133
return new WebController ();
150
134
}
151
- }
135
+ }
152
136
153
137
@ Test
154
138
public void containerWithAutomatedMultipartJettyConfiguration () {
155
139
this .context = new AnnotationConfigEmbeddedWebApplicationContext (
156
140
ContainerWithEverythingJetty .class ,
157
141
EmbeddedServletContainerAutoConfiguration .class ,
158
142
MultipartAutoConfiguration .class );
159
- try {
160
- this .context .getBean (MultipartConfigElement .class );
161
- assertSame (
162
- this .context .getBean (DispatcherServlet .class ).getMultipartResolver (),
163
- this .context .getBean (StandardServletMultipartResolver .class ));
164
- verifyServletWorks ();
165
- } finally {
166
- this .context .close ();
167
- }
143
+ this .context .getBean (MultipartConfigElement .class );
144
+ assertSame (this .context .getBean (DispatcherServlet .class ).getMultipartResolver (),
145
+ this .context .getBean (StandardServletMultipartResolver .class ));
146
+ verifyServletWorks ();
168
147
}
169
148
170
149
@ Configuration
@@ -173,10 +152,12 @@ public static class ContainerWithEverythingJetty {
173
152
MultipartConfigElement multipartConfigElement () {
174
153
return new MultipartConfigElement ("" );
175
154
}
155
+
176
156
@ Bean
177
157
JettyEmbeddedServletContainerFactory containerFactory () {
178
158
return new JettyEmbeddedServletContainerFactory ();
179
159
}
160
+
180
161
@ Bean
181
162
WebController webController () {
182
163
return new WebController ();
@@ -189,15 +170,10 @@ public void containerWithAutomatedMultipartTomcatConfiguration() {
189
170
ContainerWithEverythingTomcat .class ,
190
171
EmbeddedServletContainerAutoConfiguration .class ,
191
172
MultipartAutoConfiguration .class );
192
- try {
193
- this .context .getBean (MultipartConfigElement .class );
194
- assertSame (
195
- this .context .getBean (DispatcherServlet .class ).getMultipartResolver (),
196
- this .context .getBean (StandardServletMultipartResolver .class ));
197
- verifyServletWorks ();
198
- } finally {
199
- this .context .close ();
200
- }
173
+ this .context .getBean (MultipartConfigElement .class );
174
+ assertSame (this .context .getBean (DispatcherServlet .class ).getMultipartResolver (),
175
+ this .context .getBean (StandardServletMultipartResolver .class ));
176
+ verifyServletWorks ();
201
177
}
202
178
203
179
@ Configuration
@@ -207,10 +183,12 @@ public static class ContainerWithEverythingTomcat {
207
183
MultipartConfigElement multipartConfigElement () {
208
184
return new MultipartConfigElement ("" );
209
185
}
186
+
210
187
@ Bean
211
188
TomcatEmbeddedServletContainerFactory containerFactory () {
212
189
return new TomcatEmbeddedServletContainerFactory ();
213
190
}
191
+
214
192
@ Bean
215
193
WebController webController () {
216
194
return new WebController ();
@@ -220,15 +198,16 @@ WebController webController() {
220
198
@ Controller
221
199
public static class WebController {
222
200
@ RequestMapping ("/" )
223
- public @ ResponseBody String index () {
201
+ public @ ResponseBody
202
+ String index () {
224
203
return "Hello" ;
225
204
}
226
205
}
227
-
206
+
228
207
private void verifyServletWorks () {
229
208
RestTemplate restTemplate = new RestTemplate ();
230
- assertEquals (restTemplate .getForObject ("http://localhost:8080/" , String .class ), "Hello" );
209
+ assertEquals (restTemplate .getForObject ("http://localhost:8080/" , String .class ),
210
+ "Hello" );
231
211
}
232
212
233
-
234
213
}
0 commit comments