From c407e6258cdef00346a206135d8066b999ca4244 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Mon, 28 Oct 2024 17:22:33 +0100 Subject: [PATCH 1/7] add new integration test to check that sonar.java.jdkHome has default value even without configuration from user --- its/projects/jdkHome/extractFromJre/pom.xml | 8 ++++++++ .../src/main/java/sample/Sample.java | 12 ++++++++++++ .../src/test/java/sample/SampleTest.java | 12 ++++++++++++ .../com/sonar/maven/it/suite/JavaTest.java | 19 +++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 its/projects/jdkHome/extractFromJre/pom.xml create mode 100644 its/projects/jdkHome/extractFromJre/src/main/java/sample/Sample.java create mode 100644 its/projects/jdkHome/extractFromJre/src/test/java/sample/SampleTest.java diff --git a/its/projects/jdkHome/extractFromJre/pom.xml b/its/projects/jdkHome/extractFromJre/pom.xml new file mode 100644 index 00000000..4012f2dd --- /dev/null +++ b/its/projects/jdkHome/extractFromJre/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/extractFromJre/src/main/java/sample/Sample.java b/its/projects/jdkHome/extractFromJre/src/main/java/sample/Sample.java new file mode 100644 index 00000000..0f23f198 --- /dev/null +++ b/its/projects/jdkHome/extractFromJre/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/extractFromJre/src/test/java/sample/SampleTest.java b/its/projects/jdkHome/extractFromJre/src/test/java/sample/SampleTest.java new file mode 100644 index 00000000..baad9f8f --- /dev/null +++ b/its/projects/jdkHome/extractFromJre/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..d207ef21 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,24 @@ void setJavaVersionProperties() throws IOException { entry("sonar.java.target", "1.8")); } + @Test + void extractJdkHomeFromJre() throws IOException { + File outputProps = temp.resolve("out.properties").toFile(); + outputProps.createNewFile(); + + File pom = ItUtils.locateProjectPom("jdkHome/extractFromJre"); + MavenBuild build = MavenBuild.create(pom) + .setGoals(sonarGoal()) + .setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath()); + executeBuildAndValidateWithoutCE(build); + + Properties props = getProps(outputProps); + Object value = props.getOrDefault("sonar.java.jdkHome", ""); + if(value instanceof String){ + assertThat((String)value).isNotEmpty(); + } + } + @Test void setJdkHomeFromCompilerExecutableConfiguration() throws IOException { File outputProps = temp.resolve("out.properties").toFile(); @@ -136,6 +154,7 @@ void setJdkHomeFromCompilerExecutableConfiguration() throws IOException { assertThat(props).contains(entry("sonar.java.jdkHome", expected)); } + @Test void setJdkHomeFromGlobalToolchainsPlugin() throws IOException { File outputProps = temp.resolve("out.properties").toFile(); From 926e9821fe05034101727f5b81b91e8b741ad75d Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Mon, 28 Oct 2024 17:26:52 +0100 Subject: [PATCH 2/7] format code --- its/src/test/java/com/sonar/maven/it/suite/JavaTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 d207ef21..635726a7 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 @@ -133,8 +133,8 @@ void extractJdkHomeFromJre() throws IOException { Properties props = getProps(outputProps); Object value = props.getOrDefault("sonar.java.jdkHome", ""); - if(value instanceof String){ - assertThat((String)value).isNotEmpty(); + if (value instanceof String) { + assertThat((String) value).isNotEmpty(); } } @@ -154,7 +154,6 @@ void setJdkHomeFromCompilerExecutableConfiguration() throws IOException { assertThat(props).contains(entry("sonar.java.jdkHome", expected)); } - @Test void setJdkHomeFromGlobalToolchainsPlugin() throws IOException { File outputProps = temp.resolve("out.properties").toFile(); From 1061e7047b59bda265f595f58cd0a7ffdbf06f85 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Tue, 29 Oct 2024 11:13:38 +0100 Subject: [PATCH 3/7] improve after review --- .../com/sonar/maven/it/suite/JavaTest.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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 635726a7..f28fd337 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 @@ -26,6 +26,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Path; +import java.util.Arrays; import java.util.Properties; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -121,23 +122,30 @@ void setJavaVersionProperties() throws IOException { } @Test - void extractJdkHomeFromJre() throws IOException { + void whenPropertyJavaHomeExistsThenItIsUsedAsDefaultValue() throws IOException { + //verify that property java home exists, it may not be pointing to a jdk + if (!System.getProperties().containsKey("java.home") + && !System.getenv().containsKey("JAVA_HOME")) { + return; + } + + File outputProps = temp.resolve("out.properties").toFile(); outputProps.createNewFile(); - File pom = ItUtils.locateProjectPom("jdkHome/extractFromJre"); + MavenBuild build = MavenBuild.create(pom) .setGoals(sonarGoal()) .setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath()); executeBuildAndValidateWithoutCE(build); Properties props = getProps(outputProps); - Object value = props.getOrDefault("sonar.java.jdkHome", ""); - if (value instanceof String) { - assertThat((String) value).isNotEmpty(); - } + String jdkHome = props.getProperty("sonar.java.jdkHome"); + //the jdk is not configured in the project, so default value should be used + assertThat(jdkHome).isIn(Arrays.asList(System.getProperty("java.home"), System.getenv().get("JAVA_HOME"))); } + @Test void setJdkHomeFromCompilerExecutableConfiguration() throws IOException { File outputProps = temp.resolve("out.properties").toFile(); From f6fd6c1a7a97f45ed2d50b9836ff203a039df388 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Tue, 29 Oct 2024 14:59:10 +0100 Subject: [PATCH 4/7] improve after review --- .../{extractFromJre => defaultValue}/pom.xml | 0 .../src/main/java/sample/Sample.java | 0 .../src/test/java/sample/SampleTest.java | 0 .../com/sonar/maven/it/suite/JavaTest.java | 19 +++++++++++-------- 4 files changed, 11 insertions(+), 8 deletions(-) rename its/projects/jdkHome/{extractFromJre => defaultValue}/pom.xml (100%) rename its/projects/jdkHome/{extractFromJre => defaultValue}/src/main/java/sample/Sample.java (100%) rename its/projects/jdkHome/{extractFromJre => defaultValue}/src/test/java/sample/SampleTest.java (100%) diff --git a/its/projects/jdkHome/extractFromJre/pom.xml b/its/projects/jdkHome/defaultValue/pom.xml similarity index 100% rename from its/projects/jdkHome/extractFromJre/pom.xml rename to its/projects/jdkHome/defaultValue/pom.xml diff --git a/its/projects/jdkHome/extractFromJre/src/main/java/sample/Sample.java b/its/projects/jdkHome/defaultValue/src/main/java/sample/Sample.java similarity index 100% rename from its/projects/jdkHome/extractFromJre/src/main/java/sample/Sample.java rename to its/projects/jdkHome/defaultValue/src/main/java/sample/Sample.java diff --git a/its/projects/jdkHome/extractFromJre/src/test/java/sample/SampleTest.java b/its/projects/jdkHome/defaultValue/src/test/java/sample/SampleTest.java similarity index 100% rename from its/projects/jdkHome/extractFromJre/src/test/java/sample/SampleTest.java rename to its/projects/jdkHome/defaultValue/src/test/java/sample/SampleTest.java 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 f28fd337..0a0ded71 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 @@ -28,6 +28,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Properties; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -122,17 +123,20 @@ void setJavaVersionProperties() throws IOException { } @Test - void whenPropertyJavaHomeExistsThenItIsUsedAsDefaultValue() throws IOException { + void when_java_home_exists_it_is_used_as_default_sonar_java_jdkHome_value() throws IOException { //verify that property java home exists, it may not be pointing to a jdk - if (!System.getProperties().containsKey("java.home") - && !System.getenv().containsKey("JAVA_HOME")) { - return; - } + Assumptions.assumeTrue(System.getProperties().containsKey("java.home") + || System.getenv().containsKey("JAVA_HOME")); + + + //fix java.home and JAVA_HOME, the plugin should not modify them + String javaHomeSystem = System.getProperty("java.home"); + String javaHomeEnv = System.getenv().get("JAVA_HOME"); File outputProps = temp.resolve("out.properties").toFile(); outputProps.createNewFile(); - File pom = ItUtils.locateProjectPom("jdkHome/extractFromJre"); + File pom = ItUtils.locateProjectPom("jdkHome/defaultValue"); MavenBuild build = MavenBuild.create(pom) .setGoals(sonarGoal()) @@ -142,10 +146,9 @@ void whenPropertyJavaHomeExistsThenItIsUsedAsDefaultValue() throws IOException { 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).isIn(Arrays.asList(System.getProperty("java.home"), System.getenv().get("JAVA_HOME"))); + assertThat(jdkHome).isIn(Arrays.asList(javaHomeSystem, javaHomeEnv)); } - @Test void setJdkHomeFromCompilerExecutableConfiguration() throws IOException { File outputProps = temp.resolve("out.properties").toFile(); From 21e01803ab2fe5fcd8f4a638279a2fdad4deab36 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Tue, 29 Oct 2024 16:36:46 +0100 Subject: [PATCH 5/7] only test java.home --- its/src/test/java/com/sonar/maven/it/suite/JavaTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 0a0ded71..e1459b76 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 @@ -125,13 +125,10 @@ void setJavaVersionProperties() throws IOException { @Test void when_java_home_exists_it_is_used_as_default_sonar_java_jdkHome_value() throws IOException { //verify that property java home exists, it may not be pointing to a jdk - Assumptions.assumeTrue(System.getProperties().containsKey("java.home") - || System.getenv().containsKey("JAVA_HOME")); + Assumptions.assumeTrue(System.getProperties().containsKey("java.home")); - - //fix java.home and JAVA_HOME, the plugin should not modify them + //fix value, the plugin should not modify them String javaHomeSystem = System.getProperty("java.home"); - String javaHomeEnv = System.getenv().get("JAVA_HOME"); File outputProps = temp.resolve("out.properties").toFile(); @@ -146,7 +143,7 @@ void when_java_home_exists_it_is_used_as_default_sonar_java_jdkHome_value() thro 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).isIn(Arrays.asList(javaHomeSystem, javaHomeEnv)); + assertThat(jdkHome).isIn(Arrays.asList(javaHomeSystem)); } @Test From 45e1a2d53b021dbccce8ae8f1d6874348d33cbc1 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Tue, 29 Oct 2024 17:32:24 +0100 Subject: [PATCH 6/7] improve after code review --- its/src/test/java/com/sonar/maven/it/suite/JavaTest.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) 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 e1459b76..7051ef22 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 @@ -124,13 +124,9 @@ void setJavaVersionProperties() throws IOException { @Test void when_java_home_exists_it_is_used_as_default_sonar_java_jdkHome_value() throws IOException { - //verify that property java home exists, it may not be pointing to a jdk - Assumptions.assumeTrue(System.getProperties().containsKey("java.home")); - //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"); @@ -143,7 +139,7 @@ void when_java_home_exists_it_is_used_as_default_sonar_java_jdkHome_value() thro 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).isIn(Arrays.asList(javaHomeSystem)); + assertThat(jdkHome).isEqualTo(javaHomeSystem); } @Test From b5b2ca9e933fa07eb05ab08f5289338273015fe0 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Wed, 30 Oct 2024 10:48:25 +0100 Subject: [PATCH 7/7] optimize imports --- its/src/test/java/com/sonar/maven/it/suite/JavaTest.java | 2 -- 1 file changed, 2 deletions(-) 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 7051ef22..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 @@ -26,9 +26,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Path; -import java.util.Arrays; import java.util.Properties; -import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir;