Skip to content

Commit d50d766

Browse files
committed
Add dedicated support for Spring Session modules
Closes gh-455
1 parent cf93d5d commit d50d766

File tree

3 files changed

+232
-2
lines changed

3 files changed

+232
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2012-2017 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+
* 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.
15+
*/
16+
17+
package io.spring.initializr.service.extension;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import io.spring.initializr.generator.ProjectRequest;
23+
import io.spring.initializr.generator.ProjectRequestPostProcessor;
24+
import io.spring.initializr.metadata.Dependency;
25+
import io.spring.initializr.metadata.InitializrMetadata;
26+
import io.spring.initializr.util.Version;
27+
28+
import org.springframework.stereotype.Component;
29+
30+
/**
31+
* A {@link ProjectRequestPostProcessor} that provides explicit handling for the modules
32+
* introduced in Spring Session 2.
33+
*
34+
* @author Stephane Nicoll
35+
*/
36+
@Component
37+
public class SpringSessionRequestPostProcessor implements ProjectRequestPostProcessor {
38+
39+
private static final Version VERSION_2_0_0_M3 = Version.parse("2.0.0.M3");
40+
41+
static final Dependency REDIS = Dependency.withId("session-data-redis",
42+
"org.springframework.session", "spring-session-data-redis");
43+
44+
static final Dependency JDBC = Dependency.withId("session-jdbc",
45+
"org.springframework.session", "spring-session-jdbc");
46+
47+
48+
@Override
49+
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
50+
Version requestVersion = Version.safeParse(request.getBootVersion());
51+
if (VERSION_2_0_0_M3.compareTo(requestVersion) <= 0) {
52+
swapSpringSessionDepenendency(request);
53+
}
54+
}
55+
56+
private void swapSpringSessionDepenendency(ProjectRequest request) {
57+
Dependency session = getDependency(request, "session");
58+
if (session != null) {
59+
List<Dependency> swap = new ArrayList<>();
60+
if (hasDependency(request, "data-redis")
61+
|| hasDependency(request, "data-redis-reactive")) {
62+
swap.add(REDIS);
63+
}
64+
if (hasDependency(request, "jdbc")) {
65+
swap.add(JDBC);
66+
}
67+
if (!swap.isEmpty()) {
68+
request.getResolvedDependencies().remove(session);
69+
request.getResolvedDependencies().addAll(swap);
70+
}
71+
}
72+
}
73+
74+
private boolean hasDependency(ProjectRequest request, String id) {
75+
return getDependency(request, id) != null;
76+
}
77+
78+
private Dependency getDependency(ProjectRequest request, String id) {
79+
return request.getResolvedDependencies().stream()
80+
.filter(d -> id.equals(d.getId())).findFirst().orElse(null);
81+
}
82+
83+
}

