From 12bfadcd13353c7d00e5daa8cb578d81c3c43022 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 22 May 2024 10:48:21 +0200 Subject: [PATCH] Use underscores for illegal characters when cleaning package names Closes gh-1339 --- .../initializr/metadata/InitializrConfiguration.java | 11 ++++++----- .../metadata/InitializrConfigurationTests.java | 10 +++++----- .../initializr/metadata/InitializrMetadataTests.java | 7 +++---- .../ProjectGenerationControllerIntegrationTests.java | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/initializr-metadata/src/main/java/io/spring/initializr/metadata/InitializrConfiguration.java b/initializr-metadata/src/main/java/io/spring/initializr/metadata/InitializrConfiguration.java index 88d18b2422..2a2e2c0d30 100644 --- a/initializr-metadata/src/main/java/io/spring/initializr/metadata/InitializrConfiguration.java +++ b/initializr-metadata/src/main/java/io/spring/initializr/metadata/InitializrConfiguration.java @@ -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. @@ -42,6 +42,7 @@ * * @author Stephane Nicoll * @author Chris Bono + * @author Moritz Halbritter */ public class InitializrConfiguration { @@ -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); @@ -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) { diff --git a/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrConfigurationTests.java b/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrConfigurationTests.java index cfa7ac85e3..5ff9bf096f 100755 --- a/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrConfigurationTests.java +++ b/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrConfigurationTests.java @@ -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. @@ -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 @@ -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 diff --git a/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrMetadataTests.java b/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrMetadataTests.java index a438ec54f5..b4e0a018b2 100755 --- a/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrMetadataTests.java +++ b/initializr-metadata/src/test/java/io/spring/initializr/metadata/InitializrMetadataTests.java @@ -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. @@ -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() { diff --git a/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerIntegrationTests.java b/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerIntegrationTests.java index c5712c1e32..8d74b5a31f 100755 --- a/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerIntegrationTests.java +++ b/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerIntegrationTests.java @@ -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. @@ -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 entity, MediaType contentType, String fileName) {