aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2023-10-23 09:55:09 +0200
committersonartech <sonartech@sonarsource.com>2023-10-23 20:02:42 +0000
commit2795319a14d807340954ee81a14fff07fafed27a (patch)
treefeed72a5ab76d30b6db698bb91d1426734382c42 /server/sonar-db-migration
parenta22e9845fcc54dfde358327ed931383cd5fe3b01 (diff)
downloadsonarqube-2795319a14d807340954ee81a14fff07fafed27a.tar.gz
sonarqube-2795319a14d807340954ee81a14fff07fafed27a.zip
NO-JIRA Fix code smells
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilder.java2
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilderTest.java67
2 files changed, 28 insertions, 41 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilder.java
index f9e405c14ff..59bae4f8a97 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilder.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilder.java
@@ -136,7 +136,7 @@ public class CreateIndexBuilder {
*/
if (unique && !dialect.supportsNullNotDistinct() && PostgreSql.ID.equals(dialect.getId())) {
sql.append(columns.stream()
- .map(c -> c.isNullable() ? "COALESCE(%s, '')".formatted(c.name()) : c.name())
+ .map(c -> Boolean.TRUE.equals(c.isNullable()) ? "COALESCE(%s, '')".formatted(c.name()) : c.name())
.collect(Collectors.joining(", ")));
} else {
sql.append(columns.stream()
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilderTest.java
index fbeb7be6e02..6485fd61b0d 100644
--- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilderTest.java
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateIndexBuilderTest.java
@@ -133,76 +133,63 @@ public class CreateIndexBuilderTest {
@Test
public void throw_NPE_if_table_is_missing() {
- assertThatThrownBy(() -> {
- new CreateIndexBuilder(new Oracle())
- .setName("issues_key")
- .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
- .build();
- })
+ CreateIndexBuilder builder = new CreateIndexBuilder(new Oracle())
+ .setName("issues_key")
+ .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build());
+ assertThatThrownBy(builder::build)
.isInstanceOf(NullPointerException.class)
.hasMessage("Table name can't be null");
}
@Test
public void throw_NPE_if_index_name_is_missing() {
- assertThatThrownBy(() -> {
- new CreateIndexBuilder(new H2())
- .setTable("issues")
- .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
- .build();
- })
+ CreateIndexBuilder builder = new CreateIndexBuilder(new H2())
+ .setTable("issues")
+ .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build());
+ assertThatThrownBy(builder::build)
.isInstanceOf(NullPointerException.class)
.hasMessage("Index name can't be null");
}
@Test
public void build_shouldThrowException_whenUniqueAndColumnNullabilityIsNotProvided() {
- assertThatThrownBy(() -> {
- new CreateIndexBuilder(new H2())
- .setTable("issues")
- .setName("name")
- .addColumn("columnName")
- .setUnique(true)
- .build();
- })
+ CreateIndexBuilder builder = new CreateIndexBuilder(new H2())
+ .setTable("issues")
+ .setName("name")
+ .addColumn("columnName")
+ .setUnique(true);
+ assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Nullability of column should be provided for unique indexes");
}
@Test
public void throw_IAE_if_columns_are_missing() {
- assertThatThrownBy(() -> {
- new CreateIndexBuilder(new H2())
- .setTable("issues")
- .setName("issues_key")
- .build();
- })
+ CreateIndexBuilder builder = new CreateIndexBuilder(new H2())
+ .setTable("issues")
+ .setName("issues_key");
+ assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("at least one column must be specified");
}
@Test
public void throw_IAE_if_table_name_is_not_valid() {
- assertThatThrownBy(() -> {
- new CreateIndexBuilder(new H2())
- .setTable("(not valid)")
- .setName("issues_key")
- .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
- .build();
- })
+ CreateIndexBuilder builder = new CreateIndexBuilder(new H2())
+ .setTable("(not valid)")
+ .setName("issues_key")
+ .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build());
+ assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'");
}
@Test
public void throw_NPE_when_adding_null_column() {
- assertThatThrownBy(() -> {
- new CreateIndexBuilder(new H2())
- .setTable("issues")
- .setName("issues_key")
- .addColumn((String) null)
- .build();
- })
+ CreateIndexBuilder builder = new CreateIndexBuilder(new H2())
+ .setTable("issues")
+ .setName("issues_key");
+ assertThatThrownBy(() -> builder.addColumn((String) null))
.isInstanceOf(NullPointerException.class)
.hasMessage("Column cannot be null");
}