1 2 17 18 package org.springframework.web.servlet.view; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 26 import junit.framework.TestCase; 27 import org.easymock.MockControl; 28 29 import org.springframework.mock.web.MockHttpServletResponse; 30 import org.springframework.mock.web.MockRequestDispatcher; 31 import org.springframework.web.context.WebApplicationContext; 32 import org.springframework.web.util.UrlPathHelper; 33 34 38 public class InternalResourceViewTests extends TestCase { 39 40 43 public void testRejectsNullUrl() throws Exception { 44 InternalResourceView view = new InternalResourceView(); 45 try { 46 view.afterPropertiesSet(); 47 fail("Should be forced to set URL"); 48 } 49 catch (IllegalArgumentException ex) { 50 } 52 } 53 54 public void testForward() throws Exception { 55 HashMap model = new HashMap (); 56 Object obj = new Integer (1); 57 model.put("foo", "bar"); 58 model.put("I", obj); 59 60 MockControl wacControl = MockControl.createControl(WebApplicationContext.class); 61 WebApplicationContext wac = (WebApplicationContext) wacControl.getMock(); 62 wacControl.replay(); 63 64 String url = "forward-to"; 65 66 MockControl reqControl = MockControl.createControl(HttpServletRequest .class); 67 HttpServletRequest request = (HttpServletRequest ) reqControl.getMock(); 68 Set keys = model.keySet(); 69 for (Iterator iter = keys.iterator(); iter.hasNext();) { 70 String key = (String ) iter.next(); 71 request.setAttribute(key, model.get(key)); 72 reqControl.setVoidCallable(1); 73 } 74 75 request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE); 76 reqControl.setReturnValue(null); 77 request.getRequestDispatcher(url); 78 reqControl.setReturnValue(new MockRequestDispatcher(url)); 79 reqControl.replay(); 80 81 MockHttpServletResponse response = new MockHttpServletResponse(); 82 InternalResourceView v = new InternalResourceView(); 83 v.setUrl(url); 84 v.setApplicationContext(wac); 85 86 v.render(model, request, response); 88 assertEquals(url, response.getForwardedUrl()); 89 wacControl.verify(); 90 reqControl.verify(); 91 } 92 93 public void testIncludeOnAttribute() throws Exception { 94 HashMap model = new HashMap (); 95 Object obj = new Integer (1); 96 model.put("foo", "bar"); 97 model.put("I", obj); 98 99 MockControl wacControl = MockControl.createControl(WebApplicationContext.class); 100 WebApplicationContext wac = (WebApplicationContext) wacControl.getMock(); 101 wacControl.replay(); 102 103 String url = "forward-to"; 104 105 MockControl reqControl = MockControl.createControl(HttpServletRequest .class); 106 HttpServletRequest request = (HttpServletRequest ) reqControl.getMock(); 107 Set keys = model.keySet(); 108 for (Iterator iter = keys.iterator(); iter.hasNext();) { 109 String key = (String ) iter.next(); 110 request.setAttribute(key, model.get(key)); 111 reqControl.setVoidCallable(1); 112 } 113 114 request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE); 115 reqControl.setReturnValue("somepath"); 116 request.getRequestDispatcher(url); 117 reqControl.setReturnValue(new MockRequestDispatcher(url)); 118 reqControl.replay(); 119 120 MockHttpServletResponse response = new MockHttpServletResponse(); 121 InternalResourceView v = new InternalResourceView(); 122 v.setUrl(url); 123 v.setApplicationContext(wac); 124 125 v.render(model, request, response); 127 assertEquals(url, response.getIncludedUrl()); 128 wacControl.verify(); 129 reqControl.verify(); 130 } 131 132 public void testIncludeOnCommitted() throws Exception { 133 HashMap model = new HashMap (); 134 Object obj = new Integer (1); 135 model.put("foo", "bar"); 136 model.put("I", obj); 137 138 MockControl wacControl = MockControl.createControl(WebApplicationContext.class); 139 WebApplicationContext wac = (WebApplicationContext) wacControl.getMock(); 140 wacControl.replay(); 141 142 String url = "forward-to"; 143 144 MockControl reqControl = MockControl.createControl(HttpServletRequest .class); 145 HttpServletRequest request = (HttpServletRequest ) reqControl.getMock(); 146 Set keys = model.keySet(); 147 for (Iterator iter = keys.iterator(); iter.hasNext();) { 148 String key = (String ) iter.next(); 149 request.setAttribute(key, model.get(key)); 150 reqControl.setVoidCallable(1); 151 } 152 153 request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE); 154 reqControl.setReturnValue(null); 155 request.getRequestDispatcher(url); 156 reqControl.setReturnValue(new MockRequestDispatcher(url)); 157 reqControl.replay(); 158 159 MockHttpServletResponse response = new MockHttpServletResponse(); 160 response.setCommitted(true); 161 InternalResourceView v = new InternalResourceView(); 162 v.setUrl(url); 163 v.setApplicationContext(wac); 164 165 v.render(model, request, response); 167 assertEquals(url, response.getIncludedUrl()); 168 wacControl.verify(); 169 reqControl.verify(); 170 } 171 172 } 173 | Popular Tags |