mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +08:00
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:
@@ -108,8 +108,16 @@ public class InitializrConfiguration {
|
||||
}
|
||||
|
||||
static String cleanPackageName(String packageName) {
|
||||
return String.join(".", packageName.trim().replaceAll("-", "").split("\\W+"))
|
||||
.replaceAll("\\.[0-9]+", ".");
|
||||
String[] elements = packageName.trim().replaceAll("-", "").split("\\W+");
|
||||
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) {
|
||||
|
@@ -206,6 +206,31 @@ public class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
|
||||
.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
|
||||
public void springBoot11UseEnableAutoConfigurationJava() {
|
||||
ProjectRequest request = createProjectRequest("web");
|
||||
|
@@ -139,7 +139,13 @@ public class InitializrConfigurationTests {
|
||||
|
||||
@Test
|
||||
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
|
||||
|
Reference in New Issue
Block a user