From: Julien Lancelot Date: Thu, 10 Apr 2014 16:35:28 +0000 (+0200) Subject: Add unit test X-Git-Tag: 4.3~55 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=59b162e060a03bf75b59bede08eeaa2ac3645d10;p=sonarqube.git Add unit test --- diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/MassUpdater.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/MassUpdater.java index 0a603ab3a4a..8ebb52612f6 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/MassUpdater.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/MassUpdater.java @@ -37,7 +37,6 @@ import java.sql.*; public class MassUpdater { private static final Logger LOGGER = LoggerFactory.getLogger(MassUpdater.class); - private static final String FAILURE_MESSAGE = "Fail to migrate data"; private static final int GROUP_SIZE = 1000; private final Database db; @@ -104,14 +103,10 @@ public class MassUpdater { } } catch (SQLException e) { - LOGGER.error(FAILURE_MESSAGE, e); SqlUtil.log(LOGGER, e); - throw MessageException.of(FAILURE_MESSAGE); - + throw processError(e); } catch (Exception e) { - LOGGER.error(FAILURE_MESSAGE, e); - throw MessageException.of(FAILURE_MESSAGE); - + throw processError(e); } finally { DbUtils.closeQuietly(writeStatement); DbUtils.closeQuietly(writeConnection); @@ -121,6 +116,12 @@ public class MassUpdater { } } + private static MessageException processError(Exception e) { + String message = String.format("Fail to migrate data, error is : %s", e.getMessage()); + LOGGER.error(message, e); + throw MessageException.of(message); + } + @VisibleForTesting static String convertSelectSql(String selectSql, Database db){ String newSelectSql = selectSql; diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/MassUpdaterTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/MassUpdaterTest.java index 208c0d50687..f5aaeb81aed 100644 --- a/sonar-server/src/test/java/org/sonar/server/db/migrations/MassUpdaterTest.java +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/MassUpdaterTest.java @@ -20,25 +20,146 @@ package org.sonar.server.db.migrations; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.api.utils.MessageException; import org.sonar.core.persistence.Database; +import org.sonar.core.persistence.TestDatabase; import org.sonar.core.persistence.dialect.Dialect; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class MassUpdaterTest { - @Mock - Database db; + @ClassRule + public static TestDatabase db = new TestDatabase().schema(MassUpdaterTest.class, "schema.sql"); + + static class Row { + private Long id; + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "migrate_data.xml"); + + new MassUpdater(db.database()).execute( + new MassUpdater.InputLoader() { + @Override + public String selectSql() { + return "SELECT i.id FROM issues i"; + } + + @Override + public Row load(ResultSet rs) throws SQLException { + Row row = new Row(); + row.id = SqlUtil.getLong(rs, 1); + return row; + } + }, + new MassUpdater.InputConverter() { + @Override + public String updateSql() { + return "UPDATE issues SET severity=? WHERE id=?"; + } + + @Override + public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { + updateStatement.setString(1, "MAJOR"); + updateStatement.setLong(2, row.id); + return true; + } + } + ); + + db.assertDbUnit(getClass(), "migrate_data_result.xml", "issues"); + } + + @Test + public void fail_on_bad_sql_request() throws Exception { + db.prepareDbUnit(getClass(), "migrate_data.xml"); + + try { + new MassUpdater(db.database()).execute( + new MassUpdater.InputLoader() { + @Override + public String selectSql() { + return ""; + } + + @Override + public Row load(ResultSet rs) throws SQLException { + return new Row(); + } + }, + new MassUpdater.InputConverter() { + @Override + public String updateSql() { + return ""; + } + + @Override + public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { + return true; + } + } + ); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(MessageException.class); + } + } + + @Test + public void fail_on_unknown_error() throws Exception { + db.prepareDbUnit(getClass(), "migrate_data.xml"); + + try { + new MassUpdater(db.database()).execute( + new MassUpdater.InputLoader() { + @Override + public String selectSql() { + return "SELECT i.id FROM issues i"; + } + + @Override + public Row load(ResultSet rs) throws SQLException { + Row row = new Row(); + row.id = SqlUtil.getLong(rs, 1); + return row; + } + }, + new MassUpdater.InputConverter() { + @Override + public String updateSql() { + throw new RuntimeException("Unknown error"); + } + + @Override + public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { + return true; + } + } + ); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(MessageException.class); + } + } @Test public void convert_select_sql() throws Exception { + Database db = mock(Database.class); + Dialect dialect = mock(Dialect.class); when(dialect.getTrueSqlValue()).thenReturn("true"); when(dialect.getFalseSqlValue()).thenReturn("false"); diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data.xml new file mode 100644 index 00000000000..5eb657bf6a2 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data_result.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data_result.xml new file mode 100644 index 00000000000..ec1ac69511f --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data_result.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/schema.sql new file mode 100644 index 00000000000..f3f71cfa229 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/schema.sql @@ -0,0 +1,28 @@ +-- 4.3 + +CREATE TABLE "ISSUES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "KEE" VARCHAR(50) UNIQUE NOT NULL, + "COMPONENT_ID" INTEGER NOT NULL, + "ROOT_COMPONENT_ID" INTEGER, + "RULE_ID" INTEGER, + "SEVERITY" VARCHAR(10), + "MANUAL_SEVERITY" BOOLEAN NOT NULL, + "MESSAGE" VARCHAR(4000), + "LINE" INTEGER, + "EFFORT_TO_FIX" DOUBLE, + "STATUS" VARCHAR(20), + "RESOLUTION" VARCHAR(20), + "CHECKSUM" VARCHAR(1000), + "REPORTER" VARCHAR(40), + "ASSIGNEE" VARCHAR(40), + "AUTHOR_LOGIN" VARCHAR(100), + "ACTION_PLAN_KEY" VARCHAR(50) NULL, + "ISSUE_ATTRIBUTES" VARCHAR(4000), + "ISSUE_CREATION_DATE" TIMESTAMP, + "ISSUE_CLOSE_DATE" TIMESTAMP, + "ISSUE_UPDATE_DATE" TIMESTAMP, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, + "TECHNICAL_DEBT" INTEGER +);