Skip to content

Commit

Permalink
MSONAR-202 Improve logging with information about Maven options, Java…
Browse files Browse the repository at this point in the history
… version and OS (#202)
  • Loading branch information
dorian-burihabwa-sonarsource authored Mar 1, 2024
1 parent b637acd commit 495d1c9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonarsource.scanner.maven.bootstrap;

import com.google.common.annotations.VisibleForTesting;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -32,7 +31,6 @@
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -69,6 +67,7 @@ public ScannerBootstrapper(Log log, MavenSession session, EmbeddedScanner scanne

public void execute() throws MojoExecutionException {
try {
logEnvironmentInformation();
scanner.start();
serverVersion = scanner.serverVersion();

Expand Down Expand Up @@ -184,4 +183,25 @@ boolean isVersionPriorTo(String version) {
return new ComparableVersion(serverVersion).compareTo(new ComparableVersion(version)) < 0;
}

private void logEnvironmentInformation() {
String vmInformation = String.format(
"Java %s %s (%s-bit)",
SystemWrapper.getProperty("java.version"),
SystemWrapper.getProperty("java.vm.vendor"),
SystemWrapper.getProperty("sun.arch.data.model")
);
log.info(vmInformation);
String operatingSystem = String.format(
"%s %s (%s)",
SystemWrapper.getProperty("os.name"),
SystemWrapper.getProperty("os.version"),
SystemWrapper.getProperty("os.arch")
);
log.info(operatingSystem);
String mavenOptions = SystemWrapper.getenv("MAVEN_OPTS");
if (mavenOptions != null) {
log.info(String.format("MAVEN_OPTS=%s", mavenOptions));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* SonarQube Scanner for Maven
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.scanner.maven.bootstrap;

/**
* A wrapper around some java.lang.System methods that allows us to mock and work around the default access limitations
* enforced in Java 16+.
*/
class SystemWrapper {
private SystemWrapper() {
/* Nothing done here */
}
static String getenv(String name) {
return System.getenv(name);
}

static String getProperty(String name) {
return System.getProperty(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Expand All @@ -48,7 +50,10 @@
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -273,6 +278,49 @@ void test_not_logging_the_version_when_sonarcloud_is_used() throws MojoExecution
verify(log, never()).info("Communicating with SonarQube Server 8.0");
}

@Nested
class EnvironmentInformation {
MockedStatic<SystemWrapper> mockedSystem;

@BeforeEach
void before() {
when(scanner.serverVersion()).thenReturn("9.9");
mockedSystem = mockStatic(SystemWrapper.class);
}

@AfterEach
void after() {
mockedSystem.close();
}

@Test
void environment_information_is_logged_at_info_level() throws MojoExecutionException {
mockedSystem.when(() -> SystemWrapper.getProperty("os.name")).thenReturn("Solaris");
mockedSystem.when(() -> SystemWrapper.getProperty("os.version")).thenReturn("42.1");
mockedSystem.when(() -> SystemWrapper.getProperty("os.arch")).thenReturn("x16");

mockedSystem.when(() -> SystemWrapper.getProperty("java.vm.vendor")).thenReturn("Artisanal Distribution");
mockedSystem.when(() -> SystemWrapper.getProperty("java.version")).thenReturn("4.2.0");
mockedSystem.when(() -> SystemWrapper.getProperty("sun.arch.data.model")).thenReturn("16");

mockedSystem.when(() -> SystemWrapper.getenv("MAVEN_OPTS")).thenReturn("-XX:NotAnActualOption=42");

scannerBootstrapper.execute();
InOrder inOrderVerifier = inOrder(log);

inOrderVerifier.verify(log, times(1)).info("Java 4.2.0 Artisanal Distribution (16-bit)");
inOrderVerifier.verify(log, times(1)).info("Solaris 42.1 (x16)");
inOrderVerifier.verify(log, times(1)).info("MAVEN_OPTS=-XX:NotAnActualOption=42");
}

@Test
void maven_opts_is_not_logged_at_info_level_when_not_absent_from_environment_variables() throws MojoExecutionException {
mockedSystem.when(() -> SystemWrapper.getenv("MAVEN_OPTS")).thenReturn(null);
scannerBootstrapper.execute();
verify(log, never()).info(contains("MAVEN_OPTS="));
}
}

private void verifyCommonCalls() {
verify(scanner).start();
verify(scanner).serverVersion();
Expand Down

0 comments on commit 495d1c9

Please sign in to comment.