|
2 | 2 |
|
3 | 3 | import java.io.BufferedReader;
|
4 | 4 | import java.io.IOException;
|
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
5 | 7 | import java.io.StringReader;
|
| 8 | +import java.nio.charset.StandardCharsets; |
6 | 9 | import java.util.List;
|
| 10 | +import java.util.Properties; |
7 | 11 | import java.util.stream.Stream;
|
8 | 12 |
|
9 | 13 | import org.codehaus.plexus.compiler.CompilerMessage;
|
|
13 | 17 | import org.junit.jupiter.params.ParameterizedTest;
|
14 | 18 | import org.junit.jupiter.params.provider.Arguments;
|
15 | 19 | import org.junit.jupiter.params.provider.MethodSource;
|
| 20 | +import org.opentest4j.AssertionFailedError; |
16 | 21 |
|
17 | 22 | import static org.codehaus.plexus.compiler.javac.JavacCompiler.Messages.*;
|
18 | 23 | import static org.hamcrest.CoreMatchers.endsWith;
|
@@ -121,5 +126,51 @@ void testExtractMajorAndMinorVersion() {
|
121 | 126 | assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("javac 11.0.22"));
|
122 | 127 | assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("11.0.22"));
|
123 | 128 | assertEquals("21", JavacCompiler.extractMajorAndMinorVersion("javac 21"));
|
| 129 | + assertEquals("1.8", JavacCompiler.extractMajorAndMinorVersion("1.3.4\njavac 1.8.0_432")); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testCleanPickedUp() { |
| 134 | + assertEquals( |
| 135 | + "This text contains CRLF\n", |
| 136 | + JavacCompiler.cleanPickedUp("This text contains CRLF\r\n", new Properties())); |
| 137 | + |
| 138 | + // files were generated by using: |
| 139 | + // declare -x JAVA_TOOL_OPTIONS=$'-Daaa\n\n-Dxxx' |
| 140 | + // declare -x _JAVA_OPTIONS="-Dccc" |
| 141 | + // "${JAVA_8_HOME}/bin/javac" -version > |
| 142 | + // plexus-compilers/plexus-compiler-javac/src/test/resources/org/codehaus/plexus/compiler/javac/java8_pickedUp.txt 2>&1 |
| 143 | + // "${JAVA_17_HOME}/bin/javac" -version > |
| 144 | + // plexus-compilers/plexus-compiler-javac/src/test/resources/org/codehaus/plexus/compiler/javac/java17_pickedUp.txt 2>&1 |
| 145 | + // "${JAVA_21_HOME}/bin/javac" -version > |
| 146 | + // plexus-compilers/plexus-compiler-javac/src/test/resources/org/codehaus/plexus/compiler/javac/java21_pickedUp.txt 2>&1 |
| 147 | + Properties envvars = new Properties(); |
| 148 | + envvars.setProperty("JAVA_TOOL_OPTIONS", "-Daaa\r\n\r\n-Dxxx"); |
| 149 | + envvars.setProperty("_JAVA_OPTIONS", "-Dccc"); |
| 150 | + |
| 151 | + assertEquals( |
| 152 | + "javac 1.8.0_432\n", JavacCompiler.cleanPickedUp(readAllLines("java8_pickedUp.txt", "\n"), envvars)); |
| 153 | + assertEquals( |
| 154 | + "javac 17.0.13\n", JavacCompiler.cleanPickedUp(readAllLines("java17_pickedUp.txt", "\r\n"), envvars)); |
| 155 | + assertEquals("javac 21.0.5\n", JavacCompiler.cleanPickedUp(readAllLines("java21_pickedUp.txt", "\r"), envvars)); |
| 156 | + } |
| 157 | + |
| 158 | + private String readAllLines(String resource, String ls) { |
| 159 | + try (InputStream is = this.getClass().getResourceAsStream(resource)) { |
| 160 | + if (is == null) { |
| 161 | + throw new AssertionFailedError("No such resource: " + resource + " in class " + this.getClass()); |
| 162 | + } |
| 163 | + try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); |
| 164 | + BufferedReader br = new BufferedReader(isr)) { |
| 165 | + StringBuilder sb = new StringBuilder(); |
| 166 | + for (String line = null; (line = br.readLine()) != null; ) { |
| 167 | + sb.append(line).append(ls); |
| 168 | + } |
| 169 | + return sb.toString(); |
| 170 | + } |
| 171 | + } catch (IOException e) { |
| 172 | + throw new AssertionFailedError( |
| 173 | + "Could not fetch lines of resource: " + resource + " in class " + this.getClass(), e); |
| 174 | + } |
124 | 175 | }
|
125 | 176 | }
|
0 commit comments