KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > RedirectViewTests


1 /*
2  * Copyright 2002-2005 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.servlet.view;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import junit.framework.TestCase;
26 import org.easymock.MockControl;
27
28 import org.springframework.mock.web.MockHttpServletRequest;
29 import org.springframework.mock.web.MockHttpServletResponse;
30
31 /**
32  * Tests for redirect view, and query string construction.
33  * Doesn't test URL encoding, although it does check it's called.
34  * Uses mock objects.
35  *
36  * @author Rod Johnson
37  * @since 27.05.2003
38  */

39 public class RedirectViewTests extends TestCase {
40
41     private void test(final Map JavaDoc map, String JavaDoc url, boolean contextRelative, String JavaDoc expectedUrlForEncoding)
42             throws Exception JavaDoc {
43
44         class TestRedirectView extends RedirectView {
45             public boolean valid;
46
47             /**
48              * Test this callback method is called with correct args
49              */

50             protected Map JavaDoc queryProperties(Map JavaDoc model) {
51                 // They may not be the same model instance, but they're still equal
52
assertTrue(map.equals(model));
53                 valid = true;
54                 return super.queryProperties(model);
55             }
56         }
57
58         TestRedirectView rv = new TestRedirectView();
59         rv.setUrl(url);
60         rv.setContextRelative(contextRelative);
61
62         MockControl rc = MockControl.createControl(HttpServletRequest JavaDoc.class);
63         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) rc.getMock();
64         if (contextRelative) {
65             expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
66             request.getContextPath();
67             rc.setReturnValue("/context");
68         }
69         rc.replay();
70         
71         MockControl mc = MockControl.createControl(HttpServletResponse JavaDoc.class);
72         HttpServletResponse JavaDoc resp = (HttpServletResponse JavaDoc) mc.getMock();
73         resp.encodeRedirectURL(expectedUrlForEncoding);
74         mc.setReturnValue(expectedUrlForEncoding);
75         resp.sendRedirect(expectedUrlForEncoding);
76         mc.setVoidCallable(1);
77         mc.replay();
78         
79         rv.render(map, request, resp);
80         assertTrue(rv.valid);
81         mc.verify();
82     }
83     
84     public void testEmptyMap() throws Exception JavaDoc {
85         String JavaDoc url = "/myUrl";
86         test(new HashMap JavaDoc(), url, false, url);
87     }
88     
89     public void testEmptyMapWithContextRelative() throws Exception JavaDoc {
90         String JavaDoc url = "/myUrl";
91         test(new HashMap JavaDoc(), url, true, url);
92     }
93
94     public void testSingleParam() throws Exception JavaDoc {
95         String JavaDoc url = "http://url.somewhere.com";
96         String JavaDoc key = "foo";
97         String JavaDoc val = "bar";
98         Map JavaDoc m = new HashMap JavaDoc();
99         m.put(key, val);
100         String JavaDoc expectedUrlForEncoding = url + "?" + key + "=" + val;
101         test(m, url, false, expectedUrlForEncoding);
102     }
103     
104     public void testTwoParams() throws Exception JavaDoc {
105         String JavaDoc url = "http://url.somewhere.com";
106         String JavaDoc key = "foo";
107         String JavaDoc val = "bar";
108         String JavaDoc key2 = "thisIsKey2";
109         String JavaDoc val2 = "andThisIsVal2";
110         Map JavaDoc m = new HashMap JavaDoc();
111         m.put(key, val);
112         m.put(key2, val2);
113         String JavaDoc expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2;
114         test(m, url, false, expectedUrlForEncoding);
115     }
116     
117     public void testObjectConversion() throws Exception JavaDoc {
118         String JavaDoc url = "http://url.somewhere.com";
119         String JavaDoc key = "foo";
120         String JavaDoc val = "bar";
121         String JavaDoc key2 = "int2";
122         Object JavaDoc val2 = new Long JavaDoc(611);
123         Map JavaDoc m = new HashMap JavaDoc();
124         m.put(key, val);
125         m.put(key2, val2);
126         String JavaDoc expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2;
127         test(m, url, false, expectedUrlForEncoding);
128     }
129     
130     public void testNoUrlSet() throws Exception JavaDoc {
131         RedirectView rv = new RedirectView();
132         try {
133             rv.afterPropertiesSet();
134             fail("Should have thrown IllegalArgumentException");
135         }
136         catch (IllegalArgumentException JavaDoc ex) {
137             // expected
138
}
139     }
140
141     public void testHttp11() throws Exception JavaDoc {
142         RedirectView rv = new RedirectView();
143         rv.setUrl("http://url.somewhere.com");
144         rv.setHttp10Compatible(false);
145         MockHttpServletRequest request = new MockHttpServletRequest();
146         MockHttpServletResponse response = new MockHttpServletResponse();
147         rv.render(new HashMap JavaDoc(), request, response);
148         assertEquals(303, response.getStatus());
149         assertEquals("http://url.somewhere.com", response.getHeader("Location"));
150     }
151
152 }
153
Popular Tags