28
28
import junit .framework .AssertionFailedError ;
29
29
import junit .framework .TestCase ;
30
30
import org .easymock .MockControl ;
31
+ import static org .easymock .EasyMock .*;
32
+ import org .junit .Test ;
33
+ import static org .junit .Assert .*;
31
34
32
35
import org .springframework .beans .TestBean ;
33
36
import org .springframework .mock .web .MockHttpServletRequest ;
34
37
import org .springframework .mock .web .MockHttpServletResponse ;
38
+ import org .springframework .http .HttpStatus ;
39
+ import org .springframework .web .servlet .View ;
35
40
36
41
/**
37
42
* Tests for redirect view, and query string construction.
40
45
* @author Rod Johnson
41
46
* @author Juergen Hoeller
42
47
* @author Sam Brannen
48
+ * @author Arjen Poutsma
43
49
* @since 27.05.2003
44
50
*/
45
- public class RedirectViewTests extends TestCase {
51
+ public class RedirectViewTests {
46
52
47
- public void testNoUrlSet () throws Exception {
53
+ @ Test (expected = IllegalArgumentException .class )
54
+ public void noUrlSet () throws Exception {
48
55
RedirectView rv = new RedirectView ();
49
- try {
50
- rv .afterPropertiesSet ();
51
- fail ("Should have thrown IllegalArgumentException" );
52
- }
53
- catch (IllegalArgumentException ex ) {
54
- // expected
55
- }
56
+ rv .afterPropertiesSet ();
56
57
}
57
58
58
- public void testHttp11 () throws Exception {
59
+ @ Test
60
+ public void http11 () throws Exception {
59
61
RedirectView rv = new RedirectView ();
60
62
rv .setUrl ("http://url.somewhere.com" );
61
63
rv .setHttp10Compatible (false );
@@ -66,17 +68,46 @@ public void testHttp11() throws Exception {
66
68
assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
67
69
}
68
70
69
- public void testEmptyMap () throws Exception {
71
+ @ Test
72
+ public void explicitStatusCode () throws Exception {
73
+ RedirectView rv = new RedirectView ();
74
+ rv .setUrl ("http://url.somewhere.com" );
75
+ rv .setHttp10Compatible (false );
76
+ rv .setStatusCode (HttpStatus .CREATED );
77
+ MockHttpServletRequest request = new MockHttpServletRequest ();
78
+ MockHttpServletResponse response = new MockHttpServletResponse ();
79
+ rv .render (new HashMap (), request , response );
80
+ assertEquals (201 , response .getStatus ());
81
+ assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
82
+ }
83
+
84
+ @ Test
85
+ public void attributeStatusCode () throws Exception {
86
+ RedirectView rv = new RedirectView ();
87
+ rv .setUrl ("http://url.somewhere.com" );
88
+ rv .setHttp10Compatible (false );
89
+ MockHttpServletRequest request = new MockHttpServletRequest ();
90
+ request .setAttribute (View .RESPONSE_STATUS_ATTRIBUTE , HttpStatus .CREATED );
91
+ MockHttpServletResponse response = new MockHttpServletResponse ();
92
+ rv .render (new HashMap (), request , response );
93
+ assertEquals (201 , response .getStatus ());
94
+ assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
95
+ }
96
+
97
+ @ Test
98
+ public void emptyMap () throws Exception {
70
99
String url = "/myUrl" ;
71
100
doTest (new HashMap (), url , false , url );
72
101
}
73
102
74
- public void testEmptyMapWithContextRelative () throws Exception {
103
+ @ Test
104
+ public void emptyMapWithContextRelative () throws Exception {
75
105
String url = "/myUrl" ;
76
106
doTest (new HashMap (), url , true , url );
77
107
}
78
108
79
- public void testSingleParam () throws Exception {
109
+ @ Test
110
+ public void singleParam () throws Exception {
80
111
String url = "http://url.somewhere.com" ;
81
112
String key = "foo" ;
82
113
String val = "bar" ;
@@ -86,7 +117,8 @@ public void testSingleParam() throws Exception {
86
117
doTest (model , url , false , expectedUrlForEncoding );
87
118
}
88
119
89
- public void testSingleParamWithoutExposingModelAttributes () throws Exception {
120
+ @ Test
121
+ public void singleParamWithoutExposingModelAttributes () throws Exception {
90
122
String url = "http://url.somewhere.com" ;
91
123
String key = "foo" ;
92
124
String val = "bar" ;
@@ -96,7 +128,8 @@ public void testSingleParamWithoutExposingModelAttributes() throws Exception {
96
128
doTest (model , url , false , false , expectedUrlForEncoding );
97
129
}
98
130
99
- public void testParamWithAnchor () throws Exception {
131
+ @ Test
132
+ public void paramWithAnchor () throws Exception {
100
133
String url = "http://url.somewhere.com/test.htm#myAnchor" ;
101
134
String key = "foo" ;
102
135
String val = "bar" ;
@@ -106,7 +139,8 @@ public void testParamWithAnchor() throws Exception {
106
139
doTest (model , url , false , expectedUrlForEncoding );
107
140
}
108
141
109
- public void testTwoParams () throws Exception {
142
+ @ Test
143
+ public void twoParams () throws Exception {
110
144
String url = "http://url.somewhere.com" ;
111
145
String key = "foo" ;
112
146
String val = "bar" ;
@@ -126,7 +160,8 @@ public void testTwoParams() throws Exception {
126
160
}
127
161
}
128
162
129
- public void testArrayParam () throws Exception {
163
+ @ Test
164
+ public void arrayParam () throws Exception {
130
165
String url = "http://url.somewhere.com" ;
131
166
String key = "foo" ;
132
167
String [] val = new String [] {"bar" , "baz" };
@@ -143,7 +178,8 @@ public void testArrayParam() throws Exception {
143
178
}
144
179
}
145
180
146
- public void testCollectionParam () throws Exception {
181
+ @ Test
182
+ public void collectionParam () throws Exception {
147
183
String url = "http://url.somewhere.com" ;
148
184
String key = "foo" ;
149
185
List val = new ArrayList ();
@@ -162,7 +198,8 @@ public void testCollectionParam() throws Exception {
162
198
}
163
199
}
164
200
165
- public void testObjectConversion () throws Exception {
201
+ @ Test
202
+ public void objectConversion () throws Exception {
166
203
String url = "http://url.somewhere.com" ;
167
204
String key = "foo" ;
168
205
String val = "bar" ;
@@ -183,7 +220,7 @@ private void doTest(Map map, String url, boolean contextRelative, String expecte
183
220
doTest (map , url , contextRelative , true , expectedUrlForEncoding );
184
221
}
185
222
186
- private void doTest (final Map map , final String url , final boolean contextRelative ,
223
+ private void doTest (final Map < String , ?> map , final String url , final boolean contextRelative ,
187
224
final boolean exposeModelAttributes , String expectedUrlForEncoding ) throws Exception {
188
225
189
226
class TestRedirectView extends RedirectView {
0 commit comments