Add dedicated support for Spring Session modules

Closes gh-455
This commit is contained in:
Stephane Nicoll
2017-07-07 09:22:12 +01:00
parent cf93d5ddef
commit d50d7667e3
3 changed files with 232 additions and 2 deletions

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2012-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.service.extension;
import java.util.ArrayList;
import java.util.List;
import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.generator.ProjectRequestPostProcessor;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.util.Version;
import org.springframework.stereotype.Component;
/**
* A {@link ProjectRequestPostProcessor} that provides explicit handling for the modules
* introduced in Spring Session 2.
*
* @author Stephane Nicoll
*/
@Component
public class SpringSessionRequestPostProcessor implements ProjectRequestPostProcessor {
private static final Version VERSION_2_0_0_M3 = Version.parse("2.0.0.M3");
static final Dependency REDIS = Dependency.withId("session-data-redis",
"org.springframework.session", "spring-session-data-redis");
static final Dependency JDBC = Dependency.withId("session-jdbc",
"org.springframework.session", "spring-session-jdbc");
@Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
Version requestVersion = Version.safeParse(request.getBootVersion());
if (VERSION_2_0_0_M3.compareTo(requestVersion) <= 0) {
swapSpringSessionDepenendency(request);
}
}
private void swapSpringSessionDepenendency(ProjectRequest request) {
Dependency session = getDependency(request, "session");
if (session != null) {
List<Dependency> swap = new ArrayList<>();
if (hasDependency(request, "data-redis")
|| hasDependency(request, "data-redis-reactive")) {
swap.add(REDIS);
}
if (hasDependency(request, "jdbc")) {
swap.add(JDBC);
}
if (!swap.isEmpty()) {
request.getResolvedDependencies().remove(session);
request.getResolvedDependencies().addAll(swap);
}
}
}
private boolean hasDependency(ProjectRequest request, String id) {
return getDependency(request, id) != null;
}
private Dependency getDependency(ProjectRequest request, String id) {
return request.getResolvedDependencies().stream()
.filter(d -> id.equals(d.getId())).findFirst().orElse(null);
}
}

View File

@@ -189,10 +189,13 @@ initializr:
- name: Session
id: session
groupId: org.springframework.session
artifactId: spring-session
artifactId: spring-session-core
description: API and implementations for managing a users session information
versionRange: "[1.3.0.RELEASE,2.0.0.M2]"
versionRange: "1.3.0.RELEASE"
starter: false
mappings:
- versionRange: "[1.3.0.RELEASE,2.0.0.M2]"
artifactId: spring-session
- name: Retry
id: retry
groupId: org.springframework.retry

View File

@@ -0,0 +1,144 @@
/*
* Copyright 2012-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.service.extension;
import io.spring.initializr.generator.ProjectRequest;
import org.junit.Test;
import static io.spring.initializr.service.extension.SpringSessionRequestPostProcessor.JDBC;
import static io.spring.initializr.service.extension.SpringSessionRequestPostProcessor.REDIS;
/**
* Tests for {@link SpringSessionRequestPostProcessor}.
*
* @author Stephane Nicoll
*/
public class SpringSessionRequestPostProcessorTests
extends AbstractRequestPostProcessorTests {
@Test
public void sessionWithSpringBoot15() {
ProjectRequest request = createProjectRequest("session");
request.setBootVersion("1.5.4.RELEASE");
generateMavenPom(request)
.hasDependency("org.springframework.session", "spring-session")
.hasSpringBootStarterRootDependency()
.hasSpringBootStarterTest()
.hasDependenciesCount(3);
}
@Test
public void sessionWithRedisAndSpringBoot15() {
ProjectRequest request = createProjectRequest("session", "data-redis");
request.setBootVersion("1.5.4.RELEASE");
generateMavenPom(request)
.hasDependency("org.springframework.session", "spring-session")
.hasSpringBootStarterDependency("data-redis")
.hasSpringBootStarterTest()
.hasDependenciesCount(3);
}
@Test
public void sessionWithJdbcAndSpringBoot15() {
ProjectRequest request = createProjectRequest("session", "jdbc");
request.setBootVersion("1.5.4.RELEASE");
generateMavenPom(request)
.hasDependency("org.springframework.session", "spring-session")
.hasSpringBootStarterDependency("jdbc")
.hasSpringBootStarterTest()
.hasDependenciesCount(3);
}
@Test
public void sessionWithSpringBoot20M2() {
ProjectRequest request = createProjectRequest("session");
request.setBootVersion("2.0.0.M2");
generateMavenPom(request)
.hasDependency("org.springframework.session", "spring-session")
.hasSpringBootStarterRootDependency()
.hasSpringBootStarterTest()
.hasDependenciesCount(3);
}
@Test
public void noSessionWithRedis() {
ProjectRequest request = createProjectRequest("data-redis");
request.setBootVersion("2.0.0.M3");
generateMavenPom(request)
.hasSpringBootStarterDependency("data-redis")
.hasSpringBootStarterTest()
.hasDependenciesCount(2);
}
@Test
public void sessionWithNoStore() {
ProjectRequest request = createProjectRequest("session", "data-jpa");
request.setBootVersion("2.0.0.M3");
generateMavenPom(request)
.hasDependency("org.springframework.session", "spring-session-core")
.hasSpringBootStarterDependency("data-jpa")
.hasSpringBootStarterTest()
.hasDependenciesCount(3);
}
@Test
public void sessionWithRedis() {
ProjectRequest request = createProjectRequest("session", "data-redis");
request.setBootVersion("2.0.0.M3");
generateMavenPom(request)
.hasSpringBootStarterDependency("data-redis")
.hasSpringBootStarterTest()
.hasDependency(REDIS)
.hasDependenciesCount(3);
}
@Test
public void sessionWithRedisReactive() {
ProjectRequest request = createProjectRequest("session", "data-redis-reactive");
request.setBootVersion("2.0.0.M3");
generateMavenPom(request)
.hasSpringBootStarterDependency("data-redis-reactive")
.hasSpringBootStarterTest()
.hasDependency(REDIS)
.hasDependenciesCount(3);
}
@Test
public void sessionWithJdbc() {
ProjectRequest request = createProjectRequest("session", "jdbc");
request.setBootVersion("2.0.0.M3");
generateMavenPom(request)
.hasSpringBootStarterDependency("jdbc")
.hasSpringBootStarterTest()
.hasDependency(JDBC)
.hasDependenciesCount(3);
}
@Test
public void sessionWithRedisAndJdbc() {
ProjectRequest request = createProjectRequest("session", "data-redis", "jdbc");
request.setBootVersion("2.0.0.M3");
generateMavenPom(request)
.hasSpringBootStarterDependency("data-redis")
.hasSpringBootStarterDependency("jdbc")
.hasSpringBootStarterTest()
.hasDependency(REDIS)
.hasDependency(JDBC)
.hasDependenciesCount(5);
}
}