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 static org.mockito.Matchers.any; 025import static org.mockito.Matchers.anyInt; 026import static org.mockito.Matchers.anyString; 027import static org.mockito.Mockito.doAnswer; 028import static org.mockito.Mockito.mock; 029import static org.mockito.Mockito.when; 030 031import java.io.File; 032import java.io.FileInputStream; 033import java.io.FileNotFoundException; 034import java.util.HashMap; 035import java.util.Locale; 036import java.util.Map; 037import java.util.Properties; 038import java.util.Vector; 039 040import javax.servlet.ServletConfig; 041import javax.servlet.http.HttpServletRequest; 042import javax.servlet.http.HttpServletResponse; 043import javax.servlet.http.HttpSession; 044 045import org.apache.log4j.PropertyConfigurator; 046import org.apache.turbine.TurbineConstants; 047import org.apache.turbine.pipeline.PipelineData; 048import org.apache.turbine.services.TurbineServices; 049import org.apache.turbine.services.rundata.RunDataService; 050import org.apache.turbine.util.RunData; 051import org.junit.BeforeClass; 052import org.mockito.invocation.InvocationOnMock; 053import org.mockito.stubbing.Answer; 054 055/** 056 * Base functionality to be extended by all Apache Turbine test cases. Test 057 * case implementations are used to automate testing via JUnit. 058 * 059 * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a> 060 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 061 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 062 * @version $Id: BaseTestCase.java 1754909 2016-08-02 12:55:35Z tv $ 063 */ 064public abstract class BaseTestCase 065{ 066 static File log4jFile = new File("conf/test/Log4j.properties"); 067 068 @BeforeClass 069 public static void baseInit() 070 throws Exception 071 { 072 073 Properties p = new Properties(); 074 try 075 { 076 p.load(new FileInputStream(log4jFile)); 077 p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath()); 078 PropertyConfigurator.configure(p); 079 080 } 081 catch (FileNotFoundException fnf) 082 { 083 System.err.println("Could not open Log4J configuration file " 084 + log4jFile); 085 } 086 } 087 088 protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception { 089 RunDataService rds = 090 (RunDataService) TurbineServices.getInstance().getService( 091 RunDataService.SERVICE_NAME); 092 RunData runData = rds.getRunData(request, response, config); 093 return runData; 094 } 095 096 protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception { 097 RunData runData = getRunData(request,response,config); 098 return runData; 099 } 100 101 protected Map<String,Object> attributes = new HashMap<String,Object>(); 102 protected int maxInactiveInterval = 0; 103 104 @SuppressWarnings("boxing") 105 protected HttpServletRequest getMockRequest() 106 { 107 HttpServletRequest request = mock(HttpServletRequest.class); 108 HttpSession session = mock(HttpSession.class); 109 110 doAnswer(new Answer<Object>() 111 { 112 @Override 113 public Object answer(InvocationOnMock invocation) throws Throwable 114 { 115 String key = (String) invocation.getArguments()[0]; 116 return attributes.get(key); 117 } 118 }).when(session).getAttribute(anyString()); 119 120 doAnswer(new Answer<Object>() 121 { 122 @Override 123 public Object answer(InvocationOnMock invocation) throws Throwable 124 { 125 String key = (String) invocation.getArguments()[0]; 126 Object value = invocation.getArguments()[1]; 127 attributes.put(key, value); 128 return null; 129 } 130 }).when(session).setAttribute(anyString(), any()); 131 132 when(session.getMaxInactiveInterval()).thenReturn(maxInactiveInterval); 133 134 doAnswer(new Answer<Integer>() 135 { 136 @Override 137 public Integer answer(InvocationOnMock invocation) throws Throwable 138 { 139 return Integer.valueOf(maxInactiveInterval); 140 } 141 }).when(session).getMaxInactiveInterval(); 142 143 doAnswer(new Answer<Object>() 144 { 145 @Override 146 public Object answer(InvocationOnMock invocation) throws Throwable 147 { 148 Integer value = (Integer) invocation.getArguments()[0]; 149 maxInactiveInterval = value.intValue(); 150 return null; 151 } 152 }).when(session).setMaxInactiveInterval(anyInt()); 153 154 when(session.isNew()).thenReturn(true); 155 when(request.getSession()).thenReturn(session); 156 157 when(request.getServerName()).thenReturn("bob"); 158 when(request.getProtocol()).thenReturn("http"); 159 when(request.getScheme()).thenReturn("scheme"); 160 when(request.getPathInfo()).thenReturn("damn"); 161 when(request.getServletPath()).thenReturn("damn2"); 162 when(request.getContextPath()).thenReturn("wow"); 163 when(request.getContentType()).thenReturn("html/text"); 164 165 when(request.getCharacterEncoding()).thenReturn("US-ASCII"); 166 when(request.getServerPort()).thenReturn(8080); 167 when(request.getLocale()).thenReturn(Locale.US); 168 169 when(request.getHeader("Content-type")).thenReturn("html/text"); 170 when(request.getHeader("Accept-Language")).thenReturn("en-US"); 171 172 Vector<String> v = new Vector<String>(); 173 when(request.getParameterNames()).thenReturn(v.elements()); 174 return request; 175 } 176} 177