From 69c0e1a7d88516244e857705d583680bfe08f184 Mon Sep 17 00:00:00 2001 From: erwan-serandour Date: Wed, 30 Oct 2024 13:50:25 +0100 Subject: [PATCH] SCANMAVEN-233 check that `sonar.java.jdkHome` has default value even without configuration from user (#249) --- its/projects/jdkHome/defaultValue/pom.xml | 8 ++++++++ .../src/main/java/sample/Sample.java | 12 +++++++++++ .../src/test/java/sample/SampleTest.java | 12 +++++++++++ .../com/sonar/maven/it/suite/JavaTest.java | 20 +++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 its/projects/jdkHome/defaultValue/pom.xml create mode 100644 its/projects/jdkHome/defaultValue/src/main/java/sample/Sample.java create mode 100644 its/projects/jdkHome/defaultValue/src/test/java/sample/SampleTest.java diff --git a/its/projects/jdkHome/defaultValue/pom.xml b/its/projects/jdkHome/defaultValue/pom.xml new file mode 100644 index 00000000..4012f2dd --- /dev/null +++ b/its/projects/jdkHome/defaultValue/pom.xml @@ -0,0 +1,8 @@ + + 4.0.0 + com.sonarsource.it.samples + extract-jre + 1.0-SNAPSHOT + Sonar :: Integration Tests :: Extract Jre + + diff --git a/its/projects/jdkHome/defaultValue/src/main/java/sample/Sample.java b/its/projects/jdkHome/defaultValue/src/main/java/sample/Sample.java new file mode 100644 index 00000000..0f23f198 --- /dev/null +++ b/its/projects/jdkHome/defaultValue/src/main/java/sample/Sample.java @@ -0,0 +1,12 @@ +package sample; + +public class Sample { + + public Sample(int i) { + int j = i++; + } + + public String myMethod() { + return "hello"; + } +} diff --git a/its/projects/jdkHome/defaultValue/src/test/java/sample/SampleTest.java b/its/projects/jdkHome/defaultValue/src/test/java/sample/SampleTest.java new file mode 100644 index 00000000..baad9f8f --- /dev/null +++ b/its/projects/jdkHome/defaultValue/src/test/java/sample/SampleTest.java @@ -0,0 +1,12 @@ +package sample; + +import org.junit.Test; + +public class SampleTest { + + @Test + public void test() { + new Sample(1).myMethod(); + } + +} diff --git a/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java b/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java index 4d8812ef..94fa906f 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/JavaTest.java @@ -120,6 +120,26 @@ void setJavaVersionProperties() throws IOException { entry("sonar.java.target", "1.8")); } + @Test + void when_java_home_exists_it_is_used_as_default_sonar_java_jdkHome_value() throws IOException { + //fix value, the plugin should not modify them + String javaHomeSystem = System.getProperty("java.home"); + + File outputProps = temp.resolve("out.properties").toFile(); + outputProps.createNewFile(); + File pom = ItUtils.locateProjectPom("jdkHome/defaultValue"); + + MavenBuild build = MavenBuild.create(pom) + .setGoals(sonarGoal()) + .setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath()); + executeBuildAndValidateWithoutCE(build); + + Properties props = getProps(outputProps); + String jdkHome = props.getProperty("sonar.java.jdkHome"); + //the jdk is not configured in the project, so default value should be used + assertThat(jdkHome).isEqualTo(javaHomeSystem); + } + @Test void setJdkHomeFromCompilerExecutableConfiguration() throws IOException { File outputProps = temp.resolve("out.properties").toFile();