This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-1.5.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 347ce2a323667a0d0a252ace206206e700e5eb3f Author: Stefan Seifert <[email protected]> AuthorDate: Thu Sep 3 06:17:31 2015 +0000 SLING-4990 Support HttpSession invalidate, new, lastAccessedTime, maxInteractiveInterval git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1700936 13f79535-47bb-0310-9956-ffa450edef68 --- .../mock/sling/servlet/MockHttpSession.java | 55 ++++++++++++++----- .../testing/mock/sling/servlet/package-info.java | 2 +- .../mock/sling/servlet/MockHttpSessionTest.java | 64 ++++++++++++++++------ 3 files changed, 89 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java index f877441..5ab82fd 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java @@ -37,6 +37,9 @@ public final class MockHttpSession implements HttpSession { private final Map<String, Object> attributeMap = new HashMap<String, Object>(); private final String sessionID = UUID.randomUUID().toString(); private final long creationTime = System.currentTimeMillis(); + private boolean invalidated = false; + private boolean isNew = true; + private int maxActiveInterval = 1800; @Override public ServletContext getServletContext() { @@ -45,12 +48,14 @@ public final class MockHttpSession implements HttpSession { @Override public Object getAttribute(final String name) { + checkInvalidatedState(); return this.attributeMap.get(name); } @SuppressWarnings("unchecked") @Override public Enumeration<String> getAttributeNames() { + checkInvalidatedState(); return IteratorUtils.asEnumeration(this.attributeMap.keySet().iterator()); } @@ -61,68 +66,92 @@ public final class MockHttpSession implements HttpSession { @Override public long getCreationTime() { + checkInvalidatedState(); return this.creationTime; } @Override public Object getValue(final String name) { + checkInvalidatedState(); return getAttribute(name); } @Override public String[] getValueNames() { + checkInvalidatedState(); return this.attributeMap.keySet().toArray(new String[this.attributeMap.keySet().size()]); } @Override public void putValue(final String name, final Object value) { + checkInvalidatedState(); setAttribute(name, value); } @Override public void removeAttribute(final String name) { + checkInvalidatedState(); this.attributeMap.remove(name); } @Override public void removeValue(final String name) { + checkInvalidatedState(); this.attributeMap.remove(name); } @Override public void setAttribute(final String name, final Object value) { + checkInvalidatedState(); this.attributeMap.put(name, value); } - // --- unsupported operations --- @Override - public long getLastAccessedTime() { - throw new UnsupportedOperationException(); + public void invalidate() { + checkInvalidatedState(); + this.invalidated = true; + } + + private void checkInvalidatedState() { + if (invalidated) { + throw new IllegalStateException("Session is already invalidated."); + } + } + + public boolean isInvalidated() { + return invalidated; } @Override - public int getMaxInactiveInterval() { - throw new UnsupportedOperationException(); + public boolean isNew() { + checkInvalidatedState(); + return isNew; + } + + public void setNew(boolean isNew) { + this.isNew = isNew; } @Override - @SuppressWarnings("deprecation") - public javax.servlet.http.HttpSessionContext getSessionContext() { - throw new UnsupportedOperationException(); + public long getLastAccessedTime() { + checkInvalidatedState(); + return creationTime; } @Override - public void invalidate() { - throw new UnsupportedOperationException(); + public int getMaxInactiveInterval() { + return maxActiveInterval; } @Override - public boolean isNew() { - throw new UnsupportedOperationException(); + public void setMaxInactiveInterval(final int interval) { + this.maxActiveInterval = interval; } + // --- unsupported operations --- @Override - public void setMaxInactiveInterval(final int interval) { + @SuppressWarnings("deprecation") + public javax.servlet.http.HttpSessionContext getSessionContext() { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/apache/sling/testing/mock/sling/servlet/package-info.java b/src/main/java/org/apache/sling/testing/mock/sling/servlet/package-info.java index be537b4..88f0f19 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/servlet/package-info.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/servlet/package-info.java @@ -19,5 +19,5 @@ /** * Mock implementation of selected Servlet-related Sling APIs. */ [email protected]("1.1") [email protected]("1.2") package org.apache.sling.testing.mock.sling.servlet; diff --git a/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSessionTest.java b/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSessionTest.java index 85e0612..dda2e41 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSessionTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSessionTest.java @@ -23,52 +23,80 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import javax.servlet.http.HttpSession; - import org.junit.Before; import org.junit.Test; public class MockHttpSessionTest { - private HttpSession httpSession; + private MockHttpSession httpSession; @Before public void setUp() throws Exception { - this.httpSession = new MockHttpSession(); + httpSession = new MockHttpSession(); } @Test public void testServletContext() { - assertNotNull(this.httpSession.getServletContext()); + assertNotNull(httpSession.getServletContext()); } @Test public void testId() { - assertNotNull(this.httpSession.getId()); + assertNotNull(httpSession.getId()); } @Test public void testCreationTime() { - assertNotNull(this.httpSession.getCreationTime()); + assertNotNull(httpSession.getCreationTime()); } @Test public void testAttributes() { - this.httpSession.setAttribute("attr1", "value1"); - assertTrue(this.httpSession.getAttributeNames().hasMoreElements()); - assertEquals("value1", this.httpSession.getAttribute("attr1")); - this.httpSession.removeAttribute("attr1"); - assertFalse(this.httpSession.getAttributeNames().hasMoreElements()); + httpSession.setAttribute("attr1", "value1"); + assertTrue(httpSession.getAttributeNames().hasMoreElements()); + assertEquals("value1", httpSession.getAttribute("attr1")); + httpSession.removeAttribute("attr1"); + assertFalse(httpSession.getAttributeNames().hasMoreElements()); } - @SuppressWarnings("deprecation") @Test public void testValues() { - this.httpSession.putValue("attr1", "value1"); - assertEquals(1, this.httpSession.getValueNames().length); - assertEquals("value1", this.httpSession.getValue("attr1")); - this.httpSession.removeValue("attr1"); - assertEquals(0, this.httpSession.getValueNames().length); + httpSession.putValue("attr1", "value1"); + assertEquals(1, httpSession.getValueNames().length); + assertEquals("value1", httpSession.getValue("attr1")); + httpSession.removeValue("attr1"); + assertEquals(0, httpSession.getValueNames().length); + } + + @Test + public void testInvalidate() { + httpSession.invalidate(); + assertTrue(httpSession.isInvalidated()); + } + + @Test(expected = IllegalStateException.class) + public void testInvalidateStateCheck() { + httpSession.invalidate(); + httpSession.getAttribute("attr1"); + } + + @Test + public void testIsNew() { + assertTrue(httpSession.isNew()); + httpSession.setNew(false); + assertFalse(httpSession.isNew()); + } + + @Test + public void testGetLastAccessedTime() { + assertNotNull(httpSession.getLastAccessedTime()); + } + + @Test + public void testGetMaxInactiveInterval() { + assertTrue(httpSession.getMaxInactiveInterval() > 0); + httpSession.setMaxInactiveInterval(123); + assertEquals(123, httpSession.getMaxInactiveInterval()); } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
