Add tests for session timeouts

Add unit tests for session timeouts and also added a convenience method
to set the time with a TimeUnit.

Issue: #53667353
pull/9/head
Phillip Webb 11 years ago
parent 100edf9a4b
commit 8fdfe245ca

@ -28,6 +28,7 @@ import java.util.Arrays;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -161,6 +162,11 @@ public abstract class AbstractEmbeddedServletContainerFactory implements
this.sessionTimeout = sessionTimeout; this.sessionTimeout = sessionTimeout;
} }
public void setSessionTimeout(int sessionTimeout, TimeUnit timeUnit) {
Assert.notNull(timeUnit, "TimeUnit must not be null");
this.sessionTimeout = (int) timeUnit.toSeconds(sessionTimeout);
}
/** /**
* @return the session timeout in minutes * @return the session timeout in minutes
*/ */

@ -20,6 +20,7 @@ import java.io.File;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
/** /**
* Simple interface that represents customizations to an * Simple interface that represents customizations to an
@ -59,6 +60,14 @@ public interface ConfigurableEmbeddedServletContainerFactory extends
*/ */
void setSessionTimeout(int sessionTimeout); void setSessionTimeout(int sessionTimeout);
/**
* The session timeout in the specified {@link TimeUnit} (default 30 seconds). If 0 or
* negative then sessions never expire.
* @param sessionTimeout the session timeout
* @param timeUnit the time unit
*/
void setSessionTimeout(int sessionTimeout, TimeUnit timeUnit);
/** /**
* Sets the specific network address that the server should bind to. * Sets the specific network address that the server should bind to.
* @param address the address to set (defaults to {@code null}) * @param address the address to set (defaults to {@code null})

@ -17,17 +17,20 @@
package org.springframework.bootstrap.context.embedded.jetty; package org.springframework.bootstrap.context.embedded.jetty;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactoryTests; import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import static org.mockito.Matchers.*; import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.*; import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link JettyEmbeddedServletContainerFactory} and * Tests for {@link JettyEmbeddedServletContainerFactory} and
@ -38,6 +41,11 @@ import static org.mockito.Mockito.*;
public class JettyEmbeddedServletContainerFactoryTests extends public class JettyEmbeddedServletContainerFactoryTests extends
AbstractEmbeddedServletContainerFactoryTests { AbstractEmbeddedServletContainerFactoryTests {
@Override
protected JettyEmbeddedServletContainerFactory getFactory() {
return new JettyEmbeddedServletContainerFactory();
}
@Test @Test
public void jettyConfigurations() throws Exception { public void jettyConfigurations() throws Exception {
JettyEmbeddedServletContainerFactory factory = getFactory(); JettyEmbeddedServletContainerFactory factory = getFactory();
@ -54,8 +62,29 @@ public class JettyEmbeddedServletContainerFactoryTests extends
} }
} }
@Override @Test
protected JettyEmbeddedServletContainerFactory getFactory() { public void sessionTimeout() throws Exception {
return new JettyEmbeddedServletContainerFactory(); JettyEmbeddedServletContainerFactory factory = getFactory();
factory.setSessionTimeout(10);
assertTimeout(factory, 10);
}
@Test
public void sessionTimeoutInMins() throws Exception {
JettyEmbeddedServletContainerFactory factory = getFactory();
factory.setSessionTimeout(1, TimeUnit.MINUTES);
assertTimeout(factory, 60);
} }
private void assertTimeout(JettyEmbeddedServletContainerFactory factory, int expected) {
this.container = factory.getEmbeddedServletContainer();
JettyEmbeddedServletContainer jettyContainer = (JettyEmbeddedServletContainer) this.container;
Handler[] handlers = jettyContainer.getServer().getChildHandlersByClass(
WebAppContext.class);
WebAppContext webAppContext = (WebAppContext) handlers[0];
int actual = webAppContext.getSessionHandler().getSessionManager()
.getMaxInactiveInterval();
assertThat(actual, equalTo(expected));
}
} }

@ -17,15 +17,18 @@
package org.springframework.bootstrap.context.embedded.tomcat; package org.springframework.bootstrap.context.embedded.tomcat;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener; import org.apache.catalina.LifecycleListener;
import org.apache.catalina.startup.Tomcat;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactoryTests; import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -39,6 +42,11 @@ import static org.mockito.Mockito.mock;
public class TomcatEmbeddedServletContainerFactoryTests extends public class TomcatEmbeddedServletContainerFactoryTests extends
AbstractEmbeddedServletContainerFactoryTests { AbstractEmbeddedServletContainerFactoryTests {
@Override
protected TomcatEmbeddedServletContainerFactory getFactory() {
return new TomcatEmbeddedServletContainerFactory();
}
@Test @Test
public void tomcatListeners() throws Exception { public void tomcatListeners() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory(); TomcatEmbeddedServletContainerFactory factory = getFactory();
@ -55,9 +63,25 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
} }
} }
@Override @Test
protected TomcatEmbeddedServletContainerFactory getFactory() { public void sessionTimeout() throws Exception {
return new TomcatEmbeddedServletContainerFactory(); TomcatEmbeddedServletContainerFactory factory = getFactory();
factory.setSessionTimeout(10);
assertTimeout(factory, 10);
}
@Test
public void sessionTimeoutInMins() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
factory.setSessionTimeout(1, TimeUnit.MINUTES);
assertTimeout(factory, 60);
}
private void assertTimeout(TomcatEmbeddedServletContainerFactory factory, int expected) {
this.container = factory.getEmbeddedServletContainer();
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(context.getSessionTimeout(), equalTo(expected));
} }
// FIXME test valve // FIXME test valve

Loading…
Cancel
Save