View Javadoc

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