1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 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 39 public class RedirectViewTests extends TestCase { 40 41 private void test(final Map map, String url, boolean contextRelative, String expectedUrlForEncoding) 42 throws Exception { 43 44 class TestRedirectView extends RedirectView { 45 public boolean valid; 46 47 50 protected Map queryProperties(Map model) { 51 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 .class); 63 HttpServletRequest request = (HttpServletRequest ) 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 .class); 72 HttpServletResponse resp = (HttpServletResponse ) 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 { 85 String url = "/myUrl"; 86 test(new HashMap (), url, false, url); 87 } 88 89 public void testEmptyMapWithContextRelative() throws Exception { 90 String url = "/myUrl"; 91 test(new HashMap (), url, true, url); 92 } 93 94 public void testSingleParam() throws Exception { 95 String url = "http://url.somewhere.com"; 96 String key = "foo"; 97 String val = "bar"; 98 Map m = new HashMap (); 99 m.put(key, val); 100 String expectedUrlForEncoding = url + "?" + key + "=" + val; 101 test(m, url, false, expectedUrlForEncoding); 102 } 103 104 public void testTwoParams() throws Exception { 105 String url = "http://url.somewhere.com"; 106 String key = "foo"; 107 String val = "bar"; 108 String key2 = "thisIsKey2"; 109 String val2 = "andThisIsVal2"; 110 Map m = new HashMap (); 111 m.put(key, val); 112 m.put(key2, val2); 113 String expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2; 114 test(m, url, false, expectedUrlForEncoding); 115 } 116 117 public void testObjectConversion() throws Exception { 118 String url = "http://url.somewhere.com"; 119 String key = "foo"; 120 String val = "bar"; 121 String key2 = "int2"; 122 Object val2 = new Long (611); 123 Map m = new HashMap (); 124 m.put(key, val); 125 m.put(key2, val2); 126 String expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2; 127 test(m, url, false, expectedUrlForEncoding); 128 } 129 130 public void testNoUrlSet() throws Exception { 131 RedirectView rv = new RedirectView(); 132 try { 133 rv.afterPropertiesSet(); 134 fail("Should have thrown IllegalArgumentException"); 135 } 136 catch (IllegalArgumentException ex) { 137 } 139 } 140 141 public void testHttp11() throws Exception { 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 (), request, response); 148 assertEquals(303, response.getStatus()); 149 assertEquals("http://url.somewhere.com", response.getHeader("Location")); 150 } 151 152 } 153 | Popular Tags |