diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-15 11:01:02 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-16 14:35:27 +0100 |
commit | 714a50e499fb1f029590d44773454e453f7ed782 (patch) | |
tree | 66ffcadc3436e910b9fe2419bf082f1763dd93eb /sonar-db | |
parent | 695c9b54d7e7a97075180f17beba537114d404f1 (diff) | |
download | sonarqube-714a50e499fb1f029590d44773454e453f7ed782.tar.gz sonarqube-714a50e499fb1f029590d44773454e453f7ed782.zip |
SONAR-8445 move def and sql builder classes to sonar-db-migration
Diffstat (limited to 'sonar-db')
39 files changed, 0 insertions, 4508 deletions
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 deleted file mode 100644 index d9aae31fb29..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.Nullable; - -public abstract class AbstractColumnDef implements ColumnDef { - private final String columnName; - private final boolean isNullable; - @CheckForNull - private final Object defaultValue; - - public AbstractColumnDef(String columnName, boolean isNullable, @Nullable Object defaultValue) { - this.columnName = columnName; - this.isNullable = isNullable; - this.defaultValue = defaultValue; - } - - @Override - public String getName() { - return columnName; - } - - @Override - public boolean isNullable() { - return isNullable; - } - - @Override - public Object getDefaultValue() { - return defaultValue; - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java deleted file mode 100644 index 3806cca6c4a..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.List; -import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.MsSql; -import org.sonar.db.dialect.PostgreSql; - -import static com.google.common.collect.Lists.newArrayList; -import static java.lang.String.format; -import static org.sonar.db.version.Validations.validateTableName; - -/** - * Generate a SQL query to add multiple columns on a table - */ -public class AddColumnsBuilder { - - private final Dialect dialect; - private final String tableName; - private List<ColumnDef> columnDefs = newArrayList(); - - public AddColumnsBuilder(Dialect dialect, String tableName) { - this.tableName = validateTableName(tableName); - this.dialect = dialect; - } - - public AddColumnsBuilder addColumn(ColumnDef columnDef) { - columnDefs.add(columnDef); - return this; - } - - public String build() { - if (columnDefs.isEmpty()) { - throw new IllegalStateException("No column has been defined"); - } - - StringBuilder sql = new StringBuilder().append("ALTER TABLE ").append(tableName).append(" "); - switch (dialect.getId()) { - case PostgreSql.ID: - addColumns(sql, "ADD COLUMN "); - break; - case MsSql.ID: - sql.append("ADD "); - addColumns(sql, ""); - break; - default: - sql.append("ADD ("); - addColumns(sql, ""); - sql.append(")"); - } - return sql.toString(); - } - - private void addColumns(StringBuilder sql, String columnPrefix) { - for (int i = 0; i < columnDefs.size(); i++) { - sql.append(columnPrefix); - addColumn(sql, columnDefs.get(i)); - if (i < columnDefs.size() - 1) { - sql.append(", "); - } - } - } - - private void addColumn(StringBuilder sql, ColumnDef columnDef) { - sql.append(columnDef.getName()).append(" ").append(columnDef.generateSqlType(dialect)); - Object defaultValue = columnDef.getDefaultValue(); - if (defaultValue != null) { - sql.append(" DEFAULT "); - // TODO remove duplication with CreateTableBuilder - if (defaultValue instanceof String) { - sql.append(format("'%s'", defaultValue)); - } else if (defaultValue instanceof Boolean) { - sql.append((boolean) defaultValue ? dialect.getTrueSqlValue() : dialect.getFalseSqlValue()); - } else { - sql.append(defaultValue); - } - } - sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL"); - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java deleted file mode 100644 index 81e571d6b27..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.MySql; -import org.sonar.db.dialect.Oracle; -import org.sonar.db.dialect.PostgreSql; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.collect.Lists.newArrayList; - -/** - * Generate SQL queries to update multiple columns of a single table. - * - * Note that this operation will not be re-entrant on: - * <ul> - * <li>Oracle 11G (may raise {@code ORA-01442: column to be modified to NOT NULL is already NOT NULL} or - * {@code ORA-01451: column to be modified to NULL cannot be modified to NULL})</li> - * </ul> - */ -public class AlterColumnsBuilder { - - private static final String ALTER_TABLE = "ALTER TABLE "; - private static final String ALTER_COLUMN = "ALTER COLUMN "; - - private final Dialect dialect; - private final String tableName; - private final List<ColumnDef> columnDefs = newArrayList(); - - public AlterColumnsBuilder(Dialect dialect, String tableName) { - this.dialect = dialect; - this.tableName = tableName; - } - - public AlterColumnsBuilder updateColumn(ColumnDef columnDef) { - // limitation of Oracle, only attribute changes must be defined in ALTER. - checkArgument(columnDef.getDefaultValue()==null, "Default value is not supported on alter of column '%s'", columnDef.getName()); - columnDefs.add(columnDef); - return this; - } - - public List<String> build() { - if (columnDefs.isEmpty()) { - throw new IllegalStateException("No column has been defined"); - } - switch (dialect.getId()) { - case PostgreSql.ID: - return createPostgresQuery(); - case MySql.ID: - return createMySqlQuery(); - case Oracle.ID: - return createOracleQuery(); - default: - return createMsSqlAndH2Queries(); - } - } - - private List<String> createPostgresQuery() { - StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " "); - for (Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); columnDefIterator.hasNext();) { - ColumnDef columnDef = columnDefIterator.next(); - sql.append(ALTER_COLUMN); - addColumn(sql, columnDef, "TYPE ", false); - sql.append(", "); - sql.append(ALTER_COLUMN); - sql.append(columnDef.getName()); - sql.append(' ').append(columnDef.isNullable() ? "DROP" : "SET").append(" NOT NULL"); - if (columnDefIterator.hasNext()) { - sql.append(", "); - } - } - return Collections.singletonList(sql.toString()); - } - - private List<String> createMySqlQuery() { - StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " "); - addColumns(sql, "MODIFY COLUMN ", "", true); - return Collections.singletonList(sql.toString()); - } - - private List<String> createOracleQuery() { - List<String> sqls = new ArrayList<>(); - for (ColumnDef columnDef : columnDefs) { - StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " ").append("MODIFY ("); - addColumn(sql, columnDef, "", true); - sql.append(")"); - sqls.add(sql.toString()); - } - return sqls; - } - - private List<String> createMsSqlAndH2Queries() { - List<String> sqls = new ArrayList<>(); - for (ColumnDef columnDef : columnDefs) { - StringBuilder defaultQuery = new StringBuilder(ALTER_TABLE + tableName + " "); - defaultQuery.append(ALTER_COLUMN); - addColumn(defaultQuery, columnDef, "", true); - sqls.add(defaultQuery.toString()); - } - return sqls; - } - - private void addColumns(StringBuilder sql, String updateKeyword, String typePrefix, boolean addNotNullableProperty) { - for (Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); columnDefIterator.hasNext();) { - sql.append(updateKeyword); - addColumn(sql, columnDefIterator.next(), typePrefix, addNotNullableProperty); - if (columnDefIterator.hasNext()) { - sql.append(", "); - } - } - } - - private void addColumn(StringBuilder sql, ColumnDef columnDef, String typePrefix, boolean addNotNullableProperty) { - sql.append(columnDef.getName()) - .append(" ") - .append(typePrefix) - .append(columnDef.generateSqlType(dialect)); - if (addNotNullableProperty) { - sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL"); - } - - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java deleted file mode 100644 index 1ed9adc33b1..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.Oracle; - -import static org.sonar.db.version.Validations.validateColumnName; - -@Immutable -public class BigIntegerColumnDef extends AbstractColumnDef { - - private BigIntegerColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, null); - } - - public static Builder newBigIntegerColumnDefBuilder() { - 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 = true; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public BigIntegerColumnDef build() { - validateColumnName(columnName); - return new BigIntegerColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java deleted file mode 100644 index f7383a53514..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -@Immutable -public class BlobColumnDef extends AbstractColumnDef { - public BlobColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, null); - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - return "VARBINARY(MAX)"; - case MySql.ID: - return "LONGBLOB"; - case Oracle.ID: - case H2.ID: - return "BLOB"; - case PostgreSql.ID: - return "BYTEA"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } - } - - public static Builder newBlobColumnDefBuilder() { - return new Builder(); - } - - public static class Builder { - @CheckForNull - private String columnName; - private boolean isNullable = true; - - private Builder() { - // prevents instantiation outside - } - - public BlobColumnDef.Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public BlobColumnDef.Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public BlobColumnDef build() { - validateColumnName(columnName); - return new BlobColumnDef(this); - } - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java deleted file mode 100644 index 9bdbca763e1..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Nullable; -import javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -/** - * Used to define VARCHAR column - */ -@Immutable -public class BooleanColumnDef extends AbstractColumnDef { - - private BooleanColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, builder.defaultValue); - } - - public static Builder newBooleanColumnDefBuilder() { - return new Builder(); - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - case H2.ID: - return "BOOLEAN"; - case Oracle.ID: - return "NUMBER(1)"; - case MsSql.ID: - return "BIT"; - case MySql.ID: - return "TINYINT(1)"; - default: - throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); - } - } - - public static class Builder { - private String columnName; - private Boolean defaultValue = null; - private boolean isNullable = true; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public Builder setDefaultValue(@Nullable Boolean b) { - this.defaultValue = b; - return this; - } - - public BooleanColumnDef build() { - validateColumnName(columnName); - return new BooleanColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java deleted file mode 100644 index 7bf2c185a4a..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -/** - * Used to define CLOB columns - */ -@Immutable -public class ClobColumnDef extends AbstractColumnDef { - - private ClobColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, null); - } - - public static Builder newClobColumnDefBuilder() { - return new Builder(); - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - return "NVARCHAR (MAX)"; - case MySql.ID: - return "LONGTEXT"; - case Oracle.ID: - return "CLOB"; - case H2.ID: - return "CLOB(2147483647)"; - case PostgreSql.ID: - return "TEXT"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } - } - - public static class Builder { - private String columnName; - private boolean isNullable = true; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public ClobColumnDef build() { - validateColumnName(columnName); - return new ClobColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java deleted file mode 100644 index 7fc8e4bd013..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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; - -public interface ColumnDef { - - boolean isNullable(); - - String getName(); - - String generateSqlType(Dialect dialect); - - @CheckForNull - Object getDefaultValue(); -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/CreateIndexBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/CreateIndexBuilder.java deleted file mode 100644 index 9bebd11fb54..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/CreateIndexBuilder.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.MySql; - -import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Collections.singletonList; -import static java.util.Objects.requireNonNull; -import static org.sonar.db.version.Validations.validateIndexName; -import static org.sonar.db.version.Validations.validateTableName; - -public class CreateIndexBuilder { - - private static final int MAX_LENGTH_ON_MYSQL = 255; - - private final Dialect dialect; - private final List<ColumnDef> columns = new ArrayList<>(); - private String tableName; - private String indexName; - private boolean unique = false; - - public CreateIndexBuilder(Dialect dialect) { - this.dialect = dialect; - } - - /** - * Required name of table on which index is created - */ - public CreateIndexBuilder setTable(String s) { - this.tableName = s; - return this; - } - - /** - * Required name of index. Name must be unique among all the tables - * of the schema. - */ - public CreateIndexBuilder setName(String s) { - this.indexName = s; - return this; - } - - /** - * By default index is NOT UNIQUE (value {@code false}). - */ - public CreateIndexBuilder setUnique(boolean b) { - this.unique = b; - return this; - } - - /** - * Add a column to the scope of index. Order of calls to this - * method is important and is kept as-is when creating the index. - * The attributes used from {@link ColumnDef} are the name, the type - * and the length (in case of VARCHAR). Other attributes are ignored. - */ - public CreateIndexBuilder addColumn(ColumnDef column) { - columns.add(requireNonNull(column, "Column cannot be null")); - return this; - } - - public List<String> build() { - validateTableName(tableName); - validateIndexName(indexName); - checkArgument(!columns.isEmpty(), "at least one column must be specified"); - return singletonList(createSqlStatement()); - } - - private String createSqlStatement() { - StringBuilder sql = new StringBuilder("CREATE "); - if (unique) { - sql.append("UNIQUE "); - } - sql.append("INDEX "); - sql.append(indexName); - sql.append(" ON "); - sql.append(tableName); - sql.append(" ("); - sql.append(columns.stream().map(this::columnSql).collect(Collectors.joining(", "))); - sql.append(")"); - return sql.toString(); - } - - private String columnSql(ColumnDef column) { - String length = ""; - // Index of varchar column is limited to 767 bytes on mysql (<= 255 UTF-8 characters) - // See http://jira.sonarsource.com/browse/SONAR-4137 and - // http://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html - if (dialect.getId().equals(MySql.ID) && column instanceof VarcharColumnDef) { - VarcharColumnDef varcharColumn = (VarcharColumnDef) column; - if (varcharColumn.getColumnSize() > MAX_LENGTH_ON_MYSQL) { - length = "(" + MAX_LENGTH_ON_MYSQL + ")"; - } - } - return column.getName() + length; - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java deleted file mode 100644 index eb886caf49f..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java +++ /dev/null @@ -1,279 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.collect.HashMultimap; -import com.google.common.collect.Multimap; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.stream.Stream; -import javax.annotation.CheckForNull; -import org.sonar.core.util.stream.Collectors; -import org.sonar.db.dialect.Dialect; -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 com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkState; -import static java.lang.String.format; -import static java.util.Objects.requireNonNull; -import static java.util.stream.Stream.of; -import static org.sonar.db.version.Validations.validateConstraintName; -import static org.sonar.db.version.Validations.validateTableName; - -public class CreateTableBuilder { - - private final Dialect dialect; - private final String tableName; - private final List<ColumnDef> columnDefs = new ArrayList<>(); - private final List<ColumnDef> pkColumnDefs = new ArrayList<>(2); - private final Multimap<ColumnDef, ColumnFlag> flagsByColumn = HashMultimap.create(1, 1); - @CheckForNull - private String pkConstraintName; - - public CreateTableBuilder(Dialect dialect, String tableName) { - this.dialect = requireNonNull(dialect, "dialect can't be null"); - this.tableName = validateTableName(tableName); - } - - public List<String> build() { - checkState(!columnDefs.isEmpty() || !pkColumnDefs.isEmpty(), "at least one column must be specified"); - - return Stream.concat(of(createTableStatement()), createOracleAutoIncrementStatements()) - .collect(Collectors.toList()); - } - - public CreateTableBuilder addColumn(ColumnDef columnDef) { - columnDefs.add(requireNonNull(columnDef, "column def can't be null")); - return this; - } - - public CreateTableBuilder addPkColumn(ColumnDef columnDef, ColumnFlag... flags) { - pkColumnDefs.add(requireNonNull(columnDef, "column def can't be null")); - addFlags(columnDef, flags); - return this; - } - - private void addFlags(ColumnDef columnDef, ColumnFlag[] flags) { - Arrays.stream(flags) - .forEach(flag -> { - requireNonNull(flag, "flag can't be null"); - if (flag == ColumnFlag.AUTO_INCREMENT) { - validateColumnDefForAutoIncrement(columnDef); - } - flagsByColumn.put(columnDef, flag); - }); - } - - private void validateColumnDefForAutoIncrement(ColumnDef columnDef) { - checkArgument("id".equals(columnDef.getName()), - "Auto increment column name must be id"); - checkArgument(columnDef instanceof BigIntegerColumnDef - || columnDef instanceof IntegerColumnDef, - "Auto increment column must either be BigInteger or Integer"); - checkArgument(!columnDef.isNullable(), - "Auto increment column can't be nullable"); - checkState(pkColumnDefs.stream().filter(this::isAutoIncrement).count() == 0, - "There can't be more than one auto increment column"); - } - - public CreateTableBuilder withPkConstraintName(String pkConstraintName) { - this.pkConstraintName = validateConstraintName(pkConstraintName); - return this; - } - - private String createTableStatement() { - StringBuilder res = new StringBuilder("CREATE TABLE "); - res.append(tableName); - res.append(" ("); - appendPkColumns(res); - appendColumns(res, dialect, columnDefs); - appendPkConstraint(res); - res.append(')'); - appendCollationClause(res, dialect); - return res.toString(); - } - - private void appendPkColumns(StringBuilder res) { - appendColumns(res, dialect, pkColumnDefs); - if (!pkColumnDefs.isEmpty() && !columnDefs.isEmpty()) { - res.append(','); - } - } - - private void appendColumns(StringBuilder res, Dialect dialect, List<ColumnDef> columnDefs) { - if (columnDefs.isEmpty()) { - return; - } - Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); - while (columnDefIterator.hasNext()) { - ColumnDef columnDef = columnDefIterator.next(); - res.append(columnDef.getName()); - res.append(' '); - appendDataType(res, dialect, columnDef); - appendDefaultValue(res, columnDef); - appendNullConstraint(res, columnDef); - appendColumnFlags(res, dialect, columnDef); - if (columnDefIterator.hasNext()) { - res.append(','); - } - } - } - - private void appendDataType(StringBuilder res, Dialect dialect, ColumnDef columnDef) { - if (PostgreSql.ID.equals(dialect.getId()) && isAutoIncrement(columnDef)) { - if (columnDef instanceof BigIntegerColumnDef) { - res.append("BIGSERIAL"); - } else if (columnDef instanceof IntegerColumnDef) { - res.append("SERIAL"); - } else { - throw new IllegalStateException("Column with autoincrement is neither BigInteger nor Integer"); - } - } else { - res.append(columnDef.generateSqlType(dialect)); - } - } - - private boolean isAutoIncrement(ColumnDef columnDef) { - Collection<ColumnFlag> columnFlags = this.flagsByColumn.get(columnDef); - return columnFlags != null && columnFlags.contains(ColumnFlag.AUTO_INCREMENT); - } - - private static void appendNullConstraint(StringBuilder res, ColumnDef columnDef) { - if (columnDef.isNullable()) { - res.append(" NULL"); - } else { - res.append(" NOT NULL"); - } - } - - private void appendDefaultValue(StringBuilder sql, ColumnDef columnDef) { - Object defaultValue = columnDef.getDefaultValue(); - if (defaultValue != null) { - sql.append(" DEFAULT "); - - if (defaultValue instanceof String) { - sql.append(format("'%s'", defaultValue)); - } else if (defaultValue instanceof Boolean) { - sql.append((boolean) defaultValue ? dialect.getTrueSqlValue() : dialect.getFalseSqlValue()); - } else { - sql.append(defaultValue); - } - } - } - - private void appendColumnFlags(StringBuilder res, Dialect dialect, ColumnDef columnDef) { - Collection<ColumnFlag> columnFlags = this.flagsByColumn.get(columnDef); - if (columnFlags != null && columnFlags.contains(ColumnFlag.AUTO_INCREMENT)) { - switch (dialect.getId()) { - case Oracle.ID: - // no auto increment on Oracle, must use a sequence - break; - case PostgreSql.ID: - // no specific clause on PostgreSQL but a specific type - break; - case MsSql.ID: - res.append(" IDENTITY (1,1)"); - break; - case MySql.ID: - res.append(" AUTO_INCREMENT"); - break; - case H2.ID: - res.append(" AUTO_INCREMENT (1,1)"); - break; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } - } - } - - private void appendPkConstraint(StringBuilder res) { - if (pkColumnDefs.isEmpty()) { - return; - } - res.append(", "); - res.append("CONSTRAINT "); - appendPkConstraintName(res); - res.append(" PRIMARY KEY "); - res.append('('); - appendColumnNames(res, pkColumnDefs); - res.append(')'); - } - - private void appendPkConstraintName(StringBuilder res) { - if (pkConstraintName == null) { - res.append("pk_").append(tableName); - } else { - res.append(pkConstraintName.toLowerCase(Locale.ENGLISH)); - } - } - - private static void appendColumnNames(StringBuilder res, List<ColumnDef> columnDefs) { - Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); - while (columnDefIterator.hasNext()) { - res.append(columnDefIterator.next().getName()); - if (columnDefIterator.hasNext()) { - res.append(','); - } - } - } - - private static void appendCollationClause(StringBuilder res, Dialect dialect) { - if (MySql.ID.equals(dialect.getId())) { - res.append(" ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin"); - } - } - - private Stream<String> createOracleAutoIncrementStatements() { - if (!Oracle.ID.equals(dialect.getId())) { - return Stream.empty(); - } - return pkColumnDefs.stream() - .filter(this::isAutoIncrement) - .flatMap(columnDef -> of(createSequenceFor(tableName), createOracleTriggerForTable(tableName))); - } - - private static String createSequenceFor(String tableName) { - return "CREATE SEQUENCE " + tableName + "_seq START WITH 1 INCREMENT BY 1"; - } - - static String createOracleTriggerForTable(String tableName) { - return "CREATE OR REPLACE TRIGGER " + tableName + "_idt" + - " BEFORE INSERT ON " + tableName + - " FOR EACH ROW" + - " BEGIN" + - " IF :new.id IS null THEN" + - " SELECT " + tableName + "_seq.nextval INTO :new.id FROM dual;" + - " END IF;" + - " END;"; - } - - public enum ColumnFlag { - AUTO_INCREMENT - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java deleted file mode 100644 index dacfc351816..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -@Immutable -public class DecimalColumnDef extends AbstractColumnDef { - - public static final int DEFAULT_PRECISION = 38; - public static final int DEFAULT_SCALE = 20; - - private final int precision; - private final int scale; - - private DecimalColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, null); - this.precision = builder.precision; - this.scale = builder.scale; - } - - public static Builder newDecimalColumnDefBuilder() { - return new Builder(); - } - - public int getPrecision() { - return precision; - } - - public int getScale() { - return scale; - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - case Oracle.ID: - return String.format("NUMERIC (%s,%s)", precision, scale); - case MySql.ID: - case MsSql.ID: - return String.format("DECIMAL (%s,%s)", precision, scale); - case H2.ID: - return "DOUBLE"; - default: - throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); - } - } - - public static class Builder { - @CheckForNull - private String columnName; - private int precision = DEFAULT_PRECISION; - private int scale = DEFAULT_SCALE; - private boolean isNullable = true; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public Builder setPrecision(int precision) { - this.precision = precision; - return this; - } - - public Builder setScale(int scale) { - this.scale = scale; - return this; - } - - public DecimalColumnDef build() { - validateColumnName(columnName); - return new DecimalColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/DropColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/DropColumnsBuilder.java deleted file mode 100644 index cfd46a7af2e..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/DropColumnsBuilder.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.MsSql; -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; - private final String tableName; - private final String[] columns; - - public DropColumnsBuilder(Dialect dialect, String tableName, String... columns) { - this.tableName = tableName; - this.dialect = dialect; - this.columns = columns; - } - - public String build() { - StringBuilder sql = new StringBuilder().append("ALTER TABLE ").append(tableName).append(" "); - switch (dialect.getId()) { - case PostgreSql.ID: - case MySql.ID: - dropColumns(sql, "DROP COLUMN "); - break; - case MsSql.ID: - sql.append("DROP COLUMN "); - dropColumns(sql, ""); - break; - case Oracle.ID: - sql.append("DROP ("); - dropColumns(sql, ""); - sql.append(")"); - break; - default: - throw new IllegalStateException(String.format("Unsupported database '%s'", dialect.getId())); - } - return sql.toString(); - } - - private void dropColumns(StringBuilder sql, String columnPrefix) { - for (int i = 0; i < columns.length; i++) { - sql.append(columnPrefix); - sql.append(columns[i]); - if (i < columns.length - 1) { - sql.append(", "); - } - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java deleted file mode 100644 index 091a5085532..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.List; -import org.sonar.db.dialect.Dialect; -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 java.util.Collections.singletonList; -import static org.sonar.db.version.Validations.validateIndexName; -import static org.sonar.db.version.Validations.validateTableName; - -public class DropIndexBuilder { - - private final Dialect dialect; - private String tableName; - private String indexName; - - public DropIndexBuilder(Dialect dialect) { - this.dialect = dialect; - } - - public DropIndexBuilder setTable(String s) { - this.tableName = s; - return this; - } - - public DropIndexBuilder setName(String s) { - this.indexName = s; - return this; - } - - public List<String> build() { - validateTableName(tableName); - validateIndexName(indexName); - return singletonList(createSqlStatement()); - } - - private String createSqlStatement() { - switch (dialect.getId()) { - case MsSql.ID: - case MySql.ID: - return "DROP INDEX " + indexName + " ON " + tableName; - case Oracle.ID: - return "DROP INDEX " + indexName; - case H2.ID: - case PostgreSql.ID: - return "DROP INDEX IF EXISTS " + indexName; - default: - throw new IllegalStateException("Unsupported dialect for drop of index: " + dialect); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/DropTableBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/DropTableBuilder.java deleted file mode 100644 index 029d3a1b474..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/DropTableBuilder.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.List; -import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.Oracle; - -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; -import static java.util.Objects.requireNonNull; -import static org.sonar.db.version.Validations.validateTableName; - -public class DropTableBuilder { - - private final Dialect dialect; - private final String tableName; - - public DropTableBuilder(Dialect dialect, String tableName) { - this.dialect = requireNonNull(dialect, "dialect can't be null"); - this.tableName = validateTableName(tableName); - } - - public List<String> build() { - if (Oracle.ID.equals(dialect.getId())) { - return dropOracleSequenceAndTriggerAndTableStatements(); - } - return singletonList(dropTableStatement()); - } - - private String dropTableStatement() { - return "DROP TABLE " + tableName; - } - - private List<String> dropOracleSequenceAndTriggerAndTableStatements() { - return asList(dropSequenceFor(tableName), dropTriggerFor(tableName), dropTableStatement()); - } - - private static String dropSequenceFor(String tableName) { - return "BEGIN\n" + - " EXECUTE IMMEDIATE 'DROP SEQUENCE " + tableName + "_seq';\n" + - "EXCEPTION\n" + - " WHEN OTHERS THEN\n" + - " IF SQLCODE != -2289 THEN\n" + - " RAISE;\n" + - " END IF;\n" + - "END;"; - } - - private static String dropTriggerFor(String tableName) { - return "BEGIN\n" + - " EXECUTE IMMEDIATE 'DROP TRIGGER " + tableName + "_idt';\n" + - "EXCEPTION\n" + - " WHEN OTHERS THEN\n" + - " IF SQLCODE != -4080 THEN\n" + - " RAISE;\n" + - " END IF;\n" + - "END;"; - - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java deleted file mode 100644 index af26630785c..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -@Immutable -public class IntegerColumnDef extends AbstractColumnDef { - - private IntegerColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, builder.defaultValue); - } - - public static Builder newIntegerColumnDefBuilder() { - return new Builder(); - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - case MySql.ID: - case H2.ID: - return "INTEGER"; - case MsSql.ID: - return "INT"; - case Oracle.ID: - return "NUMBER(38,0)"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } - } - - public static class Builder { - @CheckForNull - private String columnName; - private boolean isNullable = true; - @CheckForNull - private Integer defaultValue = null; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public Builder setDefaultValue(@Nullable Integer i) { - this.defaultValue = i; - return this; - } - - public IntegerColumnDef build() { - validateColumnName(columnName); - return new IntegerColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/RenameTableBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/RenameTableBuilder.java deleted file mode 100644 index 483f59f0af8..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/RenameTableBuilder.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.Arrays; -import java.util.List; -import org.sonar.db.dialect.Dialect; -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 com.google.common.base.Preconditions.checkArgument; -import static java.util.Collections.singletonList; -import static org.sonar.db.version.Validations.validateTableName; - -/** - * Limitation: only tables with auto-generated ID column can - * be renamed as the Oracle implementation assumes that - * the sequence and trigger related to ID column exist. - */ -public class RenameTableBuilder { - - private final Dialect dialect; - private String name; - private String newName; - - public RenameTableBuilder(Dialect dialect) { - this.dialect = dialect; - } - - public RenameTableBuilder setName(String s) { - this.name = s; - return this; - } - - public RenameTableBuilder setNewName(String s) { - this.newName = s; - return this; - } - - public List<String> build() { - validateTableName(name); - validateTableName(newName); - checkArgument(!name.equals(newName), "Names must be different"); - return createSqlStatement(); - } - - private List<String> createSqlStatement() { - switch (dialect.getId()) { - case H2.ID: - case MySql.ID: - case PostgreSql.ID: - return singletonList("ALTER TABLE " + name + " RENAME TO " + newName); - case MsSql.ID: - return singletonList("EXEC sp_rename '" + name + "', '" + newName + "'"); - case Oracle.ID: - return Arrays.asList( - "DROP TRIGGER " + name + "_idt", - "RENAME " + name + " TO " + newName, - "RENAME " + name + "_seq TO " + newName + "_seq", - CreateTableBuilder.createOracleTriggerForTable(newName)); - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java deleted file mode 100644 index 1084454f840..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -/** - * Used to define TIMESTAMP columns. - * - * @deprecated implemented for compatibility with old tables, but {@link BigIntegerColumnDef} - * must be used for storing datetimes as bigints (no problems regarding timezone - * nor MySQL precision). - */ -@Immutable -@Deprecated -public class TimestampColumnDef extends AbstractColumnDef { - - private TimestampColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, null); - } - - public static Builder newTimestampColumnDefBuilder() { - return new Builder(); - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - case MySql.ID: - return "DATETIME"; - case Oracle.ID: - return "TIMESTAMP (6)"; - case H2.ID: - case PostgreSql.ID: - return "TIMESTAMP"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } - } - - public static class Builder { - private String columnName; - private boolean isNullable = true; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean b) { - this.isNullable = b; - return this; - } - - public TimestampColumnDef build() { - validateColumnName(columnName); - return new TimestampColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java deleted file mode 100644 index 7696879aee8..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -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.sonar.db.version.Validations.validateColumnName; - -/** - * Integer that supports at least range [0..128]. Full range depends on database vendor. - */ -@Immutable -public class TinyIntColumnDef extends AbstractColumnDef { - - private TinyIntColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, null); - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - return "SMALLINT"; - case Oracle.ID: - return "NUMBER(3)"; - case MySql.ID: - // do not use TINYINT(1) as it's considered as booleans by connector/J. - return "TINYINT(2)"; - case MsSql.ID: - case H2.ID: - return "TINYINT"; - default: - throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); - } - } - - public static class Builder { - @CheckForNull - private String columnName; - private boolean isNullable = true; - - public Builder setColumnName(String columnName) { - this.columnName = validateColumnName(columnName); - return this; - } - - public Builder setIsNullable(boolean isNullable) { - this.isNullable = isNullable; - return this; - } - - public TinyIntColumnDef build() { - validateColumnName(columnName); - return new TinyIntColumnDef(this); - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/Validations.java b/sonar-db/src/main/java/org/sonar/db/version/Validations.java deleted file mode 100644 index a21f5413741..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/Validations.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.anyOf; -import static com.google.common.base.CharMatcher.inRange; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static java.util.Objects.requireNonNull; - -public class Validations { - - private static final int TABLE_NAME_MAX_SIZE = 25; - private static final int CONSTRAINT_NAME_MAX_SIZE = 30; - private static final int INDEX_NAME_MAX_SIZE = 30; - - private static final CharMatcher DIGIT_CHAR_MATCHER = inRange('0', '9'); - private static final CharMatcher LOWER_CASE_ASCII_LETTERS_CHAR_MATCHER = inRange('a', 'z'); - private static final CharMatcher UNDERSCORE_CHAR_MATCHER = anyOf("_"); - - private Validations() { - // Only static stuff here - } - - /** - * Ensure {@code columnName} is a valid name for a column. - * @throws NullPointerException if {@code columnName} is {@code null} - * @throws IllegalArgumentException if {@code columnName} is not valid - * @return the same {@code columnName} - * @see #checkDbIdentifier(String, String, int) - */ - static String validateColumnName(@Nullable String columnName) { - String name = requireNonNull(columnName, "Column name cannot be null"); - checkDbIdentifierCharacters(columnName, "Column name"); - return name; - } - - /** - * Ensure {@code tableName} is a valid name for a table. - * @throws NullPointerException if {@code tableName} is {@code null} - * @throws IllegalArgumentException if {@code tableName} is not valid - * @return the same {@code tableName} - * @see #checkDbIdentifier(String, String, int) - */ - static String validateTableName(@Nullable String tableName) { - requireNonNull(tableName, "Table name cannot be null"); - checkDbIdentifier(tableName, "Table name", TABLE_NAME_MAX_SIZE); - return tableName; - } - - /** - * Ensure {@code constraintName} is a valid name for a constraint. - * @throws NullPointerException if {@code constraintName} is {@code null} - * @throws IllegalArgumentException if {@code constraintName} is not valid - * @return the same {@code constraintName} - * @see #checkDbIdentifier(String, String, int) - */ - static String validateConstraintName(@Nullable String constraintName) { - requireNonNull(constraintName, "Constraint name cannot be null"); - checkDbIdentifier(constraintName, "Constraint name", CONSTRAINT_NAME_MAX_SIZE); - return constraintName; - } - - /** - * Ensure {@code indexName} is a valid name for an index. - * @throws NullPointerException if {@code indexName} is {@code null} - * @throws IllegalArgumentException if {@code indexName} is not valid - * @return the same {@code indexName} - * @see #checkDbIdentifier(String, String, int) - */ - static String validateIndexName(@Nullable String indexName) { - requireNonNull(indexName, "Index name cannot be null"); - checkDbIdentifier(indexName, "Index name", INDEX_NAME_MAX_SIZE); - return indexName; - } - - /** - * Ensure {@code identifier} is a valid DB identifier. - * - * @throws NullPointerException if {@code identifier} is {@code null} - * @throws IllegalArgumentException if {@code identifier} is empty - * @throws IllegalArgumentException if {@code identifier} is longer than {@code maxSize} - * @throws IllegalArgumentException if {@code identifier} is not lowercase - * @throws IllegalArgumentException if {@code identifier} contains characters others than ASCII letters, ASCII numbers or {@code _} - * @throws IllegalArgumentException if {@code identifier} starts with {@code _} or a number - */ - static String checkDbIdentifier(@Nullable String identifier, String identifierDesc, int maxSize) { - String res = checkNotNull(identifier, "%s can't be null", identifierDesc); - checkArgument(!res.isEmpty(), "%s, can't be empty", identifierDesc); - checkArgument( - identifier.length() <= maxSize, - "%s length can't be more than %s", identifierDesc, maxSize); - checkDbIdentifierCharacters(identifier, identifierDesc); - return res; - } - - private static void checkDbIdentifierCharacters(String identifier, String identifierDesc) { - checkArgument( - LOWER_CASE_ASCII_LETTERS_CHAR_MATCHER.or(DIGIT_CHAR_MATCHER).or(anyOf("_")).matchesAllOf(identifier), - "%s must be lower case and contain only alphanumeric chars or '_', got '%s'", identifierDesc, identifier); - checkArgument( - DIGIT_CHAR_MATCHER.or(UNDERSCORE_CHAR_MATCHER).matchesNoneOf(identifier.subSequence(0, 1)), - "%s must not start by a number or '_', got '%s'", identifierDesc, identifier); - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java deleted file mode 100644 index f37bf610ed3..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; -import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.MsSql; -import org.sonar.db.dialect.Oracle; - -import static java.lang.String.format; -import static java.util.Objects.requireNonNull; -import static org.sonar.db.version.Validations.validateColumnName; - -/** - * Used to define VARCHAR column - */ -@Immutable -public class VarcharColumnDef extends AbstractColumnDef { - public static final int MAX_SIZE = 4_000; - /** - * @deprecated use {@link #UUID_SIZE} instead - */ - @Deprecated - public static final int UUID_VARCHAR_SIZE = 50; - public static final int UUID_SIZE = 40; - - private final int columnSize; - private final boolean ignoreOracleUnit; - - private VarcharColumnDef(Builder builder) { - super(builder.columnName, builder.isNullable, builder.defaultValue); - this.columnSize = builder.columnSize; - this.ignoreOracleUnit = builder.ignoreOracleUnit; - } - - public static Builder newVarcharColumnDefBuilder() { - return new Builder(); - } - - public int getColumnSize() { - return columnSize; - } - - @Override - public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - return format("NVARCHAR (%d)", columnSize); - case Oracle.ID: - return format("VARCHAR (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR"); - default: - return format("VARCHAR (%d)", columnSize); - } - } - - public static class Builder { - @CheckForNull - private Integer columnSize; - @CheckForNull - private String columnName; - @CheckForNull - private String defaultValue = null; - private boolean isNullable = true; - private boolean ignoreOracleUnit = false; - - 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 Builder setDefaultValue(@Nullable String s) { - this.defaultValue = s; - return this; - } - - /** - * In order to not depend on value of runtime variable NLS_LENGTH_SEMANTICS, unit of length - * is enforced to CHAR so that we're sure that type can't be BYTE. - * Unit is ignored for the columns created before SonarQube 6.3 (except for issues.message which - * has been fixed in migration 1151 of SonarQube 5.6. See SONAR-7493). - * - * @param b whether unit of length is hardcoded to CHAR. - */ - public Builder setIgnoreOracleUnit(boolean b) { - this.ignoreOracleUnit = b; - return this; - } - - public VarcharColumnDef build() { - validateColumnName(columnName); - requireNonNull(columnSize, "Limit cannot be null"); - return new VarcharColumnDef(this); - } - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java deleted file mode 100644 index 14163cb8548..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Dialect; -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; -import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder; -import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class AddColumnsBuilderTest { - - private static final String TABLE_NAME = "issues"; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void add_columns_on_h2() { - assertThat(createSampleBuilder(new H2()).build()) - .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default BOOLEAN DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)"); - } - - @Test - public void add_columns_on_mysql() { - assertThat(createSampleBuilder(new MySql()).build()) - .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default TINYINT(1) DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)"); - } - - @Test - public void add_columns_on_oracle() { - assertThat(createSampleBuilder(new Oracle()).build()) - .isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR (10 CHAR) NOT NULL, col_with_default NUMBER(1) DEFAULT 0 NOT NULL, varchar_col_with_default VARCHAR (3 CHAR) DEFAULT 'foo' NOT NULL)"); - } - - @Test - public void add_columns_on_postgresql() { - assertThat(createSampleBuilder(new PostgreSql()).build()) - .isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL, ADD COLUMN col_with_default BOOLEAN DEFAULT false NOT NULL, ADD COLUMN varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL"); - } - - @Test - public void add_columns_on_mssql() { - assertThat(createSampleBuilder(new MsSql()).build()) - .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name NVARCHAR (10) NOT NULL, col_with_default BIT DEFAULT 0 NOT NULL, varchar_col_with_default NVARCHAR (3) DEFAULT 'foo' NOT NULL"); - } - - @Test - public void fail_with_ISE_if_no_column() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("No column has been defined"); - - new AddColumnsBuilder(new H2(), TABLE_NAME).build(); - } - - private AddColumnsBuilder createSampleBuilder(Dialect dialect) { - return new AddColumnsBuilder(dialect, TABLE_NAME) - .addColumn(new BigIntegerColumnDef.Builder().setColumnName("date_in_ms").setIsNullable(true).build()) - .addColumn(new VarcharColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build()) - - // columns with default values - .addColumn(newBooleanColumnDefBuilder().setColumnName("col_with_default").setDefaultValue(false).setIsNullable(false).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_with_default").setLimit(3).setDefaultValue("foo").setIsNullable(false).build()); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java deleted file mode 100644 index de8a5897491..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Dialect; -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; -import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder; -import static org.sonar.db.version.DecimalColumnDef.newDecimalColumnDefBuilder; -import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class AlterColumnsBuilderTest { - - private static final String TABLE_NAME = "issues"; - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void update_columns_on_h2() { - assertThat(createSampleBuilder(new H2()).build()) - .containsOnly( - "ALTER TABLE issues ALTER COLUMN value DOUBLE NULL", - "ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NULL"); - } - - @Test - public void update_not_nullable_column_on_h2() { - assertThat(createNotNullableBuilder(new H2()).build()) - .containsOnly("ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NOT NULL"); - } - - @Test - public void update_columns_on_mssql() { - assertThat(createSampleBuilder(new MsSql()).build()) - .containsOnly( - "ALTER TABLE issues ALTER COLUMN value DECIMAL (30,20) NULL", - "ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NULL"); - } - - @Test - public void update_not_nullable_column_on_mssql() { - assertThat(createNotNullableBuilder(new MsSql()).build()) - .containsOnly("ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NOT NULL"); - } - - @Test - public void update_columns_on_postgres() { - assertThat(createSampleBuilder(new PostgreSql()).build()) - .containsOnly("ALTER TABLE issues " + - "ALTER COLUMN value TYPE NUMERIC (30,20), ALTER COLUMN value DROP NOT NULL, " + - "ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name DROP NOT NULL"); - } - - @Test - public void update_not_nullable_column_on_postgres() { - assertThat(createNotNullableBuilder(new PostgreSql()).build()) - .containsOnly("ALTER TABLE issues ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name SET NOT NULL"); - } - - @Test - public void update_columns_on_mysql() { - assertThat(createSampleBuilder(new MySql()).build()) - .containsOnly("ALTER TABLE issues MODIFY COLUMN value DECIMAL (30,20) NULL, MODIFY COLUMN name VARCHAR (10) NULL"); - } - - @Test - public void update_not_nullable_column_on_mysql() { - assertThat(createNotNullableBuilder(new MySql()).build()) - .containsOnly("ALTER TABLE issues MODIFY COLUMN name VARCHAR (10) NOT NULL"); - } - - @Test - public void update_columns_on_oracle() { - assertThat(createSampleBuilder(new Oracle()).build()) - .containsOnly( - "ALTER TABLE issues MODIFY (value NUMERIC (30,20) NULL)", - "ALTER TABLE issues MODIFY (name VARCHAR (10 CHAR) NULL)"); - } - - @Test - public void update_not_nullable_column_on_oracle() { - assertThat(createNotNullableBuilder(new Oracle()).build()) - .containsOnly("ALTER TABLE issues MODIFY (name VARCHAR (10 CHAR) NOT NULL)"); - } - - @Test - public void fail_with_ISE_if_no_column() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("No column has been defined"); - - new AlterColumnsBuilder(new H2(), TABLE_NAME).build(); - } - - /** - * As we want DEFAULT value to be removed from all tables, it is supported - * only on creation of tables and columns, not on alter. - */ - @Test - public void updateColumn_throws_IAE_if_default_value_is_defined() { - BooleanColumnDef column = newBooleanColumnDefBuilder() - .setColumnName("enabled") - .setIsNullable(false) - .setDefaultValue(false) - .build(); - AlterColumnsBuilder alterColumnsBuilder = new AlterColumnsBuilder(new H2(), TABLE_NAME); - - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Default value is not supported on alter of column 'enabled'"); - - alterColumnsBuilder.updateColumn(column); - } - - private AlterColumnsBuilder createSampleBuilder(Dialect dialect) { - return new AlterColumnsBuilder(dialect, TABLE_NAME) - .updateColumn( - newDecimalColumnDefBuilder() - .setColumnName("value") - .setPrecision(30) - .setScale(20) - .setIsNullable(true) - .build()) - .updateColumn( - newVarcharColumnDefBuilder() - .setColumnName("name") - .setLimit(10) - .setIsNullable(true) - .build()); - } - - private AlterColumnsBuilder createNotNullableBuilder(Dialect dialect) { - return new AlterColumnsBuilder(dialect, TABLE_NAME) - .updateColumn( - newVarcharColumnDefBuilder() - .setColumnName("name") - .setLimit(10) - .setIsNullable(false) - .build()); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/BigIntegerColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/BigIntegerColumnDefTest.java deleted file mode 100644 index 0c549a0c9ea..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/BigIntegerColumnDefTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 BigIntegerColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void build_string_column_def() throws Exception { - BigIntegerColumnDef def = new BigIntegerColumnDef.Builder() - .setColumnName("issues") - .setIsNullable(true) - .build(); - - assertThat(def.getName()).isEqualTo("issues"); - assertThat(def.isNullable()).isTrue(); - } - - @Test - public void build_string_column_def_with_default_values() throws Exception { - BigIntegerColumnDef def = new BigIntegerColumnDef.Builder() - .setColumnName("issues") - .build(); - - assertThat(def.getName()).isEqualTo("issues"); - assertThat(def.isNullable()).isTrue(); - } - - @Test - public void generate_sql_type() throws Exception { - BigIntegerColumnDef def = new BigIntegerColumnDef.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 BigIntegerColumnDef.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 BigIntegerColumnDef.Builder() - .build(); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/BlobColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/BlobColumnDefTest.java deleted file mode 100644 index 9d06d1109b5..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/BlobColumnDefTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Dialect; -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; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.db.version.BlobColumnDef.newBlobColumnDefBuilder; - -public class BlobColumnDefTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private BlobColumnDef underTest = newBlobColumnDefBuilder().setColumnName("a").build(); - - @Test - public void builder_setColumnName_throws_IAE_if_name_is_not_lowercase() { - BlobColumnDef.Builder builder = newBlobColumnDefBuilder(); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Column name must be lower case and contain only alphanumeric chars or '_', got 'T'"); - builder.setColumnName("T"); - } - - @Test - public void builder_build_throws_NPE_if_no_name_was_set() { - BlobColumnDef.Builder builder = newBlobColumnDefBuilder(); - - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Column name cannot be null"); - - builder.build(); - } - - @Test - public void blobColumDef_is_nullable_by_default() { - assertThat(newBlobColumnDefBuilder().setColumnName("a").build().isNullable()).isTrue(); - } - - @Test - public void builder_setNullable_sets_nullable_field_of_BlobColumnDef() { - assertThat(newBlobColumnDefBuilder().setColumnName("a").setIsNullable(true).build().isNullable()).isTrue(); - assertThat(newBlobColumnDefBuilder().setColumnName("a").setIsNullable(false).build().isNullable()).isFalse(); - } - - @Test - public void builder_setColumnName_sets_name_field_of_BlobColumnDef() { - assertThat(newBlobColumnDefBuilder().setColumnName("a").build().getName()).isEqualTo("a"); - } - - @Test - public void generateSqlType_for_MsSql() { - assertThat(underTest.generateSqlType(new MsSql())).isEqualTo("VARBINARY(MAX)"); - } - - @Test - public void generateSqlType_for_MySql() { - assertThat(underTest.generateSqlType(new MySql())).isEqualTo("LONGBLOB"); - } - - @Test - public void generateSqlType_for_Oracle() { - assertThat(underTest.generateSqlType(new Oracle())).isEqualTo("BLOB"); - } - - @Test - public void generateSqlType_for_H2() { - assertThat(underTest.generateSqlType(new H2())).isEqualTo("BLOB"); - } - - @Test - public void generateSqlType_for_PostgreSql() { - assertThat(underTest.generateSqlType(new PostgreSql())).isEqualTo("BYTEA"); - } - - @Test - public void generateSqlType_thows_IAE_for_unknown_dialect() { - Dialect dialect = mock(Dialect.class); - when(dialect.getId()).thenReturn("AAA"); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Unsupported dialect id AAA"); - - underTest.generateSqlType(dialect); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java deleted file mode 100644 index cb0486a39e8..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/BooleanColumnDefTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 BooleanColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void build_column_def() throws Exception { - BooleanColumnDef def = new BooleanColumnDef.Builder() - .setColumnName("enabled") - .setIsNullable(false) - .setDefaultValue(true) - .build(); - - assertThat(def.getName()).isEqualTo("enabled"); - assertThat(def.isNullable()).isFalse(); - assertThat(def.getDefaultValue()).isEqualTo(true); - } - - @Test - public void build_column_def_with_only_required_attributes() throws Exception { - BooleanColumnDef def = new BooleanColumnDef.Builder() - .setColumnName("enabled") - .build(); - - assertThat(def.getName()).isEqualTo("enabled"); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void generate_sql_type() throws Exception { - BooleanColumnDef def = new BooleanColumnDef.Builder() - .setColumnName("enabled") - .setIsNullable(true) - .build(); - - assertThat(def.generateSqlType(new H2())).isEqualTo("BOOLEAN"); - assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("BOOLEAN"); - assertThat(def.generateSqlType(new MsSql())).isEqualTo("BIT"); - assertThat(def.generateSqlType(new MySql())).isEqualTo("TINYINT(1)"); - assertThat(def.generateSqlType(new Oracle())).isEqualTo("NUMBER(1)"); - } - - @Test - public void fail_with_NPE_if_name_is_null() throws Exception { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Column name cannot be null"); - - new BooleanColumnDef.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 BooleanColumnDef.Builder().build(); - } - - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java deleted file mode 100644 index 0479e278698..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/ClobColumnDefTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 ClobColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - private final ClobColumnDef underTest = new ClobColumnDef.Builder() - .setColumnName("issues") - .setIsNullable(true) - .build(); - - @Test - public void build_string_column_def() throws Exception { - assertThat(underTest.getName()).isEqualTo("issues"); - assertThat(underTest.isNullable()).isTrue(); - assertThat(underTest.getDefaultValue()).isNull(); - } - - @Test - public void build_string_column_def_with_only_required_attributes() throws Exception { - ClobColumnDef def = new ClobColumnDef.Builder() - .setColumnName("issues") - .build(); - - assertThat(def.getName()).isEqualTo("issues"); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void generate_sql_type_on_mssql() throws Exception { - assertThat(underTest.generateSqlType(new MsSql())).isEqualTo("NVARCHAR (MAX)"); - } - - @Test - public void generate_sql_type_on_h2() throws Exception { - assertThat(underTest.generateSqlType(new H2())).isEqualTo("CLOB(2147483647)"); - } - - @Test - public void generate_sql_type_on_mysql() throws Exception { - assertThat(underTest.generateSqlType(new MySql())).isEqualTo("LONGTEXT"); - } - - @Test - public void generate_sql_type_on_oracle() throws Exception { - assertThat(underTest.generateSqlType(new Oracle())).isEqualTo("CLOB"); - } - - @Test - public void generate_sql_type_on_postgre() throws Exception { - assertThat(underTest.generateSqlType(new PostgreSql())).isEqualTo("TEXT"); - } - - @Test - public void fail_with_NPE_if_name_is_null() throws Exception { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Column name cannot be null"); - - new ClobColumnDef.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 ClobColumnDef.Builder() - .build(); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/CreateIndexBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/CreateIndexBuilderTest.java deleted file mode 100644 index 4f3850f7119..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/CreateIndexBuilderTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.Arrays; -import java.util.List; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.db.dialect.Dialect; -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; -import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class CreateIndexBuilderTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void create_index_on_single_column() { - verifySql(new CreateIndexBuilder(new H2()) - .setTable("issues") - .setName("issues_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()), - "CREATE INDEX issues_key ON issues (kee)"); - } - - @Test - public void create_unique_index_on_single_column() { - verifySql(new CreateIndexBuilder(new H2()) - .setTable("issues") - .setName("issues_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()) - .setUnique(true), - "CREATE UNIQUE INDEX issues_key ON issues (kee)"); - } - - @Test - public void create_index_on_multiple_columns() { - verifySql(new CreateIndexBuilder(new H2()) - .setTable("rules") - .setName("rules_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("repository").setLimit(10).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_key").setLimit(50).build()), - "CREATE INDEX rules_key ON rules (repository, rule_key)"); - } - - @Test - public void create_unique_index_on_multiple_columns() { - verifySql(new CreateIndexBuilder(new H2()) - .setTable("rules") - .setName("rules_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("repository").setLimit(10).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_key").setLimit(50).build()) - .setUnique(true), - "CREATE UNIQUE INDEX rules_key ON rules (repository, rule_key)"); - } - - @Test - public void index_length_is_not_specified_on_big_varchar_columns_if_not_mysql() { - Arrays.<Dialect>asList(new H2(), new MsSql(), new PostgreSql(), new Oracle()) - .forEach(dialect -> verifySql(new CreateIndexBuilder(dialect) - .setTable("issues") - .setName("issues_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(4000).build()), - "CREATE INDEX issues_key ON issues (kee)")); - } - - @Test - public void index_length_is_limited_to_255_on_big_varchar_columns_if_mysql() { - verifySql(new CreateIndexBuilder(new MySql()) - .setTable("issues") - .setName("issues_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(4000).build()), - "CREATE INDEX issues_key ON issues (kee(255))"); - } - - @Test - public void throw_NPE_if_table_is_missing() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Table name cannot be null"); - - new CreateIndexBuilder(new H2()) - .setName("issues_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()) - .build(); - } - - @Test - public void throw_NPE_if_index_name_is_missing() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Index name cannot be null"); - - new CreateIndexBuilder(new H2()) - .setTable("issues") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()) - .build(); - } - - @Test - public void throw_IAE_if_columns_are_missing() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("at least one column must be specified"); - - new CreateIndexBuilder(new H2()) - .setTable("issues") - .setName("issues_key") - .build(); - } - - @Test - public void throw_IAE_if_table_name_is_not_valid() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'"); - - new CreateIndexBuilder(new H2()) - .setTable("(not valid)") - .setName("issues_key") - .addColumn(newVarcharColumnDefBuilder().setColumnName("kee").setLimit(10).build()) - .build(); - } - - @Test - public void throw_NPE_when_adding_null_column() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Column cannot be null"); - - new CreateIndexBuilder(new H2()) - .setTable("issues") - .setName("issues_key") - .addColumn(null) - .build(); - } - - private static void verifySql(CreateIndexBuilder builder, String expectedSql) { - List<String> actual = builder.build(); - assertThat(actual).containsExactly(expectedSql); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java deleted file mode 100644 index 797f26afecc..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.Map; -import org.junit.ClassRule; -import org.junit.Test; -import org.sonar.api.utils.System2; -import org.sonar.db.DbTester; -import org.sonar.db.dialect.Dialect; - -import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -import static org.sonar.db.version.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; -import static org.sonar.db.version.BlobColumnDef.newBlobColumnDefBuilder; -import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder; -import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder; -import static org.sonar.db.version.CreateTableBuilder.ColumnFlag.AUTO_INCREMENT; -import static org.sonar.db.version.DecimalColumnDef.newDecimalColumnDefBuilder; -import static org.sonar.db.version.IntegerColumnDef.newIntegerColumnDefBuilder; -import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class CreateTableBuilderDbTesterTest { - @ClassRule - public static final DbTester dbTester = DbTester.create(System2.INSTANCE); - - private Dialect dialect = dbTester.getDbClient().getDatabase().getDialect(); - private static int tableNameGenerator = 0; - - @Test - public void create_no_primary_key_table() { - newCreateTableBuilder() - .addColumn(newBooleanColumnDefBuilder().setColumnName("bool_col_1").build()) - .addColumn(newBooleanColumnDefBuilder().setColumnName("bool_col_2").setIsNullable(false).build()) - .addColumn(newIntegerColumnDefBuilder().setColumnName("i_col_1").build()) - .addColumn(newIntegerColumnDefBuilder().setColumnName("i_col_2").setIsNullable(false).build()) - .addColumn(newBigIntegerColumnDefBuilder().setColumnName("bi_col_1").build()) - .addColumn(newBigIntegerColumnDefBuilder().setColumnName("bi_col_2").setIsNullable(false).build()) - .addColumn(newClobColumnDefBuilder().setColumnName("clob_col_1").build()) - .addColumn(newClobColumnDefBuilder().setColumnName("clob_col_2").setIsNullable(false).build()) - .addColumn(newDecimalColumnDefBuilder().setColumnName("dec_col_1").build()) - .addColumn(newDecimalColumnDefBuilder().setColumnName("dec_col_2").setIsNullable(false).build()) - .addColumn(new TinyIntColumnDef.Builder().setColumnName("tiny_col_1").build()) - .addColumn(new TinyIntColumnDef.Builder().setColumnName("tiny_col_2").setIsNullable(false).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_1").setLimit(40).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_2").setLimit(40).setIsNullable(false).build()) - .addColumn(newBlobColumnDefBuilder().setColumnName("blob_col_1").build()) - .addColumn(newBlobColumnDefBuilder().setColumnName("blob_col_2").setIsNullable(false).build()) - .build() - .forEach(dbTester::executeDdl); - } - - @Test - public void create_single_column_primary_key_table() { - newCreateTableBuilder() - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_1").setIsNullable(false).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_2").setLimit(40).setIsNullable(false).build()) - .build() - .forEach(dbTester::executeDdl); - } - - @Test - public void create_multi_column_primary_key_table() { - newCreateTableBuilder() - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_1").setIsNullable(false).build()) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_2").setIsNullable(false).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_2").setLimit(40).setIsNullable(false).build()) - .build() - .forEach(dbTester::executeDdl); - } - - @Test - public void create_autoincrement_notnullable_integer_primary_key_table() { - String tableName = createTableName(); - new CreateTableBuilder(dialect, tableName) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .addColumn(valColumnDef()) - .build() - .forEach(dbTester::executeDdl); - - verifyAutoIncrementIsWorking(tableName); - } - - @Test - public void create_autoincrement_notnullable_biginteger_primary_key_table() { - String tableName = createTableName(); - new CreateTableBuilder(dialect, tableName) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .addColumn(valColumnDef()) - .build() - .forEach(dbTester::executeDdl); - - verifyAutoIncrementIsWorking(tableName); - } - - private static VarcharColumnDef valColumnDef() { - return newVarcharColumnDefBuilder().setColumnName("val").setLimit(10).setIsNullable(false).build(); - } - - private void verifyAutoIncrementIsWorking(String tableName) { - dbTester.executeInsert(tableName, "val", "toto"); - dbTester.commit(); - - Map<String, Object> row = dbTester.selectFirst("select id as \"id\", val as \"val\" from " + tableName); - assertThat(row.get("id")).isNotNull(); - assertThat(row.get("val")).isEqualTo("toto"); - } - - private CreateTableBuilder newCreateTableBuilder() { - return new CreateTableBuilder(dialect, createTableName()); - } - - private static String createTableName() { - return "table_" + tableNameGenerator++; - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java deleted file mode 100644 index f005d1dae73..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java +++ /dev/null @@ -1,580 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.tngtech.java.junit.dataprovider.DataProvider; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; -import com.tngtech.java.junit.dataprovider.UseDataProvider; -import java.util.Arrays; -import java.util.List; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.sonar.db.dialect.Dialect; -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; -import static org.assertj.core.api.Assertions.fail; -import static org.mockito.Mockito.mock; -import static org.sonar.db.version.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; -import static org.sonar.db.version.BlobColumnDef.newBlobColumnDefBuilder; -import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder; -import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder; -import static org.sonar.db.version.CreateTableBuilder.ColumnFlag.AUTO_INCREMENT; -import static org.sonar.db.version.DecimalColumnDef.newDecimalColumnDefBuilder; -import static org.sonar.db.version.IntegerColumnDef.newIntegerColumnDefBuilder; -import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; - -@RunWith(DataProviderRunner.class) -public class CreateTableBuilderTest { - private static final H2 H2 = new H2(); - private static final Oracle ORACLE = new Oracle(); - private static final PostgreSql POSTGRESQL = new PostgreSql(); - private static final MsSql MS_SQL = new MsSql(); - private static final MySql MY_SQL = new MySql(); - private static final Dialect[] ALL_DIALECTS = {H2, MY_SQL, MS_SQL, POSTGRESQL, ORACLE}; - private static final String TABLE_NAME = "table_42"; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private CreateTableBuilder underTest = new CreateTableBuilder(mock(Dialect.class), TABLE_NAME); - - @Test - public void constructor_fails_with_NPE_if_dialect_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("dialect can't be null"); - - new CreateTableBuilder(null, TABLE_NAME); - } - - @Test - public void constructor_fails_with_NPE_if_tablename_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Table name cannot be null"); - - new CreateTableBuilder(mock(Dialect.class), null); - } - - @Test - public void constructor_throws_IAE_if_table_name_is_not_lowercase() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got 'Tooo"); - - new CreateTableBuilder(mock(Dialect.class), "Tooo"); - } - - @Test - public void constructor_throws_IAE_if_table_name_is_26_chars_long() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name length can't be more than 25"); - - new CreateTableBuilder(mock(Dialect.class), "abcdefghijklmnopqrstuvwxyz"); - } - - @Test - public void constructor_does_not_fail_if_table_name_is_25_chars_long() { - new CreateTableBuilder(mock(Dialect.class), "abcdefghijklmnopqrstuvwxy"); - } - - @Test - public void constructor_does_not_fail_if_table_name_contains_ascii_letters() { - new CreateTableBuilder(mock(Dialect.class), "abcdefghijklmnopqrstuvwxy"); - new CreateTableBuilder(mock(Dialect.class), "z"); - } - - @Test - public void constructor_throws_IAE_if_table_name_starts_with_underscore() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must not start by a number or '_', got '_a'"); - - new CreateTableBuilder(mock(Dialect.class), "_a"); - } - - @Test - @UseDataProvider("digitCharsDataProvider") - public void constructor_throws_IAE_if_table_name_starts_with_number(char number) { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must not start by a number or '_', got '" + number + "a'"); - - new CreateTableBuilder(mock(Dialect.class), number + "a"); - } - - @DataProvider - public static Object[][] digitCharsDataProvider() { - return new Object[][] { - {'0'}, - {'1'}, - {'2'}, - {'3'}, - {'4'}, - {'5'}, - {'6'}, - {'7'}, - {'8'}, - {'9'}, - }; - } - - @Test - public void constructor_does_not_fail_if_table_name_contains_underscore_or_numbers() { - new CreateTableBuilder(mock(Dialect.class), "a1234567890"); - new CreateTableBuilder(mock(Dialect.class), "a_"); - } - - @Test - public void build_throws_ISE_if_no_column_has_been_set() { - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("at least one column must be specified"); - - underTest.build(); - } - - @Test - public void addColumn_throws_NPE_if_ColumnDef_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("column def can't be null"); - - underTest.addColumn(null); - } - - @Test - public void addPkColumn_throws_NPE_if_ColumnDef_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("column def can't be null"); - - underTest.addPkColumn(null); - } - - @Test - public void addPkColumn_throws_IAE_when_AUTO_INCREMENT_flag_is_provided_with_column_name_other_than_id() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Auto increment column name must be id"); - - underTest.addPkColumn(newIntegerColumnDefBuilder().setColumnName("toto").build(), AUTO_INCREMENT); - } - - @Test - public void addPkColumn_throws_ISE_when_adding_multiple_autoincrement_columns() { - underTest.addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("There can't be more than one auto increment column"); - - underTest.addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT); - } - - @Test - public void addPkColumn_throws_IAE_when_AUTO_INCREMENT_flag_is_provided_with_def_other_than_Integer_and_BigInteger() { - ColumnDef[] columnDefs = { - newBooleanColumnDefBuilder().setColumnName("id").build(), - newClobColumnDefBuilder().setColumnName("id").build(), - newDecimalColumnDefBuilder().setColumnName("id").build(), - new TinyIntColumnDef.Builder().setColumnName("id").build(), - newVarcharColumnDefBuilder().setColumnName("id").setLimit(40).build(), - newBlobColumnDefBuilder().setColumnName("id").build() - }; - Arrays.stream(columnDefs) - .forEach(columnDef -> { - try { - underTest.addPkColumn(columnDef, AUTO_INCREMENT); - fail("A IllegalArgumentException should have been raised"); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Auto increment column must either be BigInteger or Integer"); - } - }); - } - - @Test - public void addPkColumn_throws_IAE_when_AUTO_INCREMENT_flag_is_provided_and_column_is_nullable() { - ColumnDef[] columnDefs = { - newIntegerColumnDefBuilder().setColumnName("id").build(), - newBigIntegerColumnDefBuilder().setColumnName("id").build() - }; - Arrays.stream(columnDefs) - .forEach(columnDef -> { - try { - underTest.addPkColumn(columnDef, AUTO_INCREMENT); - fail("A IllegalArgumentException should have been raised"); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Auto increment column can't be nullable"); - } - }); - } - - @Test - public void build_sets_type_SERIAL_for_autoincrement_integer_pk_column_on_Postgresql() { - List<String> stmts = new CreateTableBuilder(POSTGRESQL, TABLE_NAME) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(1); - assertThat(stmts.iterator().next()) - .isEqualTo( - "CREATE TABLE table_42 (id SERIAL NOT NULL, CONSTRAINT pk_table_42 PRIMARY KEY (id))"); - } - - @Test - public void build_sets_type_BIGSERIAL_for_autoincrement_biginteger_pk_column_on_Postgresql() { - List<String> stmts = new CreateTableBuilder(POSTGRESQL, TABLE_NAME) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(1); - assertThat(stmts.iterator().next()) - .isEqualTo( - "CREATE TABLE table_42 (id BIGSERIAL NOT NULL, CONSTRAINT pk_table_42 PRIMARY KEY (id))"); - } - - @Test - public void build_generates_a_create_trigger_statement_when_an_autoincrement_pk_column_is_specified_and_on_Oracle() { - List<String> stmts = new CreateTableBuilder(ORACLE, TABLE_NAME) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(3); - assertThat(stmts.get(0)) - .isEqualTo("CREATE TABLE table_42 (id NUMBER(38,0) NOT NULL, CONSTRAINT pk_table_42 PRIMARY KEY (id))"); - assertThat(stmts.get(1)) - .isEqualTo("CREATE SEQUENCE table_42_seq START WITH 1 INCREMENT BY 1"); - assertThat(stmts.get(2)) - .isEqualTo("CREATE OR REPLACE TRIGGER table_42_idt" + - " BEFORE INSERT ON table_42" + - " FOR EACH ROW" + - " BEGIN" + - " IF :new.id IS null THEN" + - " SELECT table_42_seq.nextval INTO :new.id FROM dual;" + - " END IF;" + - " END;"); - } - - @Test - public void build_adds_IDENTITY_clause_on_MsSql() { - List<String> stmts = new CreateTableBuilder(MS_SQL, TABLE_NAME) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(1); - assertThat(stmts.iterator().next()) - .isEqualTo( - "CREATE TABLE table_42 (id INT NOT NULL IDENTITY (1,1), CONSTRAINT pk_table_42 PRIMARY KEY (id))"); - } - - @Test - public void build_adds_AUTO_INCREMENT_clause_on_H2() { - List<String> stmts = new CreateTableBuilder(H2, TABLE_NAME) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(1); - assertThat(stmts.iterator().next()) - .isEqualTo( - "CREATE TABLE table_42 (id INTEGER NOT NULL AUTO_INCREMENT (1,1), CONSTRAINT pk_table_42 PRIMARY KEY (id))"); - } - - @Test - public void build_adds_AUTO_INCREMENT_clause_on_MySql() { - List<String> stmts = new CreateTableBuilder(MY_SQL, TABLE_NAME) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(1); - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE table_42 (id INTEGER NOT NULL AUTO_INCREMENT, CONSTRAINT pk_table_42 PRIMARY KEY (id))"); - } - - @Test - public void builds_adds_hardcoded_collation_clause_on_MySql() { - List<String> stmts = new CreateTableBuilder(MY_SQL, TABLE_NAME) - .addPkColumn(newIntegerColumnDefBuilder().setColumnName("id").setIsNullable(false).build(), AUTO_INCREMENT) - .build(); - assertThat(stmts).hasSize(1); - assertThat(stmts.iterator().next()) - .endsWith(" ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin"); - - } - - @Test - public void withPkConstraintName_throws_NPE_if_name_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Constraint name cannot be null"); - - underTest.withPkConstraintName(null); - } - - @Test - public void withPkConstraintName_throws_IAE_if_name_is_not_lowercase() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Constraint name must be lower case and contain only alphanumeric chars or '_', got 'Too'"); - - underTest.withPkConstraintName("Too"); - } - - @Test - public void withPkConstraintName_throws_IAE_if_name_is_more_than_30_char_long() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Constraint name length can't be more than 30"); - - underTest.withPkConstraintName("abcdefghijklmnopqrstuvwxyzabcdf"); - } - - @Test - public void withPkConstraintName_throws_IAE_if_name_starts_with_underscore() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Constraint name must not start by a number or '_', got '_a'"); - - underTest.withPkConstraintName("_a"); - } - - @Test - @UseDataProvider("digitCharsDataProvider") - public void withPkConstraintName_throws_IAE_if_name_starts_with_number(char number) { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Constraint name must not start by a number or '_', got '" + number + "a'"); - - underTest.withPkConstraintName(number + "a"); - } - - @Test - public void withPkConstraintName_does_not_fail_if_name_is_30_char_long() { - underTest.withPkConstraintName("abcdefghijklmnopqrstuvwxyzabcd"); - } - - @Test - public void withPkConstraintName_does_not_fail_if_name_contains_ascii_letters() { - underTest.withPkConstraintName("abcdefghijklmnopqrstuvwxyz"); - } - - @Test - public void withPkConstraintName_does_not_fail_if_name_contains_underscore() { - underTest.withPkConstraintName("a_"); - } - - @Test - public void withPkConstraintName_does_not_fail_if_name_contains_numbers() { - underTest.withPkConstraintName("a0123456789"); - } - - @Test - public void build_adds_NULL_when_column_is_nullable_for_all_DBs() { - Arrays.stream(ALL_DIALECTS) - .forEach(dialect -> { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col").build()) - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE " + TABLE_NAME + " (" + - "bg_col " + - bigIntSqlType(dialect) + " NULL" + - ")"); - }); - } - - @Test - public void build_adds_NOT_NULL_when_column_is_not_nullable_for_all_DBs() { - Arrays.stream(ALL_DIALECTS) - .forEach(dialect -> { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col").setIsNullable(false).build()) - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE " + TABLE_NAME + " (" + - "bg_col " + - bigIntSqlType(dialect) + - " NOT NULL" + - ")"); - }); - } - - @Test - public void build_of_single_column_table() { - List<String> stmts = new CreateTableBuilder(H2, TABLE_NAME) - .addColumn(newBooleanColumnDefBuilder().setColumnName("bool_col_1").build()) - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()).isEqualTo("CREATE TABLE table_42 (bool_col_1 BOOLEAN NULL)"); - } - - @Test - public void build_table_with_pk() { - List<String> stmts = new CreateTableBuilder(H2, TABLE_NAME) - .addPkColumn(newBooleanColumnDefBuilder().setColumnName("bool_col").build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col").setLimit(40).build()) - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .isEqualTo("CREATE TABLE " + TABLE_NAME + " (" + - "bool_col BOOLEAN NULL," + - "varchar_col VARCHAR (40) NULL," + - " CONSTRAINT pk_" + TABLE_NAME + " PRIMARY KEY (bool_col)" + - ")"); - - } - - @Test - public void build_adds_PRIMARY_KEY_constraint_on_single_column_with_name_computed_from_tablename() { - Arrays.asList(ALL_DIALECTS) - .forEach(dialect -> { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col").setIsNullable(false).build()) - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE " + TABLE_NAME + " (" + - "bg_col " + bigIntSqlType(dialect) + " NOT NULL," + - " CONSTRAINT pk_" + TABLE_NAME + " PRIMARY KEY (bg_col)" + - ")"); - }); - } - - @Test - public void build_adds_PRIMARY_KEY_constraint_on_single_column_with_lower_case_of_specified_name() { - Arrays.asList(ALL_DIALECTS) - .forEach(dialect -> { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col").setIsNullable(false).build()) - .withPkConstraintName("my_pk") - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE " + TABLE_NAME + " (" + - "bg_col " + - bigIntSqlType(dialect) + - " NOT NULL," + - " CONSTRAINT my_pk PRIMARY KEY (bg_col)" + - ")"); - }); - } - - @Test - public void build_adds_PRIMARY_KEY_constraint_on_multiple_columns_with_name_computed_from_tablename() { - Arrays.asList(ALL_DIALECTS) - .forEach(dialect -> { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_1").setIsNullable(false).build()) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_2").setIsNullable(false).build()) - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE " + TABLE_NAME + " (" + - "bg_col_1 " + bigIntSqlType(dialect) + " NOT NULL," + - "bg_col_2 " + bigIntSqlType(dialect) + " NOT NULL," + - " CONSTRAINT pk_" + TABLE_NAME + " PRIMARY KEY (bg_col_1,bg_col_2)" + - ")"); - }); - } - - @Test - public void build_adds_PRIMARY_KEY_constraint_on_multiple_columns_with_lower_case_of_specified_name() { - Arrays.asList(ALL_DIALECTS) - .forEach(dialect -> { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_1").setIsNullable(false).build()) - .addPkColumn(newBigIntegerColumnDefBuilder().setColumnName("bg_col_2").setIsNullable(false).build()) - .withPkConstraintName("my_pk") - .build(); - assertThat(stmts).hasSize(1); - - assertThat(stmts.iterator().next()) - .startsWith("CREATE TABLE " + TABLE_NAME + " (" + - "bg_col_1 " + bigIntSqlType(dialect) + " NOT NULL," + - "bg_col_2 " + bigIntSqlType(dialect) + " NOT NULL," + - " CONSTRAINT my_pk PRIMARY KEY (bg_col_1,bg_col_2)" + - ")"); - }); - } - - @Test - public void build_adds_DEFAULT_clause_on_varchar_column_on_H2() { - verifyDefaultClauseOnVarcharColumn(H2, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL)"); - } - - @Test - public void build_adds_DEFAULT_clause_on_varchar_column_on_MSSQL() { - verifyDefaultClauseOnVarcharColumn(MS_SQL, "CREATE TABLE table_42 (status NVARCHAR (1) DEFAULT 'P' NOT NULL)"); - } - - @Test - public void build_adds_DEFAULT_clause_on_varchar_column_on_MySQL() { - verifyDefaultClauseOnVarcharColumn(MY_SQL, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin"); - } - - @Test - public void build_adds_DEFAULT_clause_on_varchar_column_on_Oracle() { - verifyDefaultClauseOnVarcharColumn(ORACLE, "CREATE TABLE table_42 (status VARCHAR (1 CHAR) DEFAULT 'P' NOT NULL)"); - } - - @Test - public void build_adds_DEFAULT_clause_on_varchar_column_on_PostgreSQL() { - verifyDefaultClauseOnVarcharColumn(POSTGRESQL, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL)"); - } - - private static void verifyDefaultClauseOnVarcharColumn(Dialect dialect, String expectedSql) { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addColumn(newVarcharColumnDefBuilder().setColumnName("status").setLimit(1).setIsNullable(false).setDefaultValue("P").build()) - .build(); - assertThat(stmts).containsExactly(expectedSql); - } - - @Test - public void build_adds_DEFAULT_clause_on_boolean_column_on_H2() { - verifyDefaultClauseOnBooleanColumn(H2, "CREATE TABLE table_42 (enabled BOOLEAN DEFAULT true NOT NULL)"); - } - - @Test - public void build_adds_DEFAULT_clause_on_boolean_column_on_MSSQL() { - verifyDefaultClauseOnBooleanColumn(MS_SQL, "CREATE TABLE table_42 (enabled BIT DEFAULT 1 NOT NULL)"); - } - - @Test - public void build_adds_DEFAULT_clause_on_boolean_column_on_MySQL() { - verifyDefaultClauseOnBooleanColumn(MY_SQL, "CREATE TABLE table_42 (enabled TINYINT(1) DEFAULT true NOT NULL) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin"); - } - - @Test - public void build_adds_DEFAULT_clause_on_boolean_column_on_Oracle() { - verifyDefaultClauseOnBooleanColumn(ORACLE, "CREATE TABLE table_42 (enabled NUMBER(1) DEFAULT 1 NOT NULL)"); - } - - @Test - public void build_adds_DEFAULT_clause_on_boolean_column_on_PostgreSQL() { - verifyDefaultClauseOnBooleanColumn(POSTGRESQL, "CREATE TABLE table_42 (enabled BOOLEAN DEFAULT true NOT NULL)"); - } - - private static void verifyDefaultClauseOnBooleanColumn(Dialect dialect, String expectedSql) { - List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME) - .addColumn(newBooleanColumnDefBuilder().setColumnName("enabled").setIsNullable(false).setDefaultValue(true).build()) - .build(); - assertThat(stmts).containsExactly(expectedSql); - } - - private static String bigIntSqlType(Dialect dialect) { - return Oracle.ID.equals(dialect.getId()) ? "NUMBER (38)" : "BIGINT"; - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java deleted file mode 100644 index e5f70681fa6..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/DecimalColumnDefTest.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Dialect; -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; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class DecimalColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void build_string_column_def() throws Exception { - DecimalColumnDef def = new DecimalColumnDef.Builder() - .setColumnName("issues") - .setPrecision(30) - .setScale(20) - .setIsNullable(true) - .build(); - - assertThat(def.getName()).isEqualTo("issues"); - assertThat(def.getPrecision()).isEqualTo(30); - assertThat(def.getScale()).isEqualTo(20); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void fail_with_NPE_if_name_is_null() throws Exception { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Column name cannot be null"); - - new DecimalColumnDef.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 DecimalColumnDef.Builder() - .build(); - } - - @Test - public void default_precision_is_38() throws Exception { - DecimalColumnDef def = new DecimalColumnDef.Builder() - .setColumnName("issues") - .setScale(20) - .setIsNullable(true) - .build(); - - assertThat(def.getPrecision()).isEqualTo(38); - } - - @Test - public void default_precision_is_20() throws Exception { - DecimalColumnDef def = new DecimalColumnDef.Builder() - .setColumnName("issues") - .setPrecision(30) - .setIsNullable(true) - .build(); - - assertThat(def.getScale()).isEqualTo(20); - } - - @Test - public void create_builder_with_only_required_attributes() throws Exception { - DecimalColumnDef def = new DecimalColumnDef.Builder() - .setColumnName("issues") - .build(); - - assertThat(def.getPrecision()).isEqualTo(38); - assertThat(def.getScale()).isEqualTo(20); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void generate_sql_type() throws Exception { - DecimalColumnDef def = new DecimalColumnDef.Builder() - .setColumnName("issues") - .setPrecision(30) - .setScale(20) - .setIsNullable(true) - .build(); - - assertThat(def.generateSqlType(new H2())).isEqualTo("DOUBLE"); - assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("NUMERIC (30,20)"); - assertThat(def.generateSqlType(new MsSql())).isEqualTo("DECIMAL (30,20)"); - assertThat(def.generateSqlType(new MySql())).isEqualTo("DECIMAL (30,20)"); - assertThat(def.generateSqlType(new Oracle())).isEqualTo("NUMERIC (30,20)"); - } - - @Test - public void fail_with_UOE_to_generate_sql_type_when_unknown_dialect() throws Exception { - thrown.expect(UnsupportedOperationException.class); - thrown.expectMessage("Unknown dialect 'unknown'"); - - DecimalColumnDef def = new DecimalColumnDef.Builder() - .setColumnName("issues") - .setPrecision(30) - .setScale(20) - .setIsNullable(true) - .build(); - - Dialect dialect = mock(Dialect.class); - when(dialect.getId()).thenReturn("unknown"); - def.generateSqlType(dialect); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/DropColumnsBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/DropColumnsBuilderTest.java deleted file mode 100644 index 04508a5d64a..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/DropColumnsBuilderTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Test; -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 org.sonar.db.version.DropColumnsBuilder; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DropColumnsBuilderTest { - - @Test - public void drop_columns_on_mysql() { - assertThat(new DropColumnsBuilder(new MySql(), "issues", "date_in_ms", "name") - .build()).isEqualTo("ALTER TABLE issues DROP COLUMN date_in_ms, DROP COLUMN name"); - } - - @Test - public void drop_columns_on_oracle() { - assertThat(new DropColumnsBuilder(new Oracle(), "issues", "date_in_ms", "name") - .build()).isEqualTo("ALTER TABLE issues DROP (date_in_ms, name)"); - } - - @Test - public void drop_columns_on_postgresql() { - assertThat(new DropColumnsBuilder(new PostgreSql(), "issues", "date_in_ms", "name") - .build()).isEqualTo("ALTER TABLE issues DROP COLUMN date_in_ms, DROP COLUMN name"); - } - - @Test - public void drop_columns_on_mssql() { - assertThat(new DropColumnsBuilder(new MsSql(), "issues", "date_in_ms", "name") - .build()).isEqualTo("ALTER TABLE issues DROP COLUMN date_in_ms, name"); - } - - @Test(expected = IllegalStateException.class) - public void fail_to_drop_columns_on_h2() { - new DropColumnsBuilder(new H2(), "issues", "date_in_ms", "name") - .build(); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java deleted file mode 100644 index 7792248ce1c..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.List; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.db.dialect.Dialect; -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 DropIndexBuilderTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void drop_index_in_table() { - verifySql(new H2(), "DROP INDEX IF EXISTS issues_key"); - verifySql(new MsSql(), "DROP INDEX issues_key ON issues"); - verifySql(new MySql(), "DROP INDEX issues_key ON issues"); - verifySql(new Oracle(), "DROP INDEX issues_key"); - verifySql(new PostgreSql(), "DROP INDEX IF EXISTS issues_key"); - } - - private static void verifySql(Dialect dialect, String expectedSql) { - List<String> actual = new DropIndexBuilder(dialect) - .setTable("issues") - .setName("issues_key") - .build(); - assertThat(actual).containsExactly(expectedSql); - } - - @Test - public void throw_NPE_if_table_name_is_missing() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Table name cannot be null"); - - new DropIndexBuilder(new H2()) - .setName("issues_key") - .build(); - } - - @Test - public void throw_IAE_if_table_name_is_not_valid() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'"); - - new DropIndexBuilder(new H2()) - .setTable("(not valid)") - .setName("issues_key") - .build(); - } - - @Test - public void throw_NPE_if_index_name_is_missing() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Index name cannot be null"); - - new DropIndexBuilder(new H2()) - .setTable("issues") - .build(); - } - - @Test - public void throw_IAE_if_index_name_is_not_valid() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Index name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'"); - - new DropIndexBuilder(new H2()) - .setTable("issues") - .setName("(not valid)") - .build(); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/DropTableBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/DropTableBuilderTest.java deleted file mode 100644 index 918dcbd28fa..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/DropTableBuilderTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 DropTableBuilderTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void drop_tables_on_mysql() { - assertThat(new DropTableBuilder(new MySql(), "issues") - .build()).containsOnly("DROP TABLE issues"); - } - - @Test - public void drop_tables_on_postgresql() { - assertThat(new DropTableBuilder(new PostgreSql(), "issues") - .build()).containsOnly("DROP TABLE issues"); - } - - @Test - public void drop_tables_on_mssql() { - assertThat(new DropTableBuilder(new MsSql(), "issues") - .build()).containsOnly("DROP TABLE issues"); - } - - @Test - public void drop_tables_on_h2() { - assertThat(new DropTableBuilder(new H2(), "issues") - .build()).containsOnly("DROP TABLE issues"); - } - - @Test - public void drop_columns_on_oracle() { - assertThat(new DropTableBuilder(new Oracle(), "issues") - .build()).containsExactly( - "BEGIN\n" + - " EXECUTE IMMEDIATE 'DROP SEQUENCE issues_seq';\n" + - "EXCEPTION\n" + - " WHEN OTHERS THEN\n" + - " IF SQLCODE != -2289 THEN\n" + - " RAISE;\n" + - " END IF;\n" + - "END;", - "BEGIN\n" + - " EXECUTE IMMEDIATE 'DROP TRIGGER issues_idt';\n" + - "EXCEPTION\n" + - " WHEN OTHERS THEN\n" + - " IF SQLCODE != -4080 THEN\n" + - " RAISE;\n" + - " END IF;\n" + - "END;", - "DROP TABLE issues"); - } - - @Test - public void fail_when_dialect_is_null() throws Exception { - expectedException.expect(NullPointerException.class); - new DropTableBuilder(null, "issues"); - } - - @Test - public void fail_when_table_is_null() throws Exception { - expectedException.expect(NullPointerException.class); - new DropTableBuilder(new PostgreSql(), null); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java deleted file mode 100644 index 82888df3f33..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/IntegerColumnDefTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Dialect; -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; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.db.version.IntegerColumnDef.newIntegerColumnDefBuilder; - -public class IntegerColumnDefTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private IntegerColumnDef underTest = newIntegerColumnDefBuilder().setColumnName("a").build(); - - @Test - public void builder_setColumnName_throws_IAE_if_name_is_not_lowercase() { - IntegerColumnDef.Builder builder = newIntegerColumnDefBuilder(); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Column name must be lower case and contain only alphanumeric chars or '_', got 'T'"); - builder.setColumnName("T"); - } - - @Test - public void builder_build_throws_NPE_if_no_name_was_set() { - IntegerColumnDef.Builder builder = newIntegerColumnDefBuilder(); - - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("Column name cannot be null"); - - builder.build(); - } - - @Test - public void integerColumDef_is_nullable_by_default() { - assertThat(newIntegerColumnDefBuilder().setColumnName("a").build().isNullable()).isTrue(); - } - - @Test - public void builder_setNullable_sets_nullable_field_of_IntegerColumnDef() { - assertThat(newIntegerColumnDefBuilder().setColumnName("a").setIsNullable(true).build().isNullable()).isTrue(); - assertThat(newIntegerColumnDefBuilder().setColumnName("a").setIsNullable(false).build().isNullable()).isFalse(); - } - - @Test - public void builder_setColumnName_sets_name_field_of_IntegerColumnDef() { - assertThat(newIntegerColumnDefBuilder().setColumnName("a").build().getName()).isEqualTo("a"); - } - - @Test - public void builder_setDefaultValue_sets_default_value_field_of_IntegerColumnDef() { - assertThat(newIntegerColumnDefBuilder().setColumnName("a").setDefaultValue(42).build().getDefaultValue()).isEqualTo(42); - } - - @Test - public void default_value_is_null_by_default() { - assertThat(newIntegerColumnDefBuilder().setColumnName("a").build().getDefaultValue()).isNull(); - } - - @Test - public void generateSqlType_for_MsSql() { - assertThat(underTest.generateSqlType(new MsSql())).isEqualTo("INT"); - } - - @Test - public void generateSqlType_for_MySql() { - assertThat(underTest.generateSqlType(new MySql())).isEqualTo("INTEGER"); - } - - @Test - public void generateSqlType_for_Oracle() { - assertThat(underTest.generateSqlType(new Oracle())).isEqualTo("NUMBER(38,0)"); - } - - @Test - public void generateSqlType_for_H2() { - assertThat(underTest.generateSqlType(new H2())).isEqualTo("INTEGER"); - } - - @Test - public void generateSqlType_for_PostgreSql() { - assertThat(underTest.generateSqlType(new PostgreSql())).isEqualTo("INTEGER"); - } - - @Test - public void generateSqlType_thows_IAE_for_unknown_dialect() { - Dialect dialect = mock(Dialect.class); - when(dialect.getId()).thenReturn("AAA"); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Unsupported dialect id AAA"); - - underTest.generateSqlType(dialect); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/RenameTableBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/RenameTableBuilderTest.java deleted file mode 100644 index 6c97100d25a..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/RenameTableBuilderTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.List; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.db.dialect.Dialect; -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 RenameTableBuilderTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void rename_table_on_h2() { - verifySql(new H2(), "ALTER TABLE foo RENAME TO bar"); - } - - @Test - public void rename_table_on_mssql() { - verifySql(new MsSql(), "EXEC sp_rename 'foo', 'bar'"); - } - - @Test - public void rename_table_on_mysql() { - verifySql(new MySql(), "ALTER TABLE foo RENAME TO bar"); - } - - @Test - public void rename_table_on_oracle() { - verifySql(new Oracle(), - "DROP TRIGGER foo_idt", - "RENAME foo TO bar", - "RENAME foo_seq TO bar_seq", - "CREATE OR REPLACE TRIGGER bar_idt BEFORE INSERT ON bar FOR EACH ROW BEGIN IF :new.id IS null THEN SELECT bar_seq.nextval INTO :new.id FROM dual; END IF; END;"); - } - - @Test - public void rename_table_on_postgresql() { - verifySql(new PostgreSql(), "ALTER TABLE foo RENAME TO bar"); - } - - @Test - public void throw_IAE_if_name_is_not_valid() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'"); - - new RenameTableBuilder(new H2()).setName("(not valid)").build(); - } - - @Test - public void throw_IAE_if_new_name_is_not_valid() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'"); - - new RenameTableBuilder(new H2()).setName("foo").setNewName("(not valid)").build(); - } - - private static void verifySql(Dialect dialect, String... expectedSql) { - List<String> actual = new RenameTableBuilder(dialect) - .setName("foo") - .setNewName("bar") - .build(); - assertThat(actual).containsExactly(expectedSql); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java deleted file mode 100644 index 1d68c91c9c4..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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; -import static org.sonar.db.version.TimestampColumnDef.newTimestampColumnDefBuilder; - -public class TimestampColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void build_column_def() throws Exception { - TimestampColumnDef def = newTimestampColumnDefBuilder() - .setColumnName("created_at") - .setIsNullable(false) - .build(); - - assertThat(def.getName()).isEqualTo("created_at"); - assertThat(def.isNullable()).isFalse(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void build_column_def_with_only_required_attributes() throws Exception { - TimestampColumnDef def = newTimestampColumnDefBuilder() - .setColumnName("created_at") - .build(); - - assertThat(def.getName()).isEqualTo("created_at"); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void generate_sql_type() throws Exception { - TimestampColumnDef def = newTimestampColumnDefBuilder() - .setColumnName("created_at") - .build(); - - assertThat(def.generateSqlType(new H2())).isEqualTo("TIMESTAMP"); - assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("TIMESTAMP"); - assertThat(def.generateSqlType(new MsSql())).isEqualTo("DATETIME"); - assertThat(def.generateSqlType(new MySql())).isEqualTo("DATETIME"); - assertThat(def.generateSqlType(new Oracle())).isEqualTo("TIMESTAMP (6)"); - } - - @Test - public void fail_with_NPE_if_name_is_null() throws Exception { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Column name cannot be null"); - - newTimestampColumnDefBuilder().setColumnName(null); - } - - @Test - public void fail_with_NPE_if_no_name() throws Exception { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Column name cannot be null"); - - newTimestampColumnDefBuilder().build(); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java deleted file mode 100644 index 65cfbe3bec6..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/TinyIntColumnDefTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Dialect; -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; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class TinyIntColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void build_string_column_def() throws Exception { - TinyIntColumnDef def = new TinyIntColumnDef.Builder() - .setColumnName("foo") - .setIsNullable(true) - .build(); - - assertThat(def.getName()).isEqualTo("foo"); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void generate_sql_type() throws Exception { - TinyIntColumnDef def = new TinyIntColumnDef.Builder() - .setColumnName("foo") - .setIsNullable(true) - .build(); - - assertThat(def.generateSqlType(new H2())).isEqualTo("TINYINT"); - assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("SMALLINT"); - assertThat(def.generateSqlType(new MsSql())).isEqualTo("TINYINT"); - assertThat(def.generateSqlType(new MySql())).isEqualTo("TINYINT(2)"); - assertThat(def.generateSqlType(new Oracle())).isEqualTo("NUMBER(3)"); - } - - @Test - public void fail_with_UOE_to_generate_sql_type_when_unknown_dialect() throws Exception { - thrown.expect(UnsupportedOperationException.class); - thrown.expectMessage("Unknown dialect 'unknown'"); - - TinyIntColumnDef def = new TinyIntColumnDef.Builder() - .setColumnName("foo") - .setIsNullable(true) - .build(); - - Dialect dialect = mock(Dialect.class); - when(dialect.getId()).thenReturn("unknown"); - def.generateSqlType(dialect); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/ValidationsTest.java b/sonar-db/src/test/java/org/sonar/db/version/ValidationsTest.java deleted file mode 100644 index bb1913a6e4a..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/ValidationsTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.assertj.core.api.Assertions.assertThat; -import static org.sonar.db.version.Validations.validateColumnName; -import static org.sonar.db.version.Validations.validateIndexName; - -public class ValidationsTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void accept_valid_table_name() throws Exception { - validateColumnName("date_in_ms"); - validateColumnName("date_in_ms_1"); - } - - @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 must be lower case and contain only alphanumeric chars or '_', 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 must be lower case and contain only alphanumeric chars or '_', got 'date-in/ms'"); - - validateColumnName("date-in/ms"); - } - - @Test - public void validateIndexName_throws_IAE_when_index_name_contains_invalid_characters() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Index name must be lower case and contain only alphanumeric chars or '_', got '(not/valid)'"); - - validateIndexName("(not/valid)"); - } - - @Test - public void validateIndexName_throws_NPE_when_index_name_is_null() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Index name cannot be null"); - - validateIndexName(null); - } - - @Test - public void validateIndexName_returns_valid_name() { - assertThat(validateIndexName("foo")).isEqualTo("foo"); - } -} diff --git a/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java deleted file mode 100644 index dc6a163bb5f..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/version/VarcharColumnDefTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 VarcharColumnDefTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void build_string_column_def() throws Exception { - VarcharColumnDef def = new VarcharColumnDef.Builder() - .setColumnName("issues") - .setLimit(10) - .setIsNullable(true) - .setDefaultValue("foo") - .build(); - - assertThat(def.getName()).isEqualTo("issues"); - assertThat(def.getColumnSize()).isEqualTo(10); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isEqualTo("foo"); - } - - @Test - public void build_string_column_def_with_only_required_attributes() throws Exception { - VarcharColumnDef def = new VarcharColumnDef.Builder() - .setColumnName("issues") - .setLimit(10) - .build(); - - assertThat(def.getName()).isEqualTo("issues"); - assertThat(def.getColumnSize()).isEqualTo(10); - assertThat(def.isNullable()).isTrue(); - assertThat(def.getDefaultValue()).isNull(); - } - - @Test - public void generate_sql_type() throws Exception { - VarcharColumnDef def = new VarcharColumnDef.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("NVARCHAR (10)"); - assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR (10 CHAR)"); - } - - @Test - public void generateSqlType_does_not_set_unit_on_oracle_if_legacy_mode() throws Exception { - VarcharColumnDef def = new VarcharColumnDef.Builder() - .setColumnName("issues") - .setLimit(10) - .setIsNullable(true) - .setIgnoreOracleUnit(true) - .build(); - - 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 VarcharColumnDef.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 VarcharColumnDef.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 VarcharColumnDef.Builder() - .setColumnName("issues") - .build(); - } -} |