From 2795319a14d807340954ee81a14fff07fafed27a Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Mon, 23 Oct 2023 09:55:09 +0200 Subject: [PATCH] NO-JIRA Fix code smells --- .../db/migration/sql/CreateIndexBuilder.java | 2 +- .../migration/sql/CreateIndexBuilderTest.java | 67 ++++++++----------- 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"); } -- 2.39.5