View Javadoc
1   package org.apache.turbine.pipeline;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.util.Vector;
27  
28  import javax.servlet.ServletConfig;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.turbine.Turbine;
33  import org.apache.turbine.TurbineConstants;
34  import org.apache.turbine.test.BaseTestCase;
35  import org.apache.turbine.util.RunData;
36  import org.apache.turbine.util.TurbineConfig;
37  import org.apache.turbine.util.uri.URIConstants;
38  import org.junit.AfterClass;
39  import org.junit.Before;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  
43  /**
44   * Tests TurbinePipeline.
45   *
46   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
47   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
48   * @version $Id: DefaultSessionTimeoutValveTest.java 1606111 2014-06-27
49   *          14:46:47Z gk $
50   */
51  public class DefaultSessionTimeoutValveTest extends BaseTestCase
52  {
53      private static TurbineConfig tc = null;
54      private ServletConfig config = null;
55      private HttpServletRequest request = null;
56      private HttpServletResponse response = null;
57  
58      @BeforeClass
59      public static void init()
60      {
61          tc = new TurbineConfig(
62                  ".",
63                  "/conf/test/CompleteTurbineResources.properties");
64          tc.initialize();
65      }
66  
67      @Before
68      public void setUpBefore() throws Exception
69      {
70          config = mock(ServletConfig.class);
71          request = getMockRequest();
72          response = mock(HttpServletResponse.class);
73      }
74  
75      /**
76       * Tests the Valve.
77       */
78      @Test
79      public void testDefaults() throws Exception
80      {
81          // reset
82          Turbine.getConfiguration().setProperty(TurbineConstants.SESSION_TIMEOUT_KEY,
83                  Integer.valueOf(TurbineConstants.SESSION_TIMEOUT_DEFAULT));
84  
85          Vector<String> v = new Vector<String>();
86          v.add(URIConstants.CGI_ACTION_PARAM);
87          when(request.getParameterNames()).thenReturn(v.elements());
88          when(request.getParameterValues(URIConstants.CGI_ACTION_PARAM)).thenReturn(new String[] { "TestAction" });
89  
90          PipelineData pipelineData = getPipelineData(request, response, config);
91  
92          Pipeline pipeline = new TurbinePipeline();
93  
94          DefaultSessionTimeoutValve valve = new DefaultSessionTimeoutValve();
95          pipeline.addValve(valve);
96  
97          pipeline.invoke(pipelineData);
98  
99          RunData runData = (RunData) pipelineData;
100         assertEquals(0, runData.getSession().getMaxInactiveInterval());
101     }
102 
103     /**
104      * Tests the Valve.
105      */
106     @Test
107     public void testTimeoutSet() throws Exception
108     {
109         Turbine.getConfiguration().setProperty(TurbineConstants.SESSION_TIMEOUT_KEY, "3600");
110         PipelineData pipelineData = getPipelineData(request, response, config);
111 
112         Pipeline pipeline = new TurbinePipeline();
113 
114         DefaultSessionTimeoutValve valve = new DefaultSessionTimeoutValve();
115         pipeline.addValve(valve);
116         pipeline.initialize();
117 
118         pipeline.invoke(pipelineData);
119         RunData runData = (RunData) pipelineData;
120 
121         assertEquals(3600, runData.getSession().getMaxInactiveInterval());
122     }
123 
124     @AfterClass
125     public static void destroy()
126     {
127         tc.dispose();
128     }
129 
130 }