3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.sql;
22 import java.util.List;
23 import org.junit.Test;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
29 public class CreateIndexBuilderTest {
31 public void create_index_on_single_column() {
32 verifySql(new CreateIndexBuilder()
34 .setName("issues_key")
35 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()),
36 "CREATE INDEX issues_key ON issues (kee)");
40 public void create_unique_index_on_single_column() {
41 verifySql(new CreateIndexBuilder()
43 .setName("issues_key")
44 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
46 "CREATE UNIQUE INDEX issues_key ON issues (kee)");
50 public void create_index_on_multiple_columns() {
51 verifySql(new CreateIndexBuilder()
54 .addColumn(newVarcharColumnDefBuilder().setColumnName("repository").setLimit(10).build())
55 .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_key").setLimit(50).build()),
56 "CREATE INDEX rules_key ON rules (repository, rule_key)");
60 public void create_unique_index_on_multiple_columns() {
61 verifySql(new CreateIndexBuilder()
64 .addColumn(newVarcharColumnDefBuilder().setColumnName("repository").setLimit(10).build())
65 .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_key").setLimit(50).build())
67 "CREATE UNIQUE INDEX rules_key ON rules (repository, rule_key)");
71 public void index_length_is_not_specified_on_big_varchar_columns() {
72 verifySql(new CreateIndexBuilder()
74 .setName("issues_key")
75 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(4000).build()),
76 "CREATE INDEX issues_key ON issues (kee)");
80 public void throw_NPE_if_table_is_missing() {
81 assertThatThrownBy(() -> {
82 new CreateIndexBuilder()
83 .setName("issues_key")
84 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
87 .isInstanceOf(NullPointerException.class)
88 .hasMessage("Table name can't be null");
92 public void throw_NPE_if_index_name_is_missing() {
93 assertThatThrownBy(() -> {
94 new CreateIndexBuilder()
96 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
99 .isInstanceOf(NullPointerException.class)
100 .hasMessage("Index name can't be null");
104 public void throw_IAE_if_columns_are_missing() {
105 assertThatThrownBy(() -> {
106 new CreateIndexBuilder()
108 .setName("issues_key")
111 .isInstanceOf(IllegalArgumentException.class)
112 .hasMessage("at least one column must be specified");
116 public void throw_IAE_if_table_name_is_not_valid() {
117 assertThatThrownBy(() -> {
118 new CreateIndexBuilder()
119 .setTable("(not valid)")
120 .setName("issues_key")
121 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
124 .isInstanceOf(IllegalArgumentException.class)
125 .hasMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'");
129 public void throw_NPE_when_adding_null_column() {
130 assertThatThrownBy(() -> {
131 new CreateIndexBuilder()
133 .setName("issues_key")
134 .addColumn((String) null)
137 .isInstanceOf(NullPointerException.class)
138 .hasMessage("Column cannot be null");
141 private static void verifySql(CreateIndexBuilder builder, String expectedSql) {
142 List<String> actual = builder.build();
143 assertThat(actual).containsExactly(expectedSql);