3 * Copyright (C) 2009-2017 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 org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.db.dialect.Dialect;
26 import org.sonar.db.dialect.H2;
27 import org.sonar.db.dialect.MsSql;
28 import org.sonar.db.dialect.MySql;
29 import org.sonar.db.dialect.Oracle;
30 import org.sonar.db.dialect.PostgreSql;
31 import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef;
32 import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder;
36 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
38 public class AddColumnsBuilderTest {
40 private static final String TABLE_NAME = "issues";
43 public ExpectedException thrown = ExpectedException.none();
46 public void add_columns_on_h2() {
47 assertThat(createSampleBuilder(new H2()).build())
48 .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default BOOLEAN DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)");
52 public void add_columns_on_mysql() {
53 assertThat(createSampleBuilder(new MySql()).build())
54 .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default TINYINT(1) DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)");
58 public void add_columns_on_oracle() {
59 assertThat(createSampleBuilder(new Oracle()).build())
60 .isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR (10 CHAR) NOT NULL, col_with_default NUMBER(1) DEFAULT 0 NOT NULL, varchar_col_with_default VARCHAR (3 CHAR) DEFAULT 'foo' NOT NULL)");
64 public void add_columns_on_postgresql() {
65 assertThat(createSampleBuilder(new PostgreSql()).build())
66 .isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL, ADD COLUMN col_with_default BOOLEAN DEFAULT false NOT NULL, ADD COLUMN varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL");
70 public void add_columns_on_mssql() {
71 assertThat(createSampleBuilder(new MsSql()).build())
72 .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name NVARCHAR (10) NOT NULL, col_with_default BIT DEFAULT 0 NOT NULL, varchar_col_with_default NVARCHAR (3) DEFAULT 'foo' NOT NULL");
76 public void fail_with_ISE_if_no_column() {
77 thrown.expect(IllegalStateException.class);
78 thrown.expectMessage("No column has been defined");
80 new AddColumnsBuilder(new H2(), TABLE_NAME).build();
83 private AddColumnsBuilder createSampleBuilder(Dialect dialect) {
84 return new AddColumnsBuilder(dialect, TABLE_NAME)
85 .addColumn(new BigIntegerColumnDef.Builder().setColumnName("date_in_ms").setIsNullable(true).build())
86 .addColumn(new VarcharColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build())
88 // columns with default values
89 .addColumn(newBooleanColumnDefBuilder().setColumnName("col_with_default").setDefaultValue(false).setIsNullable(false).build())
90 .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_with_default").setLimit(3).setDefaultValue("foo").setIsNullable(false).build());