Browse Source

SONAR-6840 Replace ColumnDef by builders

tags/5.2-RC1
Julien Lancelot 8 years ago
parent
commit
f6379f845f

+ 41
- 0
sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java View File

@@ -0,0 +1,41 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

public abstract class AbstractColumnDef implements ColumnDef {
private final String columnName;
private final boolean isNullable;

public AbstractColumnDef(String columnName, boolean isNullable) {
this.columnName = columnName;
this.isNullable = isNullable;
}

@Override
public String getName() {
return columnName;
}

@Override
public boolean isNullable() {
return isNullable;
}
}

+ 4
- 17
sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java View File

@@ -22,11 +22,13 @@ package org.sonar.db.version;
import java.util.List;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

import static com.google.common.collect.Lists.newArrayList;

/**
* Generate a SQL query to add multiple columns on a table
*/
public class AddColumnsBuilder {

private final Dialect dialect;
@@ -76,23 +78,8 @@ public class AddColumnsBuilder {
}

private void addColumn(StringBuilder sql, ColumnDef columnDef) {
sql.append(columnDef.getName()).append(" ").append(typeToSql(columnDef));
Integer limit = columnDef.getLimit();
if (limit != null) {
sql.append(" (").append(Integer.toString(limit)).append(")");
}
sql.append(columnDef.getName()).append(" ").append(columnDef.generateSqlType(dialect));
sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
}

private String typeToSql(ColumnDef columnDef) {
switch (columnDef.getType()) {
case STRING:
return "VARCHAR";
case BIG_INTEGER:
return !dialect.getId().equals(Oracle.ID) ? "BIGINT" : "NUMBER (38)";
default:
throw new IllegalArgumentException("Unsupported type : " + columnDef.getType());
}
}

}

+ 66
- 0
sonar-db/src/main/java/org/sonar/db/version/BigDecimalColumnDef.java View File

@@ -0,0 +1,66 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import javax.annotation.CheckForNull;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.Oracle;

import static org.sonar.db.version.ColumnDefValidation.validateColumnName;

public class BigDecimalColumnDef extends AbstractColumnDef {

private BigDecimalColumnDef(Builder builder) {
super(builder.columnName, builder.isNullable);
}

public static Builder newBigDecimalColumnDefBuilder() {
return new Builder();
}

@Override
public String generateSqlType(Dialect dialect) {
return dialect.getId().equals(Oracle.ID) ? "NUMBER (38)" : "BIGINT";
}

public static class Builder {
@CheckForNull
private String columnName;

private boolean isNullable;

public Builder setColumnName(String columnName) {
this.columnName = validateColumnName(columnName);
return this;
}

public Builder setIsNullable(boolean isNullable) {
this.isNullable = isNullable;
return this;
}

public BigDecimalColumnDef build() {
validateColumnName(columnName);
return new BigDecimalColumnDef(this);
}
}

}

+ 4
- 61
sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java View File

@@ -20,71 +20,14 @@

package org.sonar.db.version;

import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.Oracle;

