Fix package name cleanup algorithm with version

This commit improves the cleanup algorithm to accept artifactId with
versions in them as it's used as part of the package name by
default. `foo-1.4.5` now generates `foo145`.

Closes gh-436
This commit is contained in:
Stephane Nicoll
2017-09-09 14:49:08 +02:00
parent 9976d8e20d
commit 444a9f461b
3 changed files with 42 additions and 3 deletions

View File

@@ -108,8 +108,16 @@ public class InitializrConfiguration {
} }
static String cleanPackageName(String packageName) { static String cleanPackageName(String packageName) {
return String.join(".", packageName.trim().replaceAll("-", "").split("\\W+")) String[] elements = packageName.trim().replaceAll("-", "").split("\\W+");
.replaceAll("\\.[0-9]+", "."); StringBuilder sb = new StringBuilder();
for (String element : elements) {
element = element.replaceFirst("^[0-9]+(?!$)", "");
if (!element.matches("[0-9]+") && sb.length() > 0) {
sb.append(".");
}
sb.append(element);
}
return sb.toString();
} }
private static String unsplitWords(String text) { private static String unsplitWords(String text) {

View File

@@ -206,6 +206,31 @@ public class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
.isJavaProject("org/acme/foo", "DemoApplication"); .isJavaProject("org/acme/foo", "DemoApplication");
} }
@Test
public void cleanPackageNameWithGroupIdAndArtifactIdWithVersion() {
ProjectRequest request = createProjectRequest("web");
request.setGroupId("org.acme");
request.setArtifactId("foo-1.4.5");
assertProjectWithPackageNameWithVersion(request);
}
@Test
public void cleanPackageNameWithInvalidPackageName() {
ProjectRequest request = createProjectRequest("web");
request.setGroupId("org.acme");
request.setArtifactId("foo");
request.setPackageName("org.acme.foo-1.4.5");
assertProjectWithPackageNameWithVersion(request);
}
private void assertProjectWithPackageNameWithVersion(ProjectRequest request) {
generateProject(request)
.isJavaProject("org/acme/foo145", "DemoApplication")
.sourceCodeAssert(
"src/main/java/org/acme/foo145/DemoApplication.java")
.contains("package org.acme.foo145;");
}
@Test @Test
public void springBoot11UseEnableAutoConfigurationJava() { public void springBoot11UseEnableAutoConfigurationJava() {
ProjectRequest request = createProjectRequest("web"); ProjectRequest request = createProjectRequest("web");

View File

@@ -139,7 +139,13 @@ public class InitializrConfigurationTests {
@Test @Test
public void generatePackageNameInvalidStartCharacter() { public void generatePackageNameInvalidStartCharacter() {
assertEquals("com.example", this.properties.cleanPackageName("0om.foo", "com.example")); assertEquals("com.foo", this.properties.cleanPackageName("0com.foo", "com.example"));
}
@Test
public void generatePackageNameVersion() {
assertEquals("com.foo.test145", this.properties.cleanPackageName(
"com.foo.test-1.4.5", "com.example"));
} }
@Test @Test