From e25fb74d23ea1f9c996805c3ec38b5482eef93b5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 2 Jan 2020 17:16:19 +0100 Subject: [PATCH] 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 --- ...tProjectRequestToDescriptionConverter.java | 98 +--------- .../MetadataProjectDescriptionCustomizer.java | 109 +++++++++++ .../web/project/ProjectGenerationInvoker.java | 2 + ...ectRequestToDescriptionConverterTests.java | 164 ---------------- ...dataProjectDescriptionCustomizerTests.java | 182 ++++++++++++++++++ 5 files changed, 301 insertions(+), 254 deletions(-) create mode 100644 initializr-web/src/main/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizer.java create mode 100644 initializr-web/src/test/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizerTests.java diff --git a/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java b/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java index 82d57e2a..c953eb6c 100644 --- a/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java +++ b/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java @@ -17,7 +17,6 @@ package io.spring.initializr.web.project; import java.util.List; -import java.util.function.Supplier; import java.util.stream.Collectors; 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.support.MetadataBuildItemMapper; -import org.springframework.util.StringUtils; - /** * A default {@link ProjectRequestToDescriptionConverter} implementation that uses the * {@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 char[] VALID_MAVEN_SPECIAL_CHARACTERS = new char[] { '_', '-', '.' }; - @Override public ProjectDescription convert(ProjectRequest request, InitializrMetadata metadata) { MutableProjectDescription description = new MutableProjectDescription(); @@ -70,87 +65,22 @@ public class DefaultProjectRequestToDescriptionConverter List resolvedDependencies = getResolvedDependencies(request, springBootVersion, metadata); validateDependencyRange(springBootVersion, resolvedDependencies); - description.setApplicationName(getApplicationName(request, metadata)); - description.setArtifactId(getArtifactId(request, metadata)); - description.setBaseDirectory(getBaseDirectory(request.getBaseDir(), request.getArtifactId())); + description.setApplicationName(request.getApplicationName()); + description.setArtifactId(request.getArtifactId()); + description.setBaseDirectory(request.getBaseDir()); description.setBuildSystem(getBuildSystem(request, metadata)); - description - .setDescription(determineValue(request.getDescription(), () -> metadata.getDescription().getContent())); - description.setGroupId(getGroupId(request, metadata)); + description.setDescription(request.getDescription()); + description.setGroupId(request.getGroupId()); description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion())); - description.setName(getName(request, metadata)); - description.setPackageName(getPackageName(request, metadata)); + description.setName(request.getName()); + description.setPackageName(request.getPackageName()); description.setPackaging(Packaging.forId(request.getPackaging())); description.setPlatformVersion(Version.parse(springBootVersion)); - description.setVersion(determineValue(request.getVersion(), () -> metadata.getVersion().getContent())); + description.setVersion(request.getVersion()); resolvedDependencies.forEach((dependency) -> description.addDependency(dependency.getId(), MetadataBuildItemMapper.toDependency(dependency))); } - private String determineValue(String candidate, Supplier 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) { validateSpringBootVersion(request); validateType(request.getType(), metadata); @@ -223,18 +153,6 @@ public class DefaultProjectRequestToDescriptionConverter 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) { return (request.getBootVersion() != null) ? request.getBootVersion() : metadata.getBootVersions().getDefault().getId(); diff --git a/initializr-web/src/main/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizer.java b/initializr-web/src/main/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizer.java new file mode 100644 index 00000000..99df47ea --- /dev/null +++ b/initializr-web/src/main/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizer.java @@ -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 fallback) { + return (StringUtils.hasText(candidate)) ? candidate : fallback.get(); + } + +} diff --git a/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectGenerationInvoker.java b/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectGenerationInvoker.java index 22919cb7..b63c8cfb 100644 --- a/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectGenerationInvoker.java +++ b/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectGenerationInvoker.java @@ -184,6 +184,8 @@ public class ProjectGenerationInvoker { context.registerBean(InitializrMetadata.class, () -> metadata); context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata, context.getBean(ProjectDescription.class).getPlatformVersion())); + context.registerBean(MetadataProjectDescriptionCustomizer.class, + () -> new MetadataProjectDescriptionCustomizer(metadata)); } private void publishProjectGeneratedEvent(R request, ProjectGenerationContext context) { diff --git a/initializr-web/src/test/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverterTests.java b/initializr-web/src/test/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverterTests.java index 8a7a126c..15da1e68 100644 --- a/initializr-web/src/test/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverterTests.java +++ b/initializr-web/src/test/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverterTests.java @@ -122,13 +122,6 @@ class DefaultProjectRequestToDescriptionConverterTests { 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 void convertShouldSetGroupIdAndArtifactIdFromRequest() { ProjectRequest request = createProjectRequest(); @@ -147,14 +140,6 @@ class DefaultProjectRequestToDescriptionConverterTests { 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 void convertShouldSetBaseDirectoryFromRequest() { ProjectRequest request = createProjectRequest(); @@ -199,13 +184,6 @@ class DefaultProjectRequestToDescriptionConverterTests { 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 void convertShouldSetLanguageForProjectDescriptionFromRequest() { ProjectRequest request = createProjectRequest(); @@ -215,148 +193,6 @@ class DefaultProjectRequestToDescriptionConverterTests { 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() { WebProjectRequest request = new WebProjectRequest(); request.initialize(this.metadata); diff --git a/initializr-web/src/test/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizerTests.java b/initializr-web/src/test/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizerTests.java new file mode 100644 index 00000000..394de3b7 --- /dev/null +++ b/initializr-web/src/test/java/io/spring/initializr/web/project/MetadataProjectDescriptionCustomizerTests.java @@ -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; + } + +}