View Javadoc
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 static org.mockito.ArgumentMatchers.any;
25  import static org.mockito.ArgumentMatchers.anyInt;
26  import static org.mockito.ArgumentMatchers.anyString;
27  import static org.mockito.Mockito.doAnswer;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.when;
30  
31  import java.io.File;
32  import java.util.HashMap;
33  import java.util.Locale;
34  import java.util.Map;
35  import java.util.Vector;
36  
37  import javax.servlet.ServletConfig;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.http.HttpSession;
41  
42  import org.apache.turbine.pipeline.PipelineData;
43  import org.apache.turbine.services.TurbineServices;
44  import org.apache.turbine.services.rundata.RunDataService;
45  import org.apache.turbine.util.RunData;
46  import org.junit.BeforeClass;
47  
48  /**
49   * Base functionality to be extended by all Apache Turbine test cases.  Test
50   * case implementations are used to automate testing via JUnit.
51   *
52   * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a>
53   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
54   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
55   * @version $Id: BaseTestCase.java 1886259 2021-02-06 16:54:03Z tv $
56   */
57  public abstract class BaseTestCase
58  {
59      static File log4j2File = new File("conf/test/log4j2.xml");
60  
61      @BeforeClass
62      public static void baseInit()
63              throws Exception
64      {
65      	// auto load log4j2 file
66      }
67  
68      protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
69          RunDataService rds =
70              (RunDataService) TurbineServices.getInstance().getService(
71                      RunDataService.SERVICE_NAME);
72          RunData runData = rds.getRunData(request, response, config);
73          return runData;
74      }
75  
76      protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
77         RunData runData = getRunData(request,response,config);
78         return runData;
79      }
80  
81      protected Map<String,Object> attributes = new HashMap<>();
82      protected int maxInactiveInterval = 0;
83  
84      @SuppressWarnings("boxing")
85      protected HttpServletRequest getMockRequest()
86      {
87          HttpServletRequest request = mock(HttpServletRequest.class);
88          HttpSession session = mock(HttpSession.class);
89  
90          doAnswer(invocation -> {
91              String key = (String) invocation.getArguments()[0];
92              return attributes.get(key);
93          }).when(session).getAttribute(anyString());
94  
95          doAnswer(invocation -> {
96              String key = (String) invocation.getArguments()[0];
97              Object value = invocation.getArguments()[1];
98              attributes.put(key, value);
99              return null;
100         }).when(session).setAttribute(anyString(), any());
101 
102         when(session.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
103 
104         doAnswer(invocation -> Integer.valueOf(maxInactiveInterval)).when(session).getMaxInactiveInterval();
105 
106         doAnswer(invocation -> {
107             Integer value = (Integer) invocation.getArguments()[0];
108             maxInactiveInterval = value.intValue();
109             return null;
110         }).when(session).setMaxInactiveInterval(anyInt());
111 
112         when(session.isNew()).thenReturn(true);
113         when(request.getSession()).thenReturn(session);
114 
115         when(request.getServerName()).thenReturn("bob");
116         when(request.getProtocol()).thenReturn("http");
117         when(request.getScheme()).thenReturn("scheme");
118         when(request.getPathInfo()).thenReturn("damn");
119         when(request.getServletPath()).thenReturn("damn2");
120         when(request.getContextPath()).thenReturn("wow");
121         when(request.getContentType()).thenReturn("html/text");
122 
123         when(request.getCharacterEncoding()).thenReturn("US-ASCII");
124         when(request.getServerPort()).thenReturn(8080);
125         when(request.getLocale()).thenReturn(Locale.US);
126 
127         when(request.getHeader("Content-type")).thenReturn("html/text");
128         when(request.getHeader("Accept-Language")).thenReturn("en-US");
129 
130         Vector<String> v = new Vector<>();
131         when(request.getParameterNames()).thenReturn(v.elements());
132         return request;
133     }
134 }
135