aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-core/src
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2023-11-10 10:05:16 +0100
committersonartech <sonartech@sonarsource.com>2023-11-17 20:03:00 +0000
commitfcc198287c941c2a785fb6d335360f3bd775b09a (patch)
tree8364f7fe82f25f57406d68e0a026e6ed91803abb /server/sonar-db-core/src
parent73847ed622327b13d9def3ca5a6f925f2f69e49a (diff)
downloadsonarqube-fcc198287c941c2a785fb6d335360f3bd775b09a.tar.gz
sonarqube-fcc198287c941c2a785fb6d335360f3bd775b09a.zip
SONAR-21002 Improve DB migration tests to use actual migration instead of schema.sql
Diffstat (limited to 'server/sonar-db-core/src')
-rw-r--r--server/sonar-db-core/src/main/java/org/sonar/db/DefaultDatabase.java6
-rw-r--r--server/sonar-db-core/src/test/java/org/sonar/db/DefaultDatabaseTest.java7
-rw-r--r--server/sonar-db-core/src/testFixtures/java/org/sonar/db/CoreTestDb.java47
-rw-r--r--server/sonar-db-core/src/testFixtures/java/org/sonar/db/DatabaseTestUtils.java74
4 files changed, 89 insertions, 45 deletions
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 27e355543d6..4e328ad86de 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
@@ -31,9 +31,9 @@ import java.util.Properties;
import java.util.Set;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
-import org.sonar.api.config.internal.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.config.internal.Settings;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.DialectUtils;
import org.sonar.db.profiling.NullConnectionInterceptor;
@@ -256,4 +256,8 @@ public class DefaultDatabase implements Database {
public String toString() {
return format("Database[%s]", properties != null ? properties.getProperty(JDBC_URL.getKey()) : "?");
}
+
+ public Settings getSettings() {
+ return settings;
+ }
}
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 068bc3ee00c..37cd0c23a6f 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
@@ -58,6 +58,13 @@ public class DefaultDatabaseTest {
}
@Test
+ public void getSettings_shouldReturnExpectedSettings() {
+ MapSettings settings = new MapSettings();
+ settings.setProperty("test", "test");
+ DefaultDatabase db = new DefaultDatabase(logbackHelper, settings);
+ assertThat(db.getSettings()).isEqualTo(settings);
+ }
+ @Test
public void shouldExtractHikariProperties() {
Properties props = new Properties();
props.setProperty("sonar.jdbc.driverClassName", "my.Driver");
diff --git a/server/sonar-db-core/src/testFixtures/java/org/sonar/db/CoreTestDb.java b/server/sonar-db-core/src/testFixtures/java/org/sonar/db/CoreTestDb.java
index 066c6e16208..3e29a5a66bf 100644
--- a/server/sonar-db-core/src/testFixtures/java/org/sonar/db/CoreTestDb.java
+++ b/server/sonar-db-core/src/testFixtures/java/org/sonar/db/CoreTestDb.java
@@ -19,22 +19,17 @@
*/
package org.sonar.db;
-import java.sql.Connection;
-import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nullable;
-import javax.sql.DataSource;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.AssumptionViolatedException;
-import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.config.internal.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.db.version.SqTables;
+import org.sonar.api.config.internal.MapSettings;
+import org.sonar.api.config.internal.Settings;
import static java.util.Objects.requireNonNull;
import static org.sonar.process.ProcessProperties.Property.JDBC_USERNAME;
@@ -132,48 +127,12 @@ class CoreTestDb implements TestDb {
public void truncateTables() {
try {
- truncateDatabase(getDatabase().getDataSource());
+ DatabaseTestUtils.truncateAllTables(getDatabase().getDataSource());
} catch (SQLException e) {
throw new IllegalStateException("Fail to truncate db tables", e);
}
}
- private void truncateDatabase(DataSource dataSource) throws SQLException {
- try (Connection connection = dataSource.getConnection()) {
- connection.setAutoCommit(false);
- try (Statement statement = connection.createStatement()) {
- for (String table : SqTables.TABLES) {
- try {
- if (shouldTruncate(connection, table)) {
- statement.executeUpdate(truncateSql(table));
- connection.commit();
- }
- } catch (Exception e) {
- connection.rollback();
- throw new IllegalStateException("Fail to truncate table " + table, e);
- }
- }
- }
- }
- }
-
- private static boolean shouldTruncate(Connection connection, String table) {
- try (Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("select count(1) from " + table)) {
- if (rs.next()) {
- return rs.getInt(1) > 0;
- }
-
- } catch (SQLException ignored) {
- // probably because table does not exist. That's the case with H2 tests.
- }
- return false;
- }
-
- private static String truncateSql(String table) {
- return "TRUNCATE TABLE " + table;
- }
-
@Override
public Database getDatabase() {
return db;
diff --git a/server/sonar-db-core/src/testFixtures/java/org/sonar/db/DatabaseTestUtils.java b/server/sonar-db-core/src/testFixtures/java/org/sonar/db/DatabaseTestUtils.java
new file mode 100644
index 00000000000..4afeb5a7cd5
--- /dev/null
+++ b/server/sonar-db-core/src/testFixtures/java/org/sonar/db/DatabaseTestUtils.java
@@ -0,0 +1,74 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.sonar.db;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import javax.sql.DataSource;
+import org.sonar.db.version.SqTables;
+
+/**
+ * Utils class for test-specific database opertations
+ */
+public class DatabaseTestUtils {
+
+ private DatabaseTestUtils() {
+
+ }
+
+ public static void truncateAllTables(DataSource dataSource) throws SQLException {
+ try (Connection connection = dataSource.getConnection()) {
+ connection.setAutoCommit(false);
+ try (Statement statement = connection.createStatement()) {
+ for (String table : SqTables.TABLES) {
+ try {
+ if (shouldTruncate(connection, table)) {
+ statement.executeUpdate(truncateSql(table));
+ connection.commit();
+ }
+ } catch (Exception e) {
+ connection.rollback();
+ throw new IllegalStateException("Fail to truncate table " + table, e);
+ }
+ }
+ }
+ }
+ }
+
+ private static boolean shouldTruncate(Connection connection, String table) {
+ try (Statement stmt = connection.createStatement();
+ ResultSet rs = stmt.executeQuery("select count(1) from " + table)) {
+ if (rs.next()) {
+ return rs.getInt(1) > 0;
+ }
+
+ } catch (SQLException ignored) {
+ // probably because table does not exist. That's the case with H2 tests.
+ }
+ return false;
+ }
+
+ private static String truncateSql(String table) {
+ return "TRUNCATE TABLE " + table;
+ }
+
+}