View Javadoc

1   package org.apache.turbine.pipeline;
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 java.util.Vector;
25  
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.turbine.Turbine;
29  import org.apache.turbine.TurbineConstants;
30  import org.apache.turbine.om.security.User;
31  import org.apache.turbine.test.BaseTestCase;
32  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
33  import org.apache.turbine.test.EnhancedMockHttpSession;
34  import org.apache.turbine.util.RunData;
35  import org.apache.turbine.util.TurbineConfig;
36  import org.apache.turbine.util.uri.URIConstants;
37  import org.junit.AfterClass;
38  import org.junit.Before;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  
42  import com.mockobjects.servlet.MockHttpServletResponse;
43  import com.mockobjects.servlet.MockServletConfig;
44  
45  import static org.junit.Assert.*;
46  
47  /**
48   * Tests TurbinePipeline.
49   *
50   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
51   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
52   * @version $Id: DefaultSessionTimeoutValveTest.java 1606111 2014-06-27 14:46:47Z gk $
53   */
54  public class DefaultSessionTimeoutValveTest extends BaseTestCase
55  {
56      private static TurbineConfig tc = null;
57      private MockServletConfig config = null;
58      private EnhancedMockHttpServletRequest request = null;
59      private EnhancedMockHttpSession session = null;
60      private HttpServletResponse response = null;
61  
62      
63      @BeforeClass
64      public static void init() {
65          tc = new TurbineConfig(
66                              ".",
67                              "/conf/test/CompleteTurbineResources.properties");
68          tc.initialize();
69      }
70  
71      @Before
72      public void setUpBefore() throws Exception {
73          config = new MockServletConfig();
74          config.setupNoParameters();
75          request = new EnhancedMockHttpServletRequest();
76          request.setupServerName("bob");
77          request.setupGetProtocol("http");
78          request.setupScheme("scheme");
79          request.setupPathInfo("damn");
80          request.setupGetServletPath("damn2");
81          request.setupGetContextPath("wow");
82          request.setupGetContentType("html/text");
83          request.setupAddHeader("Content-type", "html/text");
84          request.setupAddHeader("Accept-Language", "en-US");
85  
86          session = new EnhancedMockHttpSession();
87          response = new MockHttpServletResponse();
88  
89          session.setupGetAttribute(User.SESSION_KEY, null);
90  
91          request.setSession(session);
92  
93      }
94  
95      /**
96       * Tests the Valve.
97       */
98      @Test public void testDefaults() throws Exception
99      {
100         // reset
101         Turbine.getConfiguration().setProperty(TurbineConstants.SESSION_TIMEOUT_KEY,
102                 Integer.valueOf(TurbineConstants.SESSION_TIMEOUT_DEFAULT));
103         Vector<String> v = new Vector<String>();
104         v.add(URIConstants.CGI_ACTION_PARAM);
105         request.setupGetParameterNames(v.elements());
106         request.setupAddParameter(URIConstants.CGI_ACTION_PARAM,"TestAction");
107 
108         PipelineData pipelineData = getPipelineData(request,response,config);
109 
110         Pipeline pipeline = new TurbinePipeline();
111 
112         DefaultSessionTimeoutValve valve = new DefaultSessionTimeoutValve();
113         pipeline.addValve(valve);
114 
115         pipeline.invoke(pipelineData);
116 
117         RunData runData = (RunData)pipelineData;
118         assertEquals(0,runData.getSession().getMaxInactiveInterval());
119 
120 
121     }
122 
123     /**
124      * Tests the Valve.
125      */
126     @Test public void testTimeoutSet() throws Exception
127     {
128 
129         Turbine.getConfiguration().setProperty(TurbineConstants.SESSION_TIMEOUT_KEY,"3600");
130         PipelineData pipelineData = getPipelineData(request,response,config);
131 
132         Pipeline pipeline = new TurbinePipeline();
133 
134         DefaultSessionTimeoutValve valve = new DefaultSessionTimeoutValve();
135         pipeline.addValve(valve);
136 
137         pipeline.invoke(pipelineData);
138         RunData runData = (RunData)pipelineData;
139 
140         assertEquals(3600,runData.getSession().getMaxInactiveInterval());
141 
142 
143     }
144     
145     @AfterClass
146     public static void destroy() {
147         tc.dispose();
148     }
149 
150 
151 
152 }