@@ -21,11 +21,13 @@ package org.sonar.scanner.scan; | |||
import com.google.common.annotations.VisibleForTesting; | |||
import java.util.Optional; | |||
import org.apache.commons.lang.StringUtils; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.config.Configuration; | |||
import org.sonar.api.notifications.AnalysisWarnings; | |||
import org.sonar.api.utils.log.Logger; | |||
import org.sonar.api.utils.log.Loggers; | |||
import org.sonar.batch.bootstrapper.EnvironmentInformation; | |||
import org.sonar.scanner.bootstrap.ScannerWsClientProvider; | |||
public class DeprecatedPropertiesWarningGenerator { | |||
@@ -38,24 +40,43 @@ public class DeprecatedPropertiesWarningGenerator { | |||
@VisibleForTesting | |||
static final String LOGIN_WARN_MESSAGE = String.format("The property '%s' is deprecated and will be removed in the future. " + | |||
"Please use the '%s' property instead when passing a token.", CoreProperties.LOGIN, ScannerWsClientProvider.TOKEN_PROPERTY); | |||
@VisibleForTesting | |||
static final String SCANNER_DOTNET_WARN_MESSAGE = String.format(" The '%s' property is available from SonarScanner " + | |||
"for .NET version 5.13.", ScannerWsClientProvider.TOKEN_PROPERTY); | |||
private static final String ENV_KEY_SCANNER_DOTNET = "ScannerMSBuild"; | |||
private final Configuration configuration; | |||
private final AnalysisWarnings analysisWarnings; | |||
private final EnvironmentInformation environmentInformation; | |||
public DeprecatedPropertiesWarningGenerator(Configuration configuration, AnalysisWarnings analysisWarnings) { | |||
public DeprecatedPropertiesWarningGenerator(Configuration configuration, AnalysisWarnings analysisWarnings, | |||
EnvironmentInformation environmentInformation) { | |||
this.configuration = configuration; | |||
this.analysisWarnings = analysisWarnings; | |||
this.environmentInformation = environmentInformation; | |||
} | |||
public void execute() { | |||
Optional<String> login = configuration.get(CoreProperties.LOGIN); | |||
Optional<String> password = configuration.get(CoreProperties.PASSWORD); | |||
String warningMessage = null; | |||
if (password.isPresent()) { | |||
LOG.warn(PASSWORD_WARN_MESSAGE); | |||
analysisWarnings.addUnique(PASSWORD_WARN_MESSAGE); | |||
warningMessage = PASSWORD_WARN_MESSAGE; | |||
} else if (login.isPresent()) { | |||
LOG.warn(LOGIN_WARN_MESSAGE); | |||
analysisWarnings.addUnique(LOGIN_WARN_MESSAGE); | |||
warningMessage = LOGIN_WARN_MESSAGE; | |||
} | |||
if (warningMessage != null) { | |||
if (isScannerDotNet()) { | |||
warningMessage += SCANNER_DOTNET_WARN_MESSAGE; | |||
} | |||
LOG.warn(warningMessage); | |||
analysisWarnings.addUnique(warningMessage); | |||
} | |||
} | |||
private boolean isScannerDotNet() { | |||
return StringUtils.containsIgnoreCase(environmentInformation.getKey(), ENV_KEY_SCANNER_DOTNET); | |||
} | |||
} |
@@ -29,12 +29,15 @@ import org.sonar.api.config.internal.MapSettings; | |||
import org.sonar.api.notifications.AnalysisWarnings; | |||
import org.sonar.api.utils.log.LogTester; | |||
import org.sonar.api.utils.log.LoggerLevel; | |||
import org.sonar.batch.bootstrapper.EnvironmentInformation; | |||
import static org.mockito.Mockito.times; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoInteractions; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.LOGIN_WARN_MESSAGE; | |||
import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.PASSWORD_WARN_MESSAGE; | |||
import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.SCANNER_DOTNET_WARN_MESSAGE; | |||
public class DeprecatedPropertiesWarningGeneratorTest { | |||
@@ -44,13 +47,15 @@ public class DeprecatedPropertiesWarningGeneratorTest { | |||
private final MapSettings settings = new MapSettings(); | |||
private final AnalysisWarnings analysisWarnings = Mockito.spy(AnalysisWarnings.class); | |||
private final EnvironmentInformation environmentInformation = Mockito.mock(EnvironmentInformation.class); | |||
private final DeprecatedPropertiesWarningGenerator underTest = new DeprecatedPropertiesWarningGenerator(settings.asConfig(), | |||
analysisWarnings); | |||
analysisWarnings, environmentInformation); | |||
@Before | |||
public void setUp() throws Exception { | |||
settings.removeProperty(CoreProperties.LOGIN); | |||
settings.removeProperty(CoreProperties.PASSWORD); | |||
when(environmentInformation.getKey()).thenReturn("ScannerCLI"); | |||
} | |||
@Test | |||
@@ -74,6 +79,29 @@ public class DeprecatedPropertiesWarningGeneratorTest { | |||
Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(PASSWORD_WARN_MESSAGE); | |||
} | |||
@Test | |||
public void execute_whenUsingLoginAndDotNetScanner_shouldAddWarning() { | |||
settings.setProperty(CoreProperties.LOGIN, "test"); | |||
when(environmentInformation.getKey()).thenReturn("ScannerMSBuild"); | |||
underTest.execute(); | |||
verify(analysisWarnings, times(1)).addUnique(LOGIN_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); | |||
Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(LOGIN_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); | |||
} | |||
@Test | |||
public void execute_whenUsingPasswordAndDotNetScanner_shouldAddWarning() { | |||
settings.setProperty(CoreProperties.LOGIN, "test"); | |||
settings.setProperty(CoreProperties.PASSWORD, "winner winner chicken dinner"); | |||
when(environmentInformation.getKey()).thenReturn("ScannerMSBuild"); | |||
underTest.execute(); | |||
verify(analysisWarnings, times(1)).addUnique(PASSWORD_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); | |||
Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(PASSWORD_WARN_MESSAGE + SCANNER_DOTNET_WARN_MESSAGE); | |||
} | |||
@Test | |||
public void execute_whenNotUsingLoginOrPassword_shouldNotAddWarning() { | |||
underTest.execute(); |