Skip to content

Commit

Permalink
Use underscores for illegal characters when cleaning package names
Browse files Browse the repository at this point in the history
Closes gh-1339
  • Loading branch information
mhalbritter committed May 22, 2024
1 parent 36f4224 commit 12bfadc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -42,6 +42,7 @@
*
* @author Stephane Nicoll
* @author Chris Bono
* @author Moritz Halbritter
*/
public class InitializrConfiguration {

Expand Down Expand Up @@ -126,11 +127,11 @@ public String cleanPackageName(String packageName, String defaultPackageName) {
}

static String cleanPackageName(String packageName) {
String[] elements = packageName.trim().replaceAll("-", "").split("\\W+");
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) {
element = element.replaceFirst("^[0-9]+(?!$)", "_");
if (!element.matches("[0-9]+") && !sb.isEmpty()) {
sb.append(".");
}
sb.append(element);
Expand All @@ -140,7 +141,7 @@ static String cleanPackageName(String packageName) {

private static String unsplitWords(String text) {
return String.join("",
Arrays.stream(text.split("(_|-| |:)+")).map(StringUtils::capitalize).toArray(String[]::new));
Arrays.stream(text.split("([_\\- :])+")).map(StringUtils::capitalize).toArray(String[]::new));
}

private static String splitCamelCase(String text) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -133,8 +133,8 @@ void generatePackageNameSimpleColon() {
}

@Test
void generatePackageNameMultipleDashers() {
assertThat(this.properties.cleanPackageName("com.foo--bar", "com.example")).isEqualTo("com.foobar");
void generatePackageNameMultipleDashes() {
assertThat(this.properties.cleanPackageName("com.foo--bar", "com.example")).isEqualTo("com.foo__bar");
}

@Test
Expand All @@ -159,12 +159,12 @@ void generatePackageNameWhitespaces() {

@Test
void generatePackageNameInvalidStartCharacter() {
assertThat(this.properties.cleanPackageName("0com.foo", "com.example")).isEqualTo("com.foo");
assertThat(this.properties.cleanPackageName("0com.foo", "com.example")).isEqualTo("_com.foo");
}

@Test
void generatePackageNameVersion() {
assertThat(this.properties.cleanPackageName("com.foo.test-1.4.5", "com.example")).isEqualTo("com.foo.test145");
assertThat(this.properties.cleanPackageName("com.foo.test-1.4.5", "com.example")).isEqualTo("com.foo.test_145");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -204,12 +204,11 @@ void stripInvalidCharsFromPackage() {
InitializrMetadata metadata = initializeMetadata();
metadata.getGroupId().setContent("org.acme");
metadata.getArtifactId().setContent("2foo.bar");
assertThat(metadata.getPackageName().getContent()).isEqualTo("org.acme.foo.bar");

assertThat(metadata.getPackageName().getContent()).isEqualTo("org.acme._foo.bar");
metadata = initializeMetadata();
metadata.getGroupId().setContent("org.ac-me");
metadata.getArtifactId().setContent("foo-bar");
assertThat(metadata.getPackageName().getContent()).isEqualTo("org.acme.foobar");
assertThat(metadata.getPackageName().getContent()).isEqualTo("org.ac_me.foo_bar");
}

private InitializrMetadata initializeMetadata() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -75,7 +75,7 @@ void tgzProjectWithLongFilenames() {
assertArchiveResponseHeaders(entity, MediaType.valueOf("application/x-compress"), "spring-boot-service.tar.gz");
ProjectStructure project = tgzProjectAssert(entity.getBody());
assertThat(project).containsFiles(
"spring-boot-service/src/test/java/com/spring/boot/service/springbootservice/SpringBootServiceApplicationTests.java");
"spring-boot-service/src/test/java/com/spring/boot/service/spring_boot_service/SpringBootServiceApplicationTests.java");
}

private void assertArchiveResponseHeaders(ResponseEntity<byte[]> entity, MediaType contentType, String fileName) {
Expand Down

0 comments on commit 12bfadc

Please sign in to comment.