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 junit.framework.TestCase;
35  
36  import org.apache.log4j.PropertyConfigurator;
37  import org.apache.turbine.TurbineConstants;
38  import org.apache.turbine.om.security.User;
39  import org.apache.turbine.pipeline.PipelineData;
40  import org.apache.turbine.services.TurbineServices;
41  import org.apache.turbine.services.rundata.RunDataService;
42  import org.apache.turbine.util.RunData;
43  
44  import com.mockobjects.servlet.MockHttpServletRequest;
45  
46  /**
47   * Base functionality to be extended by all Apache Turbine test cases.  Test
48   * case implementations are used to automate testing via JUnit.
49   *
50   * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a>
51   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
52   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
53   * @version $Id: BaseTestCase.java 725441 2008-12-10 21:23:08Z tv $
54   */
55  public abstract class BaseTestCase
56          extends TestCase
57  {
58      File log4jFile = new File("conf/test/Log4j.properties");
59  
60      public BaseTestCase(String name)
61              throws Exception
62      {
63          super(name);
64  
65          Properties p = new Properties();
66          try
67          {
68              p.load(new FileInputStream(log4jFile));
69              p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath());
70              PropertyConfigurator.configure(p);
71  
72          }
73          catch (FileNotFoundException fnf)
74          {
75              System.err.println("Could not open Log4J configuration file "
76                      + log4jFile);
77          }
78  
79  
80      }
81  
82      protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
83          RunDataService rds =
84              (RunDataService) TurbineServices.getInstance().getService(
85                      RunDataService.SERVICE_NAME);
86          RunData runData = rds.getRunData(request, response, config);
87          return runData;
88      }
89      protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
90         RunData runData = getRunData(request,response,config);
91         return runData;
92      }
93  
94  
95      protected MockHttpServletRequest getMockRequest(){
96          EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest();
97          EnhancedMockHttpSession session = new EnhancedMockHttpSession();
98          session.setupGetAttribute(User.SESSION_KEY, null);
99          request.setupServerName("bob");
100         request.setupGetProtocol("http");
101         request.setupScheme("scheme");
102         request.setupPathInfo("damn");
103         request.setupGetServletPath("damn2");
104         request.setupGetContextPath("wow");
105         request.setupGetContentType("html/text");
106         request.setupAddHeader("Content-type", "html/text");
107         request.setupAddHeader("Accept-Language", "en-US");
108         Vector v = new Vector();
109         request.setupGetParameterNames(v.elements());
110         request.setSession(session);
111         return request;
112 
113     }
114 }
115