Skip to content

Commit

Permalink
Align wth Servlet 6.0 API
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona authored and rwinch committed Nov 7, 2022
1 parent d850762 commit ee4df64
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 212 deletions.
2 changes: 1 addition & 1 deletion gradle/dependency-management.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencyManagement {
dependency 'com.zaxxer:HikariCP:5.0.1'
dependency 'edu.umd.cs.mtc:multithreadedtc:1.01'
dependency 'io.lettuce:lettuce-core:6.2.1.RELEASE'
dependency 'jakarta.servlet:jakarta.servlet-api:5.0.0'
dependency 'jakarta.servlet:jakarta.servlet-api:6.0.0'
dependency 'mysql:mysql-connector-java:8.0.30'
dependencySet(group: 'org.apache.derby', version: '10.16.1.1') {
entry 'derby'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 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.
Expand All @@ -19,14 +19,11 @@
import java.time.Duration;
import java.util.Collections;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Set;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionBindingListener;
import jakarta.servlet.http.HttpSessionContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -41,7 +38,6 @@
* @author Vedran Pavic
* @since 1.1
*/
@SuppressWarnings("deprecation")
class HttpSessionAdapter<S extends Session> implements HttpSession {

private static final Log logger = LogFactory.getLog(HttpSessionAdapter.class);
Expand Down Expand Up @@ -101,35 +97,18 @@ public int getMaxInactiveInterval() {
return (int) this.session.getMaxInactiveInterval().getSeconds();
}

@Override
public HttpSessionContext getSessionContext() {
return NOOP_SESSION_CONTEXT;
}

@Override
public Object getAttribute(String name) {
checkState();
return this.session.getAttribute(name);
}

@Override
public Object getValue(String name) {
return getAttribute(name);
}

@Override
public Enumeration<String> getAttributeNames() {
checkState();
return Collections.enumeration(this.session.getAttributeNames());
}

@Override
public String[] getValueNames() {
checkState();
Set<String> attrs = this.session.getAttributeNames();
return attrs.toArray(new String[0]);
}

@Override
public void setAttribute(String name, Object value) {
checkState();
Expand All @@ -156,11 +135,6 @@ public void setAttribute(String name, Object value) {
}
}

@Override
public void putValue(String name, Object value) {
setAttribute(name, value);
}

@Override
public void removeAttribute(String name) {
checkState();
Expand All @@ -176,11 +150,6 @@ public void removeAttribute(String name) {
}
}

@Override
public void removeValue(String name) {
removeAttribute(name);
}

@Override
public void invalidate() {
checkState();
Expand All @@ -203,32 +172,4 @@ private void checkState() {
}
}

private static final HttpSessionContext NOOP_SESSION_CONTEXT = new HttpSessionContext() {

@Override
public HttpSession getSession(String sessionId) {
return null;
}

@Override
public Enumeration<String> getIds() {
return EMPTY_ENUMERATION;
}

};

private static final Enumeration<String> EMPTY_ENUMERATION = new Enumeration<String>() {

@Override
public boolean hasMoreElements() {
return false;
}

@Override
public String nextElement() {
throw new NoSuchElementException("a");
}

};

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -40,7 +39,6 @@
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionBindingListener;
import jakarta.servlet.http.HttpSessionContext;

import org.assertj.core.data.Offset;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -316,52 +314,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
});
}

@Test
void doFilterValue() throws Exception {
final String ATTR = "ATTR";
final String VALUE = "VALUE";
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
wrappedRequest.getSession().putValue(ATTR, VALUE);
assertThat(wrappedRequest.getSession().getValue(ATTR)).isEqualTo(VALUE);
assertThat(Arrays.asList(wrappedRequest.getSession().getValueNames())).containsOnly(ATTR);
}
});

nextRequest();

doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(wrappedRequest.getSession().getValue(ATTR)).isEqualTo(VALUE);
assertThat(Arrays.asList(wrappedRequest.getSession().getValueNames())).containsOnly(ATTR);
}
});

nextRequest();

doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(wrappedRequest.getSession().getValue(ATTR)).isEqualTo(VALUE);

wrappedRequest.getSession().removeValue(ATTR);

assertThat(wrappedRequest.getSession().getValue(ATTR)).isNull();
}
});

nextRequest();

doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(wrappedRequest.getSession().getValue(ATTR)).isNull();
}
});
}

@Test
void doFilterIsNewTrue() throws Exception {
doFilter(new DoInFilter() {
Expand Down Expand Up @@ -637,27 +589,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(session.getSecure()).describedAs("Session Cookie should be marked as Secure").isTrue();
}

@Test
void doFilterSessionContext() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
HttpSessionContext sessionContext = wrappedRequest.getSession().getSessionContext();
assertThat(sessionContext).isNotNull();
assertThat(sessionContext.getSession("a")).isNull();
assertThat(sessionContext.getIds()).isNotNull();
assertThat(sessionContext.getIds().hasMoreElements()).isFalse();

try {
sessionContext.getIds().nextElement();
fail("Expected Exception");
}
catch (NoSuchElementException ignored) {
}
}
});
}

// --- saving

@Test
Expand Down Expand Up @@ -741,23 +672,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
});
}

@Test
void doFilterInvalidateValueIllegalState() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
HttpSession session = wrappedRequest.getSession();
session.invalidate();
try {
session.getValue("attr");
fail("Expected Exception");
}
catch (IllegalStateException ignored) {
}
}
});
}

@Test
void doFilterInvalidateAttributeNamesIllegalState() throws Exception {
doFilter(new DoInFilter() {
Expand All @@ -775,23 +689,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
});
}

@Test
void doFilterInvalidateValueNamesIllegalState() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
HttpSession session = wrappedRequest.getSession();
session.invalidate();
try {
session.getValueNames();
fail("Expected Exception");
}
catch (IllegalStateException ignored) {
}
}
});
}

@Test
void doFilterInvalidateSetAttributeIllegalState() throws Exception {
doFilter(new DoInFilter() {
Expand All @@ -809,23 +706,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
});
}

@Test
void doFilterInvalidatePutValueIllegalState() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
HttpSession session = wrappedRequest.getSession();
session.invalidate();
try {
session.putValue("a", "b");
fail("Expected Exception");
}
catch (IllegalStateException ignored) {
}
}
});
}

@Test
void doFilterInvalidateRemoveAttributeIllegalState() throws Exception {
doFilter(new DoInFilter() {
Expand All @@ -843,23 +723,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
});
}

@Test
void doFilterInvalidateRemoveValueIllegalState() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
HttpSession session = wrappedRequest.getSession();
session.invalidate();
try {
session.removeValue("name");
fail("Expected Exception");
}
catch (IllegalStateException ignored) {
}
}
});
}

@Test
void doFilterInvalidateNewIllegalState() throws Exception {
doFilter(new DoInFilter() {
Expand Down Expand Up @@ -921,20 +784,6 @@ public void doFilter(HttpServletRequest wrappedRequest) {
});
}

@Test
void doFilterInvalidateSessionContext() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
HttpSession session = wrappedRequest.getSession();
session.invalidate();

// no exception
session.getSessionContext();
}
});
}

@Test
void doFilterInvalidateMaxInteractiveInterval() throws Exception {
doFilter(new DoInFilter() {
Expand Down

0 comments on commit ee4df64

Please sign in to comment.