View Javadoc

1   /*
2    * $Id: AbstractTagTest.java 476696 2006-11-19 03:56:18Z tmjee $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.views.jsp;
22  
23  import java.io.File;
24  import java.io.StringWriter;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.jsp.JspWriter;
30  
31  import org.apache.struts2.ServletActionContext;
32  import org.apache.struts2.StrutsTestCase;
33  import org.apache.struts2.TestAction;
34  import org.apache.struts2.dispatcher.ApplicationMap;
35  import org.apache.struts2.dispatcher.Dispatcher;
36  import org.apache.struts2.dispatcher.RequestMap;
37  import org.apache.struts2.dispatcher.SessionMap;
38  
39  import com.mockobjects.dynamic.Mock;
40  import com.opensymphony.xwork2.Action;
41  import com.opensymphony.xwork2.ActionContext;
42  import com.opensymphony.xwork2.inject.Container;
43  import com.opensymphony.xwork2.util.ValueStack;
44  import com.opensymphony.xwork2.util.ValueStackFactory;
45  
46  
47  /***
48   * Base class to extend for unit testing UI Tags.
49   *
50   */
51  public abstract class AbstractTagTest extends StrutsTestCase {
52      protected Action action;
53      protected Map context;
54      protected Map session;
55      protected ValueStack stack;
56  
57      /***
58       * contains the buffer that our unit test will write to.  we can later verify this buffer for correctness.
59       */
60      protected StringWriter writer;
61      protected StrutsMockHttpServletRequest request;
62      protected StrutsMockPageContext pageContext;
63      protected HttpServletResponse response;
64      protected StrutsMockServletContext servletContext;
65      
66      protected Mock mockContainer;
67  
68      /***
69       * Constructs the action that we're going to test against.  For most UI tests, this default action should be enough.
70       * However, simply override getAction to return a custom Action if you need something more sophisticated.
71       *
72       * @return the Action to be added to the ValueStack as part of the unit test
73       */
74      public Action getAction() {
75          return new TestAction();
76      }
77  
78      protected void setUp() throws Exception {
79          super.setUp();
80  
81          /***
82           * create our standard mock objects
83           */
84          action = this.getAction();
85          stack = ValueStackFactory.getFactory().createValueStack();
86          context = stack.getContext();
87          stack.push(action);
88  
89          request = new StrutsMockHttpServletRequest();
90          request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
91          response = new StrutsMockHttpServletResponse();
92          request.setSession(new StrutsMockHttpSession());
93          request.setupGetServletPath("/");
94  
95          writer = new StringWriter();
96  
97          JspWriter jspWriter = new StrutsMockJspWriter(writer);
98  
99          servletContext = new StrutsMockServletContext();
100         servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
101         servletContext.setServletInfo("Resin");
102 
103         pageContext = new StrutsMockPageContext();
104         pageContext.setRequest(request);
105         pageContext.setResponse(response);
106         pageContext.setJspWriter(jspWriter);
107         pageContext.setServletContext(servletContext);
108 
109         mockContainer = new Mock(Container.class);
110         Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap());
111         du.init();
112         Dispatcher.setInstance(du);
113         du.setConfigurationManager(configurationManager);
114         session = new SessionMap(request);
115         Map extraContext = du.createContextMap(new RequestMap(request),
116                 request.getParameterMap(),
117                 session,
118                 new ApplicationMap(pageContext.getServletContext()),
119                 request,
120                 response,
121                 pageContext.getServletContext());
122         // let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
123         // ... but generally we want to just use no locale (let it stay system default)
124         extraContext.remove(ActionContext.LOCALE);
125         stack.getContext().putAll(extraContext);
126 
127         context.put(ServletActionContext.HTTP_REQUEST, request);
128         context.put(ServletActionContext.HTTP_RESPONSE, response);
129         context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
130 
131         ActionContext.setContext(new ActionContext(context));
132     }
133 
134     protected void tearDown() throws Exception {
135         pageContext.verify();
136         request.verify();
137     }
138 }