Skip to content

Commit dfd7e1b

Browse files
committed
Add a test and fixes for problems revealed.
1 parent 170937d commit dfd7e1b

File tree

3 files changed

+123
-22
lines changed

3 files changed

+123
-22
lines changed

app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSession.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,18 @@ public static RollerSession getRollerSession(HttpServletRequest request) {
6666
rollerSession = (RollerSession)session.getAttribute(ROLLER_SESSION);
6767

6868
if (rollerSession == null) {
69+
// Create new session if none exists
6970
rollerSession = new RollerSession();
7071
session.setAttribute(ROLLER_SESSION, rollerSession);
7172
} else if (rollerSession.getAuthenticatedUser() != null) {
72-
RollerSessionManager sessionManager = RollerSessionManager.getInstance();
73-
if (sessionManager.get(rollerSession.getAuthenticatedUser().getUserName()) == null) {
74-
// session not present in cache means that it is invalid
73+
// Check if session is still valid in cache
74+
RollerSessionManager manager = RollerSessionManager.getInstance();
75+
String username = rollerSession.getAuthenticatedUser().getUserName();
76+
if (manager.get(username) == null) {
7577
rollerSession = new RollerSession();
7678
session.setAttribute(ROLLER_SESSION, rollerSession);
7779
}
7880
}
79-
8081
Principal principal = request.getUserPrincipal();
8182

8283
// If we've got a principal but no user object, then attempt to get
@@ -152,17 +153,4 @@ public void setAuthenticatedUser(User authenticatedUser) {
152153
RollerSessionManager sessionManager = RollerSessionManager.getInstance();
153154
sessionManager.register(authenticatedUser.getUserName(), this);
154155
}
155-
156-
private void clearSession(HttpSessionEvent se) {
157-
HttpSession session = se.getSession();
158-
try {
159-
session.removeAttribute(ROLLER_SESSION);
160-
} catch (Exception e) {
161-
if (log.isDebugEnabled()) {
162-
// ignore purge exceptions
163-
log.debug("EXCEPTION PURGING session attributes",e);
164-
}
165-
}
166-
}
167-
168156
}

app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSessionManager.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
public class RollerSessionManager {
1414
private static final Log log = LogFactory.getLog(RollerSessionManager.class);
1515
private static final String CACHE_ID = "roller.session.cache";
16-
1716
private final Cache sessionCache;
1817

1918
public static RollerSessionManager getInstance() {
@@ -24,20 +23,28 @@ private static class SingletonHolder {
2423
private static final RollerSessionManager INSTANCE = new RollerSessionManager();
2524
}
2625

27-
private class SessionCacheHandler extends CacheHandlerAdapter {
28-
public void invalidateUser(User user) {
26+
class SessionCacheHandler extends CacheHandlerAdapter {
27+
@Override
28+
public void invalidate(User user) {
2929
if (user != null && user.getUserName() != null) {
3030
sessionCache.remove(user.getUserName());
3131
}
3232
}
3333
}
3434

35+
/** Testing purpose only */
36+
RollerSessionManager(Cache cache) {
37+
this.sessionCache = cache;
38+
CacheManager.registerHandler(new SessionCacheHandler());
39+
}
40+
3541
private RollerSessionManager() {
3642
Map<String, String> cacheProps = new HashMap<>();
3743
cacheProps.put("id", CACHE_ID);
44+
cacheProps.put("size", "1000"); // Cache up to 1000 sessions
45+
cacheProps.put("timeout", "3600"); // Session timeout in seconds (1 hour)
3846
this.sessionCache = CacheManager.constructCache(null, cacheProps);
39-
SessionCacheHandler cacheHandler = new SessionCacheHandler();
40-
CacheManager.registerHandler(cacheHandler);
47+
CacheManager.registerHandler(new SessionCacheHandler());
4148
}
4249

4350
public void register(String userName, RollerSession session) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. The ASF licenses this file to You
4+
* under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License. For additional information regarding
15+
* copyright in this work, please see the NOTICE file in the top level
16+
* directory of this distribution.
17+
*/
18+
19+
package org.apache.roller.weblogger.ui.core;
20+
21+
import org.apache.roller.weblogger.pojos.User;
22+
import org.apache.roller.weblogger.util.cache.Cache;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertNull;
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.Mockito.*;
30+
31+
class RollerSessionManagerTest {
32+
private RollerSessionManager sessionManager;
33+
private Cache mockCache;
34+
35+
@BeforeEach
36+
void setUp() {
37+
mockCache = mock(Cache.class);
38+
sessionManager = new RollerSessionManager(mockCache);
39+
}
40+
41+
@Test
42+
void testRegisterSession() {
43+
RollerSession mockSession = mock(RollerSession.class);
44+
String userName = "testUser";
45+
46+
sessionManager.register(userName, mockSession);
47+
48+
verify(mockCache).put(userName, mockSession);
49+
}
50+
51+
@Test
52+
void testGetSession() {
53+
RollerSession mockSession = mock(RollerSession.class);
54+
String userName = "testUser";
55+
when(mockCache.get(userName)).thenReturn(mockSession);
56+
57+
RollerSession result = sessionManager.get(userName);
58+
59+
assertEquals(mockSession, result);
60+
verify(mockCache).get(userName);
61+
}
62+
63+
@Test
64+
void testInvalidateSession() {
65+
String userName = "testUser";
66+
67+
sessionManager.invalidate(userName);
68+
69+
verify(mockCache).remove(userName);
70+
}
71+
72+
@Test
73+
void testCacheHandlerInvalidation() {
74+
User mockUser = mock(User.class);
75+
String userName = "testUser";
76+
when(mockUser.getUserName()).thenReturn(userName);
77+
78+
sessionManager.new SessionCacheHandler().invalidate(mockUser);
79+
80+
verify(mockCache).remove(userName);
81+
}
82+
83+
@Test
84+
void testNullInputHandling() {
85+
RollerSession mockSession = mock(RollerSession.class);
86+
87+
sessionManager.register(null, mockSession);
88+
sessionManager.invalidate(null);
89+
sessionManager.get(null);
90+
91+
verify(mockCache, never()).put(any(), any());
92+
verify(mockCache, never()).remove(any());
93+
verify(mockCache, never()).get(any());
94+
}
95+
96+
@Test
97+
void testSessionTimeout() {
98+
String userName = "testUser";
99+
when(mockCache.get(userName)).thenReturn(null);
100+
101+
RollerSession result = sessionManager.get(userName);
102+
103+
assertNull(result);
104+
verify(mockCache).get(userName);
105+
}
106+
}

0 commit comments

Comments
 (0)