Skip to content

Commit

Permalink
Introduce shared utilities for ProjectRequestPostProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Feb 14, 2018
1 parent 378623f commit aad08e0
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,21 @@ public static Dependency create(String groupId, String artifactId, String versio
}

public static Dependency withId(String id, String groupId, String artifactId,
String version) {
String version, String scope) {
Dependency dependency = new Dependency();
dependency.setId(id);
dependency.groupId = groupId;
dependency.artifactId = artifactId;
dependency.version = version;
dependency.scope = (scope != null ? scope : SCOPE_COMPILE);
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) {
return withId(id, groupId, artifactId, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@
@Component
class JacksonKotlinRequestPostProcessor implements ProjectRequestPostProcessor {

private final Dependency jacksonModuleKotlin;

public JacksonKotlinRequestPostProcessor() {
this.jacksonModuleKotlin = Dependency.withId("jackson-module-kotlin",
"com.fasterxml.jackson.module", "jackson-module-kotlin");
}
static final Dependency JACKSON_KOTLIN = Dependency.withId("jackson-module-kotlin",
"com.fasterxml.jackson.module", "jackson-module-kotlin");

@Override
public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) {
if (request.getFacets().contains("json")
&& "kotlin".equals(request.getLanguage())) {
request.getResolvedDependencies().add(this.jacksonModuleKotlin);
request.getResolvedDependencies().add(JACKSON_KOTLIN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,19 @@
* @author Stephane Nicoll
*/
@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 final Dependency reactorTest;

public ReactorTestRequestPostProcessor() {
this.reactorTest = Dependency.withId(
"reactor-test", "io.projectreactor", "reactor-test");
this.reactorTest.setScope(Dependency.SCOPE_TEST);
}
static final Dependency REACTOR_TEST = Dependency.withId("reactor-test",
"io.projectreactor", "reactor-test", null, Dependency.SCOPE_TEST);

@Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
if (hasWebFlux(request) && isAtLeastAfter(request, VERSION_2_0_0_M2)) {
request.getResolvedDependencies().add(this.reactorTest);
if (hasDependency(request, "webflux")
&& 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,20 @@
* @author Tim Riemer
*/
@Component
class SpringBatchTestRequestPostProcessor implements ProjectRequestPostProcessor {
class SpringBatchTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {

private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE");

private final Dependency springBatchTest;

public SpringBatchTestRequestPostProcessor() {
this.springBatchTest = Dependency.withId("spring-batch-test",
"org.springframework.batch", "spring-batch-test");
this.springBatchTest.setScope(Dependency.SCOPE_TEST);
}
static final Dependency SPRING_BATCH_TEST = Dependency.withId("spring-batch-test",
"org.springframework.batch", "spring-batch-test", null, Dependency.SCOPE_TEST);

@Override
public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) {
if (hasSpringBatch(request) && isAtLeastAfter(request, VERSION_1_3_0)) {
request.getResolvedDependencies().add(this.springBatchTest);
if (hasDependency(request, "batch")
&& 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;

import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.generator.ProjectRequestPostProcessor;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.util.Version;

Expand All @@ -33,19 +32,18 @@
* @author Stephane Nicoll
*/
@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 List<String> VALID_VERSIONS = Arrays.asList("1.8", "9");

@Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
if (!VALID_VERSIONS.contains(request.getJavaVersion())) {
Version requestVersion = Version.safeParse(request.getBootVersion());
if (VERSION_2_0_0_M1.compareTo(requestVersion) <= 0) {
request.setJavaVersion("1.8");
}
public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) {
if (!VALID_VERSIONS.contains(request.getJavaVersion())
&& isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M1)) {
request.setJavaVersion("1.8");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,21 @@
* @author Stephane Nicoll
*/
@Component
class SpringSecurityTestRequestPostProcessor implements ProjectRequestPostProcessor {
class SpringSecurityTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {

private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE");

private final Dependency springSecurityTest;

public SpringSecurityTestRequestPostProcessor() {
this.springSecurityTest = Dependency.withId("spring-security-test",
"org.springframework.security", "spring-security-test");
this.springSecurityTest.setScope(Dependency.SCOPE_TEST);
}
static final Dependency SPRING_SECURITY_TEST = Dependency.withId(
"spring-security-test", "org.springframework.security",
"spring-security-test", null, Dependency.SCOPE_TEST);

@Override
public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) {
if (hasSpringSecurity(request) && isAtLeastAfter(request, VERSION_1_3_0)) {
request.getResolvedDependencies().add(this.springSecurityTest);
if (hasDependency(request, "security")
&& 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
* @author Stephane Nicoll
*/
@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");

Expand All @@ -46,9 +47,9 @@ public class SpringSessionRequestPostProcessor implements ProjectRequestPostProc


@Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
Version requestVersion = Version.safeParse(request.getBootVersion());
if (VERSION_2_0_0_M3.compareTo(requestVersion) <= 0) {
public void postProcessAfterResolution(ProjectRequest request,
InitializrMetadata metadata) {
if (isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M3)) {
swapSpringSessionDepenendency(request);
}
}
Expand All @@ -71,13 +72,4 @@ private void swapSpringSessionDepenendency(ProjectRequest request) {
}
}

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package io.spring.initializr.service.extension;

import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.metadata.Dependency;
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}.
*
Expand All @@ -29,12 +31,6 @@
public class JacksonKotlinRequestPostProcessorTests
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
public void jacksonModuleKotlinIsAdded() {
ProjectRequest request = createProjectRequest("webflux");
Expand Down

0 comments on commit aad08e0

Please sign in to comment.