Skip to content

Commit 79cc4c9

Browse files
SCANJLIB-246 Improve the error message when the JRE provisioning is b… (#223)
1 parent fe2c657 commit 79cc4c9

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/forked/JavaRunner.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
public class JavaRunner {
3737
private static final Logger LOG = LoggerFactory.getLogger(JavaRunner.class);
3838

39+
static final String JRE_VERSION_ERROR = "The version of the custom JRE provided to the SonarScanner using the 'sonar.scanner.javaExePath' parameter is incompatible " +
40+
"with your SonarQube target. You may need to upgrade the version of Java that executes the scanner. " +
41+
"Refer to https://docs.sonarsource.com/sonarqube-community-build/analyzing-source-code/scanners/scanner-environment/general-requirements/ for more details.";
42+
3943
private final Path javaExecutable;
4044
private final JreCacheHit jreCacheHit;
4145

@@ -68,6 +72,7 @@ public boolean execute(List<String> args, @Nullable String input, Consumer<Strin
6872
var exitCode = process.waitFor();
6973
stdoutConsummer.join();
7074
stdErrConsummer.join();
75+
7176
if (exitCode != 0) {
7277
LOG.debug("Java command exited with code {}", process.exitValue());
7378
return false;
@@ -95,7 +100,13 @@ public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
95100
@Override
96101
public void run() {
97102
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
98-
.forEach(consumer);
103+
.forEach(line -> {
104+
consumer.accept(line);
105+
if (line.contains("UnsupportedClassVersionError")) {
106+
LOG.error(JRE_VERSION_ERROR);
107+
}
108+
});
99109
}
110+
100111
}
101112
}

lib/src/test/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapperTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,15 @@ void should_set_deprecated_ssl_properties() {
403403

404404
when(httpConfig.getSslConfig())
405405
.thenReturn(new SslConfig(
406-
new CertificateStore(Paths.get("some/keystore.p12"), "keystorePass"),
407-
new CertificateStore(Paths.get("some/truststore.p12"), "truststorePass")));
406+
new CertificateStore(Paths.get("some", "keystore.p12"), "keystorePass"),
407+
new CertificateStore(Paths.get("some", "truststore.p12"), "truststorePass")));
408408

409409
underTest.adaptDeprecatedPropertiesForInProcessBootstrapping(Map.of(), httpConfig);
410410

411411
assertThat(System.getProperties()).contains(
412-
entry("javax.net.ssl.keyStore", "some/keystore.p12"),
412+
entry("javax.net.ssl.keyStore", Paths.get("some", "keystore.p12").toString()),
413413
entry("javax.net.ssl.keyStorePassword", "keystorePass"),
414-
entry("javax.net.ssl.trustStore", "some/truststore.p12"),
414+
entry("javax.net.ssl.trustStore", Paths.get("some", "truststore.p12").toString()),
415415
entry("javax.net.ssl.trustStorePassword", "truststorePass"));
416416
}
417417

lib/src/test/java/org/sonarsource/scanner/lib/internal/facade/forked/JavaRunnerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
import java.util.List;
2424
import java.util.concurrent.ConcurrentLinkedDeque;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.condition.EnabledOnOs;
27+
import org.junit.jupiter.api.condition.OS;
2628
import org.junit.jupiter.api.extension.RegisterExtension;
2729
import org.slf4j.event.Level;
2830
import testutils.LogTester;
2931

3032
import static org.assertj.core.api.Assertions.assertThat;
3133
import static org.assertj.core.api.Assertions.assertThatThrownBy;
34+
import static org.sonarsource.scanner.lib.internal.facade.forked.JavaRunner.JRE_VERSION_ERROR;
3235

3336
class JavaRunnerTest {
3437

@@ -84,4 +87,24 @@ void execute_shouldReturnFalseWhenNonZeroExitCode() {
8487
assertThat(runner.execute(command, null, stdOut::add)).isFalse();
8588
}
8689

90+
@Test
91+
@EnabledOnOs(OS.WINDOWS)
92+
void execute_shouldLogUnsupportedClassVersionError_whenOsIsWindows() {
93+
JavaRunner runner = new JavaRunner(Paths.get("cmd.exe"), JreCacheHit.DISABLED);
94+
List<String> command = List.of("/c", "echo UnsupportedClassVersionError 1>&2");
95+
96+
assertThat(runner.execute(command, null, stdOut::add)).isTrue();
97+
assertThat(logTester.logs(Level.ERROR)).contains(JRE_VERSION_ERROR);
98+
}
99+
100+
@Test
101+
@EnabledOnOs(OS.LINUX)
102+
void execute_shouldLogUnsupportedClassVersionError_whenOsIsLinux() {
103+
JavaRunner runner = new JavaRunner(Paths.get("sh"), JreCacheHit.DISABLED);
104+
List<String> command = List.of("-c", ">&2 echo UnsupportedClassVersionError");
105+
106+
assertThat(runner.execute(command, null, stdOut::add)).isTrue();
107+
assertThat(logTester.logs(Level.ERROR)).contains(JRE_VERSION_ERROR);
108+
}
109+
87110
}

0 commit comments

Comments
 (0)