KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > AbstractTagTest


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

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.webwork.TestAction;
9 import com.opensymphony.webwork.config.Configuration;
10 import com.opensymphony.webwork.views.velocity.AbstractTagDirective;
11 import com.opensymphony.xwork.Action;
12 import com.opensymphony.xwork.ActionContext;
13 import com.opensymphony.xwork.util.OgnlValueStack;
14 import junit.framework.TestCase;
15
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.jsp.JspWriter JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23
24 /**
25  * AbstractTagTest
26  * User: jcarreira
27  * Created: Oct 17, 2003 10:24:34 PM
28  */

29 public abstract class AbstractTagTest extends TestCase {
30     //~ Instance fields ////////////////////////////////////////////////////////
31

32     protected Action action;
33     protected Map JavaDoc context;
34     protected Map JavaDoc session;
35     protected OgnlValueStack stack;
36
37     /**
38      * contains the buffer that our unit test will write to. we can later verify this buffer for correctness.
39      */

40     protected StringWriter JavaDoc writer;
41     protected WebWorkMockHttpServletRequest request;
42     protected WebWorkMockPageContext pageContext;
43     protected HttpServletResponse JavaDoc response;
44
45     //~ Constructors ///////////////////////////////////////////////////////////
46

47     public AbstractTagTest() {
48         super();
49     }
50
51     public AbstractTagTest(String JavaDoc s) {
52         super(s);
53     }
54
55     //~ Methods ////////////////////////////////////////////////////////////////
56

57     /**
58      * Constructs the action that we're going to test against. For most UI tests, this default action should be enough.
59      * However, simply override getAction to return a custom Action if you need something more sophisticated.
60      *
61      * @return the Action to be added to the OgnlValueStack as part of the unit test
62      */

63     public Action getAction() {
64         return new TestAction();
65     }
66
67     protected void setUp() throws Exception JavaDoc {
68         super.setUp();
69
70         /**
71          * create our standard mock objects
72          */

73         action = this.getAction();
74         stack = new OgnlValueStack();
75         context = stack.getContext();
76         stack.push(action);
77
78         request = new WebWorkMockHttpServletRequest();
79         request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
80         response = new WebWorkMockHttpServletResponse();
81         request.setSession(new WebWorkMockHttpSession());
82
83         writer = new StringWriter JavaDoc();
84
85         JspWriter JavaDoc jspWriter = new WebWorkMockJspWriter(writer);
86         context.put(AbstractTagDirective.VELOCITY_WRITER, writer);
87
88         WebWorkMockServletContext servletContext = new WebWorkMockServletContext();
89         servletContext.setRealPath(new File JavaDoc("nosuchfile.properties").getAbsolutePath());
90         servletContext.setServletInfo("Resin");
91
92         pageContext = new WebWorkMockPageContext();
93         pageContext.setRequest(request);
94         pageContext.setResponse(response);
95         pageContext.setJspWriter(jspWriter);
96         pageContext.setServletContext(servletContext);
97
98         context.put(ServletActionContext.HTTP_REQUEST, request);
99         context.put(ServletActionContext.HTTP_RESPONSE, response);
100         context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
101
102         session = new HashMap JavaDoc();
103
104         ActionContext.setContext(new ActionContext(context));
105
106         Configuration.setConfiguration(null);
107     }
108
109     protected void tearDown() throws Exception JavaDoc {
110         pageContext.verify();
111         request.verify();
112     }
113 }
114
Popular Tags