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.xwork.ActionContext; 12 import junit.framework.TestCase; 13 import ognl.Ognl; 14 15 import javax.servlet.RequestDispatcher ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 20 26 public class ServletDispatcherResultTest extends TestCase implements WebWorkStatics { 27 29 public void testInclude() { 30 ServletDispatcherResult view = new ServletDispatcherResult(); 31 view.setLocation("foo.jsp"); 32 33 Mock dispatcherMock = new Mock(RequestDispatcher .class); 34 dispatcherMock.expect("include", C.ANY_ARGS); 35 36 Mock requestMock = new Mock(HttpServletRequest .class); 37 requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy()); 38 39 Mock responseMock = new Mock(HttpServletResponse .class); 40 responseMock.expectAndReturn("isCommitted", Boolean.TRUE); 41 42 ActionContext ac = new ActionContext(Ognl.createDefaultContext(null)); 43 ActionContext.setContext(ac); 44 ServletActionContext.setRequest((HttpServletRequest ) requestMock.proxy()); 45 ServletActionContext.setResponse((HttpServletResponse ) responseMock.proxy()); 46 47 try { 48 view.execute(null); 49 } catch (Exception e) { 50 e.printStackTrace(); 51 fail(); 52 } 53 54 dispatcherMock.verify(); 55 requestMock.verify(); 56 dispatcherMock.verify(); 57 } 58 59 public void testSimple() { 60 ServletDispatcherResult view = new ServletDispatcherResult(); 61 view.setLocation("foo.jsp"); 62 63 Mock dispatcherMock = new Mock(RequestDispatcher .class); 64 dispatcherMock.expect("forward", C.ANY_ARGS); 65 66 Mock requestMock = new Mock(HttpServletRequest .class); 67 requestMock.expectAndReturn("getAttribute", "javax.servlet.include.servlet_path", null); 68 requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy()); 69 requestMock.expect("setAttribute", C.ANY_ARGS); requestMock.expect("setAttribute", C.ANY_ARGS); requestMock.matchAndReturn("getRequestURI", "foo.jsp"); 72 73 Mock responseMock = new Mock(HttpServletResponse .class); 74 responseMock.expectAndReturn("isCommitted", Boolean.FALSE); 75 76 ActionContext ac = new ActionContext(Ognl.createDefaultContext(null)); 77 ActionContext.setContext(ac); 78 ServletActionContext.setRequest((HttpServletRequest ) requestMock.proxy()); 79 ServletActionContext.setResponse((HttpServletResponse ) responseMock.proxy()); 80 81 try { 82 view.execute(null); 83 } catch (Exception e) { 84 e.printStackTrace(); 85 fail(); 86 } 87 88 dispatcherMock.verify(); 89 requestMock.verify(); 90 dispatcherMock.verify(); 91 } 92 } 93 | Popular Tags |