001package org.apache.turbine.pipeline; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.assertEquals; 023import static org.mockito.Mockito.mock; 024import static org.mockito.Mockito.when; 025 026import java.util.Vector; 027 028import javax.servlet.ServletConfig; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031 032import org.apache.turbine.Turbine; 033import org.apache.turbine.TurbineConstants; 034import org.apache.turbine.test.BaseTestCase; 035import org.apache.turbine.util.RunData; 036import org.apache.turbine.util.TurbineConfig; 037import org.apache.turbine.util.uri.URIConstants; 038import org.junit.AfterClass; 039import org.junit.Before; 040import org.junit.BeforeClass; 041import org.junit.Test; 042 043/** 044 * Tests TurbinePipeline. 045 * 046 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a> 047 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 048 * @version $Id: DefaultSessionTimeoutValveTest.java 1606111 2014-06-27 049 * 14:46:47Z gk $ 050 */ 051public class DefaultSessionTimeoutValveTest extends BaseTestCase 052{ 053 private static TurbineConfig tc = null; 054 private ServletConfig config = null; 055 private HttpServletRequest request = null; 056 private HttpServletResponse response = null; 057 058 @BeforeClass 059 public static void init() 060 { 061 tc = new TurbineConfig( 062 ".", 063 "/conf/test/CompleteTurbineResources.properties"); 064 tc.initialize(); 065 } 066 067 @Before 068 public void setUpBefore() throws Exception 069 { 070 config = mock(ServletConfig.class); 071 request = getMockRequest(); 072 response = mock(HttpServletResponse.class); 073 } 074 075 /** 076 * Tests the Valve. 077 */ 078 @Test 079 public void testDefaults() throws Exception 080 { 081 // reset 082 Turbine.getConfiguration().setProperty(TurbineConstants.SESSION_TIMEOUT_KEY, 083 Integer.valueOf(TurbineConstants.SESSION_TIMEOUT_DEFAULT)); 084 085 Vector<String> v = new Vector<String>(); 086 v.add(URIConstants.CGI_ACTION_PARAM); 087 when(request.getParameterNames()).thenReturn(v.elements()); 088 when(request.getParameterValues(URIConstants.CGI_ACTION_PARAM)).thenReturn(new String[] { "TestAction" }); 089 090 PipelineData pipelineData = getPipelineData(request, response, config); 091 092 Pipeline pipeline = new TurbinePipeline(); 093 094 DefaultSessionTimeoutValve valve = new DefaultSessionTimeoutValve(); 095 pipeline.addValve(valve); 096 097 pipeline.invoke(pipelineData); 098 099 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}