aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-03-21 10:55:15 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-03-23 17:54:56 +0100
commit6ab855db3c2601bb1a96c8561a8849229d93e863 (patch)
tree91c630cdb45039b71edf46617415b16dec28add2
parent0db377f0345013e8f4962923278c0be6b76d82b2 (diff)
downloadsonarqube-6ab855db3c2601bb1a96c8561a8849229d93e863.tar.gz
sonarqube-6ab855db3c2601bb1a96c8561a8849229d93e863.zip
SONAR-8867 fix support for composite PK in assertPrimaryKey
-rw-r--r--server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java68
1 files changed, 48 insertions, 20 deletions
diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java b/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java
index 5777e7cb974..cd39b524349 100644
--- a/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java
+++ b/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java
@@ -23,6 +23,7 @@ import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Ordering;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Clob;
@@ -60,6 +61,7 @@ import org.dbunit.ext.mssql.InsertIdentityOperation;
import org.dbunit.operation.DatabaseOperation;
import org.junit.rules.ExternalResource;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Lists.asList;
@@ -88,7 +90,7 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
SQLException nextException = e.getNextException();
if (nextException != null) {
throw new IllegalStateException("Fail to execute sql: " + sql,
- new SQLException(e.getMessage(), nextException.getSQLState(), nextException.getErrorCode(), nextException));
+ new SQLException(e.getMessage(), nextException.getSQLState(), nextException.getErrorCode(), nextException));
}
throw new IllegalStateException("Fail to execute sql: " + sql, e);
} catch (Exception e) {
@@ -98,7 +100,7 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
public void executeDdl(String ddl) {
try (Connection connection = getConnection();
- Statement stmt = connection.createStatement()) {
+ Statement stmt = connection.createStatement()) {
stmt.execute(ddl);
} catch (SQLException e) {
throw new IllegalStateException("Failed to execute DDL: " + ddl, e);
@@ -137,10 +139,10 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
}
String sql = "insert into " + table.toLowerCase(Locale.ENGLISH) + " (" +
- COMMA_JOINER.join(valuesByColumn.keySet()) +
- ") values (" +
- COMMA_JOINER.join(Collections.nCopies(valuesByColumn.size(), '?')) +
- ")";
+ COMMA_JOINER.join(valuesByColumn.keySet()) +
+ ") values (" +
+ COMMA_JOINER.join(Collections.nCopies(valuesByColumn.size(), '?')) +
+ ")";
executeUpdateSql(sql, valuesByColumn.values().toArray(new Object[valuesByColumn.size()]));
}
@@ -167,11 +169,11 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
protected int countSql(String sql, ConnectionSupplier connectionSupplier) {
checkArgument(StringUtils.contains(sql, "count("),
- "Parameter must be a SQL request containing 'count(x)' function. Got " + sql);
+ "Parameter must be a SQL request containing 'count(x)' function. Got " + sql);
try (
- ConnectionSupplier supplier = connectionSupplier;
- PreparedStatement stmt = supplier.get().prepareStatement(sql);
- ResultSet rs = stmt.executeQuery()) {
+ ConnectionSupplier supplier = connectionSupplier;
+ PreparedStatement stmt = supplier.get().prepareStatement(sql);
+ ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt(1);
}
@@ -188,9 +190,9 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
protected List<Map<String, Object>> select(String selectSql, ConnectionSupplier connectionSupplier) {
try (
- ConnectionSupplier supplier = connectionSupplier;
- PreparedStatement stmt = supplier.get().prepareStatement(selectSql);
- ResultSet rs = stmt.executeQuery()) {
+ ConnectionSupplier supplier = connectionSupplier;
+ PreparedStatement stmt = supplier.get().prepareStatement(selectSql);
+ ResultSet rs = stmt.executeQuery()) {
return getHashMap(rs);
} catch (Exception e) {
throw new IllegalStateException("Fail to execute sql: " + selectSql, e);
@@ -346,8 +348,8 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
public void assertColumnDefinition(String table, String column, int expectedType, @Nullable Integer expectedSize, @Nullable Boolean isNullable) {
try (Connection connection = getConnection();
- PreparedStatement stmt = connection.prepareStatement("select * from " + table);
- ResultSet res = stmt.executeQuery()) {
+ PreparedStatement stmt = connection.prepareStatement("select * from " + table);
+ ResultSet res = stmt.executeQuery()) {
Integer columnIndex = getColumnIndex(res, column);
if (columnIndex == null) {
fail("The column '" + column + "' does not exist");
@@ -390,7 +392,7 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
private void assertIndexImpl(String tableName, String indexName, boolean expectedUnique, String expectedColumn, String... expectedSecondaryColumns) {
try (Connection connection = getConnection();
- ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) {
+ ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) {
List<String> onColumns = new ArrayList<>();
while (rs.next()) {
if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) {
@@ -410,7 +412,7 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
*/
public void assertIndexDoesNotExist(String tableName, String indexName) {
try (Connection connection = getConnection();
- ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) {
+ ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) {
List<String> indices = new ArrayList<>();
while (rs.next()) {
indices.add(rs.getString("INDEX_NAME").toLowerCase(Locale.ENGLISH));
@@ -448,7 +450,7 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
private PK pkOf(Connection connection, String tableName) throws SQLException {
try (ResultSet resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName)) {
String pkName = null;
- ArrayList<String> columnNames = null;
+ List<PkColumn> columnNames = null;
while (resultSet.next()) {
if (columnNames == null) {
pkName = resultSet.getString("PK_NAME");
@@ -456,12 +458,38 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
} else {
assertThat(pkName).as("Multiple primary keys found").isEqualTo(resultSet.getString("PK_NAME"));
}
- columnNames.add(resultSet.getInt("KEY_SEQ") - 1, resultSet.getString("COLUMN_NAME"));
+ columnNames.add(new PkColumn(resultSet.getInt("KEY_SEQ") - 1, resultSet.getString("COLUMN_NAME")));
}
if (columnNames == null) {
return null;
}
- return new PK(pkName, columnNames);
+ return new PK(
+ pkName,
+ columnNames.stream()
+ .sorted(PkColumn.ORDERING_BY_INDEX)
+ .map(PkColumn::getName)
+ .collect(Collectors.toList()));
+ }
+ }
+
+ private static final class PkColumn {
+ private static final Ordering<PkColumn> ORDERING_BY_INDEX = Ordering.natural().onResultOf(PkColumn::getIndex);
+
+ /** 0-based */
+ private final int index;
+ private final String name;
+
+ private PkColumn(int index, String name) {
+ this.index = index;
+ this.name = name;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public String getName() {
+ return name;
}
}