1 5 package com.opensymphony.webwork.dispatcher; 6 7 import com.mockobjects.dynamic.C; 8 import com.mockobjects.dynamic.Mock; 9 import com.opensymphony.webwork.ServletActionContext; 10 import com.opensymphony.webwork.WebWorkStatics; 11 import com.opensymphony.webwork.WebWorkTestCase; 12 import com.opensymphony.xwork.ActionContext; 13 import ognl.Ognl; 14 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import java.util.HashMap ; 18 19 20 26 public class ServletRedirectResultTest extends WebWorkTestCase implements WebWorkStatics { 27 29 protected ServletRedirectResult view; 30 private ActionContext oldContext; 31 private Mock requestMock; 32 private Mock responseMock; 33 34 36 public void testAbsoluteRedirect() { 37 view.setLocation("/bar/foo.jsp"); 38 responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp"); 39 responseMock.expect("sendRedirect", C.args(C.eq("/context/bar/foo.jsp"))); 40 41 try { 42 view.execute(null); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 fail(); 46 } 47 } 48 49 public void testPrependServletContextFalse() { 50 view.setLocation("/bar/foo.jsp"); 51 view.setPrependServletContext(false); 52 responseMock.expectAndReturn("encodeRedirectURL", "/bar/foo.jsp", "/bar/foo.jsp"); 53 responseMock.expect("sendRedirect", C.args(C.eq("/bar/foo.jsp"))); 54 55 try { 56 view.execute(null); 57 } catch (Exception e) { 58 e.printStackTrace(); 59 fail(); 60 } 61 } 62 63 public void testRelativeRedirect() throws Exception { 64 view.setLocation("foo.jsp"); 65 requestMock.expectAndReturn("getParameterMap", new HashMap ()); 66 requestMock.expectAndReturn("getServletPath", "/namespace/some.action"); 67 requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null); 68 responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp", "/context/namespace/foo.jsp"); 69 responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp"))); 70 71 view.execute(null); 72 } 73 74 protected void setUp() { 75 view = new ServletRedirectResult(); 76 77 responseMock = new Mock(HttpServletResponse .class); 78 79 requestMock = new Mock(HttpServletRequest .class); 80 requestMock.matchAndReturn("getContextPath", "/context"); 81 oldContext = ActionContext.getContext(); 82 83 ActionContext ac = new ActionContext(Ognl.createDefaultContext(null)); 84 ActionContext.setContext(ac); 85 ServletActionContext.setResponse((HttpServletResponse ) responseMock.proxy()); 86 ServletActionContext.setRequest((HttpServletRequest ) requestMock.proxy()); 87 } 88 89 protected void tearDown() { 90 requestMock.verify(); 91 responseMock.verify(); 92 ActionContext.setContext(oldContext); 93 } 94 } 95 | Popular Tags |