diff options
author | Benoit <43733395+benoit-sns@users.noreply.github.com> | 2018-12-04 13:52:42 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-12-04 20:21:26 +0100 |
commit | fb15f6fc29a3c76008b153de2c37ad270398eabf (patch) | |
tree | 03498f2ae66c27017b57b193a56df4e1bb368376 /server/sonar-db-core | |
parent | eaac2bf87391734b95368e0645bd2a215f21f62a (diff) | |
download | sonarqube-fb15f6fc29a3c76008b153de2c37ad270398eabf.tar.gz sonarqube-fb15f6fc29a3c76008b153de2c37ad270398eabf.zip |
SONAR-11539 Support new dbcp property name
Diffstat (limited to 'server/sonar-db-core')
3 files changed, 75 insertions, 3 deletions
diff --git a/server/sonar-db-core/build.gradle b/server/sonar-db-core/build.gradle index e0cd63bf72d..1d83be15e67 100644 --- a/server/sonar-db-core/build.gradle +++ b/server/sonar-db-core/build.gradle @@ -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' diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java b/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java index 92f3a93aae7..cbff7135317 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java @@ -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()) : "?"); diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java b/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java index f831bc110da..16bfc263859 100644 --- a/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java +++ b/server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java @@ -19,19 +19,33 @@ */ 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"} + }; + } } |