KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ServletRedirectResultTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19
20 /**
21  * DOCUMENT ME!
22  *
23  * @author $author$
24  * @version $Revision: 1.11 $
25  */

26 public class ServletRedirectResultTest extends WebWorkTestCase implements WebWorkStatics {
27     //~ Instance fields ////////////////////////////////////////////////////////
28

29     protected ServletRedirectResult view;
30     private ActionContext oldContext;
31     private Mock requestMock;
32     private Mock responseMock;
33
34     //~ Methods ////////////////////////////////////////////////////////////////
35

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 JavaDoc 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 JavaDoc e) {
58             e.printStackTrace();
59             fail();
60         }
61     }
62
63     public void testRelativeRedirect() throws Exception JavaDoc {
64         view.setLocation("foo.jsp");
65         requestMock.expectAndReturn("getParameterMap", new HashMap JavaDoc());
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 JavaDoc.class);
78
79         requestMock = new Mock(HttpServletRequest JavaDoc.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 JavaDoc) responseMock.proxy());
86         ServletActionContext.setRequest((HttpServletRequest JavaDoc) requestMock.proxy());
87     }
88
89     protected void tearDown() {
90         requestMock.verify();
91         responseMock.verify();
92         ActionContext.setContext(oldContext);
93     }
94 }
95
Popular Tags