View Javadoc

1   package org.apache.turbine.test;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileNotFoundException;
27  import java.util.Properties;
28  import java.util.Vector;
29  
30  import javax.servlet.ServletConfig;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.log4j.PropertyConfigurator;
35  import org.apache.turbine.TurbineConstants;
36  import org.apache.turbine.om.security.User;
37  import org.apache.turbine.pipeline.PipelineData;
38  import org.apache.turbine.services.TurbineServices;
39  import org.apache.turbine.services.rundata.RunDataService;
40  import org.apache.turbine.util.RunData;
41  import org.junit.BeforeClass;
42  
43  import com.mockobjects.servlet.MockHttpServletRequest;
44  
45  /**
46   * Base functionality to be extended by all Apache Turbine test cases.  Test
47   * case implementations are used to automate testing via JUnit.
48   *
49   * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a>
50   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
51   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
52   * @version $Id: BaseTestCase.java 1606111 2014-06-27 14:46:47Z gk $
53   */
54  public abstract class BaseTestCase
55  {
56      static File log4jFile = new File("conf/test/Log4j.properties");
57  
58      @BeforeClass
59      public static void baseInit()
60              throws Exception
61      {
62  
63          Properties p = new Properties();
64          try
65          {
66              p.load(new FileInputStream(log4jFile));
67              p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath());
68              PropertyConfigurator.configure(p);
69  
70          }
71          catch (FileNotFoundException fnf)
72          {
73              System.err.println("Could not open Log4J configuration file "
74                      + log4jFile);
75          }
76      }
77  
78      protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
79          RunDataService rds =
80              (RunDataService) TurbineServices.getInstance().getService(
81                      RunDataService.SERVICE_NAME);
82          RunData runData = rds.getRunData(request, response, config);
83          return runData;
84      }
85      protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
86         RunData runData = getRunData(request,response,config);
87         return runData;
88      }
89  
90  
91      protected MockHttpServletRequest getMockRequest(){
92          EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest();
93          EnhancedMockHttpSession session = new EnhancedMockHttpSession();
94          session.setupGetAttribute(User.SESSION_KEY, null);
95          request.setupServerName("bob");
96          request.setupGetProtocol("http");
97          request.setupScheme("scheme");
98          request.setupPathInfo("damn");
99          request.setupGetServletPath("damn2");
100         request.setupGetContextPath("wow");
101         request.setupGetContentType("html/text");
102         request.setupAddHeader("Content-type", "html/text");
103         request.setupAddHeader("Accept-Language", "en-US");
104         Vector<String> v = new Vector<String>();
105         request.setupGetParameterNames(v.elements());
106         request.setSession(session);
107         return request;
108     }
109 }
110