001 package org.apache.turbine.test; 002 003 004 /* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024 import java.io.File; 025 import java.io.FileInputStream; 026 import java.io.FileNotFoundException; 027 import java.util.Properties; 028 import java.util.Vector; 029 030 import javax.servlet.ServletConfig; 031 import javax.servlet.http.HttpServletRequest; 032 import javax.servlet.http.HttpServletResponse; 033 034 import junit.framework.TestCase; 035 036 import org.apache.log4j.PropertyConfigurator; 037 import org.apache.turbine.TurbineConstants; 038 import org.apache.turbine.om.security.User; 039 import org.apache.turbine.pipeline.PipelineData; 040 import org.apache.turbine.services.TurbineServices; 041 import org.apache.turbine.services.rundata.RunDataService; 042 import org.apache.turbine.util.RunData; 043 044 import com.mockobjects.servlet.MockHttpServletRequest; 045 046 /** 047 * Base functionality to be extended by all Apache Turbine test cases. Test 048 * case implementations are used to automate testing via JUnit. 049 * 050 * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a> 051 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 052 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 053 * @version $Id: BaseTestCase.java 725441 2008-12-10 21:23:08Z tv $ 054 */ 055 public abstract class BaseTestCase 056 extends TestCase 057 { 058 File log4jFile = new File("conf/test/Log4j.properties"); 059 060 public BaseTestCase(String name) 061 throws Exception 062 { 063 super(name); 064 065 Properties p = new Properties(); 066 try 067 { 068 p.load(new FileInputStream(log4jFile)); 069 p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath()); 070 PropertyConfigurator.configure(p); 071 072 } 073 catch (FileNotFoundException fnf) 074 { 075 System.err.println("Could not open Log4J configuration file " 076 + log4jFile); 077 } 078 079 080 } 081 082 protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception { 083 RunDataService rds = 084 (RunDataService) TurbineServices.getInstance().getService( 085 RunDataService.SERVICE_NAME); 086 RunData runData = rds.getRunData(request, response, config); 087 return runData; 088 } 089 protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception { 090 RunData runData = getRunData(request,response,config); 091 return runData; 092 } 093 094 095 protected MockHttpServletRequest getMockRequest(){ 096 EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest(); 097 EnhancedMockHttpSession session = new EnhancedMockHttpSession(); 098 session.setupGetAttribute(User.SESSION_KEY, null); 099 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