Skip to content

Commit e4c63dd

Browse files
SCANMAVEN-243 The SonarScanner for Maven does not try to contact the server when the project is skipped (#250)
1 parent 69c0e1a commit e4c63dd

File tree

2 files changed

+149
-11
lines changed

2 files changed

+149
-11
lines changed

its/projects/maven/bootstrap-small-project/pom.xml

+32
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,36 @@
2626
</dependency>
2727
</dependencies>
2828

29+
<profiles>
30+
31+
<profile>
32+
<id>test-sonar-skip</id>
33+
<activation>
34+
<activeByDefault>false</activeByDefault>
35+
</activation>
36+
<properties>
37+
<sonar.skip>true</sonar.skip>
38+
</properties>
39+
</profile>
40+
41+
<profile>
42+
<id>test-plugin-skip</id>
43+
<activation>
44+
<activeByDefault>false</activeByDefault>
45+
</activation>
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.sonarsource.scanner.maven</groupId>
50+
<artifactId>sonar-maven-plugin</artifactId>
51+
<configuration>
52+
<skip>true</skip>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</profile>
58+
59+
</profiles>
60+
2961
</project>

its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java

+117-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737

3838
class BootstrapTest extends AbstractMavenTest {
3939

40+
private static final String EFFECTIVE_JRE_PROVISIONING_LOG = "JRE provisioning:";
41+
private static final String COMMUNICATING_WITH_SONARQUBE = "Communicating with SonarQube Server";
42+
private static final String STARTING_SCANNER_ENGINE = "Starting SonarScanner Engine";
43+
private static final String SKIPPING_ANALYSIS = "Skipping analysis";
44+
public static final String JRE_PROVISIONING_IS_DISABLED = "JRE provisioning is disabled";
45+
public static final String USING_CONFIGURED_JRE = "Using the configured java executable";
46+
4047
@Test
4148
void test_unsupported_platform() {
4249
String unsupportedOS = "unsupportedOS";
@@ -50,8 +57,7 @@ void test_unsupported_platform() {
5057
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
5158
.setGoals(cleanSonarGoal());
5259

53-
boolean sonarQubeThatSupportJREProvisioning = ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6);
54-
if (sonarQubeThatSupportJREProvisioning) {
60+
if (isSonarQubeSupportsJREProvisioning()) {
5561
BuildResult result = validateBuildWithoutCE(runner.runQuietly(null, build), EXEC_FAILED);
5662
String url = ORCHESTRATOR.getServer().getUrl() + String.format("/api/v2/analysis/jres?os=%s&arch=%s", unsupportedOS, arch);
5763
String expectedLog = String.format("Error status returned by url [%s]: 400", url);
@@ -61,6 +67,93 @@ void test_unsupported_platform() {
6167
}
6268
}
6369

70+
@Test
71+
void test_bootstrapping_with_sonar_skip_in_pom_xml() {
72+
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project"))
73+
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
74+
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
75+
// activate in the pom.xml <properties><sonar.skip>true</sonar.skip></properties>
76+
.addArguments("-Ptest-sonar-skip")
77+
.setGoals(sonarGoal());
78+
BuildResult result = executeBuildAndValidateWithoutCE(build);
79+
assertThat(result.getLogs())
80+
.contains(SKIPPING_ANALYSIS)
81+
.doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE);
82+
}
83+
84+
@Test
85+
void test_bootstrapping_with_sonar_skip_in_system_property() {
86+
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project"))
87+
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
88+
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
89+
// analyze using: mvn sonar:sonar -Dsonar.skip=true
90+
.setProperty("sonar.skip", "true")
91+
.setGoals(sonarGoal());
92+
BuildResult result = executeBuildAndValidateWithoutCE(build);
93+
assertThat(result.getLogs())
94+
.contains(SKIPPING_ANALYSIS)
95+
.doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE);
96+
}
97+
98+
@Test
99+
void test_bootstrapping_with_sonar_skip_in_plugin_configuration() {
100+
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project"))
101+
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
102+
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
103+
// activate in the pom.xml <configuration><skip>true</skip></configuration>
104+
.addArguments("-Ptest-plugin-skip")
105+
.setGoals(sonarGoal());
106+
BuildResult result = executeBuildAndValidateWithoutCE(build);
107+
assertThat(result.getLogs())
108+
.contains(SKIPPING_ANALYSIS)
109+
.doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE);
110+
}
111+
112+
@Test
113+
void test_bootstrapping_that_skip_the_JRE_provisioning() throws IOException {
114+
String projectName = "maven/bootstrap-small-project";
115+
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom(projectName))
116+
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
117+
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
118+
.setProperty("sonar.scanner.skipJreProvisioning", "true")
119+
.setEnvironmentVariable("DUMP_SYSTEM_PROPERTIES", "java.home")
120+
.setGoals(sonarGoal());
121+
BuildResult result = executeBuildAndValidateWithCE(build);
122+
assertThat(result.getLogs())
123+
.doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG);
124+
if (isSonarQubeSupportsJREProvisioning()) {
125+
assertThat(result.getLogs())
126+
.contains(JRE_PROVISIONING_IS_DISABLED, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE);
127+
}
128+
Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.system.properties");
129+
Properties props = new Properties();
130+
props.load(Files.newInputStream(propertiesFile));
131+
assertThat(props.getProperty("java.home")).isEqualTo(guessJavaHomeSelectedByMvn());
132+
}
133+
134+
@Test
135+
void test_bootstrapping_that_use_the_provided_JRE_instead_of_downloading_a_JRE() throws IOException {
136+
String mvnJavaHome = guessJavaHomeSelectedByMvn();
137+
String projectName = "maven/bootstrap-small-project";
138+
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom(projectName))
139+
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
140+
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
141+
.setProperty("sonar.scanner.javaExePath", mvnJavaHome + File.separator + "bin" + File.separator + "java")
142+
.setEnvironmentVariable("DUMP_SYSTEM_PROPERTIES", "java.home")
143+
.setGoals(sonarGoal());
144+
BuildResult result = executeBuildAndValidateWithCE(build);
145+
assertThat(result.getLogs())
146+
.doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, JRE_PROVISIONING_IS_DISABLED);
147+
if (isSonarQubeSupportsJREProvisioning()) {
148+
assertThat(result.getLogs())
149+
.contains(USING_CONFIGURED_JRE, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE);
150+
}
151+
Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.system.properties");
152+
Properties props = new Properties();
153+
props.load(Files.newInputStream(propertiesFile));
154+
assertThat(props.getProperty("java.home")).isEqualTo(mvnJavaHome);
155+
}
156+
64157
@Test
65158
void test_supported_arch_to_assert_jre_used() throws IOException {
66159
BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
@@ -131,30 +224,30 @@ void test_supported_arch_to_assert_jre_used() throws IOException {
131224
softly.assertThat(props.getProperty("sonar.java.source")).isEqualTo( "11");
132225
softly.assertThat(props.getProperty("sonar.java.target")).isEqualTo( "11");
133226
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());
227+
// sonar.java.jdkHome should be the one used by "mvn sonar:sonar"
228+
softly.assertThat(props.getProperty("sonar.java.jdkHome")).isEqualTo(guessJavaHomeSelectedByMvn());
140229

141230
StringAssert javaHomeAssertion = softly.assertThat(props.getProperty("java.home")).isNotEmpty();
142-
if (ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6)) {
231+
if (isSonarQubeSupportsJREProvisioning()) {
143232
//we test that we are actually using the JRE downloaded from SQ
144233
javaHomeAssertion
145234
.isNotEqualTo(System.getProperty("java.home"))
235+
.isNotEqualTo(guessJavaHomeSelectedByMvn())
146236
.contains(".sonar" + File.separator + "cache");
147237

148238
// System properties of the initial JRE are intentionally not set on the provisioned JRE
149239
softly.assertThat(props.getProperty("http.nonProxyHosts"))
150-
.isEmpty();
240+
.isNotEqualTo("localhost|my-custom-non-proxy.server.com");
151241

152242
// System properties defined in "sonar.scanner.javaOpts" are set on the provisioned JRE
153243
softly.assertThat(props.getProperty("http.proxyUser")).isEqualTo("my-custom-user-from-system-properties");
244+
245+
softly.assertThat(result.getLogs())
246+
.contains(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE);
154247
} else {
155248
//we test that we are using the system JRE
156249
javaHomeAssertion
157-
.isEqualTo(System.getProperty("java.home"))
250+
.isEqualTo(guessJavaHomeSelectedByMvn())
158251
.doesNotContain(".sonar" + File.separator + "cache");
159252

160253
softly.assertThat(props.getProperty("http.nonProxyHosts"))
@@ -166,4 +259,17 @@ void test_supported_arch_to_assert_jre_used() throws IOException {
166259
softly.assertAll();
167260
}
168261

262+
private static boolean isSonarQubeSupportsJREProvisioning() {
263+
return ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6);
264+
}
265+
266+
private static String guessJavaHomeSelectedByMvn() throws IOException {
267+
// By default maven uses JAVA_HOME if it exists, otherwise we don't know, we hope it uses the one that is currently running
268+
String javaHome = System.getenv("JAVA_HOME");
269+
if (javaHome == null) {
270+
javaHome = System.getProperty("java.home");
271+
}
272+
return new File(javaHome).getCanonicalPath();
273+
}
274+
169275
}

0 commit comments

Comments
 (0)