Skip to content

Commit

Permalink
Throw an exception if the result can't be validated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored and MarkEWaite committed Mar 13, 2024
1 parent eef0b1f commit 9ae61e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import javax.lang.model.SourceVersion;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
Expand Down Expand Up @@ -182,11 +183,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

static String legalizePackageName(@NonNull String input) {
static String legalizePackageName(@NonNull String input) throws MojoFailureException {
String result = input.replace('-', '_');
if (!result.isEmpty() && Character.isDigit(result.charAt(0))) {
result = "_" + result;
}
if (!SourceVersion.isName(result)) {
throw new MojoFailureException(
"Could not convert " + input
+ " to a legal java package name. Please override \"injectedTestPackage\" with a valid java package name.");
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.jenkinsci.maven.plugins.hpi;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;

public class TestInsertionMojoTest {

@Test
public void legalizePackageName() {
public void legalizePackageName() throws Exception {
assertEquals(
"org.jenkinsci.maven.plugins.hpi",
TestInsertionMojo.legalizePackageName("org.jenkinsci.maven.plugins.hpi"));
assertEquals(
"_123org.jenkins_ci.maven.plugins.hpi",
TestInsertionMojo.legalizePackageName("123org.jenkins-ci.maven.plugins.hpi"));
assertThrows(
MojoExecutionException.class,
() -> TestInsertionMojo.legalizePackageName("org.jenkinsci%maven.plugins.hpi"));
}
}

0 comments on commit 9ae61e8

Please sign in to comment.