001package 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 024import java.io.File; 025import java.io.FileInputStream; 026import java.io.FileNotFoundException; 027import java.util.Properties; 028import java.util.Vector; 029 030import javax.servlet.ServletConfig; 031import javax.servlet.http.HttpServletRequest; 032import javax.servlet.http.HttpServletResponse; 033 034import org.apache.log4j.PropertyConfigurator; 035import org.apache.turbine.TurbineConstants; 036import org.apache.turbine.om.security.User; 037import org.apache.turbine.pipeline.PipelineData; 038import org.apache.turbine.services.TurbineServices; 039import org.apache.turbine.services.rundata.RunDataService; 040import org.apache.turbine.util.RunData; 041import org.junit.BeforeClass; 042 043import com.mockobjects.servlet.MockHttpServletRequest; 044 045/** 046 * Base functionality to be extended by all Apache Turbine test cases. Test 047 * case implementations are used to automate testing via JUnit. 048 * 049 * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a> 050 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 051 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 052 * @version $Id: BaseTestCase.java 1606111 2014-06-27 14:46:47Z gk $ 053 */ 054public abstract class BaseTestCase 055{ 056 static File log4jFile = new File("conf/test/Log4j.properties"); 057 058 @BeforeClass 059 public static void baseInit() 060 throws Exception 061 { 062 063 Properties p = new Properties(); 064 try 065 { 066 p.load(new FileInputStream(log4jFile)); 067 p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath()); 068 PropertyConfigurator.configure(p); 069 070 } 071 catch (FileNotFoundException fnf) 072 { 073 System.err.println("Could not open Log4J configuration file " 074 + log4jFile); 075 } 076 } 077 078 protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception { 079 RunDataService rds = 080 (RunDataService) TurbineServices.getInstance().getService( 081 RunDataService.SERVICE_NAME); 082 RunData runData = rds.getRunData(request, response, config); 083 return runData; 084 } 085 protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception { 086 RunData runData = getRunData(request,response,config); 087 return runData; 088 } 089 090 091 protected MockHttpServletRequest getMockRequest(){ 092 EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest(); 093 EnhancedMockHttpSession session = new EnhancedMockHttpSession(); 094 session.setupGetAttribute(User.SESSION_KEY, null); 095 request.setupServerName("bob"); 096 request.setupGetProtocol("http"); 097 request.setupScheme("scheme"); 098 request.setupPathInfo("damn"); 099 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