Introduce shared utilities for ProjectRequestPostProcessor

This commit is contained in:
Stephane Nicoll 2018-02-14 14:10:23 +01:00
parent 378623fcc0
commit aad08e0c21
9 changed files with 128 additions and 96 deletions

View File

@ -483,15 +483,21 @@ public class Dependency extends MetadataElement implements Describable {
} }
public static Dependency withId(String id, String groupId, String artifactId, public static Dependency withId(String id, String groupId, String artifactId,
String version) { String version, String scope) {
Dependency dependency = new Dependency(); Dependency dependency = new Dependency();
dependency.setId(id); dependency.setId(id);
dependency.groupId = groupId; dependency.groupId = groupId;
dependency.artifactId = artifactId; dependency.artifactId = artifactId;
dependency.version = version; dependency.version = version;
dependency.scope = (scope != null ? scope : SCOPE_COMPILE);
return dependency; return dependency;
} }
public static Dependency withId(String id, String groupId, String artifactId,
String version) {
return withId(id, groupId, artifactId, version, null);
}
public static Dependency withId(String id, String groupId, String artifactId) { public static Dependency withId(String id, String groupId, String artifactId) {
return withId(id, groupId, artifactId, null); return withId(id, groupId, artifactId, null);
} }

View File

@ -0,0 +1,85 @@
/*
* Copyright 2012-2018 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 io.spring.initializr.generator.ProjectRequestPostProcessor;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.util.Version;
/**
* Base {@link ProjectRequestPostProcessor} with reusable utilities.
*
* @author Stephane Nicoll
*/
public class AbstractProjectRequestPostProcessor implements ProjectRequestPostProcessor {
/**
* Determine if the {@link ProjectRequest request} defines the dependency with the
* specified {@code dependencyId}.
* @param request the request to handle
* @param dependencyId the id of a dependency
* @return {@code true} if the project defines that dependency
*/
protected boolean hasDependency(ProjectRequest request, String dependencyId) {
return hasDependencies(request, dependencyId);
}
/**
* Determine if the {@link ProjectRequest request} defines the dependencies with the
* specified {@code dependenciesId}.
* @param request the request to handle
* @param dependenciesId the dependency ids
* @return {@code true} if the project defines all dependencies
*/
protected boolean hasDependencies(ProjectRequest request, String... dependenciesId) {
for (String id : dependenciesId) {
if (getDependency(request, id) == null) {
return false;
}
}
return true;
}
/**
* Return the {@link Dependency} with the specified {@code id} or {@code null} if the
* project does not define it.
* @param request the request to handle
* @param id the id of a dependency
* @return the {@link Dependency} with that id or {@code null} if the project does not
* define such dependency
*/
protected Dependency getDependency(ProjectRequest request, String id) {
return request.getResolvedDependencies().stream()
.filter(d -> id.equals(d.getId())).findFirst().orElse(null);
}
/**
* Specify if the Spring Boot version of the {@link ProjectRequest request} is higher
* or equal to the specified {@link Version}.
* @param request the request to handle
* @param version the minimum version
* @return {@code true} if the requested version is equal or higher than the specified
* {@code version}
*/
protected boolean isSpringBootVersionAtLeastAfter(ProjectRequest request,
Version version) {
Version requestVersion = Version.safeParse(request.getBootVersion());
return version.compareTo(requestVersion) <= 0;
}
}

View File

@ -32,19 +32,15 @@ import org.springframework.stereotype.Component;
@Component @Component
class JacksonKotlinRequestPostProcessor implements ProjectRequestPostProcessor { class JacksonKotlinRequestPostProcessor implements ProjectRequestPostProcessor {
private final Dependency jacksonModuleKotlin; static final Dependency JACKSON_KOTLIN = Dependency.withId("jackson-module-kotlin",
"com.fasterxml.jackson.module", "jackson-module-kotlin");
public JacksonKotlinRequestPostProcessor() {
this.jacksonModuleKotlin = Dependency.withId("jackson-module-kotlin",
"com.fasterxml.jackson.module", "jackson-module-kotlin");
}
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) { InitializrMetadata metadata) {
if (request.getFacets().contains("json") if (request.getFacets().contains("json")
&& "kotlin".equals(request.getLanguage())) { && "kotlin".equals(request.getLanguage())) {
request.getResolvedDependencies().add(this.jacksonModuleKotlin); request.getResolvedDependencies().add(JACKSON_KOTLIN);
} }
} }

