mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Identify reserved keywords in package name
See gh-1018
This commit is contained in:
parent
75b59394b2
commit
e72b187d17
@ -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.
|
||||
*/
|
||||
|
@ -16,9 +16,14 @@
|
||||
|
||||
package io.spring.initializr.metadata;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import io.spring.initializr.generator.version.Version;
|
||||
import io.spring.initializr.metadata.InitializrConfiguration.Env.Kotlin;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -26,9 +31,18 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link InitializrConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Chris Bono
|
||||
*/
|
||||
class InitializrConfigurationTests {
|
||||
|
||||
// Taken from https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9
|
||||
private static final String[] RESERVED_KEYWORDS = { "abstract", "assert", "boolean", "break", "byte", "case",
|
||||
"catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends",
|
||||
"false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int",
|
||||
"interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
|
||||
"short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
|
||||
"try", "true", "void", "volatile", "while" };
|
||||
|
||||
private final InitializrConfiguration properties = new InitializrConfiguration();
|
||||
|
||||
@Test
|
||||
@ -171,6 +185,37 @@ class InitializrConfigurationTests {
|
||||
assertThat(this.properties.cleanPackageName("org.springframework", "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("reservedKeywords")
|
||||
void generatePackageNameReservedKeywordsMiddleOfPackageName(final String keyword) {
|
||||
final String badPackageName = String.format("com.%s.foo", keyword);
|
||||
assertThat(this.properties.cleanPackageName(badPackageName, "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("reservedKeywords")
|
||||
void generatePackageNameReservedKeywordsStartOfPackageName(final String keyword) {
|
||||
final String badPackageName = String.format("%s.com.foo", keyword);
|
||||
assertThat(this.properties.cleanPackageName(badPackageName, "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("reservedKeywords")
|
||||
void generatePackageNameReservedKeywordsEndOfPackageName(final String keyword) {
|
||||
final String badPackageName = String.format("com.foo.%s", keyword);
|
||||
assertThat(this.properties.cleanPackageName(badPackageName, "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("reservedKeywords")
|
||||
void generatePackageNameReservedKeywordsEntirePackageName(final String keyword) {
|
||||
assertThat(this.properties.cleanPackageName(keyword, "com.example")).isEqualTo("com.example");
|
||||
}
|
||||
|
||||
private static Stream<String> reservedKeywords() {
|
||||
return Arrays.stream(RESERVED_KEYWORDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateArtifactRepository() {
|
||||
this.properties.getEnv().setArtifactRepository("http://foo/bar");
|
||||
|
Loading…
Reference in New Issue
Block a user