]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6628 warn log if any sonar.jdbc property is set in Batch
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 30 Jun 2015 10:05:26 +0000 (12:05 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 30 Jun 2015 10:05:26 +0000 (12:05 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DroppedPropertyChecker.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DroppedPropertyCheckerTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java

diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DroppedPropertyChecker.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DroppedPropertyChecker.java
new file mode 100644 (file)
index 0000000..7b34ddc
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.batch.bootstrap;
+
+import java.util.Map;
+import org.sonar.api.config.Settings;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+
+import static java.util.Objects.requireNonNull;
+
+public class DroppedPropertyChecker {
+  private final Settings settings;
+  private final Logger logger;
+  private final Map<String, String> properties;
+
+  public DroppedPropertyChecker(Settings settings, Map<String, String> properties) {
+    this.settings = requireNonNull(settings);
+    this.logger = Loggers.get(settings.getClass());
+    this.properties = requireNonNull(properties);
+  }
+
+  public void checkDroppedProperties() {
+    for (Map.Entry<String, String> entry : properties.entrySet()) {
+      if (settings.hasKey(entry.getKey())) {
+        logger.warn(
+          "Property '{}' (which value is '{}') is not supported any more. {}",
+          entry.getKey(), settings.getString(entry.getKey()), entry.getValue()
+          );
+      }
+    }
+  }
+
+}
index daa3c1d0eea3268589862dcee0fe1ae02ce0236a..718dfd6acb44582e2e570c8d8a45d1f1c85cd41c 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.batch.bootstrap;
 
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.CoreProperties;
@@ -31,6 +33,17 @@ public class GlobalSettings extends Settings {
 
   private static final Logger LOG = LoggerFactory.getLogger(GlobalSettings.class);
 
+  private static final String JDBC_SPECIFIC_MESSAGE = "There is no more DB connection to the SQ database. It will be ignored.";
+  /**
+   * A map of dropped properties as key and specific message to display for that property
+   * (what will happen, what should the user do, ...) as a value
+   */
+  private static final Map<String, String> DROPPED_PROPERTIES = ImmutableMap.of(
+      "sonar.jdbc.url", JDBC_SPECIFIC_MESSAGE,
+      "sonar.jdbc.username", JDBC_SPECIFIC_MESSAGE,
+      "sonar.jdbc.password", JDBC_SPECIFIC_MESSAGE
+  );
+
   private final BootstrapProperties bootstrapProps;
   private final GlobalRepositories globalReferentials;
   private final DefaultAnalysisMode mode;
@@ -44,6 +57,7 @@ public class GlobalSettings extends Settings {
     this.bootstrapProps = bootstrapProps;
     this.globalReferentials = globalReferentials;
     init();
+    new DroppedPropertyChecker(this, DROPPED_PROPERTIES).checkDroppedProperties();
   }
 
   private void init() {
index 9a794590c43d5db7bb438a351b57c36bf3344be2..14720cbc944b9659c3afe4f7fe51e5eb251acbb1 100644 (file)
@@ -26,14 +26,12 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.MessageException;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
 import org.sonar.batch.bootstrap.DefaultAnalysisMode;
+import org.sonar.batch.bootstrap.DroppedPropertyChecker;
 import org.sonar.batch.bootstrap.GlobalSettings;
 import org.sonar.batch.protocol.input.ProjectRepositories;
 
 public class ProjectSettings extends Settings {
-  private static final Logger LOGGER = Loggers.get(ProjectSettings.class);
 
   /**
    * A map of dropped properties as key and specific message to display for that property
@@ -55,7 +53,7 @@ public class ProjectSettings extends Settings {
     this.globalSettings = globalSettings;
     this.projectRepositories = projectRepositories;
     init(reactor);
-    checkDroppedProperties();
+    new DroppedPropertyChecker(this, DROPPED_PROPERTIES).checkDroppedProperties();
   }
 
   private void init(ProjectReactor reactor) {
@@ -66,14 +64,6 @@ public class ProjectSettings extends Settings {
     addProperties(reactor.getRoot().properties());
   }
 
-  private void checkDroppedProperties() {
-    for (Map.Entry<String, String> entry : DROPPED_PROPERTIES.entrySet()) {
-      if (hasKey(entry.getKey())) {
-        LOGGER.warn("Property '{}' is not supported any more. {}", entry.getKey(), entry.getValue());
-      }
-    }
-  }
-
   @Override
   protected void doOnGetProperties(String key) {
     if (mode.isPreview() && key.endsWith(".secured") && !key.contains(".license")) {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DroppedPropertyCheckerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DroppedPropertyCheckerTest.java
new file mode 100644 (file)
index 0000000..258bfe4
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.batch.bootstrap;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.config.Settings;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DroppedPropertyCheckerTest {
+  private static final String SOME_VALUE = "some value";
+  private static final String DROPPED_PROPERTY_1 = "I'm dropped";
+  private static final String DROPPED_PROPERTY_MSG_1 = "blablabla!";
+
+  @Rule
+  public LogTester logTester = new LogTester();
+
+  @Test
+  public void no_log_if_no_dropped_property() {
+    Settings settings = new Settings();
+    settings.setProperty(DROPPED_PROPERTY_1, SOME_VALUE);
+
+    new DroppedPropertyChecker(settings, ImmutableMap.<String, String>of()).checkDroppedProperties();
+
+    assertThat(logTester.logs()).isEmpty();
+  }
+
+  @Test
+  public void no_log_if_settings_does_not_contain_any_dropped_property() {
+    new DroppedPropertyChecker(new Settings(), ImmutableMap.of(DROPPED_PROPERTY_1, DROPPED_PROPERTY_MSG_1)).checkDroppedProperties();
+
+    assertThat(logTester.logs()).isEmpty();
+  }
+
+  @Test
+  public void warn_log_if_settings_contains_any_dropped_property() {
+    Settings settings = new Settings();
+    settings.setProperty(DROPPED_PROPERTY_1, SOME_VALUE);
+
+    new DroppedPropertyChecker(settings, ImmutableMap.of(DROPPED_PROPERTY_1, DROPPED_PROPERTY_MSG_1)).checkDroppedProperties();
+
+    assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
+    assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property '" + DROPPED_PROPERTY_1 + "' (which value is '" + SOME_VALUE +"') is not supported any more. " + DROPPED_PROPERTY_MSG_1);
+    assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
+    assertThat(logTester.logs(LoggerLevel.TRACE)).isEmpty();
+  }
+}
index 738bd0d5fa0b087845d94390c23f891831e06228..cdbbe32401bed4cc756df336d148dafe5542a1f0 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
+import java.util.Collections;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.batch.protocol.input.GlobalRepositories;
 
-import java.util.Collections;
-
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
 public class GlobalSettingsTest {
 
+  public static final String SOME_VALUE = "some_value";
   @Rule
   public ExpectedException thrown = ExpectedException.none();
+  @Rule
+  public LogTester logTester = new LogTester();
 
   GlobalRepositories globalRef;
-  ProjectDefinition project = ProjectDefinition.create().setKey("struts");
   BootstrapProperties bootstrapProps;
 
   private DefaultAnalysisMode mode;
@@ -58,4 +60,19 @@ public class GlobalSettingsTest {
 
     assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue();
   }
+
+  @Test
+  public void should_log_warn_msg_for_each_jdbc_property_if_present() {
+    globalRef.globalSettings().put("sonar.jdbc.url", SOME_VALUE);
+    globalRef.globalSettings().put("sonar.jdbc.username", SOME_VALUE);
+    globalRef.globalSettings().put("sonar.jdbc.password", SOME_VALUE);
+
+    new GlobalSettings(bootstrapProps, new PropertyDefinitions(), globalRef, mode);
+
+    assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly(
+      "Property 'sonar.jdbc.url' (which value is '" + SOME_VALUE + "') is not supported any more. There is no more DB connection to the SQ database. It will be ignored.",
+      "Property 'sonar.jdbc.username' (which value is '" + SOME_VALUE + "') is not supported any more. There is no more DB connection to the SQ database. It will be ignored.",
+      "Property 'sonar.jdbc.password' (which value is '" + SOME_VALUE + "') is not supported any more. There is no more DB connection to the SQ database. It will be ignored."
+      );
+  }
 }
index c2e936ecf8bffaa326c7852ca37fde3a109c7426..efadbc6a803cbae431b7b2967f4b92781e11c1b9 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.batch.bootstrap.BootstrapProperties;
 import org.sonar.batch.bootstrap.DefaultAnalysisMode;
 import org.sonar.batch.bootstrap.GlobalSettings;
@@ -120,7 +121,7 @@ public class ProjectSettingsTest {
     GlobalSettings settings = new GlobalSettings(new BootstrapProperties(ImmutableMap.of("sonar.qualitygate", "somevalue")), new PropertyDefinitions(), new GlobalRepositories(), mode);
     new ProjectSettings(new ProjectReactor(project), settings, new PropertyDefinitions(), projectRef, mode);
 
-    logTester.logs().contains("Property 'sonar.qualitygate' is not supported any more. It will be ignored.");
+    assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property 'sonar.qualitygate' (which value is 'somevalue') is not supported any more. It will be ignored.");
 
   }
 }