001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import com.mockobjects.dynamic.C;
008: import com.mockobjects.dynamic.Mock;
009: import com.opensymphony.webwork.ServletActionContext;
010: import com.opensymphony.webwork.WebWorkStatics;
011: import com.opensymphony.webwork.WebWorkTestCase;
012: import com.opensymphony.xwork.ActionContext;
013: import ognl.Ognl;
014:
015: import javax.servlet.RequestDispatcher;
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.http.HttpServletResponse;
018:
019: /**
020: * DOCUMENT ME!
021: *
022: * @author $author$
023: * @version $Revision: 1627 $
024: */
025: public class ServletDispatcherResultTest extends WebWorkTestCase
026: implements WebWorkStatics {
027:
028: public void testInclude() {
029: ServletDispatcherResult view = new ServletDispatcherResult();
030: view.setLocation("foo.jsp");
031:
032: Mock dispatcherMock = new Mock(RequestDispatcher.class);
033: dispatcherMock.expect("include", C.ANY_ARGS);
034:
035: Mock requestMock = new Mock(HttpServletRequest.class);
036: requestMock.expectAndReturn("getRequestDispatcher", C.args(C
037: .eq("foo.jsp")), dispatcherMock.proxy());
038:
039: Mock responseMock = new Mock(HttpServletResponse.class);
040: responseMock.expectAndReturn("isCommitted", Boolean.TRUE);
041:
042: ActionContext ac = new ActionContext(Ognl
043: .createDefaultContext(null));
044: ActionContext.setContext(ac);
045: ServletActionContext
046: .setRequest((HttpServletRequest) requestMock.proxy());
047: ServletActionContext
048: .setResponse((HttpServletResponse) responseMock.proxy());
049:
050: try {
051: view.execute(null);
052: } catch (Exception e) {
053: e.printStackTrace();
054: fail();
055: }
056:
057: dispatcherMock.verify();
058: requestMock.verify();
059: dispatcherMock.verify();
060: }
061:
062: public void testSimple() {
063: ServletDispatcherResult view = new ServletDispatcherResult();
064: view.setLocation("foo.jsp");
065:
066: Mock dispatcherMock = new Mock(RequestDispatcher.class);
067: dispatcherMock.expect("forward", C.ANY_ARGS);
068:
069: Mock requestMock = new Mock(HttpServletRequest.class);
070: requestMock.expectAndReturn("getAttribute",
071: "javax.servlet.include.servlet_path", null);
072: requestMock.expectAndReturn("getRequestDispatcher", C.args(C
073: .eq("foo.jsp")), dispatcherMock.proxy());
074: requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
075: requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
076: requestMock.matchAndReturn("getRequestURI", "foo.jsp");
077:
078: Mock responseMock = new Mock(HttpServletResponse.class);
079: responseMock.expectAndReturn("isCommitted", Boolean.FALSE);
080:
081: ActionContext ac = new ActionContext(Ognl
082: .createDefaultContext(null));
083: ActionContext.setContext(ac);
084: ServletActionContext
085: .setRequest((HttpServletRequest) requestMock.proxy());
086: ServletActionContext
087: .setResponse((HttpServletResponse) responseMock.proxy());
088:
089: try {
090: view.execute(null);
091: } catch (Exception e) {
092: e.printStackTrace();
093: fail();
094: }
095:
096: dispatcherMock.verify();
097: requestMock.verify();
098: dispatcherMock.verify();
099: }
100: }
|