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.def;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.db.dialect.H2;
26 import org.sonar.db.dialect.MsSql;
27 import org.sonar.db.dialect.MySql;
28 import org.sonar.db.dialect.Oracle;
29 import org.sonar.db.dialect.PostgreSql;
31 import static org.assertj.core.api.Assertions.assertThat;
33 public class VarcharColumnDefTest {
36 public ExpectedException thrown = ExpectedException.none();
39 public void build_string_column_def() throws Exception {
40 VarcharColumnDef def = new VarcharColumnDef.Builder()
41 .setColumnName("issues")
44 .setDefaultValue("foo")
47 assertThat(def.getName()).isEqualTo("issues");
48 assertThat(def.getColumnSize()).isEqualTo(10);
49 assertThat(def.isNullable()).isTrue();
50 assertThat(def.getDefaultValue()).isEqualTo("foo");
54 public void build_string_column_def_with_only_required_attributes() throws Exception {
55 VarcharColumnDef def = new VarcharColumnDef.Builder()
56 .setColumnName("issues")
60 assertThat(def.getName()).isEqualTo("issues");
61 assertThat(def.getColumnSize()).isEqualTo(10);
62 assertThat(def.isNullable()).isTrue();
63 assertThat(def.getDefaultValue()).isNull();
67 public void generate_sql_type() throws Exception {
68 VarcharColumnDef def = new VarcharColumnDef.Builder()
69 .setColumnName("issues")
74 assertThat(def.generateSqlType(new H2())).isEqualTo("VARCHAR (10)");
75 assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("VARCHAR (10)");
76 assertThat(def.generateSqlType(new MySql())).isEqualTo("VARCHAR (10)");
77 assertThat(def.generateSqlType(new MsSql())).isEqualTo("NVARCHAR (10)");
78 assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR (10 CHAR)");
82 public void generateSqlType_does_not_set_unit_on_oracle_if_legacy_mode() throws Exception {
83 VarcharColumnDef def = new VarcharColumnDef.Builder()
84 .setColumnName("issues")
87 .setIgnoreOracleUnit(true)
90 assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR (10)");
94 public void fail_with_NPE_if_name_is_null() throws Exception {
95 thrown.expect(NullPointerException.class);
96 thrown.expectMessage("Column name cannot be null");
98 new VarcharColumnDef.Builder()
103 public void fail_with_NPE_if_no_name() throws Exception {
104 thrown.expect(NullPointerException.class);
105 thrown.expectMessage("Column name cannot be null");
107 new VarcharColumnDef.Builder()
112 public void fail_with_NPE_if_size_is_null() throws Exception {
113 thrown.expect(NullPointerException.class);
114 thrown.expectMessage("Limit cannot be null");
116 new VarcharColumnDef.Builder()
117 .setColumnName("issues")