]> source.dussan.org Git - sonarqube.git/blob
04137db1b3e9e0f37846f8d0a5ba86056736a4f0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.db.migration.def;
21
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;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
30
31 public class VarcharColumnDefTest {
32   @Test
33   public void build_string_column_def() {
34     VarcharColumnDef def = new VarcharColumnDef.Builder()
35       .setColumnName("issues")
36       .setLimit(10)
37       .setIsNullable(true)
38       .setDefaultValue("foo")
39       .build();
40
41     assertThat(def.getName()).isEqualTo("issues");
42     assertThat(def.getColumnSize()).isEqualTo(10);
43     assertThat(def.isNullable()).isTrue();
44     assertThat(def.getDefaultValue()).isEqualTo("foo");
45   }
46
47   @Test
48   public void build_string_column_def_with_only_required_attributes() {
49     VarcharColumnDef def = new VarcharColumnDef.Builder()
50       .setColumnName("issues")
51       .setLimit(10)
52       .build();
53
54     assertThat(def.getName()).isEqualTo("issues");
55     assertThat(def.getColumnSize()).isEqualTo(10);
56     assertThat(def.isNullable()).isTrue();
57     assertThat(def.getDefaultValue()).isNull();
58   }
59
60   @Test
61   public void generate_sql_type() {
62     VarcharColumnDef def = new VarcharColumnDef.Builder()
63       .setColumnName("issues")
64       .setLimit(10)
65       .setIsNullable(true)
66       .build();
67
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)");
72   }
73
74   @Test
75   public void generateSqlType_does_not_set_unit_on_oracle_if_legacy_mode() {
76     VarcharColumnDef def = new VarcharColumnDef.Builder()
77       .setColumnName("issues")
78       .setLimit(10)
79       .setIsNullable(true)
80       .setIgnoreOracleUnit(true)
81       .build();
82
83     assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR2 (10)");
84   }
85
86   @Test
87   public void fail_with_NPE_if_name_is_null() {
88     assertThatThrownBy(() -> {
89       new VarcharColumnDef.Builder()
90         .setColumnName(null);
91     })
92       .isInstanceOf(NullPointerException.class)
93       .hasMessage("Column name cannot be null");
94   }
95
96   @Test
97   public void fail_with_NPE_if_no_name() {
98     assertThatThrownBy(() -> {
99       new VarcharColumnDef.Builder()
100         .build();
101     })
102       .isInstanceOf(NullPointerException.class)
103       .hasMessage("Column name cannot be null");
104   }
105
106   @Test
107   public void fail_with_NPE_if_size_is_null() {
108     assertThatThrownBy(() -> {
109       new VarcharColumnDef.Builder()
110         .setColumnName("issues")
111         .build();
112     })
113       .isInstanceOf(NullPointerException.class)
114       .hasMessage("Limit cannot be null");
115   }
116 }