View Javadoc

1   /*
2    * $Id: StrutsTestCaseHelper.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.util;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts2.dispatcher.Dispatcher;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  import com.opensymphony.xwork2.util.LocalizedTextUtil;
31  import com.opensymphony.xwork2.util.ValueStack;
32  import com.opensymphony.xwork2.util.ValueStackFactory;
33  
34  import javax.servlet.ServletContext;
35  
36  /***
37   * Generic test setup methods to be used with any unit testing framework. 
38   */
39  public class StrutsTestCaseHelper {
40      
41      /***
42       * Sets up the configuration settings, XWork configuration, and
43       * message resources
44       */
45      public static void setUp() throws Exception {
46          LocalizedTextUtil.clearDefaultResourceBundles();
47      }
48      
49      public static Dispatcher initDispatcher(ServletContext ctx, Map<String,String> params) {
50          if (params == null) {
51              params = new HashMap<String,String>();
52          }
53          Dispatcher du = new Dispatcher(ctx, params);
54          du.init();
55          Dispatcher.setInstance(du);
56          
57          // Reset the value stack
58          ValueStack stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack();
59          stack.getContext().put(ActionContext.CONTAINER, du.getContainer());
60          ActionContext.setContext(new ActionContext(stack.getContext()));
61          
62          return du;
63      }
64  
65      public static void tearDown() throws Exception {
66          Dispatcher.setInstance(null);
67          ActionContext.setContext(null);
68      }
69  }