From 3b9833c538be64cda4285f8e6b62aec7dd16ec8e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 16 Aug 2012 13:38:51 +0200 Subject: [PATCH] Update MockHttpSession across the test suite --- .../mock/web/MockHttpSession.java | 28 ++++++++++--- .../mock/web/MockHttpSession.java | 9 +++- .../mock/web/MockHttpSession.java | 37 ++++++++++++----- .../mock/web/MockHttpSession.java | 41 +++++++++++++------ .../mock/web/MockHttpSession.java | 37 ++++++++++++----- 5 files changed, 109 insertions(+), 43 deletions(-) diff --git a/spring-orm/src/test/java/org/springframework/mock/web/MockHttpSession.java b/spring-orm/src/test/java/org/springframework/mock/web/MockHttpSession.java index a8d81fc81d..bc08077830 100644 --- a/spring-orm/src/test/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-orm/src/test/java/org/springframework/mock/web/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,12 @@ package org.springframework.mock.web; import java.io.Serializable; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Vector; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; @@ -36,14 +36,20 @@ import org.springframework.util.Assert; * *

Compatible with Servlet 2.5 as well as Servlet 3.0. * + *

Used for testing the web framework; also useful for testing application + * controllers. + * * @author Juergen Hoeller * @author Rod Johnson * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ @SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { + public static final String SESSION_COOKIE_NAME = "JSESSION"; + private static int nextId = 1; private final String id; @@ -64,9 +70,9 @@ public class MockHttpSession implements HttpSession { /** - * Create a new MockHttpSession with a default {@link org.springframework.mock.web.MockServletContext}. - * - * @see org.springframework.mock.web.MockServletContext + * Create a new MockHttpSession with a default {@link MockServletContext}. + * + * @see MockServletContext */ public MockHttpSession() { this(null); @@ -135,7 +141,7 @@ public class MockHttpSession implements HttpSession { } public Enumeration getAttributeNames() { - return new Vector(this.attributes.keySet()).elements(); + return Collections.enumeration(this.attributes.keySet()); } public String[] getValueNames() { @@ -186,7 +192,17 @@ public class MockHttpSession implements HttpSession { } } + /** + * Invalidates this session then unbinds any objects bound to it. + * + * @throws IllegalStateException if this method is called on an already invalidated session + */ public void invalidate() { + if (this.invalid) { + throw new IllegalStateException("The session has already been invalidated"); + } + + // else this.invalid = true; clearAttributes(); } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java index 39d771b2f5..bc08077830 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java @@ -17,12 +17,12 @@ package org.springframework.mock.web; import java.io.Serializable; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Vector; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; @@ -36,6 +36,9 @@ import org.springframework.util.Assert; * *

Compatible with Servlet 2.5 as well as Servlet 3.0. * + *

Used for testing the web framework; also useful for testing application + * controllers. + * * @author Juergen Hoeller * @author Rod Johnson * @author Mark Fisher @@ -45,6 +48,8 @@ import org.springframework.util.Assert; @SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { + public static final String SESSION_COOKIE_NAME = "JSESSION"; + private static int nextId = 1; private final String id; @@ -136,7 +141,7 @@ public class MockHttpSession implements HttpSession { } public Enumeration getAttributeNames() { - return new Vector(this.attributes.keySet()).elements(); + return Collections.enumeration(this.attributes.keySet()); } public String[] getValueNames() { diff --git a/spring-web/src/test/java/org/springframework/mock/web/MockHttpSession.java b/spring-web/src/test/java/org/springframework/mock/web/MockHttpSession.java index 757414dbbc..bc08077830 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-web/src/test/java/org/springframework/mock/web/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,23 +33,25 @@ import org.springframework.util.Assert; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. - * Supports the Servlet 2.4 API level. * - *

Used for testing the web framework; also useful for testing - * application controllers. + *

Compatible with Servlet 2.5 as well as Servlet 3.0. + * + *

Used for testing the web framework; also useful for testing application + * controllers. * * @author Juergen Hoeller * @author Rod Johnson * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ +@SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { public static final String SESSION_COOKIE_NAME = "JSESSION"; private static int nextId = 1; - private final String id; private final long creationTime = System.currentTimeMillis(); @@ -69,6 +71,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession with a default {@link MockServletContext}. + * * @see MockServletContext */ public MockHttpSession() { @@ -77,6 +80,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession. + * * @param servletContext the ServletContext that the session runs in */ public MockHttpSession(ServletContext servletContext) { @@ -85,6 +89,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession. + * * @param servletContext the ServletContext that the session runs in * @param id a unique identifier for this session */ @@ -93,7 +98,6 @@ public class MockHttpSession implements HttpSession { this.id = (id != null ? id : Integer.toString(nextId++)); } - public long getCreationTime() { return this.creationTime; } @@ -188,7 +192,17 @@ public class MockHttpSession implements HttpSession { } } + /** + * Invalidates this session then unbinds any objects bound to it. + * + * @throws IllegalStateException if this method is called on an already invalidated session + */ public void invalidate() { + if (this.invalid) { + throw new IllegalStateException("The session has already been invalidated"); + } + + // else this.invalid = true; clearAttributes(); } @@ -205,10 +219,10 @@ public class MockHttpSession implements HttpSession { return this.isNew; } - /** - * Serialize the attributes of this session into an object that can - * be turned into a byte array with standard Java serialization. + * Serialize the attributes of this session into an object that can be + * turned into a byte array with standard Java serialization. + * * @return a representation of this session's serialized state */ public Serializable serializeState() { @@ -233,8 +247,9 @@ public class MockHttpSession implements HttpSession { } /** - * Deserialize the attributes of this session from a state object - * created by {@link #serializeState()}. + * Deserialize the attributes of this session from a state object created by + * {@link #serializeState()}. + * * @param state a representation of this session's serialized state */ @SuppressWarnings("unchecked") diff --git a/spring-webmvc-portlet/src/test/java/org/springframework/mock/web/MockHttpSession.java b/spring-webmvc-portlet/src/test/java/org/springframework/mock/web/MockHttpSession.java index 82d2838382..bc08077830 100644 --- a/spring-webmvc-portlet/src/test/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-webmvc-portlet/src/test/java/org/springframework/mock/web/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,23 +33,25 @@ import org.springframework.util.Assert; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. - * Supports the Servlet 2.4 API level. * - *

Used for testing the web framework; also useful for testing - * application controllers. + *

Compatible with Servlet 2.5 as well as Servlet 3.0. + * + *

Used for testing the web framework; also useful for testing application + * controllers. * * @author Juergen Hoeller * @author Rod Johnson * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ +@SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { public static final String SESSION_COOKIE_NAME = "JSESSION"; private static int nextId = 1; - private final String id; private final long creationTime = System.currentTimeMillis(); @@ -68,8 +70,9 @@ public class MockHttpSession implements HttpSession { /** - * Create a new MockHttpSession with a default {@link org.springframework.mock.web.MockServletContext}. - * @see org.springframework.mock.web.MockServletContext + * Create a new MockHttpSession with a default {@link MockServletContext}. + * + * @see MockServletContext */ public MockHttpSession() { this(null); @@ -77,6 +80,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession. + * * @param servletContext the ServletContext that the session runs in */ public MockHttpSession(ServletContext servletContext) { @@ -85,6 +89,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession. + * * @param servletContext the ServletContext that the session runs in * @param id a unique identifier for this session */ @@ -93,7 +98,6 @@ public class MockHttpSession implements HttpSession { this.id = (id != null ? id : Integer.toString(nextId++)); } - public long getCreationTime() { return this.creationTime; } @@ -188,7 +192,17 @@ public class MockHttpSession implements HttpSession { } } + /** + * Invalidates this session then unbinds any objects bound to it. + * + * @throws IllegalStateException if this method is called on an already invalidated session + */ public void invalidate() { + if (this.invalid) { + throw new IllegalStateException("The session has already been invalidated"); + } + + // else this.invalid = true; clearAttributes(); } @@ -205,10 +219,10 @@ public class MockHttpSession implements HttpSession { return this.isNew; } - /** - * Serialize the attributes of this session into an object that can - * be turned into a byte array with standard Java serialization. + * Serialize the attributes of this session into an object that can be + * turned into a byte array with standard Java serialization. + * * @return a representation of this session's serialized state */ public Serializable serializeState() { @@ -233,8 +247,9 @@ public class MockHttpSession implements HttpSession { } /** - * Deserialize the attributes of this session from a state object - * created by {@link #serializeState()}. + * Deserialize the attributes of this session from a state object created by + * {@link #serializeState()}. + * * @param state a representation of this session's serialized state */ @SuppressWarnings("unchecked") diff --git a/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpSession.java b/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpSession.java index 757414dbbc..bc08077830 100644 --- a/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-webmvc/src/test/java/org/springframework/mock/web/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,23 +33,25 @@ import org.springframework.util.Assert; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. - * Supports the Servlet 2.4 API level. * - *

Used for testing the web framework; also useful for testing - * application controllers. + *

Compatible with Servlet 2.5 as well as Servlet 3.0. + * + *

Used for testing the web framework; also useful for testing application + * controllers. * * @author Juergen Hoeller * @author Rod Johnson * @author Mark Fisher + * @author Sam Brannen * @since 1.0.2 */ +@SuppressWarnings("deprecation") public class MockHttpSession implements HttpSession { public static final String SESSION_COOKIE_NAME = "JSESSION"; private static int nextId = 1; - private final String id; private final long creationTime = System.currentTimeMillis(); @@ -69,6 +71,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession with a default {@link MockServletContext}. + * * @see MockServletContext */ public MockHttpSession() { @@ -77,6 +80,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession. + * * @param servletContext the ServletContext that the session runs in */ public MockHttpSession(ServletContext servletContext) { @@ -85,6 +89,7 @@ public class MockHttpSession implements HttpSession { /** * Create a new MockHttpSession. + * * @param servletContext the ServletContext that the session runs in * @param id a unique identifier for this session */ @@ -93,7 +98,6 @@ public class MockHttpSession implements HttpSession { this.id = (id != null ? id : Integer.toString(nextId++)); } - public long getCreationTime() { return this.creationTime; } @@ -188,7 +192,17 @@ public class MockHttpSession implements HttpSession { } } + /** + * Invalidates this session then unbinds any objects bound to it. + * + * @throws IllegalStateException if this method is called on an already invalidated session + */ public void invalidate() { + if (this.invalid) { + throw new IllegalStateException("The session has already been invalidated"); + } + + // else this.invalid = true; clearAttributes(); } @@ -205,10 +219,10 @@ public class MockHttpSession implements HttpSession { return this.isNew; } - /** - * Serialize the attributes of this session into an object that can - * be turned into a byte array with standard Java serialization. + * Serialize the attributes of this session into an object that can be + * turned into a byte array with standard Java serialization. + * * @return a representation of this session's serialized state */ public Serializable serializeState() { @@ -233,8 +247,9 @@ public class MockHttpSession implements HttpSession { } /** - * Deserialize the attributes of this session from a state object - * created by {@link #serializeState()}. + * Deserialize the attributes of this session from a state object created by + * {@link #serializeState()}. + * * @param state a representation of this session's serialized state */ @SuppressWarnings("unchecked")