View File

@ -31,33 +31,19 @@ import org.springframework.stereotype.Component;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Component @Component
class ReactorTestRequestPostProcessor implements ProjectRequestPostProcessor { class ReactorTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {
private static final Version VERSION_2_0_0_M2 = Version.parse("2.0.0.M2"); private static final Version VERSION_2_0_0_M2 = Version.parse("2.0.0.M2");
private final Dependency reactorTest; static final Dependency REACTOR_TEST = Dependency.withId("reactor-test",
"io.projectreactor", "reactor-test", null, Dependency.SCOPE_TEST);
public ReactorTestRequestPostProcessor() {
this.reactorTest = Dependency.withId(
"reactor-test", "io.projectreactor", "reactor-test");
this.reactorTest.setScope(Dependency.SCOPE_TEST);
}
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) { public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
if (hasWebFlux(request) && isAtLeastAfter(request, VERSION_2_0_0_M2)) { if (hasDependency(request, "webflux")
request.getResolvedDependencies().add(this.reactorTest); && isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M2)) {
request.getResolvedDependencies().add(REACTOR_TEST);
} }
} }
private boolean hasWebFlux(ProjectRequest request) {
return request.getResolvedDependencies().stream()
.anyMatch(d -> "webflux".equals(d.getId()));
}
private boolean isAtLeastAfter(ProjectRequest request, Version version) {
Version requestVersion = Version.safeParse(request.getBootVersion());
return version.compareTo(requestVersion) <= 0;
}
} }

View File

@ -31,34 +31,20 @@ import org.springframework.stereotype.Component;
* @author Tim Riemer * @author Tim Riemer
*/ */
@Component @Component
class SpringBatchTestRequestPostProcessor implements ProjectRequestPostProcessor { class SpringBatchTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {
private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE"); private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE");
private final Dependency springBatchTest; static final Dependency SPRING_BATCH_TEST = Dependency.withId("spring-batch-test",
"org.springframework.batch", "spring-batch-test", null, Dependency.SCOPE_TEST);
public SpringBatchTestRequestPostProcessor() {
this.springBatchTest = Dependency.withId("spring-batch-test",
"org.springframework.batch", "spring-batch-test");
this.springBatchTest.setScope(Dependency.SCOPE_TEST);
}
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) { InitializrMetadata metadata) {
if (hasSpringBatch(request) && isAtLeastAfter(request, VERSION_1_3_0)) { if (hasDependency(request, "batch")
request.getResolvedDependencies().add(this.springBatchTest); && isSpringBootVersionAtLeastAfter(request, VERSION_1_3_0)) {
request.getResolvedDependencies().add(SPRING_BATCH_TEST);
} }
} }
private boolean hasSpringBatch(ProjectRequest request) {
return request.getResolvedDependencies().stream()
.anyMatch(d -> "batch".equals(d.getId()));
}
private boolean isAtLeastAfter(ProjectRequest request, Version version) {
Version requestVersion = Version.safeParse(request.getBootVersion());
return version.compareTo(requestVersion) <= 0;
}
} }

View File

@ -20,7 +20,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.ProjectRequest; import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.generator.ProjectRequestPostProcessor;
import io.spring.initializr.metadata.InitializrMetadata; import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.util.Version; import io.spring.initializr.util.Version;
@ -33,19 +32,18 @@ import org.springframework.stereotype.Component;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Component @Component
class SpringBoot2RequestPostProcessor implements ProjectRequestPostProcessor { class SpringBoot2RequestPostProcessor extends AbstractProjectRequestPostProcessor {
private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1"); private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1");
private static final List<String> VALID_VERSIONS = Arrays.asList("1.8", "9"); private static final List<String> VALID_VERSIONS = Arrays.asList("1.8", "9");
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) { public void postProcessAfterResolution(ProjectRequest request,
if (!VALID_VERSIONS.contains(request.getJavaVersion())) { InitializrMetadata metadata) {
Version requestVersion = Version.safeParse(request.getBootVersion()); if (!VALID_VERSIONS.contains(request.getJavaVersion())
if (VERSION_2_0_0_M1.compareTo(requestVersion) <= 0) { && isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M1)) {
request.setJavaVersion("1.8"); request.setJavaVersion("1.8");
}
} }
} }

