aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-09-13 11:32:31 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-09-16 12:22:11 +0200
commit83d9b2868824c5b088f969d3acba81c84913ff9a (patch)
treef102812c5d4e1e349c9e32ebdf3c3c4db4ede39e /sonar-db
parentec3da287e305f793d28969f2dfda201de4dac00f (diff)
downloadsonarqube-83d9b2868824c5b088f969d3acba81c84913ff9a.tar.gz
sonarqube-83d9b2868824c5b088f969d3acba81c84913ff9a.zip
SONAR-8025 add BlobColumnDef
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java82
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/BlobColumnDefTest.java113
2 files changed, 195 insertions, 0 deletions
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
new file mode 100644
index 00000000000..c049be66cd9
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
@@ -0,0 +1,82 @@
+/*
+ * 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;
+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.ColumnDefValidation.validateColumnName;
+
+public class BlobColumnDef extends AbstractColumnDef {
+ public BlobColumnDef(Builder builder) {
+ super(builder.columnName, builder.isNullable);
+ }
+
+ @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/test/java/org/sonar/db/version/BlobColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/BlobColumnDefTest.java
new file mode 100644
index 00000000000..94b6419b985
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/BlobColumnDefTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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 should only contains lowercase and _ characters, 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);
+ }
+}