Set collectionName attribute in MongoOperationsSessionRepository

Previous to this commit, collectionName could be set in
MongoHttpSessionConfiguration but it was never used. Now, attribute
can be set into MongoOperationsSessionRepository to take effect.

See gh-489
This commit is contained in:
Eddú Meléndez Gonzales
2016-04-27 05:27:02 +10:00
committed by Rob Winch
parent edbf8bf587
commit f973e63fce
2 changed files with 18 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
import org.springframework.util.StringUtils;
/**
* Configuration class registering {@code MongoSessionRepository} bean. To import this
@@ -52,6 +53,9 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
if (this.mongoSessionConverter != null) {
repository.setMongoSessionConverter(this.mongoSessionConverter);
}
if (StringUtils.hasText(this.collectionName)) {
repository.setCollectionName(this.collectionName);
}
return repository;
}

View File

@@ -26,6 +26,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.IndexOperations;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -60,6 +61,9 @@ public class MongoHttpSessionConfigurationTest {
assertThat(session).isNotNull();
assertThat(ReflectionTestUtils.getField(session, "collectionName")).isEqualTo(
"sessions");
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo(
"sessions");
}
@Test
@@ -70,6 +74,10 @@ public class MongoHttpSessionConfigurationTest {
assertThat(session).isNotNull();
assertThat(ReflectionTestUtils.getField(session, "collectionName")).isEqualTo(
"testSessions");
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo(
"testSessions");
}
@Test
@@ -80,6 +88,9 @@ public class MongoHttpSessionConfigurationTest {
assertThat(session).isNotNull();
assertThat(ReflectionTestUtils.getField(session, "collectionName")).isEqualTo(
"customSession");
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo(
"customSession");
}
@Test
@@ -90,6 +101,9 @@ public class MongoHttpSessionConfigurationTest {
assertThat(session).isNotNull();
assertThat(ReflectionTestUtils.getField(session, "maxInactiveIntervalInSeconds")).isEqualTo(
10);
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")).isEqualTo(
10);
}
private void registerAndRefresh(Class<?>... annotatedClasses) {