]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6840 Replace ColumnDef by builders
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 17 Sep 2015 15:14:21 +0000 (17:14 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 18 Sep 2015 13:32:50 +0000 (15:32 +0200)
14 files changed:
sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
sonar-db/src/main/java/org/sonar/db/version/BigDecimalColumnDef.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
sonar-db/src/main/java/org/sonar/db/version/ColumnDefValidation.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DropColumnsBuilder.java
sonar-db/src/main/java/org/sonar/db/version/StringColumnDef.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/v51/AddIssuesColumns.java
sonar-db/src/main/java/org/sonar/db/version/v52/AddManualMeasuresComponentUuidColumn.java
sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java
sonar-db/src/test/java/org/sonar/db/version/BigDecimalColumnDefTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/ColumnDefTest.java [deleted file]
sonar-db/src/test/java/org/sonar/db/version/ColumnDefValidationTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/StringColumnDefTest.java [new file with mode: 0644]

diff --git a/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
new file mode 100644 (file)
index 0000000..b261f0b
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+public abstract class AbstractColumnDef implements ColumnDef {
+  private final String columnName;
+  private final boolean isNullable;
+
+  public AbstractColumnDef(String columnName, boolean isNullable) {
+    this.columnName = columnName;
+    this.isNullable = isNullable;
+  }
+
+  @Override
+  public String getName() {
+    return columnName;
+  }
+
+  @Override
+  public boolean isNullable() {
+    return isNullable;
+  }
+}
index 2bfb76535450f5863c766d224d247764cdd31c04..c33bf52736e0abb4517f0f2cae949ad58c67d3bf 100644 (file)
@@ -22,11 +22,13 @@ package org.sonar.db.version;
 import java.util.List;
 import org.sonar.db.dialect.Dialect;
 import org.sonar.db.dialect.MsSql;
-import org.sonar.db.dialect.Oracle;
 import org.sonar.db.dialect.PostgreSql;
 
 import static com.google.common.collect.Lists.newArrayList;
 
+/**
+ * Generate a SQL query to add multiple columns on a table
+ */
 public class AddColumnsBuilder {
 
   private final Dialect dialect;
@@ -76,23 +78,8 @@ public class AddColumnsBuilder {
   }
 
   private void addColumn(StringBuilder sql, ColumnDef columnDef) {
-    sql.append(columnDef.getName()).append(" ").append(typeToSql(columnDef));
-    Integer limit = columnDef.getLimit();
-    if (limit != null) {
-      sql.append(" (").append(Integer.toString(limit)).append(")");
-    }
+    sql.append(columnDef.getName()).append(" ").append(columnDef.generateSqlType(dialect));
     sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
   }
 
-  private String typeToSql(ColumnDef columnDef) {
-    switch (columnDef.getType()) {
-      case STRING:
-        return "VARCHAR";
-      case BIG_INTEGER:
-        return !dialect.getId().equals(Oracle.ID) ? "BIGINT" : "NUMBER (38)";
-      default:
-        throw new IllegalArgumentException("Unsupported type : " + columnDef.getType());
-    }
-  }
-
 }
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BigDecimalColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BigDecimalColumnDef.java
new file mode 100644 (file)
index 0000000..773913f
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+import javax.annotation.CheckForNull;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.Oracle;
+
+import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+
+public class BigDecimalColumnDef extends AbstractColumnDef {
+
+  private BigDecimalColumnDef(Builder builder) {
+    super(builder.columnName, builder.isNullable);
+  }
+
+  public static Builder newBigDecimalColumnDefBuilder() {
+    return new Builder();
+  }
+
+  @Override
+  public String generateSqlType(Dialect dialect) {
+    return dialect.getId().equals(Oracle.ID) ? "NUMBER (38)" : "BIGINT";
+  }
+
+  public static class Builder {
+    @CheckForNull
+    private String columnName;
+
+    private boolean isNullable;
+
+    public Builder setColumnName(String columnName) {
+      this.columnName = validateColumnName(columnName);
+      return this;
+    }
+
+    public Builder setIsNullable(boolean isNullable) {
+      this.isNullable = isNullable;
+      return this;
+    }
+
+    public BigDecimalColumnDef build() {
+      validateColumnName(columnName);
+      return new BigDecimalColumnDef(this);
+    }
+  }
+
+}
index a95fba6464f8a25310bf7baec342c06975a934b3..a04ce29cd564a0f764ecbf7642634b094dc17251 100644 (file)
 
 package org.sonar.db.version;
 
-import com.google.common.base.CharMatcher;
-import com.google.common.base.Preconditions;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
 import org.sonar.db.dialect.Dialect;
-import org.sonar.db.dialect.Oracle;
 
-public class ColumnDef {
+public interface ColumnDef {
 
-  private String name;
-  private Type type;
-  private boolean isNullable;
-  private Integer limit;
+  boolean isNullable();
 
-  public enum Type {
-    STRING, BIG_INTEGER
-  }
+  String getName();
 
-  public ColumnDef setNullable(boolean isNullable) {
-    this.isNullable = isNullable;
-    return this;
-  }
-
-  public ColumnDef setLimit(@Nullable Integer limit) {
-    this.limit = limit;
-    return this;
-  }
-
-  public ColumnDef setName(String name) {
-    Preconditions.checkArgument(CharMatcher.JAVA_LOWER_CASE.or(CharMatcher.anyOf("_")).matchesAllOf(name), "Column name should only contains lowercase and _ characters");
-    this.name = name;
-    return this;
-  }
-
-  public ColumnDef setType(Type type) {
-    this.type = type;
-    return this;
-  }
-
-  public boolean isNullable() {
-    return isNullable;
-  }
-
-  @CheckForNull
-  public Integer getLimit() {
-    return limit;
-  }
-
-  public String getName() {
-    return name;
-  }
-
-  public Type getType() {
-    return type;
-  }
-
-  public String getSqlType(Dialect dialect) {
-    switch (type) {
-      case STRING:
-        return "VARCHAR";
-      case BIG_INTEGER:
-        return dialect.getId().equals(Oracle.ID) ? "NUMBER (38)" : "BIGINT";
-      default:
-        throw new IllegalArgumentException("Unsupported type : " + type);
-    }
-  }
+  String generateSqlType(Dialect dialect);
 
 }
diff --git a/sonar-db/src/main/java/org/sonar/db/version/ColumnDefValidation.java b/sonar-db/src/main/java/org/sonar/db/version/ColumnDefValidation.java
new file mode 100644 (file)
index 0000000..ed017bc
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+import com.google.common.base.CharMatcher;
+import javax.annotation.Nullable;
+
+import static com.google.common.base.CharMatcher.JAVA_LOWER_CASE;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+public class ColumnDefValidation {
+
+  private ColumnDefValidation() {
+    // Only static stuff here
+  }
+
+  public static String validateColumnName(@Nullable String columnName) {
+    String name = requireNonNull(columnName, "Column name cannot be null");
+    checkArgument(JAVA_LOWER_CASE.or(CharMatcher.anyOf("_")).matchesAllOf(name), String.format("Column name should only contains lowercase and _ characters, got '%s'", columnName));
+    return name;
+  }
+}
index 61a0fbc45c8a67e76f6d8e3d79f71a003222b407..ce0d5f9e771fd3bd1befa163d42df8101aaad261 100644 (file)
@@ -25,6 +25,9 @@ import org.sonar.db.dialect.MySql;
 import org.sonar.db.dialect.Oracle;
 import org.sonar.db.dialect.PostgreSql;
 
+/**
+ * Generate a SQL query to drop multiple columns from a table
+ */
 public class DropColumnsBuilder {
 
   private final Dialect dialect;
diff --git a/sonar-db/src/main/java/org/sonar/db/version/StringColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/StringColumnDef.java
new file mode 100644 (file)
index 0000000..6753e85
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+import javax.annotation.CheckForNull;
+import org.sonar.db.dialect.Dialect;
+
+import static java.util.Objects.requireNonNull;
+import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+
+public class StringColumnDef extends AbstractColumnDef {
+  private final int columnSize;
+
+  private StringColumnDef(Builder builder) {
+    super(builder.columnName, builder.isNullable);
+    this.columnSize = builder.columnSize;
+  }
+
+  public static Builder newStringColumnDefBuilder() {
+    return new Builder();
+  }
+
+  public int getColumnSize() {
+    return columnSize;
+  }
+
+  @Override
+  public String generateSqlType(Dialect dialect) {
+    return "VARCHAR (" + Integer.toString(columnSize) + ")";
+  }
+
+  public static class Builder {
+    @CheckForNull
+    private Integer columnSize;
+
+    @CheckForNull
+    private String columnName;
+
+    private boolean isNullable;
+
+    public Builder setColumnName(String columnName) {
+      this.columnName = validateColumnName(columnName);
+      return this;
+    }
+
+    public Builder setLimit(int limit) {
+      this.columnSize = limit;
+      return this;
+    }
+
+    public Builder setIsNullable(boolean isNullable) {
+      this.isNullable = isNullable;
+      return this;
+    }
+
+    public StringColumnDef build() {
+      validateColumnName(columnName);
+      requireNonNull(columnSize, "Limit cannot be null");
+      return new StringColumnDef(this);
+    }
+  }
+
+}
index f8070a7b34c314c266931bc052c2088c3be2b1aa..34ab5edcf5beb14b6f8d0633b0bfde2b3753b12f 100644 (file)
@@ -23,9 +23,11 @@ package org.sonar.db.version.v51;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.db.version.AddColumnsBuilder;
-import org.sonar.db.version.ColumnDef;
 import org.sonar.db.version.DdlChange;
 
+import static org.sonar.db.version.BigDecimalColumnDef.newBigDecimalColumnDefBuilder;
+import static org.sonar.db.version.StringColumnDef.newStringColumnDefBuilder;
+
 /**
  * Add the following columns to the issues table :
  * - issue_creation_date_ms
@@ -51,42 +53,12 @@ public class AddIssuesColumns extends DdlChange {
 
   private String generateSql() {
     return new AddColumnsBuilder(db.getDialect(), "issues")
-      .addColumn(
-        new ColumnDef()
-          .setName("issue_creation_date_ms")
-          .setType(ColumnDef.Type.BIG_INTEGER)
-          .setNullable(true)
-      )
-      .addColumn(
-        new ColumnDef()
-          .setName("issue_update_date_ms")
-          .setType(ColumnDef.Type.BIG_INTEGER)
-          .setNullable(true)
-      )
-      .addColumn(
-        new ColumnDef()
-          .setName("issue_close_date_ms")
-          .setType(ColumnDef.Type.BIG_INTEGER)
-          .setNullable(true)
-      )
-      .addColumn(
-        new ColumnDef()
-          .setName("tags")
-          .setType(ColumnDef.Type.STRING)
-          .setLimit(4000)
-          .setNullable(true))
-      .addColumn(
-        new ColumnDef()
-          .setName("component_uuid")
-          .setType(ColumnDef.Type.STRING)
-          .setLimit(50)
-          .setNullable(true))
-      .addColumn(
-        new ColumnDef()
-          .setName("project_uuid")
-          .setType(ColumnDef.Type.STRING)
-          .setLimit(50)
-          .setNullable(true))
+      .addColumn(newBigDecimalColumnDefBuilder().setColumnName("issue_creation_date_ms").setIsNullable(true).build())
+      .addColumn(newBigDecimalColumnDefBuilder().setColumnName("issue_update_date_ms").setIsNullable(true).build())
+      .addColumn(newBigDecimalColumnDefBuilder().setColumnName("issue_close_date_ms").setIsNullable(true).build())
+      .addColumn(newStringColumnDefBuilder().setColumnName("tags").setLimit(4000).setIsNullable(true).build())
+      .addColumn(newStringColumnDefBuilder().setColumnName("component_uuid").setLimit(50).setIsNullable(true).build())
+      .addColumn(newStringColumnDefBuilder().setColumnName("project_uuid").setLimit(50).setIsNullable(true).build())
       .build();
   }
 
index 9f5e23218b01075f39444be72dfcacf0f66ef197..a1708e6537536473a8e1e8049761794467f975f3 100644 (file)
@@ -23,10 +23,9 @@ package org.sonar.db.version.v52;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.db.version.AddColumnsBuilder;
-import org.sonar.db.version.ColumnDef;
 import org.sonar.db.version.DdlChange;
 
-import static org.sonar.db.version.ColumnDef.Type.STRING;
+import static org.sonar.db.version.StringColumnDef.newStringColumnDefBuilder;
 
 /**
  * Add the following column to the manual_measures table :
@@ -45,12 +44,7 @@ public class AddManualMeasuresComponentUuidColumn extends DdlChange {
 
   private String generateSql() {
     return new AddColumnsBuilder(getDatabase().getDialect(), "manual_measures")
-      .addColumn(
-        new ColumnDef()
-          .setName("component_uuid")
-          .setType(STRING)
-          .setLimit(50)
-          .setNullable(true))
+      .addColumn(newStringColumnDefBuilder().setColumnName("component_uuid").setLimit(50).setIsNullable(true).build())
       .build();
   }
 }
index 58ac7014cd72d5bed6576f5d19a73946e0fa5479..7970db2afec3057bef8f5815f9125e58ed9af25f 100644 (file)
@@ -30,8 +30,6 @@ import org.sonar.db.dialect.Oracle;
 import org.sonar.db.dialect.PostgreSql;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.version.ColumnDef.Type.BIG_INTEGER;
-import static org.sonar.db.version.ColumnDef.Type.STRING;
 
 public class AddColumnsBuilderTest {
 
@@ -40,39 +38,33 @@ public class AddColumnsBuilderTest {
 
   static final String TABLE_NAME = "issues";
 
-  static final H2 H2_DIALECT = new H2();
-  static final MySql MYSQL_DIALECT = new MySql();
-  static final Oracle ORACLE_DIALECT = new Oracle();
-  static final PostgreSql POSTGRES_DIALECT = new PostgreSql();
-  static final MsSql MSSQL_DIALECT = new MsSql();
-
   @Test
   public void add_columns_on_h2() {
-    assertThat(createSampleBuilder(H2_DIALECT).build())
+    assertThat(createSampleBuilder(new H2()).build())
       .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)");
   }
 
   @Test
   public void add_columns_on_mysql() {
-    assertThat(createSampleBuilder(MYSQL_DIALECT).build())
+    assertThat(createSampleBuilder(new MySql()).build())
       .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)");
   }
 
   @Test
   public void add_columns_on_oracle() {
-    assertThat(createSampleBuilder(ORACLE_DIALECT).build())
+    assertThat(createSampleBuilder(new Oracle()).build())
       .isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR (10) NOT NULL)");
   }
 
   @Test
   public void add_columns_on_postgresql() {
-    assertThat(createSampleBuilder(POSTGRES_DIALECT).build())
+    assertThat(createSampleBuilder(new PostgreSql()).build())
       .isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL");
   }
 
   @Test
   public void add_columns_on_mssql() {
-    assertThat(createSampleBuilder(MSSQL_DIALECT).build())
+    assertThat(createSampleBuilder(new MsSql()).build())
       .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL");
   }
 
@@ -81,20 +73,12 @@ public class AddColumnsBuilderTest {
     thrown.expect(IllegalStateException.class);
     thrown.expectMessage("No column has been defined");
 
-    new AddColumnsBuilder(H2_DIALECT, TABLE_NAME).build();
+    new AddColumnsBuilder(new H2(), TABLE_NAME).build();
   }
 
   private AddColumnsBuilder createSampleBuilder(Dialect dialect) {
     return new AddColumnsBuilder(dialect, TABLE_NAME)
-      .addColumn(new ColumnDef()
-        .setName("date_in_ms")
-        .setType(BIG_INTEGER)
-        .setNullable(true))
-      .addColumn(new ColumnDef()
-        .setName("name")
-        .setType(STRING)
-        .setNullable(false)
-        .setLimit(10));
+      .addColumn(new BigDecimalColumnDef.Builder().setColumnName("date_in_ms").setIsNullable(true).build())
+      .addColumn(new StringColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build());
   }
-
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/BigDecimalColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/BigDecimalColumnDefTest.java
new file mode 100644 (file)
index 0000000..286df38
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.db.dialect.PostgreSql;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class BigDecimalColumnDefTest {
+
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
+  @Test
+  public void build_string_column_def() throws Exception {
+    BigDecimalColumnDef def = new BigDecimalColumnDef.Builder()
+      .setColumnName("issues")
+      .setIsNullable(true)
+      .build();
+
+    assertThat(def.getName()).isEqualTo("issues");
+    assertThat(def.isNullable()).isTrue();
+  }
+
+  @Test
+  public void generate_sql_type() throws Exception {
+    BigDecimalColumnDef def = new BigDecimalColumnDef.Builder()
+      .setColumnName("issues")
+      .setIsNullable(true)
+      .build();
+
+    assertThat(def.generateSqlType(new H2())).isEqualTo("BIGINT");
+    assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("BIGINT");
+    assertThat(def.generateSqlType(new MsSql())).isEqualTo("BIGINT");
+    assertThat(def.generateSqlType(new MySql())).isEqualTo("BIGINT");
+    assertThat(def.generateSqlType(new Oracle())).isEqualTo("NUMBER (38)");
+  }
+
+  @Test
+  public void fail_with_NPE_if_name_is_null() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Column name cannot be null");
+
+    new BigDecimalColumnDef.Builder()
+      .setColumnName(null);
+  }
+
+  @Test
+  public void fail_with_NPE_if_no_name() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Column name cannot be null");
+
+    new BigDecimalColumnDef.Builder()
+      .build();
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/ColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/ColumnDefTest.java
deleted file mode 100644 (file)
index c018d84..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.db.version;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.db.dialect.H2;
-import org.sonar.db.dialect.MsSql;
-import org.sonar.db.dialect.MySql;
-import org.sonar.db.dialect.Oracle;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.version.ColumnDef.Type.BIG_INTEGER;
-import static org.sonar.db.version.ColumnDef.Type.STRING;
-
-public class ColumnDefTest {
-
-  @Rule
-  public ExpectedException thrown = ExpectedException.none();
-
-  @Test
-  public void create_column_def() throws Exception {
-    ColumnDef def = new ColumnDef()
-      .setName("date_in_ms")
-      .setType(STRING)
-      .setLimit(10)
-      .setNullable(true);
-
-    assertThat(def.getName()).isEqualTo("date_in_ms");
-    assertThat(def.getType()).isEqualTo(STRING);
-    assertThat(def.getLimit()).isEqualTo(10);
-    assertThat(def.isNullable()).isTrue();
-  }
-
-  @Test
-  public void convert_varchar_type_to_sql() throws Exception {
-    assertThat(new ColumnDef().setType(STRING).getSqlType(new H2())).isEqualTo("VARCHAR");
-    assertThat(new ColumnDef().setType(STRING).getSqlType(new Oracle())).isEqualTo("VARCHAR");
-    assertThat(new ColumnDef().setType(STRING).getSqlType(new MsSql())).isEqualTo("VARCHAR");
-    assertThat(new ColumnDef().setType(STRING).getSqlType(new MySql())).isEqualTo("VARCHAR");
-  }
-
-  @Test
-  public void convert_big_integer_type_to_sql() throws Exception {
-    assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new H2())).isEqualTo("BIGINT");
-    assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new Oracle())).isEqualTo("NUMBER (38)");
-    assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new MsSql())).isEqualTo("BIGINT");
-    assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new MySql())).isEqualTo("BIGINT");
-  }
-
-
-  @Test
-  public void fail_when_column_name_is_in_upper_case() {
-    thrown.expect(IllegalArgumentException.class);
-    thrown.expectMessage("Column name should only contains lowercase and _ characters");
-
-    new ColumnDef()
-      .setName("DATE_IN_MS")
-      .setType(BIG_INTEGER)
-      .setNullable(true);
-  }
-
-  @Test
-  public void fail_when_column_name_contains_invalid_character() {
-    thrown.expect(IllegalArgumentException.class);
-    thrown.expectMessage("Column name should only contains lowercase and _ characters");
-
-    new ColumnDef()
-      .setName("date-in/ms")
-      .setType(BIG_INTEGER)
-      .setNullable(true);
-  }
-}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/ColumnDefValidationTest.java b/sonar-db/src/test/java/org/sonar/db/version/ColumnDefValidationTest.java
new file mode 100644 (file)
index 0000000..6cbdb35
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+
+public class ColumnDefValidationTest {
+
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
+  @Test
+  public void accept_valid_table_name() throws Exception {
+    validateColumnName("date_in_ms");
+  }
+
+  @Test
+  public void fail_with_NPE_if_name_is_null() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Column name cannot be null");
+
+    validateColumnName(null);
+  }
+
+  @Test
+  public void fail_when_column_name_is_in_upper_case() {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Column name should only contains lowercase and _ characters, got 'DATE_IN_MS'");
+
+    validateColumnName("DATE_IN_MS");
+  }
+
+  @Test
+  public void fail_when_column_name_contains_invalid_character() {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Column name should only contains lowercase and _ characters, got 'date-in/ms'");
+
+    validateColumnName("date-in/ms");
+  }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/StringColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/StringColumnDefTest.java
new file mode 100644 (file)
index 0000000..82e50b5
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.version;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.db.dialect.PostgreSql;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class StringColumnDefTest {
+
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
+  @Test
+  public void build_string_column_def() throws Exception {
+    StringColumnDef def = new StringColumnDef.Builder()
+      .setColumnName("issues")
+      .setLimit(10)
+      .setIsNullable(true)
+      .build();
+
+    assertThat(def.getName()).isEqualTo("issues");
+    assertThat(def.getColumnSize()).isEqualTo(10);
+    assertThat(def.isNullable()).isTrue();
+  }
+
+  @Test
+  public void generate_sql_type() throws Exception {
+    StringColumnDef def = new StringColumnDef.Builder()
+      .setColumnName("issues")
+      .setLimit(10)
+      .setIsNullable(true)
+      .build();
+
+    assertThat(def.generateSqlType(new H2())).isEqualTo("VARCHAR (10)");
+    assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("VARCHAR (10)");
+    assertThat(def.generateSqlType(new MySql())).isEqualTo("VARCHAR (10)");
+    assertThat(def.generateSqlType(new MsSql())).isEqualTo("VARCHAR (10)");
+    assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR (10)");
+  }
+
+  @Test
+  public void fail_with_NPE_if_name_is_null() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Column name cannot be null");
+
+    new StringColumnDef.Builder()
+      .setColumnName(null);
+  }
+
+  @Test
+  public void fail_with_NPE_if_no_name() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Column name cannot be null");
+
+    new StringColumnDef.Builder()
+      .build();
+  }
+
+  @Test
+  public void fail_with_NPE_if_size_is_null() throws Exception {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("Limit cannot be null");
+
+    new StringColumnDef.Builder()
+      .setColumnName("issues")
+      .build();
+  }
+}