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.def;
22 import org.junit.Test;
23 import org.sonar.db.dialect.H2;
24 import org.sonar.db.dialect.MsSql;
25 import org.sonar.db.dialect.Oracle;
26 import org.sonar.db.dialect.PostgreSql;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
31 public class VarcharColumnDefTest {
33 public void build_string_column_def() {
34 VarcharColumnDef def = new VarcharColumnDef.Builder()
35 .setColumnName("issues")
38 .setDefaultValue("foo")
41 assertThat(def.getName()).isEqualTo("issues");
42 assertThat(def.getColumnSize()).isEqualTo(10);
43 assertThat(def.isNullable()).isTrue();
44 assertThat(def.getDefaultValue()).isEqualTo("foo");
48 public void build_string_column_def_with_only_required_attributes() {
49 VarcharColumnDef def = new VarcharColumnDef.Builder()
50 .setColumnName("issues")
54 assertThat(def.getName()).isEqualTo("issues");
55 assertThat(def.getColumnSize()).isEqualTo(10);
56 assertThat(def.isNullable()).isTrue();
57 assertThat(def.getDefaultValue()).isNull();
61 public void generate_sql_type() {
62 VarcharColumnDef def = new VarcharColumnDef.Builder()
63 .setColumnName("issues")
68 assertThat(def.generateSqlType(new H2())).isEqualTo("VARCHAR (10)");
69 assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("VARCHAR (10)");
70 assertThat(def.generateSqlType(new MsSql())).isEqualTo("NVARCHAR (10)");
71 assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR2 (10 CHAR)");
75 public void generateSqlType_does_not_set_unit_on_oracle_if_legacy_mode() {
76 VarcharColumnDef def = new VarcharColumnDef.Builder()
77 .setColumnName("issues")
80 .setIgnoreOracleUnit(true)
83 assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR2 (10)");
87 public void fail_with_NPE_if_name_is_null() {
88 assertThatThrownBy(() -> {
89 new VarcharColumnDef.Builder()
92 .isInstanceOf(NullPointerException.class)
93 .hasMessage("Column name cannot be null");
97 public void fail_with_NPE_if_no_name() {
98 assertThatThrownBy(() -> {
99 new VarcharColumnDef.Builder()
102 .isInstanceOf(NullPointerException.class)
103 .hasMessage("Column name cannot be null");
107 public void fail_with_NPE_if_size_is_null() {
108 assertThatThrownBy(() -> {
109 new VarcharColumnDef.Builder()
110 .setColumnName("issues")
113 .isInstanceOf(NullPointerException.class)
114 .hasMessage("Limit cannot be null");