mirror of
https://github.com/jlengrand/quarkus.git
synced 2026-03-10 08:41:22 +00:00
CreateProjectMojo - validate className
This commit is contained in:
@@ -26,6 +26,8 @@ import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.lang.model.SourceVersion;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.maven.execution.DefaultMavenExecutionRequest;
|
||||
import org.apache.maven.execution.MavenExecutionRequest;
|
||||
@@ -192,6 +194,10 @@ public class CreateProjectMojo extends AbstractMojo {
|
||||
final SourceType sourceType = CreateProject.determineSourceType(extensions);
|
||||
sanitizeOptions(sourceType);
|
||||
|
||||
if (className != null && !isClassNameValid(className)) {
|
||||
throw new MojoExecutionException("Unable to create the project, " + className + " is not valid FQCN.");
|
||||
}
|
||||
|
||||
final Map<String, Object> context = new HashMap<>();
|
||||
context.put("path", path);
|
||||
|
||||
@@ -383,6 +389,10 @@ public class CreateProjectMojo extends AbstractMojo {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isClassNameValid(String className) {
|
||||
return SourceVersion.isName(className) && !SourceVersion.isKeyword(className);
|
||||
}
|
||||
|
||||
private void sanitizeExtensions() {
|
||||
extensions = extensions.stream().filter(Objects::nonNull).map(String::trim).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@@ -133,6 +133,23 @@ public class CreateProjectMojoIT extends QuarkusPlatformAwareMojoTestBase {
|
||||
check(new File(testDir, "src/main/java/org/acme/MyResource.java"), "package org.acme;");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProjectGenerationWithInvalidPackage() throws Exception {
|
||||
testDir = initEmptyProject("projects/project-generation-invalid-package");
|
||||
assertThat(testDir).isDirectory();
|
||||
invoker = initInvoker(testDir);
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.put("projectGroupId", "org.acme");
|
||||
properties.put("projectArtifactId", "acme");
|
||||
properties.put("className", "org.acme.invalid-package-name.MyResource");
|
||||
|
||||
InvocationResult result = setup(properties);
|
||||
|
||||
assertThat(result.getExitCode()).isNotZero();
|
||||
assertThat(new File(testDir, "src/main/java/org/acme")).doesNotExist();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProjectGenerationFromMinimalPomWithResource() throws Exception {
|
||||
testDir = initProject("projects/simple-pom-it", "projects/project-generation-from-empty-pom-with-resource");
|
||||
|
||||
Reference in New Issue
Block a user