View File

@ -31,34 +31,21 @@ import org.springframework.stereotype.Component;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Component @Component
class SpringSecurityTestRequestPostProcessor implements ProjectRequestPostProcessor { class SpringSecurityTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {
private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE"); private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE");
private final Dependency springSecurityTest; static final Dependency SPRING_SECURITY_TEST = Dependency.withId(
"spring-security-test", "org.springframework.security",
public SpringSecurityTestRequestPostProcessor() { "spring-security-test", null, Dependency.SCOPE_TEST);
this.springSecurityTest = Dependency.withId("spring-security-test",
"org.springframework.security", "spring-security-test");
this.springSecurityTest.setScope(Dependency.SCOPE_TEST);
}
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) { InitializrMetadata metadata) {
if (hasSpringSecurity(request) && isAtLeastAfter(request, VERSION_1_3_0)) { if (hasDependency(request, "security")
request.getResolvedDependencies().add(this.springSecurityTest); && isSpringBootVersionAtLeastAfter(request, VERSION_1_3_0)) {
request.getResolvedDependencies().add(SPRING_SECURITY_TEST);
} }
} }
private boolean hasSpringSecurity(ProjectRequest request) {
return request.getResolvedDependencies().stream()
.anyMatch(d -> "security".equals(d.getId()));
}
private boolean isAtLeastAfter(ProjectRequest request, Version version) {
Version requestVersion = Version.safeParse(request.getBootVersion());
return version.compareTo(requestVersion) <= 0;
}
} }

View File

@ -34,7 +34,8 @@ import org.springframework.stereotype.Component;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Component @Component
public class SpringSessionRequestPostProcessor implements ProjectRequestPostProcessor { public class SpringSessionRequestPostProcessor
extends AbstractProjectRequestPostProcessor {
private static final Version VERSION_2_0_0_M3 = Version.parse("2.0.0.M3"); private static final Version VERSION_2_0_0_M3 = Version.parse("2.0.0.M3");
@ -46,9 +47,9 @@ public class SpringSessionRequestPostProcessor implements ProjectRequestPostProc
@Override @Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) { public void postProcessAfterResolution(ProjectRequest request,
Version requestVersion = Version.safeParse(request.getBootVersion()); InitializrMetadata metadata) {
if (VERSION_2_0_0_M3.compareTo(requestVersion) <= 0) { if (isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M3)) {
swapSpringSessionDepenendency(request); swapSpringSessionDepenendency(request);
} }
} }
@ -71,13 +72,4 @@ public class SpringSessionRequestPostProcessor implements ProjectRequestPostProc
} }
} }
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

@ -17,9 +17,11 @@
package io.spring.initializr.service.extension; package io.spring.initializr.service.extension;
import io.spring.initializr.generator.ProjectRequest; import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.metadata.Dependency;
import org.junit.Test; import org.junit.Test;
import static io.spring.initializr.service.extension.JacksonKotlinRequestPostProcessor.JACKSON_KOTLIN;
import static io.spring.initializr.service.extension.ReactorTestRequestPostProcessor.REACTOR_TEST;
/** /**
* Tests for {@link JacksonKotlinRequestPostProcessor}. * Tests for {@link JacksonKotlinRequestPostProcessor}.
* *
@ -29,12 +31,6 @@ import org.junit.Test;
public class JacksonKotlinRequestPostProcessorTests public class JacksonKotlinRequestPostProcessorTests
extends AbstractRequestPostProcessorTests { extends AbstractRequestPostProcessorTests {
static final Dependency JACKSON_KOTLIN = Dependency.withId("jackson-module-kotlin",
"com.fasterxml.jackson.module", "jackson-module-kotlin");
static final Dependency REACTOR_TEST = Dependency.create(
"io.projectreactor", "reactor-test", null, Dependency.SCOPE_TEST);
@Test @Test
public void jacksonModuleKotlinIsAdded() { public void jacksonModuleKotlinIsAdded() {
ProjectRequest request = createProjectRequest("webflux"); ProjectRequest request = createProjectRequest("webflux");