mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-26 22:25:51 +08:00
Merge pull request #1018 from bono007
* pr/1018: Polish "Identify reserved keywords in package name" Identify reserved keywords in package name Closes gh-1018
This commit is contained in:
@@ -25,6 +25,8 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.lang.model.SourceVersion;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import io.spring.initializr.generator.version.InvalidVersionException;
|
||||
import io.spring.initializr.generator.version.Version;
|
||||
@@ -38,6 +40,7 @@ import org.springframework.util.StringUtils;
|
||||
* Various configuration options used by the service.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Chris Bono
|
||||
*/
|
||||
public class InitializrConfiguration {
|
||||
|
||||
@@ -113,6 +116,9 @@ public class InitializrConfiguration {
|
||||
if (hasInvalidChar(candidate.replace(".", "")) || this.env.invalidPackageNames.contains(candidate)) {
|
||||
return defaultPackageName;
|
||||
}
|
||||
if (hasReservedKeyword(candidate)) {
|
||||
return defaultPackageName;
|
||||
}
|
||||
else {
|
||||
return candidate;
|
||||
}
|
||||
@@ -155,6 +161,10 @@ public class InitializrConfiguration {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean hasReservedKeyword(final String packageName) {
|
||||
return Arrays.stream(packageName.split("\\.")).anyMatch(SourceVersion::isKeyword);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines additional environment settings.
|
||||
*/
|
||||
|
||||
@@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link InitializrConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Chris Bono
|
||||
*/
|
||||
class InitializrConfigurationTests {
|
||||
|
||||
@@ -171,6 +172,26 @@ class InitializrConfigurationTests {
|
||||
assertThat(this.properties.cleanPackageName("org.springframework", "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatePackageNameReservedKeywordsMiddleOfPackageName() {
|
||||
assertThat(this.properties.cleanPackageName("com.return.foo", "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatePackageNameReservedKeywordsStartOfPackageName() {
|
||||
assertThat(this.properties.cleanPackageName("false.com.foo", "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatePackageNameReservedKeywordsEndOfPackageName() {
|
||||
assertThat(this.properties.cleanPackageName("com.foo.null", "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatePackageNameReservedKeywordsEntirePackageName() {
|
||||
assertThat(this.properties.cleanPackageName("public", "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateArtifactRepository() {
|
||||
this.properties.getEnv().setArtifactRepository("http://foo/bar");
|
||||
|
||||
Reference in New Issue
Block a user