|
28 | 28 | import java.nio.file.Files;
|
29 | 29 | import java.nio.file.Path;
|
30 | 30 | import java.util.Properties;
|
| 31 | +import org.assertj.core.api.SoftAssertions; |
| 32 | +import org.assertj.core.api.StringAssert; |
31 | 33 | import org.junit.jupiter.api.Test;
|
32 | 34 |
|
| 35 | +import static com.sonar.maven.it.ItUtils.locateProjectDir; |
33 | 36 | import static org.assertj.core.api.Assertions.assertThat;
|
34 | 37 |
|
35 |
| -public class BootstrapTest extends AbstractMavenTest { |
| 38 | +class BootstrapTest extends AbstractMavenTest { |
36 | 39 |
|
37 | 40 | @Test
|
38 | 41 | void test_unsupported_platform() {
|
@@ -65,28 +68,102 @@ void test_supported_arch_to_assert_jre_used() throws IOException {
|
65 | 68 | MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom(projectName))
|
66 | 69 | .setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
|
67 | 70 | .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
|
| 71 | + .setEnvironmentVariable("DUMP_SENSOR_PROPERTIES", "" + |
| 72 | + "any.property.name," + |
| 73 | + "sonar.java.fileByFile," + |
| 74 | + "sonar.java.file.suffixes," + |
| 75 | + "sonar.projectKey," + |
| 76 | + "sonar.projectBaseDir," + |
| 77 | + "sonar.sources," + |
| 78 | + "sonar.working.directory," + |
| 79 | + "sonar.java.libraries," + |
| 80 | + "sonar.java.source," + |
| 81 | + "sonar.java.target," + |
| 82 | + "sonar.java.test.libraries," + |
| 83 | + "sonar.java.jdkHome") |
| 84 | + .setEnvironmentVariable("DUMP_ENV_PROPERTIES", "" + |
| 85 | + "ANY_ENV_VAR") |
| 86 | + .setEnvironmentVariable("DUMP_SYSTEM_PROPERTIES", "" + |
| 87 | + "http.proxyUser,"+ |
| 88 | + "http.nonProxyHosts," + |
| 89 | + "java.home") |
| 90 | + .setEnvironmentVariable("ANY_ENV_VAR", "42") |
| 91 | + // http.nonProxyHosts will only be present in the maven context and not in the provisioned JRE |
| 92 | + .setProperty("http.nonProxyHosts", "localhost|my-custom-non-proxy.server.com") |
| 93 | + // Any property will be passed to the sensorContext.config() |
| 94 | + .setProperty("any.property.name", "foo42") |
| 95 | + // This property will be ignored because of the bellow "sonar.scanner.javaOpts" property that has priority |
| 96 | + .setEnvironmentVariable("SONAR_SCANNER_JAVA_OPTS", "-Dhttp.proxyUser=my-custom-user-from-env") |
| 97 | + // Set system property on the provisioned JRE |
| 98 | + .setProperty("sonar.scanner.javaOpts", "-Dhttp.proxyUser=my-custom-user-from-system-properties") |
68 | 99 | .setGoals(cleanSonarGoal());
|
69 | 100 |
|
70 |
| - |
71 | 101 | BuildResult result = validateBuildWithCE(runner.runQuietly(null, build));
|
72 | 102 | assertThat(result.isSuccess()).isTrue();
|
73 | 103 | Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.system.properties");
|
74 | 104 | Properties props = new Properties();
|
75 | 105 | props.load(Files.newInputStream(propertiesFile));
|
76 | 106 |
|
| 107 | + Path smallProjectDir = locateProjectDir("maven").getAbsoluteFile().toPath().resolve("bootstrap-small-project"); |
| 108 | + |
| 109 | + SoftAssertions softly = new SoftAssertions(); |
| 110 | + |
| 111 | + // Environment variables |
| 112 | + softly.assertThat(props.getProperty("ANY_ENV_VAR")).isEqualTo( "42"); |
| 113 | + |
| 114 | + // User defined in its/projects/maven/bootstrap-small-project/pom.xml properties |
| 115 | + softly.assertThat(props.getProperty("sonar.java.fileByFile")).isEqualTo( "true"); |
| 116 | + |
| 117 | + // SonarQube properties |
| 118 | + softly.assertThat(props.getProperty("sonar.java.file.suffixes")).isEqualTo( ".java,.jav"); |
| 119 | + |
| 120 | + // Project properties |
| 121 | + softly.assertThat(props.getProperty("sonar.projectKey")).isEqualTo( "org.sonarsource.maven.its:bootstrap-small-project"); |
| 122 | + softly.assertThat(props.getProperty("sonar.projectBaseDir")).isEqualTo( smallProjectDir.toString()); |
| 123 | + softly.assertThat(props.getProperty("sonar.sources")).isEqualTo( smallProjectDir.resolve("pom.xml") + "," + smallProjectDir.resolve("src").resolve("main").resolve("java")); |
| 124 | + softly.assertThat(props.getProperty("sonar.working.directory")).isEqualTo( smallProjectDir.resolve("target").resolve("sonar").toString()); |
| 125 | + |
| 126 | + // Any properties are present in the sensor context |
| 127 | + softly.assertThat(props.getProperty("any.property.name")).contains("foo42"); |
| 128 | + |
| 129 | + // Java analyzers properties |
| 130 | + softly.assertThat(props.getProperty("sonar.java.libraries")).contains("jsr305-3.0.2.jar"); |
| 131 | + softly.assertThat(props.getProperty("sonar.java.source")).isEqualTo( "11"); |
| 132 | + softly.assertThat(props.getProperty("sonar.java.target")).isEqualTo( "11"); |
| 133 | + softly.assertThat(props.getProperty("sonar.java.test.libraries")).contains("jsr305-3.0.2.jar"); |
| 134 | + // sonar.java.jdkHome should be the one used by "mvn sonar:sonar", by default maven uses JAVA_HOME |
| 135 | + String javaHome = System.getenv("JAVA_HOME"); |
| 136 | + if (javaHome == null) { |
| 137 | + javaHome = System.getProperty("java.home"); |
| 138 | + } |
| 139 | + softly.assertThat(props.getProperty("sonar.java.jdkHome")).isEqualTo( new File(javaHome).getCanonicalPath()); |
| 140 | + |
| 141 | + StringAssert javaHomeAssertion = softly.assertThat(props.getProperty("java.home")).isNotEmpty(); |
77 | 142 | if (ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6)) {
|
78 | 143 | //we test that we are actually using the JRE downloaded from SQ
|
79 |
| - assertThat(props.getProperty("java.home")) |
80 |
| - .isNotEmpty() |
| 144 | + javaHomeAssertion |
81 | 145 | .isNotEqualTo(System.getProperty("java.home"))
|
82 | 146 | .contains(".sonar" + File.separator + "cache");
|
| 147 | + |
| 148 | + // System properties of the initial JRE are intentionally not set on the provisioned JRE |
| 149 | + softly.assertThat(props.getProperty("http.nonProxyHosts")) |
| 150 | + .isEmpty(); |
| 151 | + |
| 152 | + // System properties defined in "sonar.scanner.javaOpts" are set on the provisioned JRE |
| 153 | + softly.assertThat(props.getProperty("http.proxyUser")).isEqualTo("my-custom-user-from-system-properties"); |
83 | 154 | } else {
|
84 | 155 | //we test that we are using the system JRE
|
85 |
| - assertThat(props.getProperty("java.home")) |
86 |
| - .isNotEmpty() |
| 156 | + javaHomeAssertion |
87 | 157 | .isEqualTo(System.getProperty("java.home"))
|
88 | 158 | .doesNotContain(".sonar" + File.separator + "cache");
|
| 159 | + |
| 160 | + softly.assertThat(props.getProperty("http.nonProxyHosts")) |
| 161 | + .isEqualTo("localhost|my-custom-non-proxy.server.com"); |
| 162 | + |
| 163 | + // System properties defined in "sonar.scanner.javaOpts" are ignored outside the provisioned JRE |
| 164 | + softly.assertThat(props.getProperty("http.proxyUser")).isEmpty(); |
89 | 165 | }
|
| 166 | + softly.assertAll(); |
90 | 167 | }
|
91 | 168 |
|
92 | 169 | }
|
0 commit comments