CreateProjectMojo - validate className

This commit is contained in:
Rostislav Svoboda
2020-02-01 18:10:46 +01:00
parent cdb9a1d62b
commit 7cfb0d9ef1
2 changed files with 27 additions and 0 deletions

View File

@@ -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());
}

View File

@@ -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");