|
| 1 | +package org.codehaus.plexus.compiler.eclipse; |
| 2 | + |
| 3 | +import javax.inject.Inject; |
| 4 | +import javax.inject.Named; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import org.codehaus.plexus.compiler.Compiler; |
| 11 | +import org.codehaus.plexus.compiler.CompilerConfiguration; |
| 12 | +import org.codehaus.plexus.compiler.CompilerException; |
| 13 | +import org.codehaus.plexus.testing.PlexusTest; |
| 14 | +import org.codehaus.plexus.util.FileUtils; |
| 15 | +import org.hamcrest.MatcherAssert; |
| 16 | +import org.hamcrest.Matchers; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.parallel.Isolated; |
| 21 | + |
| 22 | +import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | + |
| 25 | +@PlexusTest |
| 26 | +@Isolated("changes static variable") |
| 27 | +public class EclipseCompilerUnsupportedJdkTest { |
| 28 | + static final boolean IS_JDK_SUPPORTED = EclipseJavaCompiler.isJdkSupported; |
| 29 | + |
| 30 | + @Inject |
| 31 | + @Named("eclipse") |
| 32 | + Compiler compiler; |
| 33 | + |
| 34 | + @BeforeAll |
| 35 | + public static void setUpClass() { |
| 36 | + EclipseJavaCompiler.isJdkSupported = false; |
| 37 | + } |
| 38 | + |
| 39 | + @AfterAll |
| 40 | + public static void cleanUpClass() { |
| 41 | + EclipseJavaCompiler.isJdkSupported = IS_JDK_SUPPORTED; |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testUnsupportedJdk() { |
| 46 | + CompilerException error = assertThrows(CompilerException.class, () -> compiler.performCompile(getConfig())); |
| 47 | + MatcherAssert.assertThat(error.getMessage(), Matchers.containsString("ECJ needs JRE 17+")); |
| 48 | + } |
| 49 | + |
| 50 | + private CompilerConfiguration getConfig() throws Exception { |
| 51 | + String sourceDir = getBasedir() + "/src/test-input/src/main"; |
| 52 | + Set<File> sourceFiles = FileUtils.getFileNames(new File(sourceDir), "**/*.java", null, false, true).stream() |
| 53 | + .map(File::new) |
| 54 | + .collect(Collectors.toSet()); |
| 55 | + CompilerConfiguration compilerConfig = new CompilerConfiguration(); |
| 56 | + compilerConfig.addSourceLocation(sourceDir); |
| 57 | + compilerConfig.setOutputLocation(getBasedir() + "/target/eclipse/classes"); |
| 58 | + compilerConfig.setSourceFiles(sourceFiles); |
| 59 | + compilerConfig.setTargetVersion("1.8"); |
| 60 | + compilerConfig.setSourceVersion("1.8"); |
| 61 | + return compilerConfig; |
| 62 | + } |
| 63 | +} |
0 commit comments