Skip to content

Commit bf139db

Browse files
gregturneleftherias
authored andcommitted
Introduce Spring Session MongoDB
* Migrate the module's code back into this project. * Fold the documentation in. * Update to current Gradle conventions. * Reformat to match styling.
1 parent d10c18e commit bf139db

File tree

63 files changed

+5773
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+5773
-17
lines changed

gradle/dependency-management.gradle

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dependencyManagement {
22
imports {
33
mavenBom 'io.projectreactor:reactor-bom:2020.0.12'
4+
mavenBom 'com.fasterxml.jackson:jackson-bom:2.11.2'
45
mavenBom 'org.junit:junit-bom:5.8.1'
56
mavenBom 'org.springframework:spring-framework-bom:5.3.11'
67
mavenBom 'org.springframework.data:spring-data-bom:2021.1.0-RC1'
@@ -15,6 +16,8 @@ dependencyManagement {
1516
}
1617

1718
dependency 'org.aspectj:aspectjweaver:1.9.7'
19+
dependency 'ch.qos.logback:logback-core:1.2.3'
20+
dependency 'com.google.code.findbugs:jsr305:3.0.2'
1821
dependency 'com.h2database:h2:1.4.200'
1922
dependency 'com.ibm.db2:jcc:11.5.6.0'
2023
dependency 'com.microsoft.sqlserver:mssql-jdbc:9.4.0.jre8'
@@ -28,9 +31,20 @@ dependencyManagement {
2831
dependency 'mysql:mysql-connector-java:8.0.26'
2932
dependency 'org.apache.derby:derby:10.14.2.0'
3033
dependency 'org.assertj:assertj-core:3.21.0'
34+
dependency 'org.hamcrest:hamcrest:2.1'
3135
dependency 'org.hsqldb:hsqldb:2.5.2'
3236
dependency 'org.mariadb.jdbc:mariadb-java-client:2.7.4'
33-
dependency 'org.mockito:mockito-core:4.0.0'
37+
dependencySet(group: 'org.mockito', version: '4.0.0') {
38+
entry 'mockito-core'
39+
entry 'mockito-junit-jupiter'
40+
}
41+
42+
dependencySet(group: 'org.mongodb', version: '4.2.3') {
43+
entry 'mongodb-driver-core'
44+
entry 'mongodb-driver-sync'
45+
entry 'mongodb-driver-reactivestreams'
46+
}
3447
dependency 'org.postgresql:postgresql:42.2.24'
3548
}
3649
}
50+

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins {
1313
rootProject.name = 'spring-session-build'
1414

1515
include 'spring-session-core'
16+
include 'spring-session-data-mongodb'
1617
include 'spring-session-data-redis'
1718
include 'spring-session-docs'
1819
include 'spring-session-hazelcast'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apply plugin: 'io.spring.convention.spring-module'
2+
3+
description = "Spring Session and Spring MongoDB integration"
4+
5+
dependencies {
6+
7+
compile project(':spring-session-core')
8+
9+
// Spring Data MongoDB
10+
11+
compile("org.springframework.data:spring-data-mongodb") {
12+
exclude group: "org.mongodb", module: "mongo-java-driver"
13+
exclude group: "org.slf4j", module: "jcl-over-slf4j"
14+
}
15+
16+
// MongoDB dependencies
17+
18+
optional "org.mongodb:mongodb-driver-core"
19+
testCompile "org.mongodb:mongodb-driver-sync"
20+
testCompile "org.mongodb:mongodb-driver-reactivestreams"
21+
integrationTestCompile "org.testcontainers:mongodb"
22+
23+
// Everything else
24+
25+
compile "com.fasterxml.jackson.core:jackson-databind"
26+
compile "org.springframework.security:spring-security-core"
27+
compile "com.google.code.findbugs:jsr305"
28+
29+
optional "io.projectreactor:reactor-core"
30+
31+
testCompile "org.springframework:spring-web"
32+
testCompile "org.springframework:spring-webflux"
33+
testCompile "org.springframework.security:spring-security-config"
34+
testCompile "org.springframework.security:spring-security-web"
35+
testCompile "org.assertj:assertj-core"
36+
testCompile "org.junit.jupiter:junit-jupiter-engine"
37+
testCompile "org.junit.jupiter:junit-jupiter-params"
38+
testCompile "org.springframework:spring-test"
39+
testCompile "org.hamcrest:hamcrest"
40+
testCompile "ch.qos.logback:logback-core"
41+
testCompile "org.mockito:mockito-core"
42+
testCompile "org.mockito:mockito-junit-jupiter"
43+
testCompile "io.projectreactor:reactor-test"
44+
testCompile "javax.servlet:javax.servlet-api"
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://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.
15+
*/
16+
package org.springframework.session.data.mongo.integration;
17+
18+
import java.lang.reflect.Field;
19+
20+
import org.assertj.core.api.AssertionsForClassTypes;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.core.serializer.DefaultDeserializer;
26+
import org.springframework.core.serializer.support.DeserializingConverter;
27+
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
28+
import org.springframework.session.data.mongo.Assert;
29+
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
30+
import org.springframework.util.ReflectionUtils;
31+
32+
/**
33+
* Verify container's {@link ClassLoader} is injected into session converter (reactive and
34+
* traditional).
35+
*
36+
* @author Greg Turnquist
37+
*/
38+
public abstract class AbstractClassLoaderTest<T> extends AbstractITest {
39+
40+
@Autowired
41+
T sessionRepository;
42+
43+
@Autowired
44+
ApplicationContext applicationContext;
45+
46+
@Test
47+
void verifyContainerClassLoaderLoadedIntoConverter() {
48+
49+
Field mongoSessionConverterField = ReflectionUtils.findField(this.sessionRepository.getClass(),
50+
"mongoSessionConverter");
51+
ReflectionUtils.makeAccessible(
52+
Assert.requireNonNull(mongoSessionConverterField, "mongoSessionConverter must not be null!"));
53+
AbstractMongoSessionConverter sessionConverter = (AbstractMongoSessionConverter) ReflectionUtils
54+
.getField(mongoSessionConverterField, this.sessionRepository);
55+
56+
AssertionsForClassTypes.assertThat(sessionConverter).isInstanceOf(JdkMongoSessionConverter.class);
57+
58+
JdkMongoSessionConverter jdkMongoSessionConverter = (JdkMongoSessionConverter) sessionConverter;
59+
60+
DeserializingConverter deserializingConverter = (DeserializingConverter) extractField(
61+
JdkMongoSessionConverter.class, "deserializer", jdkMongoSessionConverter);
62+
DefaultDeserializer deserializer = (DefaultDeserializer) extractField(DeserializingConverter.class,
63+
"deserializer", deserializingConverter);
64+
ClassLoader classLoader = (ClassLoader) extractField(DefaultDeserializer.class, "classLoader", deserializer);
65+
66+
AssertionsForClassTypes.assertThat(classLoader).isEqualTo(this.applicationContext.getClassLoader());
67+
}
68+
69+
private static Object extractField(Class<?> clazz, String fieldName, Object obj) {
70+
71+
Field field = ReflectionUtils.findField(clazz, fieldName);
72+
ReflectionUtils.makeAccessible(Assert.requireNonNull(field, fieldName + " must not be null!"));
73+
return ReflectionUtils.getField(field, obj);
74+
}
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://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.
15+
*/
16+
package org.springframework.session.data.mongo.integration;
17+
18+
import java.util.UUID;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
23+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
24+
import org.springframework.security.core.authority.AuthorityUtils;
25+
import org.springframework.security.core.context.SecurityContext;
26+
import org.springframework.security.core.context.SecurityContextHolder;
27+
import org.springframework.test.context.junit.jupiter.SpringExtension;
28+
import org.springframework.test.context.web.WebAppConfiguration;
29+
30+
/**
31+
* Base class for repositories integration tests
32+
*
33+
* @author Jakub Kubrynski
34+
*/
35+
@ExtendWith(SpringExtension.class)
36+
@WebAppConfiguration
37+
public abstract class AbstractITest {
38+
39+
protected SecurityContext context;
40+
41+
protected SecurityContext changedContext;
42+
43+
// @Autowired(required = false)
44+
// protected SessionEventRegistry registry;
45+
46+
@BeforeEach
47+
void setup() {
48+
49+
// if (this.registry != null) {
50+
// this.registry.clear();
51+
// }
52+
53+
this.context = SecurityContextHolder.createEmptyContext();
54+
this.context.setAuthentication(new UsernamePasswordAuthenticationToken("username-" + UUID.randomUUID(), "na",
55+
AuthorityUtils.createAuthorityList("ROLE_USER")));
56+
57+
this.changedContext = SecurityContextHolder.createEmptyContext();
58+
this.changedContext.setAuthentication(new UsernamePasswordAuthenticationToken(
59+
"changedContext-" + UUID.randomUUID(), "na", AuthorityUtils.createAuthorityList("ROLE_USER")));
60+
}
61+
62+
}

0 commit comments

Comments
 (0)