e.g. Calendar Search Help
You must enter a value before pressing Search
spring

Class: org.springframework.web.servlet.view.InternalResourceViewTests   ©

 OK to copy?
001 
002 /*
003  * Copyright 2002-2004 the original author or authors.
004  * 
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  * 
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  * 
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  */ 
017 
018 package org.springframework.web.servlet.view;
019 
020 import java.util.HashMap;
021 import java.util.Iterator;
022 import java.util.Set;
023 
024 import javax.servlet.http.HttpServletRequest;
025 
026 import junit.framework.TestCase;
027 import org.easymock.MockControl;
028 
029 import org.springframework.mock.web.MockHttpServletResponse;
030 import org.springframework.mock.web.MockRequestDispatcher;
031 import org.springframework.web.context.WebApplicationContext;
032 import org.springframework.web.util.UrlPathHelper;
033 
034 /**
035  * @author Rod Johnson
036  * @author Juergen Hoeller
037  */
038 public class InternalResourceViewTests extends TestCase {
039 
040     /**
041      * Test that if the url property isn't supplied, view initialization fails.
042      */
043     public void testRejectsNullUrl() throws Exception {
044         MockControl mc = MockControl.createControl(WebApplicationContext.class);
045         WebApplicationContext wac = (WebApplicationContext) mc.getMock();
046         mc.replay();
047     
048         InternalResourceView v = new InternalResourceView();
049         try {
050             v.setApplicationContext(wac);
051             fail("Should be forced to set URL");
052         }
053         catch (IllegalArgumentException ex) {
054             // expected
055         }
056     }
057     
058     public void testForward() throws Exception {
059         HashMap model = new HashMap();
060         Object obj = new Integer(1);
061         model.put("foo", "bar");
062         model.put("I", obj);
063 
064         MockControl wacControl = MockControl.createControl(WebApplicationContext.class);
065         WebApplicationContext wac = (WebApplicationContext) wacControl.getMock();
066         wacControl.replay();
067         
068         String url = "forward-to";
069         
070         MockControl reqControl = MockControl.createControl(HttpServletRequest.class);
071         HttpServletRequest request = (HttpServletRequest) reqControl.getMock();
Rate072         Set keys = model.keySet();
073         for (Iterator iter = keys.iterator(); iter.hasNext();) {
074             String key = (String) iter.next();
075             request.setAttribute(key, model.get(key));
076             reqControl.setVoidCallable(1);
077         }
078 
079         request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE);
080         reqControl.setReturnValue(null);
081         request.getRequestDispatcher(url);
082         reqControl.setReturnValue(new MockRequestDispatcher(url));
083         reqControl.replay();
084         
085         MockHttpServletResponse response = new MockHttpServletResponse();
086         InternalResourceView v = new InternalResourceView();
087         v.setUrl(url);
088         v.setApplicationContext(wac);
089         
090         // Can now try multiple tests
091         v.render(model, request, response);
092         assertEquals(url, response.getForwardedUrl());
093         wacControl.verify();
094         reqControl.verify();
095     }
096     
097     public void testIncludeOnAttribute() throws Exception {
098         HashMap model = new HashMap();
099         Object obj = new Integer(1);
100         model.put("foo", "bar");
101         model.put("I", obj);
102 
103         MockControl wacControl = MockControl.createControl(WebApplicationContext.class);
104         WebApplicationContext wac = (WebApplicationContext) wacControl.getMock();
105         wacControl.replay();
106 
107         String url = "forward-to";
108 
109         MockControl reqControl = MockControl.createControl(HttpServletRequest.class);
110         HttpServletRequest request = (HttpServletRequest) reqControl.getMock();
Rate111         Set keys = model.keySet();
112         for (Iterator iter = keys.iterator(); iter.hasNext();) {
113             String key = (String) iter.next();
114             request.setAttribute(key, model.get(key));
115             reqControl.setVoidCallable(1);
116         }
117 
118         request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE);
119         reqControl.setReturnValue("somepath");
120         request.getRequestDispatcher(url);
121         reqControl.setReturnValue(new MockRequestDispatcher(url));
122         reqControl.replay();
123 
124         MockHttpServletResponse response = new MockHttpServletResponse();
125         InternalResourceView v = new InternalResourceView();
126         v.setUrl(url);
127         v.setApplicationContext(wac);
128 
129         // Can now try multiple tests
130         v.render(model, request, response);
131         assertEquals(url, response.getIncludedUrl());
132         wacControl.verify();
133         reqControl.verify();
134     }
135 
136     public void testIncludeOnCommitted() throws Exception {
137         HashMap model = new HashMap();
138         Object obj = new Integer(1);
139         model.put("foo", "bar");
140         model.put("I", obj);
141 
142         MockControl wacControl = MockControl.createControl(WebApplicationContext.class);
143         WebApplicationContext wac = (WebApplicationContext) wacControl.getMock();
144         wacControl.replay();
145 
146         String url = "forward-to";
147 
148         MockControl reqControl = MockControl.createControl(HttpServletRequest.class);
149         HttpServletRequest request = (HttpServletRequest) reqControl.getMock();
Rate150         Set keys = model.keySet();
151         for (Iterator iter = keys.iterator(); iter.hasNext();) {
152             String key = (String) iter.next();
153             request.setAttribute(key, model.get(key));
154             reqControl.setVoidCallable(1);
155         }
156 
157         request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE);
158         reqControl.setReturnValue(null);
159         request.getRequestDispatcher(url);
160         reqControl.setReturnValue(new MockRequestDispatcher(url));
161         reqControl.replay();
162 
163         MockHttpServletResponse response = new MockHttpServletResponse();
164         response.setCommitted(true);
165         InternalResourceView v = new InternalResourceView();
166         v.setUrl(url);
167         v.setApplicationContext(wac);
168 
169         // Can now try multiple tests
170         v.render(model, request, response);
171         assertEquals(url, response.getIncludedUrl());
172         wacControl.verify();
173         reqControl.verify();
174     }
175 
176 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
72 0% of 0
111 0% of 0
150 0% of 0