37
37
38
38
class BootstrapTest extends AbstractMavenTest {
39
39
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
+
40
47
@ Test
41
48
void test_unsupported_platform () {
42
49
String unsupportedOS = "unsupportedOS" ;
@@ -50,8 +57,7 @@ void test_unsupported_platform() {
50
57
.setProperty ("sonar.host.url" , ORCHESTRATOR .getServer ().getUrl ())
51
58
.setGoals (cleanSonarGoal ());
52
59
53
- boolean sonarQubeThatSupportJREProvisioning = ORCHESTRATOR .getServer ().version ().isGreaterThanOrEquals (10 , 6 );
54
- if (sonarQubeThatSupportJREProvisioning ) {
60
+ if (isSonarQubeSupportsJREProvisioning ()) {
55
61
BuildResult result = validateBuildWithoutCE (runner .runQuietly (null , build ), EXEC_FAILED );
56
62
String url = ORCHESTRATOR .getServer ().getUrl () + String .format ("/api/v2/analysis/jres?os=%s&arch=%s" , unsupportedOS , arch );
57
63
String expectedLog = String .format ("Error status returned by url [%s]: 400" , url );
@@ -61,6 +67,93 @@ void test_unsupported_platform() {
61
67
}
62
68
}
63
69
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
+
64
157
@ Test
65
158
void test_supported_arch_to_assert_jre_used () throws IOException {
66
159
BuildRunner runner = new BuildRunner (ORCHESTRATOR .getConfiguration ());
@@ -131,30 +224,30 @@ void test_supported_arch_to_assert_jre_used() throws IOException {
131
224
softly .assertThat (props .getProperty ("sonar.java.source" )).isEqualTo ( "11" );
132
225
softly .assertThat (props .getProperty ("sonar.java.target" )).isEqualTo ( "11" );
133
226
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 ());
140
229
141
230
StringAssert javaHomeAssertion = softly .assertThat (props .getProperty ("java.home" )).isNotEmpty ();
142
- if (ORCHESTRATOR . getServer (). version (). isGreaterThanOrEquals ( 10 , 6 )) {
231
+ if (isSonarQubeSupportsJREProvisioning ( )) {
143
232
//we test that we are actually using the JRE downloaded from SQ
144
233
javaHomeAssertion
145
234
.isNotEqualTo (System .getProperty ("java.home" ))
235
+ .isNotEqualTo (guessJavaHomeSelectedByMvn ())
146
236
.contains (".sonar" + File .separator + "cache" );
147
237
148
238
// System properties of the initial JRE are intentionally not set on the provisioned JRE
149
239
softly .assertThat (props .getProperty ("http.nonProxyHosts" ))
150
- .isEmpty ( );
240
+ .isNotEqualTo ( "localhost|my-custom-non-proxy.server.com" );
151
241
152
242
// System properties defined in "sonar.scanner.javaOpts" are set on the provisioned JRE
153
243
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 );
154
247
} else {
155
248
//we test that we are using the system JRE
156
249
javaHomeAssertion
157
- .isEqualTo (System . getProperty ( "java.home" ))
250
+ .isEqualTo (guessJavaHomeSelectedByMvn ( ))
158
251
.doesNotContain (".sonar" + File .separator + "cache" );
159
252
160
253
softly .assertThat (props .getProperty ("http.nonProxyHosts" ))
@@ -166,4 +259,17 @@ void test_supported_arch_to_assert_jre_used() throws IOException {
166
259
softly .assertAll ();
167
260
}
168
261
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
+
169
275
}
0 commit comments