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.Matchers.any;
25  import static org.mockito.Matchers.anyInt;
26  import static org.mockito.Matchers.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.io.FileInputStream;
33  import java.io.FileNotFoundException;
34  import java.util.HashMap;
35  import java.util.Locale;
36  import java.util.Map;
37  import java.util.Properties;
38  import java.util.Vector;
39  
40  import javax.servlet.ServletConfig;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import javax.servlet.http.HttpSession;
44  
45  import org.apache.log4j.PropertyConfigurator;
46  import org.apache.turbine.TurbineConstants;
47  import org.apache.turbine.pipeline.PipelineData;
48  import org.apache.turbine.services.TurbineServices;
49  import org.apache.turbine.services.rundata.RunDataService;
50  import org.apache.turbine.util.RunData;
51  import org.junit.BeforeClass;
52  import org.mockito.invocation.InvocationOnMock;
53  import org.mockito.stubbing.Answer;
54  
55  /**
56   * Base functionality to be extended by all Apache Turbine test cases.  Test
57   * case implementations are used to automate testing via JUnit.
58   *
59   * @author <a href="mailto:celkins@scardini.com">Christopher Elkins</a>
60   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
61   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
62   * @version $Id: BaseTestCase.java 1754909 2016-08-02 12:55:35Z tv $
63   */
64  public abstract class BaseTestCase
65  {
66      static File log4jFile = new File("conf/test/Log4j.properties");
67  
68      @BeforeClass
69      public static void baseInit()
70              throws Exception
71      {
72  
73          Properties p = new Properties();
74          try
75          {
76              p.load(new FileInputStream(log4jFile));
77              p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath());
78              PropertyConfigurator.configure(p);
79  
80          }
81          catch (FileNotFoundException fnf)
82          {
83              System.err.println("Could not open Log4J configuration file "
84                      + log4jFile);
85          }
86      }
87  
88      protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
89          RunDataService rds =
90              (RunDataService) TurbineServices.getInstance().getService(
91                      RunDataService.SERVICE_NAME);
92          RunData runData = rds.getRunData(request, response, config);
93          return runData;
94      }
95  
96      protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
97         RunData runData = getRunData(request,response,config);
98         return runData;
99      }
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