mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 18:22:26 +08:00
Move ProjectRequest input validation to ProjectDescriptionCustomizer
This commit simplifies DefaultProjectRequestToDescriptionConverter to converting the request to a description only. Applying default or cleaning values are now part of a ProjectDescriptionCustomizer callback. Closes gh-1045
This commit is contained in:
@@ -17,7 +17,6 @@
|
|||||||
package io.spring.initializr.web.project;
|
package io.spring.initializr.web.project;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import io.spring.initializr.generator.buildsystem.BuildSystem;
|
import io.spring.initializr.generator.buildsystem.BuildSystem;
|
||||||
@@ -32,8 +31,6 @@ import io.spring.initializr.metadata.InitializrMetadata;
|
|||||||
import io.spring.initializr.metadata.Type;
|
import io.spring.initializr.metadata.Type;
|
||||||
import io.spring.initializr.metadata.support.MetadataBuildItemMapper;
|
import io.spring.initializr.metadata.support.MetadataBuildItemMapper;
|
||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default {@link ProjectRequestToDescriptionConverter} implementation that uses the
|
* A default {@link ProjectRequestToDescriptionConverter} implementation that uses the
|
||||||
* {@link InitializrMetadata metadata} to set default values for missing attributes if
|
* {@link InitializrMetadata metadata} to set default values for missing attributes if
|
||||||
@@ -47,8 +44,6 @@ public class DefaultProjectRequestToDescriptionConverter
|
|||||||
|
|
||||||
private static final Version VERSION_1_5_0 = Version.parse("1.5.0.RELEASE");
|
private static final Version VERSION_1_5_0 = Version.parse("1.5.0.RELEASE");
|
||||||
|
|
||||||
private static final char[] VALID_MAVEN_SPECIAL_CHARACTERS = new char[] { '_', '-', '.' };
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProjectDescription convert(ProjectRequest request, InitializrMetadata metadata) {
|
public ProjectDescription convert(ProjectRequest request, InitializrMetadata metadata) {
|
||||||
MutableProjectDescription description = new MutableProjectDescription();
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
@@ -70,87 +65,22 @@ public class DefaultProjectRequestToDescriptionConverter
|
|||||||
List<Dependency> resolvedDependencies = getResolvedDependencies(request, springBootVersion, metadata);
|
List<Dependency> resolvedDependencies = getResolvedDependencies(request, springBootVersion, metadata);
|
||||||
validateDependencyRange(springBootVersion, resolvedDependencies);
|
validateDependencyRange(springBootVersion, resolvedDependencies);
|
||||||
|
|
||||||
description.setApplicationName(getApplicationName(request, metadata));
|
description.setApplicationName(request.getApplicationName());
|
||||||
description.setArtifactId(getArtifactId(request, metadata));
|
description.setArtifactId(request.getArtifactId());
|
||||||
description.setBaseDirectory(getBaseDirectory(request.getBaseDir(), request.getArtifactId()));
|
description.setBaseDirectory(request.getBaseDir());
|
||||||
description.setBuildSystem(getBuildSystem(request, metadata));
|
description.setBuildSystem(getBuildSystem(request, metadata));
|
||||||
description
|
description.setDescription(request.getDescription());
|
||||||
.setDescription(determineValue(request.getDescription(), () -> metadata.getDescription().getContent()));
|
description.setGroupId(request.getGroupId());
|
||||||
description.setGroupId(getGroupId(request, metadata));
|
|
||||||
description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion()));
|
description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion()));
|
||||||
description.setName(getName(request, metadata));
|
description.setName(request.getName());
|
||||||
description.setPackageName(getPackageName(request, metadata));
|
description.setPackageName(request.getPackageName());
|
||||||
description.setPackaging(Packaging.forId(request.getPackaging()));
|
description.setPackaging(Packaging.forId(request.getPackaging()));
|
||||||
description.setPlatformVersion(Version.parse(springBootVersion));
|
description.setPlatformVersion(Version.parse(springBootVersion));
|
||||||
description.setVersion(determineValue(request.getVersion(), () -> metadata.getVersion().getContent()));
|
description.setVersion(request.getVersion());
|
||||||
resolvedDependencies.forEach((dependency) -> description.addDependency(dependency.getId(),
|
resolvedDependencies.forEach((dependency) -> description.addDependency(dependency.getId(),
|
||||||
MetadataBuildItemMapper.toDependency(dependency)));
|
MetadataBuildItemMapper.toDependency(dependency)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String determineValue(String candidate, Supplier<String> fallback) {
|
|
||||||
return (StringUtils.hasText(candidate)) ? candidate : fallback.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getBaseDirectory(String baseDir, String artifactId) {
|
|
||||||
if (baseDir != null && baseDir.equals(artifactId)) {
|
|
||||||
return cleanMavenCoordinate(baseDir, "-");
|
|
||||||
}
|
|
||||||
return baseDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getName(ProjectRequest request, InitializrMetadata metadata) {
|
|
||||||
String name = request.getName();
|
|
||||||
if (!StringUtils.hasText(name)) {
|
|
||||||
return metadata.getName().getContent();
|
|
||||||
}
|
|
||||||
if (name.equals(request.getArtifactId())) {
|
|
||||||
return cleanMavenCoordinate(name, "-");
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getGroupId(ProjectRequest request, InitializrMetadata metadata) {
|
|
||||||
if (!StringUtils.hasText(request.getGroupId())) {
|
|
||||||
return metadata.getGroupId().getContent();
|
|
||||||
}
|
|
||||||
return cleanMavenCoordinate(request.getGroupId(), ".");
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getArtifactId(ProjectRequest request, InitializrMetadata metadata) {
|
|
||||||
if (!StringUtils.hasText(request.getArtifactId())) {
|
|
||||||
return metadata.getArtifactId().getContent();
|
|
||||||
}
|
|
||||||
return cleanMavenCoordinate(request.getArtifactId(), "-");
|
|
||||||
}
|
|
||||||
|
|
||||||
private String cleanMavenCoordinate(String coordinate, String delimiter) {
|
|
||||||
String[] elements = coordinate.split("[^\\w\\-.]+");
|
|
||||||
if (elements.length == 1) {
|
|
||||||
return coordinate;
|
|
||||||
}
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
for (String element : elements) {
|
|
||||||
if (shouldAppendDelimiter(element, builder)) {
|
|
||||||
builder.append(delimiter);
|
|
||||||
}
|
|
||||||
builder.append(element);
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldAppendDelimiter(String element, StringBuilder builder) {
|
|
||||||
if (builder.length() == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (char c : VALID_MAVEN_SPECIAL_CHARACTERS) {
|
|
||||||
int prevIndex = builder.length() - 1;
|
|
||||||
if (element.charAt(0) == c || builder.charAt(prevIndex) == c) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validate(ProjectRequest request, InitializrMetadata metadata) {
|
private void validate(ProjectRequest request, InitializrMetadata metadata) {
|
||||||
validateSpringBootVersion(request);
|
validateSpringBootVersion(request);
|
||||||
validateType(request.getType(), metadata);
|
validateType(request.getType(), metadata);
|
||||||
@@ -223,18 +153,6 @@ public class DefaultProjectRequestToDescriptionConverter
|
|||||||
return BuildSystem.forId(typeFromMetadata.getTags().get("build"));
|
return BuildSystem.forId(typeFromMetadata.getTags().get("build"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPackageName(ProjectRequest request, InitializrMetadata metadata) {
|
|
||||||
return metadata.getConfiguration().cleanPackageName(request.getPackageName(),
|
|
||||||
metadata.getPackageName().getContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getApplicationName(ProjectRequest request, InitializrMetadata metadata) {
|
|
||||||
if (!StringUtils.hasText(request.getApplicationName())) {
|
|
||||||
return metadata.getConfiguration().generateApplicationName(request.getName());
|
|
||||||
}
|
|
||||||
return request.getApplicationName();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getSpringBootVersion(ProjectRequest request, InitializrMetadata metadata) {
|
private String getSpringBootVersion(ProjectRequest request, InitializrMetadata metadata) {
|
||||||
return (request.getBootVersion() != null) ? request.getBootVersion()
|
return (request.getBootVersion() != null) ? request.getBootVersion()
|
||||||
: metadata.getBootVersions().getDefault().getId();
|
: metadata.getBootVersions().getDefault().getId();
|
||||||
|
@@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2020 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
|
||||||
|
*
|
||||||
|
* https://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.web.project;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import io.spring.initializr.generator.project.MutableProjectDescription;
|
||||||
|
import io.spring.initializr.generator.project.ProjectDescriptionCustomizer;
|
||||||
|
import io.spring.initializr.generator.version.Version;
|
||||||
|
import io.spring.initializr.metadata.InitializrMetadata;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link ProjectDescriptionCustomizer} that uses the {@link InitializrMetadata
|
||||||
|
* metadata} to set default values for missing attributes if necessary.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class MetadataProjectDescriptionCustomizer implements ProjectDescriptionCustomizer {
|
||||||
|
|
||||||
|
private static final char[] VALID_MAVEN_SPECIAL_CHARACTERS = new char[] { '_', '-', '.' };
|
||||||
|
|
||||||
|
private final InitializrMetadata metadata;
|
||||||
|
|
||||||
|
public MetadataProjectDescriptionCustomizer(InitializrMetadata metadata) {
|
||||||
|
this.metadata = metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(MutableProjectDescription description) {
|
||||||
|
if (!StringUtils.hasText(description.getApplicationName())) {
|
||||||
|
description.setApplicationName(
|
||||||
|
this.metadata.getConfiguration().generateApplicationName(description.getName()));
|
||||||
|
}
|
||||||
|
String targetArtifactId = determineValue(description.getArtifactId(),
|
||||||
|
() -> this.metadata.getArtifactId().getContent());
|
||||||
|
description.setArtifactId(cleanMavenCoordinate(targetArtifactId, "-"));
|
||||||
|
if (targetArtifactId.equals(description.getBaseDirectory())) {
|
||||||
|
description.setBaseDirectory(cleanMavenCoordinate(targetArtifactId, "-"));
|
||||||
|
}
|
||||||
|
if (!StringUtils.hasText(description.getDescription())) {
|
||||||
|
description.setDescription(this.metadata.getDescription().getContent());
|
||||||
|
}
|
||||||
|
String targetGroupId = determineValue(description.getGroupId(), () -> this.metadata.getGroupId().getContent());
|
||||||
|
description.setGroupId(cleanMavenCoordinate(targetGroupId, "."));
|
||||||
|
if (!StringUtils.hasText(description.getName())) {
|
||||||
|
description.setName(this.metadata.getName().getContent());
|
||||||
|
}
|
||||||
|
else if (targetArtifactId.equals(description.getName())) {
|
||||||
|
description.setName(cleanMavenCoordinate(targetArtifactId, "-"));
|
||||||
|
}
|
||||||
|
description.setPackageName(this.metadata.getConfiguration().cleanPackageName(description.getPackageName(),
|
||||||
|
this.metadata.getPackageName().getContent()));
|
||||||
|
if (description.getPlatformVersion() == null) {
|
||||||
|
description.setPlatformVersion(Version.parse(this.metadata.getBootVersions().getDefault().getId()));
|
||||||
|
}
|
||||||
|
if (!StringUtils.hasText(description.getVersion())) {
|
||||||
|
description.setVersion(this.metadata.getVersion().getContent());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cleanMavenCoordinate(String coordinate, String delimiter) {
|
||||||
|
String[] elements = coordinate.split("[^\\w\\-.]+");
|
||||||
|
if (elements.length == 1) {
|
||||||
|
return coordinate;
|
||||||
|
}
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (String element : elements) {
|
||||||
|
if (shouldAppendDelimiter(element, builder)) {
|
||||||
|
builder.append(delimiter);
|
||||||
|
}
|
||||||
|
builder.append(element);
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldAppendDelimiter(String element, StringBuilder builder) {
|
||||||
|
if (builder.length() == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (char c : VALID_MAVEN_SPECIAL_CHARACTERS) {
|
||||||
|
int prevIndex = builder.length() - 1;
|
||||||
|
if (element.charAt(0) == c || builder.charAt(prevIndex) == c) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String determineValue(String candidate, Supplier<String> fallback) {
|
||||||
|
return (StringUtils.hasText(candidate)) ? candidate : fallback.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -184,6 +184,8 @@ public class ProjectGenerationInvoker<R extends ProjectRequest> {
|
|||||||
context.registerBean(InitializrMetadata.class, () -> metadata);
|
context.registerBean(InitializrMetadata.class, () -> metadata);
|
||||||
context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata,
|
context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata,
|
||||||
context.getBean(ProjectDescription.class).getPlatformVersion()));
|
context.getBean(ProjectDescription.class).getPlatformVersion()));
|
||||||
|
context.registerBean(MetadataProjectDescriptionCustomizer.class,
|
||||||
|
() -> new MetadataProjectDescriptionCustomizer(metadata));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void publishProjectGeneratedEvent(R request, ProjectGenerationContext context) {
|
private void publishProjectGeneratedEvent(R request, ProjectGenerationContext context) {
|
||||||
|
@@ -122,13 +122,6 @@ class DefaultProjectRequestToDescriptionConverterTests {
|
|||||||
assertThat(description.getApplicationName()).isEqualTo("MyApplication");
|
assertThat(description.getApplicationName()).isEqualTo("MyApplication");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldSetApplicationNameForProjectDescriptionUsingNameWhenAbsentFromRequest() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getApplicationName()).isEqualTo("DemoApplication");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void convertShouldSetGroupIdAndArtifactIdFromRequest() {
|
void convertShouldSetGroupIdAndArtifactIdFromRequest() {
|
||||||
ProjectRequest request = createProjectRequest();
|
ProjectRequest request = createProjectRequest();
|
||||||
@@ -147,14 +140,6 @@ class DefaultProjectRequestToDescriptionConverterTests {
|
|||||||
assertThat(description.getVersion()).isEqualTo("1.0.2-SNAPSHOT");
|
assertThat(description.getVersion()).isEqualTo("1.0.2-SNAPSHOT");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataOnEmptyVersion() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setVersion(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getVersion()).isEqualTo("0.0.1-SNAPSHOT");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void convertShouldSetBaseDirectoryFromRequest() {
|
void convertShouldSetBaseDirectoryFromRequest() {
|
||||||
ProjectRequest request = createProjectRequest();
|
ProjectRequest request = createProjectRequest();
|
||||||
@@ -199,13 +184,6 @@ class DefaultProjectRequestToDescriptionConverterTests {
|
|||||||
assertThat(description.getPlatformVersion()).isEqualTo(Version.parse("2.0.3"));
|
assertThat(description.getPlatformVersion()).isEqualTo(Version.parse("2.0.3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultPlatformVersionFromMetadata() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getPlatformVersion()).isEqualTo(Version.parse("2.1.1.RELEASE"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void convertShouldSetLanguageForProjectDescriptionFromRequest() {
|
void convertShouldSetLanguageForProjectDescriptionFromRequest() {
|
||||||
ProjectRequest request = createProjectRequest();
|
ProjectRequest request = createProjectRequest();
|
||||||
@@ -215,148 +193,6 @@ class DefaultProjectRequestToDescriptionConverterTests {
|
|||||||
assertThat(description.getLanguage().jvmVersion()).isEqualTo("1.8");
|
assertThat(description.getLanguage().jvmVersion()).isEqualTo("1.8");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataOnEmptyGroup() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setGroupId(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getGroupId()).isEqualTo("com.example");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataOnEmptyArtifact() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setArtifactId("");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getArtifactId()).isEqualTo("demo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataOnEmptyName() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setName(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getName()).isEqualTo("demo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataOnEmptyDescription() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setDescription(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getDescription()).isEqualTo("Demo project for Spring Boot");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataOnEmptyPackageName() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setPackageName(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getPackageName()).isEqualTo("com.example.demo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataWhenGeneratingPackageNameWithEmptyGroup() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setGroupId(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getPackageName()).isEqualTo("com.example.demo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convertShouldUseDefaultFromMetadataWhenGeneratingPackageNameWithEmptyArtifact() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
request.setArtifactId(" ");
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getPackageName()).isEqualTo("com.example.demo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void baseDirWhenNotSameAsArtifactIdShouldNotBeCleaned() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct ! ID @";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getBaseDirectory()).isEqualTo(request.getBaseDir());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void baseDirWhenSameAsArtifactIdShouldBeCleaned() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct ! ID @";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
request.setBaseDir(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getBaseDirectory()).isEqualTo("correct-ID");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void nameWhenNotSameAsArtifactIdShouldNotBeCleaned() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct ! ID @";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getName()).isEqualTo(request.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void nameWhenSameAsArtifactIdShouldBeCleaned() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct ! ID @";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
request.setName(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getName()).isEqualTo("correct-ID");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void artifactIdWhenHasValidCharsOnlyShouldNotBeCleaned() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct_test";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
request.setBaseDir(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getArtifactId()).isEqualTo("correct_test");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void artifactIdWhenInvalidShouldBeCleanedWithHyphenDelimiter() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct ! ID @";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
request.setBaseDir(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getArtifactId()).isEqualTo("correct-ID");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void artifactIdWhenCleanedShouldNotContainHyphenBeforeOrAfterValidSpecialCharacter() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String artifactId = "correct !_!ID @";
|
|
||||||
request.setArtifactId(artifactId);
|
|
||||||
request.setBaseDir(artifactId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getArtifactId()).isEqualTo("correct_ID");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void groupIdWhenInvalidShouldBeCleanedWithDotDelimiter() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String groupId = "correct ! ID12 @";
|
|
||||||
request.setGroupId(groupId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getGroupId()).isEqualTo("correct.ID12");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void groupIdWhenHasValidCharactersOnlyShouldNotBeCleaned() {
|
|
||||||
ProjectRequest request = createProjectRequest();
|
|
||||||
String groupId = "correct_ID12";
|
|
||||||
request.setGroupId(groupId);
|
|
||||||
ProjectDescription description = this.converter.convert(request, this.metadata);
|
|
||||||
assertThat(description.getGroupId()).isEqualTo("correct_ID12");
|
|
||||||
}
|
|
||||||
|
|
||||||
private ProjectRequest createProjectRequest() {
|
private ProjectRequest createProjectRequest() {
|
||||||
WebProjectRequest request = new WebProjectRequest();
|
WebProjectRequest request = new WebProjectRequest();
|
||||||
request.initialize(this.metadata);
|
request.initialize(this.metadata);
|
||||||
|
@@ -0,0 +1,182 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2020 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
|
||||||
|
*
|
||||||
|
* https://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.web.project;
|
||||||
|
|
||||||
|
import io.spring.initializr.generator.project.MutableProjectDescription;
|
||||||
|
import io.spring.initializr.generator.test.InitializrMetadataTestBuilder;
|
||||||
|
import io.spring.initializr.generator.version.Version;
|
||||||
|
import io.spring.initializr.metadata.InitializrMetadata;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MetadataProjectDescriptionCustomizer}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
class MetadataProjectDescriptionCustomizerTests {
|
||||||
|
|
||||||
|
private InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().build();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultApplicationNameFromMetadata() {
|
||||||
|
assertThat(customize(new MutableProjectDescription()).getApplicationName()).isEqualTo("Application");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldSetApplicationNameUsingNameWhenAbsent() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setName("MyTest");
|
||||||
|
assertThat(customize(description).getApplicationName()).isEqualTo("MyTestApplication");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultPlatformVersionFromMetadata() {
|
||||||
|
assertThat(customize(new MutableProjectDescription()).getPlatformVersion())
|
||||||
|
.isEqualTo(Version.parse("2.1.1.RELEASE"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataOnEmptyGroup() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setGroupId(" ");
|
||||||
|
assertThat(customize(description).getGroupId()).isEqualTo("com.example");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataOnEmptyArtifact() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("");
|
||||||
|
assertThat(customize(description).getArtifactId()).isEqualTo("demo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataOnEmptyName() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setName(" ");
|
||||||
|
assertThat(customize(description).getName()).isEqualTo("demo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataOnEmptyDescription() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setDescription(" ");
|
||||||
|
assertThat(customize(description).getDescription()).isEqualTo("Demo project for Spring Boot");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataOnEmptyPackageName() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setPackageName(" ");
|
||||||
|
assertThat(customize(description).getPackageName()).isEqualTo("com.example.demo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataWhenGeneratingPackageNameWithEmptyGroup() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setGroupId(" ");
|
||||||
|
assertThat(customize(description).getPackageName()).isEqualTo("com.example.demo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataWhenGeneratingPackageNameWithEmptyArtifact() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId(" ");
|
||||||
|
assertThat(customize(description).getPackageName()).isEqualTo("com.example.demo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldUseDefaultFromMetadataOnEmptyVersion() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setVersion(" ");
|
||||||
|
assertThat(customize(description).getVersion()).isEqualTo("0.0.1-SNAPSHOT");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldNotCleanBaseDirWhenNotSameAsArtifactId() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct ! ID @");
|
||||||
|
description.setBaseDirectory("test");
|
||||||
|
assertThat(customize(description).getBaseDirectory()).isEqualTo("test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldCleanBaseDirWhenSameAsArtifactId() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct ! ID @");
|
||||||
|
description.setBaseDirectory("correct ! ID @");
|
||||||
|
assertThat(customize(description).getBaseDirectory()).isEqualTo("correct-ID");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldNotCleanNameWhenNotSameAsArtifactId() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct ! ID @");
|
||||||
|
description.setName("test");
|
||||||
|
assertThat(customize(description).getName()).isEqualTo("test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldCleanNameWhenSameAsArtifactId() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct ! ID @");
|
||||||
|
description.setName("correct ! ID @");
|
||||||
|
assertThat(customize(description).getName()).isEqualTo("correct-ID");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldNotCleanArtifactIdWithValidChars() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct_test");
|
||||||
|
assertThat(customize(description).getArtifactId()).isEqualTo("correct_test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldCleanInvalidArtifactIdWithHyphenSeparator() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct ! ID @");
|
||||||
|
assertThat(customize(description).getArtifactId()).isEqualTo("correct-ID");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeWithCleanedArtifactIdShouldNotContainHyphenBeforeOrAfterValidSpecialCharacter() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setArtifactId("correct !_!ID @");
|
||||||
|
assertThat(customize(description).getArtifactId()).isEqualTo("correct_ID");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldNotCleanGroupIdWithValidChars() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setGroupId("correct.ID12");
|
||||||
|
assertThat(customize(description).getGroupId()).isEqualTo("correct.ID12");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customizeShouldCleanInvalidGroupIdWithDotDelimiter() {
|
||||||
|
MutableProjectDescription description = new MutableProjectDescription();
|
||||||
|
description.setGroupId("correct ! ID12 @");
|
||||||
|
assertThat(customize(description).getGroupId()).isEqualTo("correct.ID12");
|
||||||
|
}
|
||||||
|
|
||||||
|
MutableProjectDescription customize(MutableProjectDescription description) {
|
||||||
|
new MetadataProjectDescriptionCustomizer(this.metadata).customize(description);
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user