From c210a4a3cf756e127168236cc67dc35d04f183e1 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Sat, 21 Jan 2017 22:58:06 +0100 Subject: [PATCH] Prevent NPE in`MongoOperationsSessionRepository` when creating session if max inactive interval is undefined Fixes gh-761 --- .../MongoOperationsSessionRepository.java | 8 +++- ...MongoOperationsSessionRepositoryTests.java | 45 +++++++++++++------ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/spring-session/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java b/spring-session/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java index 9c78caf7..7f6f11e8 100644 --- a/spring-session/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java +++ b/spring-session/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 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. @@ -66,7 +66,11 @@ public class MongoOperationsSessionRepository } public MongoExpiringSession createSession() { - return new MongoExpiringSession(this.maxInactiveIntervalInSeconds); + MongoExpiringSession session = new MongoExpiringSession(); + if (this.maxInactiveIntervalInSeconds != null) { + session.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds); + } + return session; } public void save(MongoExpiringSession session) { diff --git a/spring-session/src/test/java/org/springframework/session/data/mongo/MongoOperationsSessionRepositoryTests.java b/spring-session/src/test/java/org/springframework/session/data/mongo/MongoOperationsSessionRepositoryTests.java index 5e8c440b..1a7c72e8 100644 --- a/spring-session/src/test/java/org/springframework/session/data/mongo/MongoOperationsSessionRepositoryTests.java +++ b/spring-session/src/test/java/org/springframework/session/data/mongo/MongoOperationsSessionRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.session.data.mongo; import java.util.Collections; @@ -44,28 +45,44 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; /** + * Tests for {@link MongoOperationsSessionRepository}. + * * @author Jakub Kubrynski + * @author Vedran Pavic */ @RunWith(MockitoJUnitRunner.class) public class MongoOperationsSessionRepositoryTests { @Mock - MongoOperations mongoOperations; - @Mock - AbstractMongoSessionConverter converter; + private MongoOperations mongoOperations; - MongoOperationsSessionRepository sut; + @Mock + private AbstractMongoSessionConverter converter; + + private MongoOperationsSessionRepository repository; @Before public void setUp() throws Exception { - this.sut = new MongoOperationsSessionRepository(this.mongoOperations); - this.sut.setMongoSessionConverter(this.converter); + this.repository = new MongoOperationsSessionRepository(this.mongoOperations); + this.repository.setMongoSessionConverter(this.converter); } @Test public void shouldCreateSession() throws Exception { // when - ExpiringSession session = this.sut.createSession(); + ExpiringSession session = this.repository.createSession(); + + // then + assertThat(session.getId()).isNotEmpty(); + assertThat(session.getMaxInactiveIntervalInSeconds()) + .isEqualTo(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL); + } + + @Test + public void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() throws Exception { + // when + this.repository.setMaxInactiveIntervalInSeconds(null); + ExpiringSession session = this.repository.createSession(); // then assertThat(session.getId()).isNotEmpty(); @@ -87,7 +104,7 @@ public class MongoOperationsSessionRepositoryTests { .getCollection(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)) .willReturn(collection); // when - this.sut.save(session); + this.repository.save(session); // then verify(collection).save(dbSession); @@ -106,7 +123,7 @@ public class MongoOperationsSessionRepositoryTests { TypeDescriptor.valueOf(MongoExpiringSession.class))).willReturn(session); // when - ExpiringSession retrievedSession = this.sut.getSession(sessionId); + ExpiringSession retrievedSession = this.repository.getSession(sessionId); // then assertThat(retrievedSession).isEqualTo(session); @@ -127,7 +144,7 @@ public class MongoOperationsSessionRepositoryTests { TypeDescriptor.valueOf(MongoExpiringSession.class))).willReturn(session); // when - this.sut.getSession(sessionId); + this.repository.getSession(sessionId); // then verify(this.mongoOperations).remove(any(DBObject.class), @@ -140,7 +157,7 @@ public class MongoOperationsSessionRepositoryTests { String sessionId = UUID.randomUUID().toString(); // when - this.sut.delete(sessionId); + this.repository.delete(sessionId); // then verify(this.mongoOperations).remove(any(DBObject.class), @@ -165,7 +182,7 @@ public class MongoOperationsSessionRepositoryTests { given(this.converter.convert(dbSession, TypeDescriptor.valueOf(DBObject.class), TypeDescriptor.valueOf(MongoExpiringSession.class))).willReturn(session); // when - Map sessionsMap = this.sut + Map sessionsMap = this.repository .findByIndexNameAndIndexValue(principalNameIndexName, "john"); // then @@ -179,7 +196,7 @@ public class MongoOperationsSessionRepositoryTests { String index = "some_not_supported_index_name"; // when - Map sessionsMap = this.sut + Map sessionsMap = this.repository .findByIndexNameAndIndexValue(index, "some_value"); // then