public class ColumnDef {
public interface ColumnDef {

private String name;
private Type type;
private boolean isNullable;
private Integer limit;
boolean isNullable();

public enum Type {
STRING, BIG_INTEGER
}
String getName();

public ColumnDef setNullable(boolean isNullable) {
this.isNullable = isNullable;
return this;
}

public ColumnDef setLimit(@Nullable Integer limit) {
this.limit = limit;
return this;
}

public ColumnDef setName(String name) {
Preconditions.checkArgument(CharMatcher.JAVA_LOWER_CASE.or(CharMatcher.anyOf("_")).matchesAllOf(name), "Column name should only contains lowercase and _ characters");
this.name = name;
return this;
}

public ColumnDef setType(Type type) {
this.type = type;
return this;
}

public boolean isNullable() {
return isNullable;
}

@CheckForNull
public Integer getLimit() {
return limit;
}

public String getName() {
return name;
}

public Type getType() {
return type;
}

public String getSqlType(Dialect dialect) {
switch (type) {
case STRING:
return "VARCHAR";
case BIG_INTEGER:
return dialect.getId().equals(Oracle.ID) ? "NUMBER (38)" : "BIGINT";
default:
throw new IllegalArgumentException("Unsupported type : " + type);
}
}
String generateSqlType(Dialect dialect);

}

+ 41
- 0
sonar-db/src/main/java/org/sonar/db/version/ColumnDefValidation.java View File

@@ -0,0 +1,41 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import com.google.common.base.CharMatcher;
import javax.annotation.Nullable;

import static com.google.common.base.CharMatcher.JAVA_LOWER_CASE;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

public class ColumnDefValidation {

private ColumnDefValidation() {
// Only static stuff here
}

public static String validateColumnName(@Nullable String columnName) {
String name = requireNonNull(columnName, "Column name cannot be null");
checkArgument(JAVA_LOWER_CASE.or(CharMatcher.anyOf("_")).matchesAllOf(name), String.format("Column name should only contains lowercase and _ characters, got '%s'", columnName));
return name;
}
}

+ 3
- 0
sonar-db/src/main/java/org/sonar/db/version/DropColumnsBuilder.java View File

@@ -25,6 +25,9 @@ import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

/**
* Generate a SQL query to drop multiple columns from a table
*/
public class DropColumnsBuilder {

private final Dialect dialect;

+ 81
- 0
sonar-db/src/main/java/org/sonar/db/version/StringColumnDef.java View File

@@ -0,0 +1,81 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import javax.annotation.CheckForNull;
import org.sonar.db.dialect.Dialect;

import static java.util.Objects.requireNonNull;
import static org.sonar.db.version.ColumnDefValidation.validateColumnName;

public class StringColumnDef extends AbstractColumnDef {
private final int columnSize;

private StringColumnDef(Builder builder) {
super(builder.columnName, builder.isNullable);
this.columnSize = builder.columnSize;
}

public static Builder newStringColumnDefBuilder() {
return new Builder();
}

public int getColumnSize() {
return columnSize;
}

@Override
public String generateSqlType(Dialect dialect) {
return "VARCHAR (" + Integer.toString(columnSize) + ")";
}

public static class Builder {
@CheckForNull
private Integer columnSize;

@CheckForNull
private String columnName;

private boolean isNullable;

public Builder setColumnName(String columnName) {
this.columnName = validateColumnName(columnName);
return this;
}

public Builder setLimit(int limit) {
this.columnSize = limit;
return this;
}

public Builder setIsNullable(boolean isNullable) {
this.isNullable = isNullable;
return this;
}

public StringColumnDef build() {
validateColumnName(columnName);
requireNonNull(columnSize, "Limit cannot be null");
return new StringColumnDef(this);
}
}

}

+ 9
- 37
sonar-db/src/main/java/org/sonar/db/version/v51/AddIssuesColumns.java View File

@@ -23,9 +23,11 @@ package org.sonar.db.version.v51;
import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.db.version.AddColumnsBuilder;
import org.sonar.db.version.ColumnDef;
import org.sonar.db.version.DdlChange;

import static org.sonar.db.version.BigDecimalColumnDef.newBigDecimalColumnDefBuilder;
import static org.sonar.db.version.StringColumnDef.newStringColumnDefBuilder;

/**
* Add the following columns to the issues table :
* - issue_creation_date_ms
@@ -51,42 +53,12 @@ public class AddIssuesColumns extends DdlChange {

private String generateSql() {
return new AddColumnsBuilder(db.getDialect(), "issues")
.addColumn(
new ColumnDef()
.setName("issue_creation_date_ms")
.setType(ColumnDef.Type.BIG_INTEGER)
.setNullable(true)
)
.addColumn(
new ColumnDef()
.setName("issue_update_date_ms")
.setType(ColumnDef.Type.BIG_INTEGER)
.setNullable(true)
)
.addColumn(
new ColumnDef()
.setName("issue_close_date_ms")
.setType(ColumnDef.Type.BIG_INTEGER)
.setNullable(true)
)
.addColumn(
new ColumnDef()
.setName("tags")
.setType(ColumnDef.Type.STRING)
.setLimit(4000)
.setNullable(true))
.addColumn(
new ColumnDef()
.setName("component_uuid")
.setType(ColumnDef.Type.STRING)
.setLimit(50)
.setNullable(true))
.addColumn(
new ColumnDef()
.setName("project_uuid")
.setType(ColumnDef.Type.STRING)
.setLimit(50)
.setNullable(true))
.addColumn(newBigDecimalColumnDefBuilder().setColumnName("issue_creation_date_ms").setIsNullable(true).build())
.addColumn(newBigDecimalColumnDefBuilder().setColumnName("issue_update_date_ms").setIsNullable(true).build())
.addColumn(newBigDecimalColumnDefBuilder().setColumnName("issue_close_date_ms").setIsNullable(true).build())
.addColumn(newStringColumnDefBuilder().setColumnName("tags").setLimit(4000).setIsNullable(true).build())
.addColumn(newStringColumnDefBuilder().setColumnName("component_uuid").setLimit(50).setIsNullable(true).build())
.addColumn(newStringColumnDefBuilder().setColumnName("project_uuid").setLimit(50).setIsNullable(true).build())
.build();
}


+ 2
- 8
sonar-db/src/main/java/org/sonar/db/version/v52/AddManualMeasuresComponentUuidColumn.java View File

@@ -23,10 +23,9 @@ package org.sonar.db.version.v52;
import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.db.version.AddColumnsBuilder;
import org.sonar.db.version.ColumnDef;
import org.sonar.db.version.DdlChange;

import static org.sonar.db.version.ColumnDef.Type.STRING;
import static org.sonar.db.version.StringColumnDef.newStringColumnDefBuilder;

/**
* Add the following column to the manual_measures table :
@@ -45,12 +44,7 @@ public class AddManualMeasuresComponentUuidColumn extends DdlChange {

private String generateSql() {
return new AddColumnsBuilder(getDatabase().getDialect(), "manual_measures")
.addColumn(
new ColumnDef()
.setName("component_uuid")
.setType(STRING)
.setLimit(50)
.setNullable(true))
.addColumn(newStringColumnDefBuilder().setColumnName("component_uuid").setLimit(50).setIsNullable(true).build())
.build();
}
}

+ 8
- 24
sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java View File

@@ -30,8 +30,6 @@ import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.version.ColumnDef.Type.BIG_INTEGER;
import static org.sonar.db.version.ColumnDef.Type.STRING;

public class AddColumnsBuilderTest {

@@ -40,39 +38,33 @@ public class AddColumnsBuilderTest {

static final String TABLE_NAME = "issues";

static final H2 H2_DIALECT = new H2();
static final MySql MYSQL_DIALECT = new MySql();
static final Oracle ORACLE_DIALECT = new Oracle();
static final PostgreSql POSTGRES_DIALECT = new PostgreSql();
static final MsSql MSSQL_DIALECT = new MsSql();

@Test
public void add_columns_on_h2() {
assertThat(createSampleBuilder(H2_DIALECT).build())
assertThat(createSampleBuilder(new H2()).build())
.isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)");
}

@Test
public void add_columns_on_mysql() {
assertThat(createSampleBuilder(MYSQL_DIALECT).build())
assertThat(createSampleBuilder(new MySql()).build())
.isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)");
}

@Test
public void add_columns_on_oracle() {
assertThat(createSampleBuilder(ORACLE_DIALECT).build())
assertThat(createSampleBuilder(new Oracle()).build())
.isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR (10) NOT NULL)");
}

@Test
public void add_columns_on_postgresql() {
assertThat(createSampleBuilder(POSTGRES_DIALECT).build())
assertThat(createSampleBuilder(new PostgreSql()).build())
.isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL");
}

@Test
public void add_columns_on_mssql() {
assertThat(createSampleBuilder(MSSQL_DIALECT).build())
assertThat(createSampleBuilder(new MsSql()).build())
.isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL");
}

@@ -81,20 +73,12 @@ public class AddColumnsBuilderTest {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("No column has been defined");

new AddColumnsBuilder(H2_DIALECT, TABLE_NAME).build();
new AddColumnsBuilder(new H2(), TABLE_NAME).build();
}

private AddColumnsBuilder createSampleBuilder(Dialect dialect) {
return new AddColumnsBuilder(dialect, TABLE_NAME)
.addColumn(new ColumnDef()
.setName("date_in_ms")
.setType(BIG_INTEGER)
.setNullable(true))
.addColumn(new ColumnDef()
.setName("name")
.setType(STRING)
.setNullable(false)
.setLimit(10));
.addColumn(new BigDecimalColumnDef.Builder().setColumnName("date_in_ms").setIsNullable(true).build())
.addColumn(new StringColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build());
}

}

+ 82
- 0
sonar-db/src/test/java/org/sonar/db/version/BigDecimalColumnDefTest.java View File

@@ -0,0 +1,82 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

import static org.assertj.core.api.Assertions.assertThat;

public class BigDecimalColumnDefTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void build_string_column_def() throws Exception {
BigDecimalColumnDef def = new BigDecimalColumnDef.Builder()
.setColumnName("issues")
.setIsNullable(true)
.build();

assertThat(def.getName()).isEqualTo("issues");
assertThat(def.isNullable()).isTrue();
}

@Test
public void generate_sql_type() throws Exception {
BigDecimalColumnDef def = new BigDecimalColumnDef.Builder()
.setColumnName("issues")
.setIsNullable(true)
.build();

assertThat(def.generateSqlType(new H2())).isEqualTo("BIGINT");
assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("BIGINT");
assertThat(def.generateSqlType(new MsSql())).isEqualTo("BIGINT");
assertThat(def.generateSqlType(new MySql())).isEqualTo("BIGINT");
assertThat(def.generateSqlType(new Oracle())).isEqualTo("NUMBER (38)");
}

@Test
public void fail_with_NPE_if_name_is_null() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("Column name cannot be null");

new BigDecimalColumnDef.Builder()
.setColumnName(null);
}

@Test
public void fail_with_NPE_if_no_name() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("Column name cannot be null");

new BigDecimalColumnDef.Builder()
.build();
}

}

+ 0
- 92
sonar-db/src/test/java/org/sonar/db/version/ColumnDefTest.java View File

@@ -1,92 +0,0 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.version.ColumnDef.Type.BIG_INTEGER;
import static org.sonar.db.version.ColumnDef.Type.STRING;

public class ColumnDefTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void create_column_def() throws Exception {
ColumnDef def = new ColumnDef()
.setName("date_in_ms")
.setType(STRING)
.setLimit(10)
.setNullable(true);

assertThat(def.getName()).isEqualTo("date_in_ms");
assertThat(def.getType()).isEqualTo(STRING);
assertThat(def.getLimit()).isEqualTo(10);
assertThat(def.isNullable()).isTrue();
}

@Test
public void convert_varchar_type_to_sql() throws Exception {
assertThat(new ColumnDef().setType(STRING).getSqlType(new H2())).isEqualTo("VARCHAR");
assertThat(new ColumnDef().setType(STRING).getSqlType(new Oracle())).isEqualTo("VARCHAR");
assertThat(new ColumnDef().setType(STRING).getSqlType(new MsSql())).isEqualTo("VARCHAR");
assertThat(new ColumnDef().setType(STRING).getSqlType(new MySql())).isEqualTo("VARCHAR");
}

@Test
public void convert_big_integer_type_to_sql() throws Exception {
assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new H2())).isEqualTo("BIGINT");
assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new Oracle())).isEqualTo("NUMBER (38)");
assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new MsSql())).isEqualTo("BIGINT");
assertThat(new ColumnDef().setType(BIG_INTEGER).getSqlType(new MySql())).isEqualTo("BIGINT");
}


@Test
public void fail_when_column_name_is_in_upper_case() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Column name should only contains lowercase and _ characters");

new ColumnDef()
.setName("DATE_IN_MS")
.setType(BIG_INTEGER)
.setNullable(true);
}

@Test
public void fail_when_column_name_contains_invalid_character() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Column name should only contains lowercase and _ characters");

new ColumnDef()
.setName("date-in/ms")
.setType(BIG_INTEGER)
.setNullable(true);
}
}

+ 63
- 0
sonar-db/src/test/java/org/sonar/db/version/ColumnDefValidationTest.java View File

@@ -0,0 +1,63 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.sonar.db.version.ColumnDefValidation.validateColumnName;

public class ColumnDefValidationTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void accept_valid_table_name() throws Exception {
validateColumnName("date_in_ms");
}

@Test
public void fail_with_NPE_if_name_is_null() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("Column name cannot be null");

validateColumnName(null);
}

@Test
public void fail_when_column_name_is_in_upper_case() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Column name should only contains lowercase and _ characters, got 'DATE_IN_MS'");

validateColumnName("DATE_IN_MS");
}

@Test
public void fail_when_column_name_contains_invalid_character() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Column name should only contains lowercase and _ characters, got 'date-in/ms'");

validateColumnName("date-in/ms");
}

}

+ 94
- 0
sonar-db/src/test/java/org/sonar/db/version/StringColumnDefTest.java View File

@@ -0,0 +1,94 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.db.version;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

import static org.assertj.core.api.Assertions.assertThat;

public class StringColumnDefTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void build_string_column_def() throws Exception {
StringColumnDef def = new StringColumnDef.Builder()
.setColumnName("issues")
.setLimit(10)
.setIsNullable(true)
.build();

assertThat(def.getName()).isEqualTo("issues");
assertThat(def.getColumnSize()).isEqualTo(10);
assertThat(def.isNullable()).isTrue();
}

@Test
public void generate_sql_type() throws Exception {
StringColumnDef def = new StringColumnDef.Builder()
.setColumnName("issues")
.setLimit(10)
.setIsNullable(true)
.build();

assertThat(def.generateSqlType(new H2())).isEqualTo("VARCHAR (10)");
assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("VARCHAR (10)");
assertThat(def.generateSqlType(new MySql())).isEqualTo("VARCHAR (10)");
assertThat(def.generateSqlType(new MsSql())).isEqualTo("VARCHAR (10)");
assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR (10)");
}

@Test
public void fail_with_NPE_if_name_is_null() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("Column name cannot be null");

new StringColumnDef.Builder()
.setColumnName(null);
}

@Test
public void fail_with_NPE_if_no_name() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("Column name cannot be null");

new StringColumnDef.Builder()
.build();
}

@Test
public void fail_with_NPE_if_size_is_null() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("Limit cannot be null");

new StringColumnDef.Builder()
.setColumnName("issues")
.build();
}
}

Loading…
Cancel
Save