Skip to content

Commit

Permalink
[MCOMPILER-591] testCompile - fix detections of target less than 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 6, 2024
1 parent 15905f3 commit cf697e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Expand Up @@ -182,8 +182,14 @@ under the License.
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -111,9 +111,6 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {

static final String DEFAULT_TARGET = "1.8";

// Used to compare with older targets
static final String MODULE_INFO_TARGET = "1.9";

// ----------------------------------------------------------------------
// Configurables
// ----------------------------------------------------------------------
Expand Down
Expand Up @@ -316,13 +316,13 @@ protected void preparePaths(Set<File> sourceFiles) {
return;
}
if (StringUtils.isNotEmpty(getRelease())) {
if (Integer.parseInt(getRelease()) < 9) {
if (isOlderThanJDK9(getRelease())) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
return;
}
} else if (Double.parseDouble(getTarget()) < Double.parseDouble(MODULE_INFO_TARGET)) {
} else if (isOlderThanJDK9(getTarget())) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
Expand Down Expand Up @@ -436,6 +436,14 @@ protected SourceInclusionScanner getSourceInclusionScanner(String inputFileEndin
return scanner;
}

static boolean isOlderThanJDK9(String version) {
if (version.startsWith("1.")) {
return Integer.parseInt(version.substring(2)) < 9;
}

return Integer.parseInt(version) < 9;
}

protected String getSource() {
return testSource == null ? source : testSource;
}
Expand Down
Expand Up @@ -24,11 +24,15 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

import org.apache.maven.plugin.compiler.stubs.CompilerManagerStub;
import org.apache.maven.plugin.testing.junit5.InjectMojo;
import org.apache.maven.plugin.testing.junit5.MojoTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenProject;
import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenSession;
Expand Down Expand Up @@ -186,4 +190,20 @@ private void setUpCompilerMojoTestEnv(TestCompilerMojo mojo) throws Exception {
setVariableValueToObject(mojo, "source", AbstractCompilerMojo.DEFAULT_SOURCE);
setVariableValueToObject(mojo, "target", AbstractCompilerMojo.DEFAULT_TARGET);
}

static Stream<Arguments> olderThanJDK9() {
return Stream.of(
Arguments.of("1.8", true),
Arguments.of("8", true),
Arguments.of("1.9", false),
Arguments.of("1.9", false),
Arguments.of("9", false),
Arguments.of("11", false));
}

@ParameterizedTest
@MethodSource
void olderThanJDK9(String version, boolean expected) {
assertEquals(expected, TestCompilerMojo.isOlderThanJDK9(version));
}
}

0 comments on commit cf697e5

Please sign in to comment.