]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11539 Support new dbcp property name
authorBenoit <43733395+benoit-sns@users.noreply.github.com>
Tue, 4 Dec 2018 12:52:42 +0000 (13:52 +0100)
committerSonarTech <sonartech@sonarsource.com>
Tue, 4 Dec 2018 19:21:26 +0000 (20:21 +0100)
server/sonar-db-core/build.gradle
server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java
server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java

index e0cd63bf72dbd5c91a9ceab9ac2e476d28006c45..1d83be15e6755e0a87923649815f19f2282689a5 100644 (file)
@@ -26,6 +26,7 @@ dependencies {
   testCompile 'com.google.code.findbugs:jsr305'
   testCompile 'com.microsoft.sqlserver:mssql-jdbc'
   testCompile 'com.oracle.jdbc:ojdbc8'
+  testCompile 'com.tngtech.java:junit-dataprovider'
   testCompile 'junit:junit'
   testCompile 'mysql:mysql-connector-java'
   testCompile 'org.assertj:assertj-core'
index 92f3a93aae7e3defe451678ad5d3785bbcda6972..cbff7135317e574e6eca199adadbc200ee4cf1e3 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db;
 
 import ch.qos.logback.classic.Level;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableMap;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.List;
@@ -41,6 +42,7 @@ import org.sonar.db.profiling.ProfiledConnectionInterceptor;
 import org.sonar.db.profiling.ProfiledDataSource;
 import org.sonar.process.logging.LogbackHelper;
 
+import static com.google.common.base.Preconditions.checkState;
 import static java.lang.String.format;
 import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
 
@@ -55,6 +57,14 @@ public class DefaultDatabase implements Database {
   private static final String SONAR_JDBC = "sonar.jdbc.";
   private static final String SONAR_JDBC_DIALECT = "sonar.jdbc.dialect";
   private static final String SONAR_JDBC_DRIVER = "sonar.jdbc.driverClassName";
+  private static final String SONAR_JDBC_MAX_ACTIVE = "sonar.jdbc.maxActive";
+  private static final String DBCP_JDBC_MAX_ACTIVE = "maxTotal";
+  private static final String SONAR_JDBC_MAX_WAIT = "sonar.jdbc.maxWait";
+  private static final String DBCP_JDBC_MAX_WAIT = "maxWaitMillis";
+  private static final Map<String, String> SONAR_JDBC_TO_DBCP_PROPERTY_MAPPINGS = ImmutableMap.of(
+    SONAR_JDBC_MAX_ACTIVE, DBCP_JDBC_MAX_ACTIVE,
+    SONAR_JDBC_MAX_WAIT, DBCP_JDBC_MAX_WAIT
+  );
 
   private final LogbackHelper logbackHelper;
   private final Settings settings;
@@ -168,7 +178,11 @@ public class DefaultDatabase implements Database {
     for (Map.Entry<Object, Object> entry : properties.entrySet()) {
       String key = (String) entry.getKey();
       if (StringUtils.startsWith(key, SONAR_JDBC)) {
-        result.setProperty(StringUtils.removeStart(key, SONAR_JDBC), (String) entry.getValue());
+        String resolvedKey = toDbcpPropertyKey(key);
+        String existingValue = (String) result.setProperty(resolvedKey, (String) entry.getValue());
+        checkState(existingValue == null || existingValue.equals(entry.getValue()),
+          "Duplicate property declaration for resolved jdbc key '%s': conflicting values are '%s' and '%s'", resolvedKey, existingValue, entry.getValue());
+        result.setProperty(toDbcpPropertyKey(key), (String) entry.getValue());
       }
     }
     return result;
@@ -180,6 +194,14 @@ public class DefaultDatabase implements Database {
     }
   }
 
+  private static String toDbcpPropertyKey(String key) {
+    if (SONAR_JDBC_TO_DBCP_PROPERTY_MAPPINGS.containsKey(key)) {
+      return SONAR_JDBC_TO_DBCP_PROPERTY_MAPPINGS.get(key);
+    }
+
+    return StringUtils.removeStart(key, SONAR_JDBC);
+  }
+
   @Override
   public String toString() {
     return format("Database[%s]", properties != null ? properties.getProperty(JDBC_URL.getKey()) : "?");
index f831bc110da2fc9a8c9275e6c8a096fe3b89002a..16bfc2638593388973b61a65aea6f27d5547915e 100644 (file)
  */
 package org.sonar.db;
 
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.util.Properties;
 import org.apache.commons.dbcp2.BasicDataSource;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
 import org.sonar.api.config.Settings;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.db.dialect.PostgreSql;
 import org.sonar.process.logging.LogbackHelper;
 
+import static org.apache.commons.lang.StringUtils.removeStart;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.rules.ExpectedException.none;
 import static org.mockito.Mockito.mock;
 
+@RunWith(DataProviderRunner.class)
 public class DefaultDatabaseTest {
   private LogbackHelper logbackHelper = mock(LogbackHelper.class);
+  private static final String SONAR_JDBC = "sonar.jdbc.";
+
+
+  @Rule
+  public ExpectedException expectedException = none();
 
   @Test
   public void shouldLoadDefaultValues() {
@@ -50,12 +64,39 @@ public class DefaultDatabaseTest {
     props.setProperty("sonar.jdbc.driverClassName", "my.Driver");
     props.setProperty("sonar.jdbc.username", "me");
     props.setProperty("sonar.jdbc.maxActive", "5");
+    props.setProperty("sonar.jdbc.maxWait", "5000");
 
     Properties commonsDbcpProps = DefaultDatabase.extractCommonsDbcpProperties(props);
 
     assertThat(commonsDbcpProps.getProperty("username")).isEqualTo("me");
     assertThat(commonsDbcpProps.getProperty("driverClassName")).isEqualTo("my.Driver");
-    assertThat(commonsDbcpProps.getProperty("maxActive")).isEqualTo("5");
+    assertThat(commonsDbcpProps.getProperty("maxTotal")).isEqualTo("5");
+    assertThat(commonsDbcpProps.getProperty("maxWaitMillis")).isEqualTo("5000");
+  }
+
+  @Test
+  @UseDataProvider("sonarJdbcAndDbcpProperties")
+  public void shouldExtractCommonsDbcpPropertiesIfDuplicatedPropertiesWithSameValue(String jdbcProperty, String dbcpProperty) {
+    Properties props = new Properties();
+    props.setProperty(jdbcProperty, "100");
+    props.setProperty(dbcpProperty, "100");
+
+    Properties commonsDbcpProps = DefaultDatabase.extractCommonsDbcpProperties(props);
+
+    assertThat(commonsDbcpProps.getProperty(removeStart(dbcpProperty, SONAR_JDBC))).isEqualTo("100");
+  }
+
+  @Test
+  @UseDataProvider("sonarJdbcAndDbcpProperties")
+  public void shouldThrowISEIfDuplicatedResolvedPropertiesWithDifferentValue(String jdbcProperty, String dbcpProperty) {
+    Properties props = new Properties();
+    props.setProperty(jdbcProperty, "100");
+    props.setProperty(dbcpProperty, "200");
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage(String.format("Duplicate property declaration for resolved jdbc key '%s': conflicting values are", removeStart(dbcpProperty, SONAR_JDBC)));
+
+    DefaultDatabase.extractCommonsDbcpProperties(props);
   }
 
   @Test
@@ -89,7 +130,7 @@ public class DefaultDatabaseTest {
     db.stop();
 
     assertThat(db.getDialect().getId()).isEqualTo("h2");
-    assertThat(((BasicDataSource) db.getDataSource()).getMaxTotal()).isEqualTo(8);
+    assertThat(((BasicDataSource) db.getDataSource()).getMaxTotal()).isEqualTo(1);
   }
 
   @Test
@@ -113,4 +154,12 @@ public class DefaultDatabaseTest {
 
     assertThat(database.getProperties().getProperty("sonar.jdbc.driverClassName")).isEqualTo("org.postgresql.Driver");
   }
+
+  @DataProvider
+  public static Object[][] sonarJdbcAndDbcpProperties() {
+    return new Object[][] {
+      {"sonar.jdbc.maxActive", "sonar.jdbc.maxTotal"},
+      {"sonar.jdbc.maxWait", "sonar.jdbc.maxWaitMillis"}
+    };
+  }
 }