2828import junit .framework .AssertionFailedError ;
2929import junit .framework .TestCase ;
3030import org .easymock .MockControl ;
31+ import static org .easymock .EasyMock .*;
32+ import org .junit .Test ;
33+ import static org .junit .Assert .*;
3134
3235import org .springframework .beans .TestBean ;
3336import org .springframework .mock .web .MockHttpServletRequest ;
3437import org .springframework .mock .web .MockHttpServletResponse ;
38+ import org .springframework .http .HttpStatus ;
39+ import org .springframework .web .servlet .View ;
3540
3641/**
3742 * Tests for redirect view, and query string construction.
4045 * @author Rod Johnson
4146 * @author Juergen Hoeller
4247 * @author Sam Brannen
48+ * @author Arjen Poutsma
4349 * @since 27.05.2003
4450 */
45- public class RedirectViewTests extends TestCase {
51+ public class RedirectViewTests {
4652
47- public void testNoUrlSet () throws Exception {
53+ @ Test (expected = IllegalArgumentException .class )
54+ public void noUrlSet () throws Exception {
4855 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 ();
5657 }
5758
58- public void testHttp11 () throws Exception {
59+ @ Test
60+ public void http11 () throws Exception {
5961 RedirectView rv = new RedirectView ();
6062 rv .setUrl ("http://url.somewhere.com" );
6163 rv .setHttp10Compatible (false );
@@ -66,17 +68,46 @@ public void testHttp11() throws Exception {
6668 assertEquals ("http://url.somewhere.com" , response .getHeader ("Location" ));
6769 }
6870
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 {
7099 String url = "/myUrl" ;
71100 doTest (new HashMap (), url , false , url );
72101 }
73102
74- public void testEmptyMapWithContextRelative () throws Exception {
103+ @ Test
104+ public void emptyMapWithContextRelative () throws Exception {
75105 String url = "/myUrl" ;
76106 doTest (new HashMap (), url , true , url );
77107 }
78108
79- public void testSingleParam () throws Exception {
109+ @ Test
110+ public void singleParam () throws Exception {
80111 String url = "http://url.somewhere.com" ;
81112 String key = "foo" ;
82113 String val = "bar" ;
@@ -86,7 +117,8 @@ public void testSingleParam() throws Exception {
86117 doTest (model , url , false , expectedUrlForEncoding );
87118 }
88119
89- public void testSingleParamWithoutExposingModelAttributes () throws Exception {
120+ @ Test
121+ public void singleParamWithoutExposingModelAttributes () throws Exception {
90122 String url = "http://url.somewhere.com" ;
91123 String key = "foo" ;
92124 String val = "bar" ;
@@ -96,7 +128,8 @@ public void testSingleParamWithoutExposingModelAttributes() throws Exception {
96128 doTest (model , url , false , false , expectedUrlForEncoding );
97129 }
98130
99- public void testParamWithAnchor () throws Exception {
131+ @ Test
132+ public void paramWithAnchor () throws Exception {
100133 String url = "http://url.somewhere.com/test.htm#myAnchor" ;
101134 String key = "foo" ;
102135 String val = "bar" ;
@@ -106,7 +139,8 @@ public void testParamWithAnchor() throws Exception {
106139 doTest (model , url , false , expectedUrlForEncoding );
107140 }
108141
109- public void testTwoParams () throws Exception {
142+ @ Test
143+ public void twoParams () throws Exception {
110144 String url = "http://url.somewhere.com" ;
111145 String key = "foo" ;
112146 String val = "bar" ;
@@ -126,7 +160,8 @@ public void testTwoParams() throws Exception {
126160 }
127161 }
128162
129- public void testArrayParam () throws Exception {
163+ @ Test
164+ public void arrayParam () throws Exception {
130165 String url = "http://url.somewhere.com" ;
131166 String key = "foo" ;
132167 String [] val = new String [] {"bar" , "baz" };
@@ -143,7 +178,8 @@ public void testArrayParam() throws Exception {
143178 }
144179 }
145180
146- public void testCollectionParam () throws Exception {
181+ @ Test
182+ public void collectionParam () throws Exception {
147183 String url = "http://url.somewhere.com" ;
148184 String key = "foo" ;
149185 List val = new ArrayList ();
@@ -162,7 +198,8 @@ public void testCollectionParam() throws Exception {
162198 }
163199 }
164200
165- public void testObjectConversion () throws Exception {
201+ @ Test
202+ public void objectConversion () throws Exception {
166203 String url = "http://url.somewhere.com" ;
167204 String key = "foo" ;
168205 String val = "bar" ;
@@ -183,7 +220,7 @@ private void doTest(Map map, String url, boolean contextRelative, String expecte
183220 doTest (map , url , contextRelative , true , expectedUrlForEncoding );
184221 }
185222
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 ,
187224 final boolean exposeModelAttributes , String expectedUrlForEncoding ) throws Exception {
188225
189226 class TestRedirectView extends RedirectView {
0 commit comments