From 9e1ac06ecd2f9998f5126401e4ee3d6af205e004 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 17 Mar 2015 11:27:18 +0100 Subject: Merge multiple issues column requests alter into one request --- .../org/sonar/core/persistence/rows-h2.sql | 2 +- .../java/org/sonar/core/persistence/DbTester.java | 49 ++++++++++++++++++---- 2 files changed, 43 insertions(+), 8 deletions(-) (limited to 'sonar-core') diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index a410243e678..5a090866dec 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -300,7 +300,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('769'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('770'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('771'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('772'); -INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('773'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('774'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('775'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('776'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('777'); diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java b/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java index 48470d92f36..9f827de7751 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java @@ -50,24 +50,23 @@ import org.sonar.core.cluster.NullQueue; import org.sonar.core.config.Logback; import org.sonar.core.persistence.dialect.Dialect; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; -import java.sql.Clob; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; +import java.sql.*; import java.util.List; import java.util.Map; import java.util.Properties; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; /** @@ -230,7 +229,7 @@ public class DbTester extends ExternalResource { for (int i = 1; i <= colCount; i++) { Object value = resultSet.getObject(i); if (value instanceof Clob) { - value = IOUtils.toString(((Clob)value).getAsciiStream()); + value = IOUtils.toString(((Clob) value).getAsciiStream()); } columns.put(metaData.getColumnLabel(i), value); } @@ -311,6 +310,42 @@ public class DbTester extends ExternalResource { } } + public void assertColumnDefinition(String table, String column, int expectedType, @Nullable Integer expectedSize) { + try (Connection connection = openConnection(); + PreparedStatement stmt = connection.prepareStatement("select * from " + table); + ResultSet res = stmt.executeQuery()) { + Integer columnIndex = getColumnIndex(res, column); + if (columnIndex == null) { + fail("The column '" + column + "' does not exist"); + } + + assertThat(res.getMetaData().getColumnType(columnIndex)).isEqualTo(expectedType); + if (expectedSize != null) { + assertThat(res.getMetaData().getColumnDisplaySize(columnIndex)).isEqualTo(expectedSize); + } + + } catch (Exception e) { + throw new IllegalStateException("Fail to check column"); + } + } + + @CheckForNull + private Integer getColumnIndex(ResultSet res, String column) { + try { + ResultSetMetaData meta = res.getMetaData(); + int numCol = meta.getColumnCount(); + for (int i = 1; i < numCol + 1; i++) { + if (meta.getColumnLabel(i).toLowerCase().equals(column.toLowerCase())) { + return i; + } + } + return null; + + } catch (Exception e) { + throw new IllegalStateException("Fail to get column idnex"); + } + } + private IDataSet dbUnitDataSet(InputStream stream) { try { ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream)); -- cgit v1.2.3