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