View Javadoc

1   /*
2    * $Id$
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;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.Map;
27  import java.util.logging.ConsoleHandler;
28  import java.util.logging.Formatter;
29  import java.util.logging.Level;
30  import java.util.logging.LogRecord;
31  import java.util.logging.Logger;
32  import java.util.logging.SimpleFormatter;
33  
34  import org.apache.struts2.dispatcher.Dispatcher;
35  import org.apache.struts2.util.StrutsTestCaseHelper;
36  import org.springframework.mock.web.MockServletContext;
37  
38  import com.opensymphony.xwork2.XWorkTestCase;
39  import com.opensymphony.xwork2.util.logging.LoggerFactory;
40  import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory;
41  
42  /***
43   * Base test case for JUnit testing Struts.
44   */
45  public abstract class StrutsTestCase extends XWorkTestCase {
46  
47      static {
48          ConsoleHandler handler = new ConsoleHandler();
49          final SimpleDateFormat df = new SimpleDateFormat("mm:ss.SSS");
50          Formatter formatter = new Formatter() {
51              @Override
52              public String format(LogRecord record) {
53                  StringBuilder sb = new StringBuilder();
54                  sb.append(record.getLevel());
55                  sb.append(':');
56                  for (int x=9-record.getLevel().toString().length(); x>0; x--) {
57                      sb.append(' ');
58                  }
59                  sb.append('[');
60                  sb.append(df.format(new Date(record.getMillis())));
61                  sb.append("] ");
62                  sb.append(formatMessage(record));
63                  sb.append('\n');
64                  return sb.toString();
65              }
66          };
67          handler.setFormatter(formatter);
68          Logger logger = Logger.getLogger("");
69          if (logger.getHandlers().length > 0)
70              logger.removeHandler(logger.getHandlers ()[0]);
71          logger.addHandler(handler);
72          logger.setLevel(Level.WARNING);
73          LoggerFactory.setLoggerFactory(new JdkLoggerFactory());
74      }
75      
76      /***
77       * Sets up the configuration settings, XWork configuration, and
78       * message resources
79       */
80      protected void setUp() throws Exception {
81          super.setUp();
82          initDispatcher(null);
83      }
84      
85      protected Dispatcher initDispatcher(Map<String,String> params) {
86          Dispatcher du = StrutsTestCaseHelper.initDispatcher(new MockServletContext(), params);
87          configurationManager = du.getConfigurationManager();
88          configuration = configurationManager.getConfiguration();
89          container = configuration.getContainer();
90          return du;
91      }
92  
93      protected void tearDown() throws Exception {
94          super.tearDown();
95          StrutsTestCaseHelper.tearDown();
96      }
97  
98  }