initializr-service/src/main/resources/application.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,13 @@ initializr:
189189
- name: Session
190190
id: session
191191
groupId: org.springframework.session
192-
artifactId: spring-session
192+
artifactId: spring-session-core
193193
description: API and implementations for managing a user’s session information
194-
versionRange: "[1.3.0.RELEASE,2.0.0.M2]"
194+
versionRange: "1.3.0.RELEASE"
195195
starter: false
196+
mappings:
197+
- versionRange: "[1.3.0.RELEASE,2.0.0.M2]"
198+
artifactId: spring-session
196199
- name: Retry
197200
id: retry
198201
groupId: org.springframework.retry
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2012-2017 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+
* 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.
15+
*/
16+
17+
package io.spring.initializr.service.extension;
18+
19+
import io.spring.initializr.generator.ProjectRequest;
20+
import org.junit.Test;
21+
22+
import static io.spring.initializr.service.extension.SpringSessionRequestPostProcessor.JDBC;
23+
import static io.spring.initializr.service.extension.SpringSessionRequestPostProcessor.REDIS;
24+
25+
/**
26+
* Tests for {@link SpringSessionRequestPostProcessor}.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
public class SpringSessionRequestPostProcessorTests
31+
extends AbstractRequestPostProcessorTests {
32+
33+
@Test
34+
public void sessionWithSpringBoot15() {
35+
ProjectRequest request = createProjectRequest("session");
36+
request.setBootVersion("1.5.4.RELEASE");
37+
generateMavenPom(request)
38+
.hasDependency("org.springframework.session", "spring-session")
39+
.hasSpringBootStarterRootDependency()
40+
.hasSpringBootStarterTest()
41+
.hasDependenciesCount(3);
42+
}
43+
44+
@Test
45+
public void sessionWithRedisAndSpringBoot15() {
46+
ProjectRequest request = createProjectRequest("session", "data-redis");
47+
request.setBootVersion("1.5.4.RELEASE");
48+
generateMavenPom(request)
49+
.hasDependency("org.springframework.session", "spring-session")
50+
.hasSpringBootStarterDependency("data-redis")
51+
.hasSpringBootStarterTest()
52+
.hasDependenciesCount(3);
53+
}
54+
55+
@Test
56+
public void sessionWithJdbcAndSpringBoot15() {
57+
ProjectRequest request = createProjectRequest("session", "jdbc");
58+
request.setBootVersion("1.5.4.RELEASE");
59+
generateMavenPom(request)
60+
.hasDependency("org.springframework.session", "spring-session")
61+
.hasSpringBootStarterDependency("jdbc")
62+
.hasSpringBootStarterTest()
63+
.hasDependenciesCount(3);
64+
}
65+
66+
@Test
67+
public void sessionWithSpringBoot20M2() {
68+
ProjectRequest request = createProjectRequest("session");
69+
request.setBootVersion("2.0.0.M2");
70+
generateMavenPom(request)
71+
.hasDependency("org.springframework.session", "spring-session")
72+
.hasSpringBootStarterRootDependency()
73+
.hasSpringBootStarterTest()
74+
.hasDependenciesCount(3);
75+
}
76+
77+
@Test
78+
public void noSessionWithRedis() {
79+
ProjectRequest request = createProjectRequest("data-redis");
80+
request.setBootVersion("2.0.0.M3");
81+
generateMavenPom(request)
82+
.hasSpringBootStarterDependency("data-redis")
83+
.hasSpringBootStarterTest()
84+
.hasDependenciesCount(2);
85+
}
86+
87+
@Test
88+
public void sessionWithNoStore() {
89+
ProjectRequest request = createProjectRequest("session", "data-jpa");
90+
request.setBootVersion("2.0.0.M3");
91+
generateMavenPom(request)
92+
.hasDependency("org.springframework.session", "spring-session-core")
93+
.hasSpringBootStarterDependency("data-jpa")
94+
.hasSpringBootStarterTest()
95+
.hasDependenciesCount(3);
96+
}
97+
98+
@Test
99+
public void sessionWithRedis() {
100+
ProjectRequest request = createProjectRequest("session", "data-redis");
101+
request.setBootVersion("2.0.0.M3");
102+
generateMavenPom(request)
103+
.hasSpringBootStarterDependency("data-redis")
104+
.hasSpringBootStarterTest()
105+
.hasDependency(REDIS)
106+
.hasDependenciesCount(3);
107+
}
108+
109+
@Test
110+
public void sessionWithRedisReactive() {
111+
ProjectRequest request = createProjectRequest("session", "data-redis-reactive");
112+
request.setBootVersion("2.0.0.M3");
113+
generateMavenPom(request)
114+
.hasSpringBootStarterDependency("data-redis-reactive")
115+
.hasSpringBootStarterTest()
116+
.hasDependency(REDIS)
117+
.hasDependenciesCount(3);
118+
}
119+
120+
@Test
121+
public void sessionWithJdbc() {
122+
ProjectRequest request = createProjectRequest("session", "jdbc");
123+
request.setBootVersion("2.0.0.M3");
124+
generateMavenPom(request)
125+
.hasSpringBootStarterDependency("jdbc")
126+
.hasSpringBootStarterTest()
127+
.hasDependency(JDBC)
128+
.hasDependenciesCount(3);
129+
}
130+
131+
@Test
132+
public void sessionWithRedisAndJdbc() {
133+
ProjectRequest request = createProjectRequest("session", "data-redis", "jdbc");
134+
request.setBootVersion("2.0.0.M3");
135+
generateMavenPom(request)
136+
.hasSpringBootStarterDependency("data-redis")
137+
.hasSpringBootStarterDependency("jdbc")
138+
.hasSpringBootStarterTest()
139+
.hasDependency(REDIS)
140+
.hasDependency(JDBC)
141+
.hasDependenciesCount(5);
142+
}
143+
144+
}

0 commit comments

Comments
 (0)