View Javadoc

1   /*
2    * $Id: AbstractTagTest.java 670170 2008-06-21 09:40:34Z hermanns $
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  
22  package org.apache.struts2.views.jsp;
23  
24  import java.io.File;
25  import java.io.StringWriter;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.jsp.JspWriter;
31  
32  import org.apache.struts2.ServletActionContext;
33  import org.apache.struts2.StrutsTestCase;
34  import org.apache.struts2.TestAction;
35  import org.apache.struts2.dispatcher.ApplicationMap;
36  import org.apache.struts2.dispatcher.Dispatcher;
37  import org.apache.struts2.dispatcher.RequestMap;
38  import org.apache.struts2.dispatcher.SessionMap;
39  
40  import com.mockobjects.dynamic.Mock;
41  import com.opensymphony.xwork2.Action;
42  import com.opensymphony.xwork2.ActionContext;
43  import com.opensymphony.xwork2.inject.Container;
44  import com.opensymphony.xwork2.util.ValueStack;
45  import com.opensymphony.xwork2.util.ValueStackFactory;
46  
47  
48  /***
49   * Base class to extend for unit testing UI Tags.
50   *
51   */
52  public abstract class AbstractTagTest extends StrutsTestCase {
53      protected Action action;
54      protected Map context;
55      protected Map session;
56      protected ValueStack stack;
57  
58      /***
59       * contains the buffer that our unit test will write to.  we can later verify this buffer for correctness.
60       */
61      protected StringWriter writer;
62      protected StrutsMockHttpServletRequest request;
63      protected StrutsMockPageContext pageContext;
64      protected HttpServletResponse response;
65      protected StrutsMockServletContext servletContext;
66      
67      protected Mock mockContainer;
68  
69      /***
70       * Constructs the action that we're going to test against.  For most UI tests, this default action should be enough.
71       * However, simply override getAction to return a custom Action if you need something more sophisticated.
72       *
73       * @return the Action to be added to the ValueStack as part of the unit test
74       */
75      public Action getAction() {
76          return new TestAction();
77      }
78  
79      protected void setUp() throws Exception {
80          super.setUp();
81  
82          /***
83           * create our standard mock objects
84           */
85          action = this.getAction();
86          stack = ActionContext.getContext().getValueStack();
87          context = stack.getContext();
88          stack.push(action);
89  
90          request = new StrutsMockHttpServletRequest();
91          request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
92          response = new StrutsMockHttpServletResponse();
93          request.setSession(new StrutsMockHttpSession());
94          request.setupGetServletPath("/");
95  
96          writer = new StringWriter();
97  
98          JspWriter jspWriter = new StrutsMockJspWriter(writer);
99  
100         servletContext = new StrutsMockServletContext();
101         servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
102         servletContext.setServletInfo("Resin");
103 
104         pageContext = new StrutsMockPageContext();
105         pageContext.setRequest(request);
106         pageContext.setResponse(response);
107         pageContext.setJspWriter(jspWriter);
108         pageContext.setServletContext(servletContext);
109 
110         mockContainer = new Mock(Container.class);
111         Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap());
112         du.init();
113         Dispatcher.setInstance(du);
114         du.setConfigurationManager(configurationManager);
115         session = new SessionMap(request);
116         Map<String, Object> extraContext = du.createContextMap(new RequestMap(request),
117                 request.getParameterMap(),
118                 session,
119                 new ApplicationMap(pageContext.getServletContext()),
120                 request,
121                 response,
122                 pageContext.getServletContext());
123         // let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
124         // ... but generally we want to just use no locale (let it stay system default)
125         extraContext.remove(ActionContext.LOCALE);
126         stack.getContext().putAll(extraContext);
127 
128         context.put(ServletActionContext.HTTP_REQUEST, request);
129         context.put(ServletActionContext.HTTP_RESPONSE, response);
130         context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
131 
132         ActionContext.setContext(new ActionContext(context));
133     }
134 
135     protected void tearDown() throws Exception {
136         super.tearDown();
137         pageContext.verify();
138         request.verify();
139         action = null;
140         context = null;
141         session = null;
142         stack = null;
143         writer = null;
144         request = null;
145         pageContext = null;
146         response = null;
147         servletContext = null;
148         mockContainer = null;
149     }
150 }