1
1
/*
2
- * Copyright 2002-2008 the original author or authors.
2
+ * Copyright 2002-2010 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .util .ArrayList ;
20
20
import java .util .HashMap ;
21
- import java .util .LinkedHashMap ;
22
21
import java .util .List ;
23
22
import java .util .Map ;
24
23
import javax .servlet .http .HttpServletRequest ;
25
24
import javax .servlet .http .HttpServletResponse ;
26
25
27
26
import junit .framework .AssertionFailedError ;
28
- import org .easymock .MockControl ;
27
+ import static org .easymock .EasyMock .* ;
29
28
import static org .junit .Assert .*;
30
29
import org .junit .Test ;
31
30
34
33
import org .springframework .mock .web .MockHttpServletRequest ;
35
34
import org .springframework .mock .web .MockHttpServletResponse ;
36
35
import org .springframework .web .servlet .View ;
36
+ import org .springframework .web .util .WebUtils ;
37
37
38
38
/**
39
39
* Tests for redirect view, and query string construction.
@@ -60,7 +60,7 @@ public void http11() throws Exception {
60
60
rv .setHttp10Compatible (false );
61
61
MockHttpServletRequest request = new MockHttpServletRequest ();
62
62
MockHttpServletResponse response = new MockHttpServletResponse ();
63
- rv .render (new HashMap (), request , response );
63
+ rv .render (new HashMap < String , Object > (), request , response );
64
64
assertEquals (303 , response .getStatus ());
65
65
assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
66
66
}
@@ -73,7 +73,7 @@ public void explicitStatusCode() throws Exception {
73
73
rv .setStatusCode (HttpStatus .CREATED );
74
74
MockHttpServletRequest request = new MockHttpServletRequest ();
75
75
MockHttpServletResponse response = new MockHttpServletResponse ();
76
- rv .render (new HashMap (), request , response );
76
+ rv .render (new HashMap < String , Object > (), request , response );
77
77
assertEquals (201 , response .getStatus ());
78
78
assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
79
79
}
@@ -86,29 +86,29 @@ public void attributeStatusCode() throws Exception {
86
86
MockHttpServletRequest request = new MockHttpServletRequest ();
87
87
request .setAttribute (View .RESPONSE_STATUS_ATTRIBUTE , HttpStatus .CREATED );
88
88
MockHttpServletResponse response = new MockHttpServletResponse ();
89
- rv .render (new HashMap (), request , response );
89
+ rv .render (new HashMap < String , Object > (), request , response );
90
90
assertEquals (201 , response .getStatus ());
91
91
assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
92
92
}
93
93
94
94
@ Test
95
95
public void emptyMap () throws Exception {
96
96
String url = "/myUrl" ;
97
- doTest (new HashMap (), url , false , url );
97
+ doTest (new HashMap < String , Object > (), url , false , url );
98
98
}
99
99
100
100
@ Test
101
101
public void emptyMapWithContextRelative () throws Exception {
102
102
String url = "/myUrl" ;
103
- doTest (new HashMap (), url , true , url );
103
+ doTest (new HashMap < String , Object > (), url , true , url );
104
104
}
105
105
106
106
@ Test
107
107
public void singleParam () throws Exception {
108
108
String url = "http://url.somewhere.com" ;
109
109
String key = "foo" ;
110
110
String val = "bar" ;
111
- Map model = new HashMap ();
111
+ Map < String , String > model = new HashMap < String , String > ();
112
112
model .put (key , val );
113
113
String expectedUrlForEncoding = url + "?" + key + "=" + val ;
114
114
doTest (model , url , false , expectedUrlForEncoding );
@@ -119,7 +119,7 @@ public void singleParamWithoutExposingModelAttributes() throws Exception {
119
119
String url = "http://url.somewhere.com" ;
120
120
String key = "foo" ;
121
121
String val = "bar" ;
122
- Map model = new HashMap ();
122
+ Map < String , String > model = new HashMap < String , String > ();
123
123
model .put (key , val );
124
124
String expectedUrlForEncoding = url ; // + "?" + key + "=" + val;
125
125
doTest (model , url , false , false , expectedUrlForEncoding );
@@ -130,7 +130,7 @@ public void paramWithAnchor() throws Exception {
130
130
String url = "http://url.somewhere.com/test.htm#myAnchor" ;
131
131
String key = "foo" ;
132
132
String val = "bar" ;
133
- Map model = new HashMap ();
133
+ Map < String , String > model = new HashMap < String , String > ();
134
134
model .put (key , val );
135
135
String expectedUrlForEncoding = "http://url.somewhere.com/test.htm" + "?" + key + "=" + val + "#myAnchor" ;
136
136
doTest (model , url , false , expectedUrlForEncoding );
@@ -143,7 +143,7 @@ public void twoParams() throws Exception {
143
143
String val = "bar" ;
144
144
String key2 = "thisIsKey2" ;
145
145
String val2 = "andThisIsVal2" ;
146
- Map model = new HashMap ();
146
+ Map < String , String > model = new HashMap < String , String > ();
147
147
model .put (key , val );
148
148
model .put (key2 , val2 );
149
149
try {
@@ -162,7 +162,7 @@ public void arrayParam() throws Exception {
162
162
String url = "http://url.somewhere.com" ;
163
163
String key = "foo" ;
164
164
String [] val = new String [] {"bar" , "baz" };
165
- Map model = new HashMap ();
165
+ Map < String , String []> model = new HashMap < String , String []> ();
166
166
model .put (key , val );
167
167
try {
168
168
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val [0 ] + "&" + key + "=" + val [1 ];
@@ -179,10 +179,10 @@ public void arrayParam() throws Exception {
179
179
public void collectionParam () throws Exception {
180
180
String url = "http://url.somewhere.com" ;
181
181
String key = "foo" ;
182
- List val = new ArrayList ();
182
+ List < String > val = new ArrayList < String > ();
183
183
val .add ("bar" );
184
184
val .add ("baz" );
185
- Map model = new HashMap ();
185
+ Map < String , List < String >> model = new HashMap < String , List < String >> ();
186
186
model .put (key , val );
187
187
try {
188
188
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val .get (0 ) + "&" + key + "=" + val .get (1 );
@@ -202,17 +202,17 @@ public void objectConversion() throws Exception {
202
202
String val = "bar" ;
203
203
String key2 = "int2" ;
204
204
Object val2 = new Long (611 );
205
- Object key3 = "tb" ;
205
+ String key3 = "tb" ;
206
206
Object val3 = new TestBean ();
207
- Map model = new LinkedHashMap ();
207
+ Map < String , Object > model = new HashMap < String , Object > ();
208
208
model .put (key , val );
209
209
model .put (key2 , val2 );
210
210
model .put (key3 , val3 );
211
211
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val + "&" + key2 + "=" + val2 ;
212
212
doTest (model , url , false , expectedUrlForEncoding );
213
213
}
214
214
215
- private void doTest (Map map , String url , boolean contextRelative , String expectedUrlForEncoding )
215
+ private void doTest (Map < String , ?> map , String url , boolean contextRelative , String expectedUrlForEncoding )
216
216
throws Exception {
217
217
doTest (map , url , contextRelative , true , expectedUrlForEncoding );
218
218
}
@@ -227,7 +227,8 @@ class TestRedirectView extends RedirectView {
227
227
/**
228
228
* Test whether this callback method is called with correct args
229
229
*/
230
- protected Map queryProperties (Map model ) {
230
+ @ Override
231
+ protected Map <String , Object > queryProperties (Map <String , Object > model ) {
231
232
// They may not be the same model instance, but they're still equal
232
233
assertTrue ("Map and model must be equal." , map .equals (model ));
233
234
this .queryPropertiesCalled = true ;
@@ -240,30 +241,27 @@ protected Map queryProperties(Map model) {
240
241
rv .setContextRelative (contextRelative );
241
242
rv .setExposeModelAttributes (exposeModelAttributes );
242
243
243
- MockControl requestControl = MockControl . createControl ( HttpServletRequest .class );
244
- HttpServletRequest request = ( HttpServletRequest ) requestControl . getMock ();
245
- request .getCharacterEncoding ();
246
- requestControl . setReturnValue ( null , 1 );
244
+ HttpServletRequest request = createNiceMock ( "request" , HttpServletRequest .class );
245
+ if ( exposeModelAttributes ) {
246
+ expect ( request .getCharacterEncoding ()). andReturn ( WebUtils . DEFAULT_CHARACTER_ENCODING );
247
+ }
247
248
if (contextRelative ) {
248
249
expectedUrlForEncoding = "/context" + expectedUrlForEncoding ;
249
- request .getContextPath ();
250
- requestControl .setReturnValue ("/context" );
250
+ expect (request .getContextPath ()).andReturn ("/context" );
251
251
}
252
- requestControl .replay ();
253
252
254
- MockControl responseControl = MockControl .createControl (HttpServletResponse .class );
255
- HttpServletResponse resp = (HttpServletResponse ) responseControl .getMock ();
256
- resp .encodeRedirectURL (expectedUrlForEncoding );
257
- responseControl .setReturnValue (expectedUrlForEncoding );
258
- resp .sendRedirect (expectedUrlForEncoding );
259
- responseControl .setVoidCallable (1 );
260
- responseControl .replay ();
253
+ HttpServletResponse response = createMock ("response" , HttpServletResponse .class );
254
+ expect (response .encodeRedirectURL (expectedUrlForEncoding )).andReturn (expectedUrlForEncoding );
255
+ response .sendRedirect (expectedUrlForEncoding );
261
256
262
- rv .render (map , request , resp );
257
+ replay (request , response );
258
+
259
+ rv .render (map , request , response );
263
260
if (exposeModelAttributes ) {
264
261
assertTrue ("queryProperties() should have been called." , rv .queryPropertiesCalled );
265
262
}
266
- responseControl .verify ();
263
+
264
+ verify (request , response );
267
265
}
268
266
269
267
}
0 commit comments