Add reactor-test automatically when webflux is selected

Closes gh-456
This commit is contained in:
Stephane Nicoll 2017-07-03 08:11:10 +02:00
parent 69c2d852ea
commit d562968093
4 changed files with 200 additions and 33 deletions

View File

@ -0,0 +1,60 @@
/*
* 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 io.spring.initializr.generator.ProjectRequestPostProcessor;
import io.spring.initializr.generator.ProjectRequestPostProcessorAdapter;
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 automatically adds "reactor-test" when
* webflux is selected.
*
* @author Stephane Nicoll
*/
@Component
public class ReactorTestRequestPostProcessor extends ProjectRequestPostProcessorAdapter {
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);
}
@Override
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
Version requestVersion = Version.safeParse(request.getBootVersion());
if (hasWebFlux(request) && VERSION_2_0_0_M2.compareTo(requestVersion) <= 0) {
request.getResolvedDependencies().add(this.reactorTest);
}
}
private boolean hasWebFlux(ProjectRequest request) {
return request.getResolvedDependencies().stream()
.anyMatch(d -> "webflux".equals(d.getId()));
}
}

View File

@ -0,0 +1,67 @@
/*
* 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.Arrays;
import io.spring.initializr.generator.ProjectGenerator;
import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.generator.ProjectRequestPostProcessor;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.test.generator.GradleBuildAssert;
import io.spring.initializr.test.generator.PomAssert;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Base test class for {@link ProjectRequestPostProcessor} implementations.
*
* @author Stephane Nicoll
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract class AbstractRequestPostProcessorTests {
@Autowired
private ProjectGenerator projectGenerator;
@Autowired
private InitializrMetadataProvider metadataProvider;
protected PomAssert generateMavenPom(ProjectRequest request) {
request.setType("maven-build");
String content = new String(projectGenerator.generateMavenPom(request));
return new PomAssert(content);
}
protected GradleBuildAssert generateGradleBuild(ProjectRequest request) {
request.setType("gradle-build");
String content = new String(projectGenerator.generateGradleBuild(request));
return new GradleBuildAssert(content);
}
protected ProjectRequest createProjectRequest(String... styles) {
ProjectRequest request = new ProjectRequest();
request.initialize(metadataProvider.get());
request.getStyle().addAll(Arrays.asList(styles));
return request;
}
}

View File

@ -0,0 +1,71 @@
/*
* 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 io.spring.initializr.metadata.Dependency;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Tests for {@link ReactorTestRequestPostProcessor}.
*
* @author Stephane Nicoll
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ReactorTestRequestPostProcessorTests
extends AbstractRequestPostProcessorTests {
@Test
public void reactorTestIsAdded() {
ProjectRequest request = createProjectRequest("webflux");
request.setBootVersion("2.0.0.M2");
Dependency reactorTest = Dependency.withId(
"reactor-test", "io.projectreactor", "reactor-test");
reactorTest.setScope(Dependency.SCOPE_TEST);
generateMavenPom(request)
.hasSpringBootStarterDependency("webflux")
.hasSpringBootStarterTest()
.hasDependency(reactorTest)
.hasDependenciesCount(3);
}
@Test
public void reactorTestIsNotAddedWithEarlierVersions() {
ProjectRequest request = createProjectRequest("webflux");
request.setBootVersion("2.0.0.M1");
generateMavenPom(request)
.hasSpringBootStarterDependency("webflux")
.hasSpringBootStarterTest()
.hasDependenciesCount(2);
}
@Test
public void reactorTestIsNotAddedWithoutWebFlux() {
ProjectRequest request = createProjectRequest("web");
request.setBootVersion("2.0.0.M2");
generateMavenPom(request)
.hasSpringBootStarterDependency("web")
.hasSpringBootStarterTest()
.hasDependenciesCount(2);
}
}

View File

@ -16,17 +16,10 @@
package io.spring.initializr.service.extension;
import java.util.Arrays;
import io.spring.initializr.generator.ProjectGenerator;
import io.spring.initializr.generator.ProjectRequest;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.test.generator.GradleBuildAssert;
import io.spring.initializr.test.generator.PomAssert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -37,13 +30,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SpringBoot2RequestPostProcessorTests {
@Autowired
private ProjectGenerator projectGenerator;
@Autowired
private InitializrMetadataProvider metadataProvider;
public class SpringBoot2RequestPostProcessorTests
extends AbstractRequestPostProcessorTests {
@Test
public void java8IsMandatoryMaven() {
@ -61,23 +49,4 @@ public class SpringBoot2RequestPostProcessorTests {
generateGradleBuild(request).hasJavaVersion("1.8");
}
private PomAssert generateMavenPom(ProjectRequest request) {
request.setType("maven-build");
String content = new String(projectGenerator.generateMavenPom(request));
return new PomAssert(content);
}
private GradleBuildAssert generateGradleBuild(ProjectRequest request) {
request.setType("gradle-build");
String content = new String(projectGenerator.generateGradleBuild(request));
return new GradleBuildAssert(content);
}
private ProjectRequest createProjectRequest(String... styles) {
ProjectRequest request = new ProjectRequest();
request.initialize(metadataProvider.get());
request.getStyle().addAll(Arrays.asList(styles));
return request;
}
}