3 * Copyright (C) 2009-2020 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.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
30 public class CreateIndexBuilderTest {
33 public ExpectedException expectedException = ExpectedException.none();
36 public void create_index_on_single_column() {
37 verifySql(new CreateIndexBuilder()
39 .setName("issues_key")
40 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()),
41 "CREATE INDEX issues_key ON issues (kee)");
45 public void create_unique_index_on_single_column() {
46 verifySql(new CreateIndexBuilder()
48 .setName("issues_key")
49 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
51 "CREATE UNIQUE INDEX issues_key ON issues (kee)");
55 public void create_index_on_multiple_columns() {
56 verifySql(new CreateIndexBuilder()
59 .addColumn(newVarcharColumnDefBuilder().setColumnName("repository").setLimit(10).build())
60 .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_key").setLimit(50).build()),
61 "CREATE INDEX rules_key ON rules (repository, rule_key)");
65 public void create_unique_index_on_multiple_columns() {
66 verifySql(new CreateIndexBuilder()
69 .addColumn(newVarcharColumnDefBuilder().setColumnName("repository").setLimit(10).build())
70 .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_key").setLimit(50).build())
72 "CREATE UNIQUE INDEX rules_key ON rules (repository, rule_key)");
76 public void index_length_is_not_specified_on_big_varchar_columns() {
77 verifySql(new CreateIndexBuilder()
79 .setName("issues_key")
80 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(4000).build()),
81 "CREATE INDEX issues_key ON issues (kee)");
85 public void throw_NPE_if_table_is_missing() {
86 expectedException.expect(NullPointerException.class);
87 expectedException.expectMessage("Table name cannot be null");
89 new CreateIndexBuilder()
90 .setName("issues_key")
91 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
96 public void throw_NPE_if_index_name_is_missing() {
97 expectedException.expect(NullPointerException.class);
98 expectedException.expectMessage("Index name cannot be null");
100 new CreateIndexBuilder()
102 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
107 public void throw_IAE_if_columns_are_missing() {
108 expectedException.expect(IllegalArgumentException.class);
109 expectedException.expectMessage("at least one column must be specified");
111 new CreateIndexBuilder()
113 .setName("issues_key")
118 public void throw_IAE_if_table_name_is_not_valid() {
119 expectedException.expect(IllegalArgumentException.class);
120 expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'");
122 new CreateIndexBuilder()
123 .setTable("(not valid)")
124 .setName("issues_key")
125 .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build())
130 public void throw_NPE_when_adding_null_column() {
131 expectedException.expect(NullPointerException.class);
132 expectedException.expectMessage("Column cannot be null");
134 new CreateIndexBuilder()
136 .setName("issues_key")
141 private static void verifySql(CreateIndexBuilder builder, String expectedSql) {
142 List<String> actual = builder.build();
143 assertThat(actual).containsExactly(expectedSql);