mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-17 19:37:35 +08:00
Polish
See gh-849
This commit is contained in:
parent
b24cd2e9ca
commit
4ac8ab3d24
@ -30,27 +30,22 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
|
|||||||
* {@link ConditionalOnGradleVersion}.
|
* {@link ConditionalOnGradleVersion}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class OnGradleVersionCondition extends ProjectGenerationCondition {
|
public class OnGradleVersionCondition extends ProjectGenerationCondition {
|
||||||
|
|
||||||
private static final VersionRange GRADLE_3_BOOT_VERSION_RANGE = VersionParser.DEFAULT
|
private static final VersionRange GRADLE_3_VERSION_RANGE = VersionParser.DEFAULT
|
||||||
.parseRange("[1.5.0.M1,2.0.0.M1)");
|
.parseRange("[1.5.0.M1,2.0.0.M1)");
|
||||||
|
|
||||||
private static final VersionRange GRADLE_4_BOOT_VERSION_RANGE = VersionParser.DEFAULT
|
private static final VersionRange GRADLE_4_VERSION_RANGE = VersionParser.DEFAULT
|
||||||
.parseRange("2.0.0.M1");
|
.parseRange("2.0.0.M1");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean matches(ResolvedProjectDescription projectDescription,
|
protected boolean matches(ResolvedProjectDescription projectDescription,
|
||||||
ConditionContext context, AnnotatedTypeMetadata metadata) {
|
ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
Version springBootVersion = projectDescription.getPlatformVersion();
|
String gradleVersion = determineGradleGeneration(
|
||||||
String gradleVersion;
|
projectDescription.getPlatformVersion());
|
||||||
if (GRADLE_3_BOOT_VERSION_RANGE.match(springBootVersion)) {
|
if (gradleVersion == null) {
|
||||||
gradleVersion = "3";
|
|
||||||
}
|
|
||||||
else if (GRADLE_4_BOOT_VERSION_RANGE.match(springBootVersion)) {
|
|
||||||
gradleVersion = "4";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String value = (String) metadata
|
String value = (String) metadata
|
||||||
@ -59,4 +54,19 @@ public class OnGradleVersionCondition extends ProjectGenerationCondition {
|
|||||||
return gradleVersion.equals(value);
|
return gradleVersion.equals(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String determineGradleGeneration(Version platformVersion) {
|
||||||
|
if (platformVersion == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else if (GRADLE_3_VERSION_RANGE.match(platformVersion)) {
|
||||||
|
return "3";
|
||||||
|
}
|
||||||
|
else if (GRADLE_4_VERSION_RANGE.match(platformVersion)) {
|
||||||
|
return "4";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2019 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.generator.spring.build.gradle;
|
||||||
|
|
||||||
|
import io.spring.initializr.generator.project.ProjectDescription;
|
||||||
|
import io.spring.initializr.generator.test.project.ProjectAssetTester;
|
||||||
|
import io.spring.initializr.generator.version.Version;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link ConditionalOnGradleVersion}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class ConditionalOnGradleVersionTests {
|
||||||
|
|
||||||
|
private final ProjectAssetTester projectTester = new ProjectAssetTester()
|
||||||
|
.withConfiguration(GradleVersionTestConfiguration.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void outcomeWithSpringBoot15() {
|
||||||
|
ProjectDescription projectDescription = new ProjectDescription();
|
||||||
|
projectDescription.setPlatformVersion(Version.parse("1.5.18.RELEASE"));
|
||||||
|
String bean = outcomeFor(projectDescription);
|
||||||
|
assertThat(bean).isEqualTo("testGradle3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void outcomeWithSpringBoot2() {
|
||||||
|
ProjectDescription projectDescription = new ProjectDescription();
|
||||||
|
projectDescription.setPlatformVersion(Version.parse("2.0.9.RELEASE"));
|
||||||
|
String bean = outcomeFor(projectDescription);
|
||||||
|
assertThat(bean).isEqualTo("testGradle4");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void outcomeWithNoMatch() {
|
||||||
|
ProjectDescription projectDescription = new ProjectDescription();
|
||||||
|
projectDescription.setPlatformVersion(Version.parse("1.0.0.RELEASE"));
|
||||||
|
this.projectTester.generate(projectDescription, (projectGenerationContext) -> {
|
||||||
|
assertThat(projectGenerationContext.getBeansOfType(String.class)).isEmpty();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void outcomeWithNoAvailableSpringBootVersion() {
|
||||||
|
ProjectDescription projectDescription = new ProjectDescription();
|
||||||
|
this.projectTester.generate(projectDescription, (projectGenerationContext) -> {
|
||||||
|
assertThat(projectGenerationContext.getBeansOfType(String.class)).isEmpty();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private String outcomeFor(ProjectDescription projectDescription) {
|
||||||
|
return this.projectTester.generate(projectDescription,
|
||||||
|
(projectGenerationContext) -> {
|
||||||
|
assertThat(projectGenerationContext.getBeansOfType(String.class))
|
||||||
|
.hasSize(1);
|
||||||
|
return projectGenerationContext.getBean(String.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class GradleVersionTestConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnGradleVersion("3")
|
||||||
|
public String gradle3() {
|
||||||
|
return "testGradle3";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnGradleVersion("4")
|
||||||
|
public String gradle4() {
|
||||||
|
return "testGradle4";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user