Skip to content

Commit

Permalink
SCANJLIB-246 Improve the error message when the JRE provisioning is b… (
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine-vinot-sonarsource committed Jan 10, 2025
1 parent 2858ffd commit bbfbc75
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
public class JavaRunner {
private static final Logger LOG = LoggerFactory.getLogger(JavaRunner.class);

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

private final Path javaExecutable;
private final JreCacheHit jreCacheHit;

Expand Down Expand Up @@ -68,6 +72,7 @@ public boolean execute(List<String> args, @Nullable String input, Consumer<Strin
var exitCode = process.waitFor();
stdoutConsummer.join();
stdErrConsummer.join();

if (exitCode != 0) {
LOG.debug("Java command exited with code {}", process.exitValue());
return false;
Expand Down Expand Up @@ -95,7 +100,13 @@ public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
.forEach(consumer);
.forEach(line -> {
consumer.accept(line);
if (line.contains("UnsupportedClassVersionError")) {
LOG.error(JRE_VERSION_ERROR);
}
});
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,15 @@ void should_set_deprecated_ssl_properties() {

when(httpConfig.getSslConfig())
.thenReturn(new SslConfig(
new CertificateStore(Paths.get("some/keystore.p12"), "keystorePass"),
new CertificateStore(Paths.get("some/truststore.p12"), "truststorePass")));
new CertificateStore(Paths.get("some", "keystore.p12"), "keystorePass"),
new CertificateStore(Paths.get("some", "truststore.p12"), "truststorePass")));

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

assertThat(System.getProperties()).contains(
entry("javax.net.ssl.keyStore", "some/keystore.p12"),
entry("javax.net.ssl.keyStore", Paths.get("some", "keystore.p12").toString()),
entry("javax.net.ssl.keyStorePassword", "keystorePass"),
entry("javax.net.ssl.trustStore", "some/truststore.p12"),
entry("javax.net.ssl.trustStore", Paths.get("some", "truststore.p12").toString()),
entry("javax.net.ssl.trustStorePassword", "truststorePass"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import testutils.LogTester;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonarsource.scanner.lib.internal.facade.forked.JavaRunner.JRE_VERSION_ERROR;

class JavaRunnerTest {

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

@Test
@EnabledOnOs(OS.WINDOWS)
void execute_shouldLogUnsupportedClassVersionError_whenOsIsWindows() {
JavaRunner runner = new JavaRunner(Paths.get("cmd.exe"), JreCacheHit.DISABLED);
List<String> command = List.of("/c", "echo UnsupportedClassVersionError 1>&2");

assertThat(runner.execute(command, null, stdOut::add)).isTrue();
assertThat(logTester.logs(Level.ERROR)).contains(JRE_VERSION_ERROR);
}

@Test
@EnabledOnOs(OS.LINUX)
void execute_shouldLogUnsupportedClassVersionError_whenOsIsLinux() {
JavaRunner runner = new JavaRunner(Paths.get("sh"), JreCacheHit.DISABLED);
List<String> command = List.of("-c", ">&2 echo UnsupportedClassVersionError");

assertThat(runner.execute(command, null, stdOut::add)).isTrue();
assertThat(logTester.logs(Level.ERROR)).contains(JRE_VERSION_ERROR);
}

}

0 comments on commit bbfbc75

Please sign in to comment.