]> source.dussan.org Git - sonarqube.git/blob
63f1e304dc84550db15cd4b5ba2995c044b97eeb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.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;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32
33 public class VarcharColumnDefTest {
34
35   @Rule
36   public ExpectedException thrown = ExpectedException.none();
37
38   @Test
39   public void build_string_column_def() throws Exception {
40     VarcharColumnDef def = new VarcharColumnDef.Builder()
41       .setColumnName("issues")
42       .setLimit(10)
43       .setIsNullable(true)
44       .setDefaultValue("foo")
45       .build();
46
47     assertThat(def.getName()).isEqualTo("issues");
48     assertThat(def.getColumnSize()).isEqualTo(10);
49     assertThat(def.isNullable()).isTrue();
50     assertThat(def.getDefaultValue()).isEqualTo("foo");
51   }
52
53   @Test
54   public void build_string_column_def_with_only_required_attributes() throws Exception {
55     VarcharColumnDef def = new VarcharColumnDef.Builder()
56       .setColumnName("issues")
57       .setLimit(10)
58       .build();
59
60     assertThat(def.getName()).isEqualTo("issues");
61     assertThat(def.getColumnSize()).isEqualTo(10);
62     assertThat(def.isNullable()).isTrue();
63     assertThat(def.getDefaultValue()).isNull();
64   }
65
66   @Test
67   public void generate_sql_type() throws Exception {
68     VarcharColumnDef def = new VarcharColumnDef.Builder()
69       .setColumnName("issues")
70       .setLimit(10)
71       .setIsNullable(true)
72       .build();
73
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)");
79   }
80
81   @Test
82   public void generateSqlType_does_not_set_unit_on_oracle_if_legacy_mode() throws Exception {
83     VarcharColumnDef def = new VarcharColumnDef.Builder()
84       .setColumnName("issues")
85       .setLimit(10)
86       .setIsNullable(true)
87       .setIgnoreOracleUnit(true)
88       .build();
89
90     assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR (10)");
91   }
92
93   @Test
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");
97
98     new VarcharColumnDef.Builder()
99       .setColumnName(null);
100   }
101
102   @Test
103   public void fail_with_NPE_if_no_name() throws Exception {
104     thrown.expect(NullPointerException.class);
105     thrown.expectMessage("Column name cannot be null");
106
107     new VarcharColumnDef.Builder()
108       .build();
109   }
110
111   @Test
112   public void fail_with_NPE_if_size_is_null() throws Exception {
113     thrown.expect(NullPointerException.class);
114     thrown.expectMessage("Limit cannot be null");
115
116     new VarcharColumnDef.Builder()
117       .setColumnName("issues")
118       .build();
119   }
120 }