diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-17 10:38:17 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-17 12:38:17 +0200 |
commit | bdcfb7eeb7caaeb9019ff0d3946153c33bc6c5e9 (patch) | |
tree | 8e0bc944f83d81a003061543f9db9139966ba985 /sonar-db/src/test/java | |
parent | 48697451f2b0ba517c7d1861af3897b4ace896f9 (diff) | |
download | sonarqube-bdcfb7eeb7caaeb9019ff0d3946153c33bc6c5e9.tar.gz sonarqube-bdcfb7eeb7caaeb9019ff0d3946153c33bc6c5e9.zip |
Move Java db migrations from sonar-server to sonar-db
Diffstat (limited to 'sonar-db/src/test/java')
40 files changed, 3594 insertions, 0 deletions
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 new file mode 100644 index 00000000000..e5160243c7f --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/AddColumnsBuilderTest.java @@ -0,0 +1,136 @@ +/* + * 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.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.AddColumnsBuilder; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; + +public class AddColumnsBuilderTest { + + @Test + public void add_columns_on_h2() { + assertThat(new AddColumnsBuilder(new H2(), "issues") + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("date_in_ms") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true)) + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("name") + .setType(AddColumnsBuilder.ColumnDef.Type.STRING) + .setNullable(false) + .setLimit(10)) + .build()).isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)"); + } + + @Test + public void add_columns_on_mysql() { + assertThat(new AddColumnsBuilder(new MySql(), "issues") + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("date_in_ms") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true)) + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("name") + .setType(AddColumnsBuilder.ColumnDef.Type.STRING) + .setNullable(false) + .setLimit(10)) + .build()).isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL)"); + } + + @Test + public void add_columns_on_oracle() { + assertThat(new AddColumnsBuilder(new Oracle(), "issues") + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("date_in_ms") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true)) + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("name") + .setType(AddColumnsBuilder.ColumnDef.Type.STRING) + .setNullable(false) + .setLimit(10)) + .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(new AddColumnsBuilder(new PostgreSql(), "issues") + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("date_in_ms") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true)) + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("name") + .setType(AddColumnsBuilder.ColumnDef.Type.STRING) + .setNullable(false) + .setLimit(10)) + .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(new AddColumnsBuilder(new MsSql(), "issues") + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("date_in_ms") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true)) + .addColumn(new AddColumnsBuilder.ColumnDef() + .setName("name") + .setType(AddColumnsBuilder.ColumnDef.Type.STRING) + .setNullable(false) + .setLimit(10)) + .build()).isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL"); + } + + @Test + public void fail_when_column_name_is_in_upper_case() { + try { + new AddColumnsBuilder.ColumnDef() + .setName("DATE_IN_MS") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true); + failBecauseExceptionWasNotThrown(IllegalArgumentException.class); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Column name should only contains lowercase and _ characters"); + } + } + + @Test + public void fail_when_column_name_contains_invalid_character() { + try { + new AddColumnsBuilder.ColumnDef() + .setName("date-in/ms") + .setType(AddColumnsBuilder.ColumnDef.Type.BIG_INTEGER) + .setNullable(true); + failBecauseExceptionWasNotThrown(IllegalArgumentException.class); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Column name should only contains lowercase and _ characters"); + } + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/BaseDataChangeTest.java b/sonar-db/src/test/java/org/sonar/db/version/BaseDataChangeTest.java new file mode 100644 index 00000000000..56a5ed91799 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/BaseDataChangeTest.java @@ -0,0 +1,481 @@ +/* + * 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 java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.BatchSession; +import org.sonar.db.DbTester; +import org.sonar.db.version.Select.Row; +import org.sonar.db.version.Select.RowReader; +import org.sonar.test.DbTests; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +@Category(DbTests.class) +public class BaseDataChangeTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, BaseDataChangeTest.class, "schema.sql"); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table persons"); + } + + @Test + public void query() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + final AtomicBoolean executed = new AtomicBoolean(false); + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + assertThat(context.prepareSelect("select id from persons order by id desc").list(Select.LONG_READER)) + .containsExactly(3L, 2L, 1L); + assertThat(context.prepareSelect("select id from persons where id=?").setLong(1, 2L).get(Select.LONG_READER)) + .isEqualTo(2L); + assertThat(context.prepareSelect("select id from persons where id=?").setLong(1, 12345L).get(Select.LONG_READER)) + .isNull(); + executed.set(true); + } + }.execute(); + assertThat(executed.get()).isTrue(); + } + + @Test + public void read_column_types() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + final List<Object[]> persons = new ArrayList<>(); + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + persons.addAll(context + .prepareSelect("select id,login,age,enabled,updated_at,coeff from persons where id=2") + .list(new UserReader())); + } + }.execute(); + assertThat(persons).hasSize(1); + assertThat(persons.get(0)[0]).isEqualTo(2L); + assertThat(persons.get(0)[1]).isEqualTo("emmerik"); + assertThat(persons.get(0)[2]).isEqualTo(14); + assertThat(persons.get(0)[3]).isEqualTo(true); + assertThat(persons.get(0)[4]).isNotNull(); + assertThat(persons.get(0)[5]).isEqualTo(5.2); + } + + @Test + public void parameterized_query() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + final List<Long> ids = new ArrayList<>(); + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + ids.addAll(context.prepareSelect("select id from persons where id>=?").setLong(1, 2L).list(Select.LONG_READER)); + } + }.execute(); + assertThat(ids).containsOnly(2L, 3L); + } + + @Test + public void display_current_row_details_if_error_during_get() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Error during processing of row: [id=2]"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + context.prepareSelect("select id from persons where id>=?").setLong(1, 2L).get(new RowReader<Long>() { + @Override + public Long read(Row row) throws SQLException { + throw new IllegalStateException("Unexpected error"); + } + }); + } + }.execute(); + + } + + @Test + public void display_current_row_details_if_error_during_list() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Error during processing of row: [id=2]"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + context.prepareSelect("select id from persons where id>=?").setLong(1, 2L).list(new RowReader<Long>() { + @Override + public Long read(Row row) throws SQLException { + throw new IllegalStateException("Unexpected error"); + } + }); + } + }.execute(); + + } + + @Test + public void bad_parameterized_query() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + final List<Long> ids = new ArrayList<>(); + BaseDataChange change = new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + // parameter value is not set + ids.addAll(context.prepareSelect("select id from persons where id>=?").list(Select.LONG_READER)); + } + }; + + thrown.expect(SQLException.class); + + change.execute(); + } + + @Test + public void scroll() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + final List<Long> ids = new ArrayList<>(); + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + context.prepareSelect("select id from persons order by id desc").scroll(new Select.RowHandler() { + @Override + public void handle(Select.Row row) throws SQLException { + ids.add(row.getNullableLong(1)); + } + }); + } + }.execute(); + assertThat(ids).containsExactly(3L, 2L, 1L); + } + + @Test + public void insert() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + context.prepareUpsert("insert into persons(id,login,age,enabled,coeff) values (?,?,?,?,?)") + .setLong(1, 10L) + .setString(2, "kurt") + .setInt(3, 27) + .setBoolean(4, true) + .setDouble(5, 2.2) + .execute().commit().close(); + } + }.execute(); + + db.assertDbUnit(getClass(), "insert-result.xml", "persons"); + } + + @Test + public void batch_insert() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + Upsert upsert = context.prepareUpsert("insert into persons(id,login,age,enabled,coeff) values (?,?,?,?,?)"); + upsert + .setLong(1, 10L) + .setString(2, "kurt") + .setInt(3, 27) + .setBoolean(4, true) + .setDouble(5, 2.2) + .addBatch(); + upsert + .setLong(1, 11L) + .setString(2, "courtney") + .setInt(3, 25) + .setBoolean(4, false) + .setDouble(5, 2.3) + .addBatch(); + upsert.execute().commit().close(); + } + }.execute(); + + db.assertDbUnit(getClass(), "batch-insert-result.xml", "persons"); + } + + @Test + public void update_null() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + Upsert upsert = context.prepareUpsert("update persons set login=?,age=?,enabled=?, updated_at=?, coeff=? where id=?"); + upsert + .setString(1, null) + .setInt(2, null) + .setBoolean(3, null) + .setDate(4, null) + .setDouble(5, null) + .setLong(6, 2L) + .execute() + .commit() + .close(); + } + }.execute(); + + db.assertDbUnit(getClass(), "update-null-result.xml", "persons"); + } + + @Test + public void mass_batch_insert() throws Exception { + db.executeUpdateSql("truncate table persons"); + + final int count = BatchSession.MAX_BATCH_SIZE + 10; + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + Upsert upsert = context.prepareUpsert("insert into persons(id,login,age,enabled,coeff) values (?,?,?,?,?)"); + for (int i = 0; i < count; i++) { + upsert + .setLong(1, 10L + i) + .setString(2, "login" + i) + .setInt(3, 10 + i) + .setBoolean(4, true) + .setDouble(4, i + 0.5) + .addBatch(); + } + upsert.execute().commit().close(); + + } + }.execute(); + + assertThat(db.countRowsOfTable("persons")).isEqualTo(count); + } + + @Test + public void scroll_and_update() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + final Upsert upsert = context.prepareUpsert("update persons set login=?, age=? where id=?"); + context.prepareSelect("select id from persons").scroll(new Select.RowHandler() { + @Override + public void handle(Select.Row row) throws SQLException { + long id = row.getNullableLong(1); + upsert.setString(1, "login" + id).setInt(2, 10 + (int) id).setLong(3, id); + upsert.execute(); + } + }); + upsert.commit().close(); + } + }.execute(); + + db.assertDbUnit(getClass(), "scroll-and-update-result.xml", "persons"); + } + + @Test + public void display_current_row_details_if_error_during_scroll() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Error during processing of row: [id=1]"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + final Upsert upsert = context.prepareUpsert("update persons set login=?, age=? where id=?"); + context.prepareSelect("select id from persons").scroll(new Select.RowHandler() { + @Override + public void handle(Select.Row row) throws SQLException { + throw new IllegalStateException("Unexpected error"); + } + }); + upsert.commit().close(); + } + }.execute(); + } + + @Test + public void mass_update() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from persons where id>=?").setLong(1, 2L); + massUpdate.update("update persons set login=?, age=? where id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + long id = row.getNullableLong(1); + update + .setString(1, "login" + id) + .setInt(2, 10 + (int) id) + .setLong(3, id); + return true; + } + }); + } + }.execute(); + + db.assertDbUnit(getClass(), "mass-update-result.xml", "persons"); + } + + @Test + public void display_current_row_details_if_error_during_mass_update() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Error during processing of row: [id=2]"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from persons where id>=?").setLong(1, 2L); + massUpdate.update("update persons set login=?, age=? where id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + throw new IllegalStateException("Unexpected error"); + } + }); + } + }.execute(); + } + + @Test + public void mass_update_nothing() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from persons where id>=?").setLong(1, 2L); + massUpdate.update("update persons set login=?, age=? where id=?"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + return false; + } + }); + } + }.execute(); + + db.assertDbUnit(getClass(), "persons.xml", "persons"); + } + + @Test + public void bad_mass_update() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + BaseDataChange change = new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from persons where id>=?").setLong(1, 2L); + // update is not set + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + return false; + } + }); + } + }; + try { + change.execute(); + fail(); + } catch (IllegalStateException e) { + assertThat(e).hasMessage("SELECT or UPDATE requests are not defined"); + } + } + + @Test + public void read_not_null_fields() throws Exception { + db.prepareDbUnit(getClass(), "persons.xml"); + + final List<Object[]> persons = new ArrayList<>(); + new BaseDataChange(db.database()) { + @Override + public void execute(Context context) throws SQLException { + persons.addAll(context + .prepareSelect("select id,login,age,enabled,updated_at,coeff from persons where id=2") + .list(new Select.RowReader<Object[]>() { + @Override + public Object[] read(Select.Row row) throws SQLException { + return new Object[] { + // id, login, age, enabled + row.getLong(1), + row.getString(2), + row.getInt(3), + row.getBoolean(4), + row.getDate(5), + row.getDouble(6), + }; + } + })); + } + }.execute(); + assertThat(persons).hasSize(1); + assertThat(persons.get(0)[0]).isEqualTo(2L); + assertThat(persons.get(0)[1]).isEqualTo("emmerik"); + assertThat(persons.get(0)[2]).isEqualTo(14); + assertThat(persons.get(0)[3]).isEqualTo(true); + assertThat(persons.get(0)[4]).isNotNull(); + assertThat(persons.get(0)[5]).isEqualTo(5.2); + } + + static class UserReader implements Select.RowReader<Object[]> { + @Override + public Object[] read(Select.Row row) throws SQLException { + return new Object[] { + // id, login, age, enabled + row.getNullableLong(1), + row.getNullableString(2), + row.getNullableInt(3), + row.getNullableBoolean(4), + row.getNullableDate(5), + row.getNullableDouble(6), + }; + } + } +} 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 new file mode 100644 index 00000000000..e7c6955f185 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/DropColumnsBuilderTest.java @@ -0,0 +1,64 @@ +/* + * 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.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/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java new file mode 100644 index 00000000000..6c3fb4cba25 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -0,0 +1,35 @@ +/* + * 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.Test; +import org.sonar.core.platform.ComponentContainer; +import org.sonar.db.version.MigrationStepModule; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MigrationStepModuleTest { + @Test + public void verify_count_of_added_MigrationStep_types() { + ComponentContainer container = new ComponentContainer(); + new MigrationStepModule().configure(container); + assertThat(container.size()).isEqualTo(38); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v451/AddMissingCustomRuleParametersMigrationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v451/AddMissingCustomRuleParametersMigrationTest.java new file mode 100644 index 00000000000..eb291dda5c9 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v451/AddMissingCustomRuleParametersMigrationTest.java @@ -0,0 +1,76 @@ +/* + * 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.v451; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AddMissingCustomRuleParametersMigrationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddMissingCustomRuleParametersMigrationTest.class, "schema.sql"); + + MigrationStep migration; + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table rules"); + db.executeUpdateSql("truncate table rules_parameters"); + migration = new AddMissingCustomRuleParametersMigrationStep(db.getDbClient(), system); + when(system.now()).thenReturn(DateUtils.parseDate("2014-10-09").getTime()); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "execute.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "execute-result.xml", "rules", "rules_parameters"); + } + + @Test + public void execute_when_custom_rule_have_no_parameter() throws Exception { + db.prepareDbUnit(getClass(), "execute_when_custom_rule_have_no_parameter.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "execute_when_custom_rule_have_no_parameter-result.xml", "rules", "rules_parameters"); + } + + @Test + public void no_changes() throws Exception { + db.prepareDbUnit(getClass(), "no_changes.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "no_changes.xml", "rules", "rules_parameters"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v451/DeleteUnescapedActivitiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v451/DeleteUnescapedActivitiesTest.java new file mode 100644 index 00000000000..00808bb8f0a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v451/DeleteUnescapedActivitiesTest.java @@ -0,0 +1,60 @@ +/* + * 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.v451; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DeleteUnescapedActivitiesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteUnescapedActivitiesTest.class, "schema.sql"); + + MigrationStep migration; + + @Test + public void execute() throws Exception { + migration = new DeleteUnescapedActivities(db.database()); + db.prepareDbUnit(getClass(), "execute.xml"); + migration.execute(); + db.assertDbUnit(getClass(), "execute-result.xml", "activities"); + } + + @Test + public void is_unescaped() { + assertThat(DeleteUnescapedActivities.isUnescaped( + "ruleKey=findbugs:PT_RELATIVE_PATH_TRAVERSAL;profileKey=java-findbugs-74105;severity=MAJOR;" + + "key=java-findbugs-74105:findbugs:PT_RELATIVE_PATH_TRAVERSAL")) + .isFalse(); + assertThat(DeleteUnescapedActivities.isUnescaped(null)).isFalse(); + assertThat(DeleteUnescapedActivities.isUnescaped("")).isFalse(); + assertThat(DeleteUnescapedActivities.isUnescaped("foo=bar")).isFalse(); + assertThat(DeleteUnescapedActivities.isUnescaped("param_xpath=/foo/bar")).isFalse(); + + assertThat(DeleteUnescapedActivities.isUnescaped("param_xpath=/foo/bar;foo;ruleKey=S001")).isTrue(); + assertThat(DeleteUnescapedActivities.isUnescaped("param_xpath=/foo=foo;ruleKey=S001")).isTrue(); + + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java new file mode 100644 index 00000000000..1ac8362180a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java @@ -0,0 +1,319 @@ +/* + * 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.v50; + +import java.nio.charset.StandardCharsets; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.util.List; +import java.util.Map; +import org.apache.commons.dbutils.DbUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedFileSourcesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedFileSourcesTest.class, "schema.sql"); + + private static final long NOW = 1414770242000L; + + FeedFileSources migration; + + System2 system; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table metrics"); + db.executeUpdateSql("truncate table snapshots"); + db.executeUpdateSql("truncate table snapshot_sources"); + db.executeUpdateSql("truncate table projects"); + db.executeUpdateSql("truncate table project_measures"); + db.executeUpdateSql("truncate table file_sources"); + + system = mock(System2.class); + when(system.now()).thenReturn(NOW); + migration = new FeedFileSources(db.database(), system); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate_sources_with_no_scm_no_coverage() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + db.executeUpdateSql("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')"); + + db.executeUpdateSql("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(7, '', '2014-10-31 16:44:02.000')"); + + migration.execute(); + + List<Map<String, Object>> results = db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " + + "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources"); + assertThat(results).hasSize(2); + + assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject"); + assertThat(results.get(0).get("fileUuid")).isEqualTo("uuid-Migrated.xoo"); + assertThat(results.get(0).get("data")).isEqualTo(""); + assertThat(results.get(0).get("lineHashes")).isEqualTo(""); + assertThat(results.get(0).get("dataHash")).isEqualTo(""); + assertThat(results.get(0).get("updatedAt")).isEqualTo(NOW); + assertThat(results.get(0).get("createdAt")).isEqualTo(1416238020000L); + + assertThat(results.get(1).get("projectUuid")).isEqualTo("uuid-MyProject"); + assertThat(results.get(1).get("fileUuid")).isEqualTo("uuid-MyFile.xoo"); + assertThat(results.get(1).get("data")).isEqualTo(",,,,,,,,,,,,,,,class Foo {\r\n,,,,,,,,,,,,,,, // Empty\r\n,,,,,,,,,,,,,,,}\r\n,,,,,,,,,,,,,,,\r\n"); + assertThat(results.get(1).get("lineHashes")).isEqualTo("6a19ce786467960a3a9b0d26383a464a\naab2dbc5fdeaa80b050b1d049ede357c\ncbb184dd8e05c9709e5dcaedaa0495cf\n\n"); + assertThat(results.get(1).get("dataHash")).isEqualTo(""); + assertThat(formatLongDate((long) results.get(1).get("updatedAt")).toString()).startsWith("2014-10-31"); + assertThat(results.get(1).get("createdAt")).isEqualTo(NOW); + } + + @Test + public void migrate_sources_with_scm_and_coverage_in_text_value() throws Exception { + migrate_sources_with_scm_and_coverage_in("text_value"); + } + + @Test + public void migrate_sources_with_scm_and_coverage_in_measure_data() throws Exception { + migrate_sources_with_scm_and_coverage_in("measure_data"); + } + + private void migrate_sources_with_scm_and_coverage_in(String columnName) throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + Connection connection = null; + try { + connection = db.openConnection(); + + connection.prepareStatement("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')") + .executeUpdate(); + + db.executeUpdateSql("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(7, '', '2014-10-31 16:44:02.000')"); + + PreparedStatement revisionStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(1, 6, ?)"); + revisionStmt.setBytes(1, "1=aef12a;2=abe465;3=afb789;4=afb789".getBytes(StandardCharsets.UTF_8)); + revisionStmt.executeUpdate(); + + PreparedStatement authorStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(2, 6, ?)"); + authorStmt.setBytes(1, "1=alice;2=bob;3=carol;4=carol".getBytes(StandardCharsets.UTF_8)); + authorStmt.executeUpdate(); + + PreparedStatement dateStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(3, 6, ?)"); + dateStmt.setBytes(1, "1=2014-04-25T12:34:56+0100;2=2014-07-25T12:34:56+0100;3=2014-03-23T12:34:56+0100;4=2014-03-23T12:34:56+0100".getBytes(StandardCharsets.UTF_8)); + dateStmt.executeUpdate(); + + PreparedStatement utHitsStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(4, 6, ?)"); + utHitsStmt.setBytes(1, "1=1;3=0".getBytes(StandardCharsets.UTF_8)); + utHitsStmt.executeUpdate(); + + PreparedStatement utCondStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(5, 6, ?)"); + utCondStmt.setBytes(1, "1=4".getBytes(StandardCharsets.UTF_8)); + utCondStmt.executeUpdate(); + + PreparedStatement utCoveredCondStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(6, 6, ?)"); + utCoveredCondStmt.setBytes(1, "1=2".getBytes(StandardCharsets.UTF_8)); + utCoveredCondStmt.executeUpdate(); + + PreparedStatement itHitsStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(7, 6, ?)"); + itHitsStmt.setBytes(1, "1=2;3=0".getBytes(StandardCharsets.UTF_8)); + itHitsStmt.executeUpdate(); + + PreparedStatement itCondStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(8, 6, ?)"); + itCondStmt.setBytes(1, "1=5".getBytes(StandardCharsets.UTF_8)); + itCondStmt.executeUpdate(); + + PreparedStatement itCoveredCondStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(9, 6, ?)"); + itCoveredCondStmt.setBytes(1, "1=3".getBytes(StandardCharsets.UTF_8)); + itCoveredCondStmt.executeUpdate(); + + PreparedStatement overallHitsStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(10, 6, ?)"); + overallHitsStmt.setBytes(1, "1=3;3=0".getBytes(StandardCharsets.UTF_8)); + overallHitsStmt.executeUpdate(); + + PreparedStatement overallCondStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(11, 6, ?)"); + overallCondStmt.setBytes(1, "1=6".getBytes(StandardCharsets.UTF_8)); + overallCondStmt.executeUpdate(); + + PreparedStatement overallCoveredCondStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(12, 6, ?)"); + overallCoveredCondStmt.setBytes(1, "1=4".getBytes(StandardCharsets.UTF_8)); + overallCoveredCondStmt.executeUpdate(); + + PreparedStatement duplicationDataStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, " + columnName + ") " + + "values " + + "(13, 6, ?)"); + duplicationDataStmt + .setBytes( + 1, + "<duplications><g><b s=\"1\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"2\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"3\" l=\"1\" r=\"MyProject:src/main/xoo/prj/AnotherFile.xoo\"/></g></duplications>" + .getBytes(StandardCharsets.UTF_8)); + duplicationDataStmt.executeUpdate(); + } finally { + DbUtils.commitAndCloseQuietly(connection); + } + + migration.execute(); + + List<Map<String, Object>> results = db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " + + "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources"); + assertThat(results).hasSize(2); + + assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject"); + assertThat(results.get(0).get("fileUuid")).isEqualTo("uuid-Migrated.xoo"); + assertThat(results.get(0).get("data")).isEqualTo(""); + assertThat(results.get(0).get("lineHashes")).isEqualTo(""); + assertThat(results.get(0).get("dataHash")).isEqualTo(""); + assertThat(results.get(0).get("updatedAt")).isEqualTo(NOW); + assertThat(results.get(0).get("createdAt")).isEqualTo(1416238020000L); + + assertThat(results.get(1).get("projectUuid")).isEqualTo("uuid-MyProject"); + assertThat(results.get(1).get("fileUuid")).isEqualTo("uuid-MyFile.xoo"); + assertThat(results.get(1).get("data")).isEqualTo( + "aef12a,alice,2014-04-25T12:34:56+0100,1,4,2,2,5,3,3,6,4,,,1,class Foo {\r\nabe465,bob,2014-07-25T12:34:56+0100,,,,,,,,,,,,2, " + + "// Empty\r\nafb789,carol,2014-03-23T12:34:56+0100,0,,,0,,,0,,,,,,}\r\nafb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,,\r\n"); + assertThat(results.get(1).get("lineHashes")).isEqualTo("6a19ce786467960a3a9b0d26383a464a\naab2dbc5fdeaa80b050b1d049ede357c\ncbb184dd8e05c9709e5dcaedaa0495cf\n\n"); + assertThat(results.get(1).get("dataHash")).isEqualTo(""); + assertThat(formatLongDate((long) results.get(1).get("updatedAt")).toString()).startsWith("2014-10-31"); + assertThat(results.get(1).get("createdAt")).isEqualTo(NOW); + } + + @Test + public void migrate_sources_with_invalid_duplication() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + Connection connection = null; + try { + connection = db.openConnection(); + + connection.prepareStatement("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')") + .executeUpdate(); + + db.executeUpdateSql("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(7, '', '2014-10-31 16:44:02.000')"); + + PreparedStatement duplicationDataStmt = connection.prepareStatement("insert into project_measures " + + "(metric_id, snapshot_id, text_value) " + + "values " + + "(13, 6, ?)"); + duplicationDataStmt + .setBytes( + 1, + "<duplications><g><b s=\"1\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"2\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"3\" l=\"1\" r=\"MyProject:src/main/xoo/prj/AnotherFile.xoo\"/" + .getBytes(StandardCharsets.UTF_8)); + duplicationDataStmt.executeUpdate(); + } finally { + DbUtils.commitAndCloseQuietly(connection); + } + + migration.execute(); + + // db.assertDbUnit(getClass(), "after-with-invalid-duplication.xml", "file_sources"); + + List<Map<String, Object>> results = db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " + + "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources"); + assertThat(results).hasSize(2); + + assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject"); + assertThat(results.get(0).get("fileUuid")).isEqualTo("uuid-Migrated.xoo"); + assertThat(results.get(0).get("data")).isEqualTo(""); + assertThat(results.get(0).get("lineHashes")).isEqualTo(""); + assertThat(results.get(0).get("dataHash")).isEqualTo(""); + assertThat(results.get(0).get("updatedAt")).isEqualTo(NOW); + assertThat(results.get(0).get("createdAt")).isEqualTo(1416238020000L); + + assertThat(results.get(1).get("projectUuid")).isEqualTo("uuid-MyProject"); + assertThat(results.get(1).get("fileUuid")).isEqualTo("uuid-MyFile.xoo"); + assertThat(results.get(1).get("data")).isEqualTo(",,,,,,,,,,,,,,,class Foo {\r\n,,,,,,,,,,,,,,, // Empty\r\n,,,,,,,,,,,,,,,}\r\n,,,,,,,,,,,,,,,\r\n"); + assertThat(results.get(1).get("lineHashes")).isEqualTo("6a19ce786467960a3a9b0d26383a464a\naab2dbc5fdeaa80b050b1d049ede357c\ncbb184dd8e05c9709e5dcaedaa0495cf\n\n"); + assertThat(results.get(1).get("dataHash")).isEqualTo(""); + assertThat(formatLongDate((long) results.get(1).get("updatedAt")).toString()).startsWith("2014-10-31"); + assertThat(results.get(1).get("createdAt")).isEqualTo(NOW); + } + + private String formatLongDate(long dateInMs) { + return DateUtils.formatDateTime(DateUtils.longToDate(dateInMs)); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/FeedIssueLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/FeedIssueLongDatesTest.java new file mode 100644 index 00000000000..5969a262803 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/FeedIssueLongDatesTest.java @@ -0,0 +1,51 @@ +/* + * 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.v50; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedIssueLongDatesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssueLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + System2 system = mock(System2.class); + when(system.now()).thenReturn(1500000000000L); + MigrationStep migration = new FeedIssueLongDates(db.database(), system); + migration.execute(); + + int count = db.countSql("select count(*) from issues where created_at_ms is not null and updated_at_ms is not null"); + assertThat(count).isEqualTo(3); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/FileSourceDtoTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/FileSourceDtoTest.java new file mode 100644 index 00000000000..ebb137512cd --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/FileSourceDtoTest.java @@ -0,0 +1,35 @@ +/* + * 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.v50; + +import org.junit.Test; +import org.sonar.db.version.v50.FileSourceDto; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FileSourceDtoTest { + + @Test + public void checksumOfBlankLine() { + assertThat(FileSourceDto.lineChecksum("")).isEmpty(); + assertThat(FileSourceDto.lineChecksum(" \r\n")).isEmpty(); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/InsertProjectsAuthorizationUpdatedAtMigrationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/InsertProjectsAuthorizationUpdatedAtMigrationTest.java new file mode 100644 index 00000000000..4226635344a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/InsertProjectsAuthorizationUpdatedAtMigrationTest.java @@ -0,0 +1,57 @@ +/* + * 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.v50; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class InsertProjectsAuthorizationUpdatedAtMigrationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, InsertProjectsAuthorizationUpdatedAtMigrationTest.class, "schema.sql"); + + MigrationStep migration; + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table projects"); + migration = new InsertProjectsAuthorizationUpdatedAtMigrationStep(db.database(), system); + when(system.now()).thenReturn(123456789L); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "after.xml", "projects"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/PopulateProjectsUuidColumnsMigrationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/PopulateProjectsUuidColumnsMigrationTest.java new file mode 100644 index 00000000000..4de215aae6f --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/PopulateProjectsUuidColumnsMigrationTest.java @@ -0,0 +1,324 @@ +/* + * 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.v50; + +import com.google.common.collect.ImmutableSet; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateProjectsUuidColumnsMigrationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, PopulateProjectsUuidColumnsMigrationTest.class, "schema.sql"); + + DbClient dbClient = db.getDbClient(); + + Migration50Mapper mapper; + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table projects"); + db.executeUpdateSql("truncate table snapshots"); + mapper = db.getSession().getMapper(Migration50Mapper.class); + migration = new PopulateProjectsUuidColumnsMigrationStep(dbClient); + } + + @Test + public void migrate_components() throws Exception { + db.prepareDbUnit(getClass(), "migrate_components.xml"); + + migration.execute(); + db.getSession().commit(); + + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + assertThat(root.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isNotNull(); + assertThat(module.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(module.getModuleUuid()).isEqualTo(root.getUuid()); + assertThat(module.getModuleUuidPath()).isEqualTo(root.getUuid()); + + Component subModule = mapper.selectComponentByKey("org.struts:struts-db"); + assertThat(subModule.getUuid()).isNotNull(); + assertThat(subModule.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(subModule.getModuleUuid()).isEqualTo(module.getUuid()); + assertThat(subModule.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid()); + + Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts"); + assertThat(directory.getUuid()).isNotNull(); + assertThat(directory.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(directory.getModuleUuid()).isEqualTo(subModule.getUuid()); + assertThat(directory.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid() + "." + subModule.getUuid()); + + Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(file.getUuid()).isNotNull(); + assertThat(file.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(file.getModuleUuid()).isEqualTo(subModule.getUuid()); + assertThat(file.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid() + "." + subModule.getUuid()); + + // Verify that each generated uuid is unique + assertThat(ImmutableSet.of(root.getUuid(), module.getUuid(), subModule.getUuid(), directory.getUuid(), file.getUuid())).hasSize(5); + } + + @Test + public void not_migrate_already_migrated_components() throws Exception { + db.prepareDbUnit(getClass(), "not_migrate_already_migrated_components.xml"); + + migration.execute(); + db.getSession().commit(); + + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isEqualTo("ABCD"); + assertThat(root.getProjectUuid()).isEqualTo("ABCD"); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isEqualTo("BCDE"); + assertThat(module.getProjectUuid()).isEqualTo("ABCD"); + assertThat(module.getModuleUuid()).isEqualTo("ABCD"); + assertThat(module.getModuleUuidPath()).isEqualTo("ABCD"); + + Component subModule = mapper.selectComponentByKey("org.struts:struts-db"); + assertThat(subModule.getUuid()).isNotNull(); + assertThat(subModule.getProjectUuid()).isEqualTo("ABCD"); + assertThat(subModule.getModuleUuid()).isEqualTo("BCDE"); + assertThat(subModule.getModuleUuidPath()).isEqualTo("ABCD.BCDE"); + + Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts"); + assertThat(directory.getUuid()).isNotNull(); + assertThat(directory.getProjectUuid()).isEqualTo("ABCD"); + assertThat(directory.getModuleUuid()).isEqualTo(subModule.getUuid()); + assertThat(directory.getModuleUuidPath()).isEqualTo("ABCD.BCDE." + subModule.getUuid()); + + Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(file.getUuid()).isNotNull(); + assertThat(file.getProjectUuid()).isEqualTo("ABCD"); + assertThat(file.getModuleUuid()).isEqualTo(subModule.getUuid()); + assertThat(file.getModuleUuidPath()).isEqualTo("ABCD.BCDE." + subModule.getUuid()); + + Component removedFile = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext2.java"); + assertThat(removedFile.getUuid()).isEqualTo("DCBA"); + assertThat(removedFile.getProjectUuid()).isEqualTo("ABCD"); + assertThat(removedFile.getModuleUuid()).isEqualTo("BCDE"); + assertThat(removedFile.getModuleUuidPath()).isEqualTo("ABCD.BCDE"); + } + + @Test + public void migrate_disable_components() throws Exception { + db.prepareDbUnit(getClass(), "migrate_disable_components.xml"); + + migration.execute(); + db.getSession().commit(); + + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isNotNull(); + assertThat(module.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(module.getModuleUuid()).isNull(); + assertThat(module.getModuleUuidPath()).isEmpty(); + + Component subModule = mapper.selectComponentByKey("org.struts:struts-db"); + assertThat(subModule.getUuid()).isNotNull(); + assertThat(subModule.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(subModule.getModuleUuid()).isNull(); + assertThat(subModule.getModuleUuidPath()).isEmpty(); + + Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts"); + assertThat(directory.getUuid()).isNotNull(); + assertThat(directory.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(directory.getModuleUuid()).isNull(); + assertThat(directory.getModuleUuidPath()).isEmpty(); + + Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(file.getUuid()).isNotNull(); + assertThat(file.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(file.getModuleUuid()).isNull(); + assertThat(file.getModuleUuidPath()).isEmpty(); + } + + @Test + public void migrate_provisioned_project() throws Exception { + db.prepareDbUnit(getClass(), "migrate_provisioned_project.xml"); + + migration.execute(); + db.getSession().commit(); + + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + assertThat(root.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + } + + @Test + public void migrate_library() throws Exception { + db.prepareDbUnit(getClass(), "migrate_library.xml"); + + migration.execute(); + db.getSession().commit(); + + Component root = mapper.selectComponentByKey("org.hamcrest:hamcrest-library"); + assertThat(root.getUuid()).isNotNull(); + assertThat(root.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + } + + @Test + public void migrate_view() throws Exception { + db.prepareDbUnit(getClass(), "migrate_view.xml"); + + migration.execute(); + db.getSession().commit(); + + Component view = mapper.selectComponentByKey("view"); + assertThat(view.getUuid()).isNotNull(); + assertThat(view.getProjectUuid()).isEqualTo(view.getUuid()); + assertThat(view.getModuleUuid()).isNull(); + assertThat(view.getModuleUuidPath()).isEmpty(); + + Component subView = mapper.selectComponentByKey("subView"); + assertThat(subView.getUuid()).isNotNull(); + assertThat(subView.getProjectUuid()).isEqualTo(view.getUuid()); + assertThat(subView.getModuleUuid()).isEqualTo(view.getUuid()); + assertThat(subView.getModuleUuidPath()).isEqualTo(view.getUuid()); + + Component techProject = mapper.selectComponentByKey("vieworg.struts:struts"); + assertThat(techProject.getUuid()).isNotNull(); + assertThat(techProject.getProjectUuid()).isEqualTo(view.getUuid()); + assertThat(techProject.getModuleUuid()).isEqualTo(subView.getUuid()); + assertThat(techProject.getModuleUuidPath()).isEqualTo(view.getUuid() + "." + subView.getUuid()); + } + + @Test + public void migrate_developer() throws Exception { + db.prepareDbUnit(getClass(), "migrate_developer.xml"); + + migration.execute(); + db.getSession().commit(); + + Component dev = mapper.selectComponentByKey("DEV:developer@company.net"); + assertThat(dev.getUuid()).isNotNull(); + assertThat(dev.getProjectUuid()).isEqualTo(dev.getUuid()); + assertThat(dev.getModuleUuid()).isNull(); + assertThat(dev.getModuleUuidPath()).isEmpty(); + + Component techDev = mapper.selectComponentByKey("DEV:developer@company.net:org.struts:struts"); + assertThat(techDev.getUuid()).isNotNull(); + assertThat(techDev.getProjectUuid()).isEqualTo(dev.getUuid()); + assertThat(techDev.getModuleUuid()).isEqualTo(dev.getUuid()); + assertThat(techDev.getModuleUuidPath()).isEqualTo(dev.getUuid()); + } + + @Test + public void migrate_components_without_uuid() throws Exception { + db.prepareDbUnit(getClass(), "migrate_components_without_uuid.xml"); + + migration.execute(); + db.getSession().commit(); + + // Root project migrated + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + assertThat(root.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + + // Module with a snapshot having no islast=true + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isNotNull(); + assertThat(module.getProjectUuid()).isEqualTo(module.getUuid()); + assertThat(module.getModuleUuid()).isNull(); + assertThat(module.getModuleUuidPath()).isEmpty(); + + // File linked on a no more existing project + Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(file.getUuid()).isNotNull(); + assertThat(file.getProjectUuid()).isEqualTo(file.getUuid()); + assertThat(file.getModuleUuid()).isNull(); + assertThat(file.getModuleUuidPath()).isEmpty(); + } + + @Test + public void not_fail_when_module_has_no_root_id() throws Exception { + db.prepareDbUnit(getClass(), "not_fail_when_module_has_no_root_id.xml"); + + migration.execute(); + db.getSession().commit(); + + // Root project migrated + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + assertThat(root.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + + // The module without uuid will be migrated as a standalone component + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isNotNull(); + assertThat(module.getProjectUuid()).isEqualTo(module.getUuid()); + assertThat(module.getModuleUuid()).isNull(); + assertThat(module.getModuleUuidPath()).isEmpty(); + } + + @Test + public void not_fail_when_project_has_two_active_snapshots() throws Exception { + db.prepareDbUnit(getClass(), "not_fail_when_project_has_two_active_snapshots.xml"); + + migration.execute(); + db.getSession().commit(); + + // Root project migrated + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + assertThat(root.getProjectUuid()).isEqualTo(root.getUuid()); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isEmpty(); + + // The module linked on second active snapshot should be migrated a standalone component + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isNotNull(); + assertThat(module.getProjectUuid()).isEqualTo(module.getUuid()); + assertThat(module.getModuleUuid()).isNull(); + assertThat(module.getModuleUuidPath()).isEmpty(); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/RemoveSortFieldFromIssueFiltersMigrationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/RemoveSortFieldFromIssueFiltersMigrationTest.java new file mode 100644 index 00000000000..bf96e3af226 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/RemoveSortFieldFromIssueFiltersMigrationTest.java @@ -0,0 +1,56 @@ +/* + * 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.v50; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RemoveSortFieldFromIssueFiltersMigrationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveSortFieldFromIssueFiltersMigrationTest.class, "schema.sql"); + + MigrationStep migration; + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table issue_filters"); + migration = new RemoveSortFieldFromIssueFiltersMigrationStep(db.database(), system); + when(system.now()).thenReturn(DateUtils.parseDate("2014-10-29").getTime()); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "execute.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "execute-result.xml", "issue_filters"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/ReplaceIssueFiltersProjectKeyByUuidTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/ReplaceIssueFiltersProjectKeyByUuidTest.java new file mode 100644 index 00000000000..e51c5fd2b32 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/ReplaceIssueFiltersProjectKeyByUuidTest.java @@ -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.v50; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ReplaceIssueFiltersProjectKeyByUuidTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, ReplaceIssueFiltersProjectKeyByUuidTest.class, "schema.sql"); + + MigrationStep migration; + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table issue_filters"); + migration = new ReplaceIssueFiltersProjectKeyByUuid(db.database(), system); + when(system.now()).thenReturn(DateUtils.parseDate("2014-10-29").getTime()); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "execute.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "execute-result.xml", "issue_filters"); + } + + @Test + public void do_not_execute_if_already_migrated() throws Exception { + db.prepareDbUnit(getClass(), "do_not_execute_if_already_migrated.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "do_not_execute_if_already_migrated-result.xml", "issue_filters"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/AddIssuesColumnsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/AddIssuesColumnsTest.java new file mode 100644 index 00000000000..40683d1ebb4 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/AddIssuesColumnsTest.java @@ -0,0 +1,54 @@ +/* + * 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.v51; + +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class AddIssuesColumnsTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddIssuesColumnsTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + migration = new AddIssuesColumns(db.database()); + } + + @Test + public void update_columns() throws Exception { + migration.execute(); + + db.assertColumnDefinition("issues", "issue_creation_date_ms", Types.BIGINT, null); + db.assertColumnDefinition("issues", "issue_update_date_ms", Types.BIGINT, null); + db.assertColumnDefinition("issues", "issue_close_date_ms", Types.BIGINT, null); + db.assertColumnDefinition("issues", "tags", Types.VARCHAR, 4000); + db.assertColumnDefinition("issues", "component_uuid", Types.VARCHAR, 50); + db.assertColumnDefinition("issues", "project_uuid", Types.VARCHAR, 50); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/AddNewCharacteristicsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/AddNewCharacteristicsTest.java new file mode 100644 index 00000000000..cfc43d2246d --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/AddNewCharacteristicsTest.java @@ -0,0 +1,138 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.MessageException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static junit.framework.TestCase.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AddNewCharacteristicsTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddNewCharacteristicsTest.class, "schema.sql"); + + MigrationStep migration; + + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table characteristics"); + + when(system.now()).thenReturn(DateUtils.parseDate("2015-02-15").getTime()); + + migration = new AddNewCharacteristics(db.database(), system); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(getClass(), "migrate-result.xml", "characteristics"); + } + + @Test + public void do_nothing_when_already_migrated() throws Exception { + db.prepareDbUnit(getClass(), "do_nothing_when_already_migrated.xml"); + migration.execute(); + db.assertDbUnit(getClass(), "do_nothing_when_already_migrated.xml", "characteristics"); + } + + @Test + public void do_nothing_when_no_characteristics() throws Exception { + db.prepareDbUnit(getClass(), "empty.xml"); + migration.execute(); + assertThat(db.countRowsOfTable("characteristics")).isEqualTo(0); + } + + @Test + public void insert_usability_at_the_top_if_security_does_exists() throws Exception { + db.prepareDbUnit(getClass(), "insert_usability_at_the_top_if_security_does_exists.xml"); + migration.execute(); + db.assertDbUnit(getClass(), "insert_usability_at_the_top_if_security_does_exists-result.xml", "characteristics"); + } + + @Test + public void update_usability_order_if_already_exists() throws Exception { + db.prepareDbUnit(getClass(), "update_usability_if_already_exists.xml"); + migration.execute(); + db.assertDbUnit(getClass(), "update_usability_if_already_exists-result.xml", "characteristics"); + } + + @Test + public void fail_if_usability_exists_as_sub_characteristic() { + db.prepareDbUnit(getClass(), "fail_if_usability_exists_as_sub_characteristic.xml"); + + try { + migration.execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(MessageException.class).hasMessage( + "'Usability' must be a characteristic. Please restore your DB backup, start the previous version of SonarQube " + + "and update your SQALE model to fix this issue before trying again to run the migration."); + } + } + + @Test + public void fail_if_compliance_already_exists_as_characteristic() { + db.prepareDbUnit(getClass(), "fail_if_compliance_already_exists_as_characteristic.xml"); + + try { + migration.execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(MessageException.class).hasMessage( + "'Compliance' must be a sub-characteristic. Please restore your DB backup, start the previous version of SonarQube " + + "and update your SQALE model to fix this issue before trying again to run the migration."); + } + } + + @Test + public void fail_if_compliance_already_exists_under_wrong_characteristic() { + db.prepareDbUnit(getClass(), "fail_if_compliance_already_exists_under_wrong_characteristic.xml"); + + try { + migration.execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(MessageException.class).hasMessage( + "'Reusability Compliance' must be defined under 'Reusability'. Please restore your DB backup, start the previous version of SonarQube " + + "and update your SQALE model to fix this issue before trying again to run the migration."); + } + } + + @Test + public void not_fail_if_some_deprecated_requirements_still_exists_in_db() throws Exception { + db.prepareDbUnit(getClass(), "not_fail_if_some_deprecated_requirements_still_exists_in_db.xml"); + migration.execute(); + db.assertDbUnit(getClass(), "not_fail_if_some_deprecated_requirements_still_exists_in_db.xml", "characteristics"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsersTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsersTest.java new file mode 100644 index 00000000000..c0345ad2862 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/CopyScmAccountsFromAuthorsToUsersTest.java @@ -0,0 +1,102 @@ +/* + * 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.v51; + +import java.util.Map; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.user.UserDto; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CopyScmAccountsFromAuthorsToUsersTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, CopyScmAccountsFromAuthorsToUsersTest.class, "schema.sql"); + + MigrationStep migration; + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table authors"); + db.executeUpdateSql("truncate table users"); + migration = new CopyScmAccountsFromAuthorsToUsers(db.database(), system); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + Long oldDate = 1500000000000L; + Long updatedDate = 2000000000000L; + when(system.now()).thenReturn(updatedDate); + + migration.execute(); + + User simon = getUserByLogin("simon"); + assertThat(simon.scmAccounts).isEqualTo(UserDto.SCM_ACCOUNTS_SEPARATOR + "Simon B" + UserDto.SCM_ACCOUNTS_SEPARATOR + "simon@codehaus.org" + UserDto.SCM_ACCOUNTS_SEPARATOR); + assertThat(simon.updatedAt).isEqualTo(updatedDate); + + User fabrice = getUserByLogin("fabrice"); + assertThat(fabrice.scmAccounts).isEqualTo(UserDto.SCM_ACCOUNTS_SEPARATOR + "fab" + UserDto.SCM_ACCOUNTS_SEPARATOR); + assertThat(fabrice.updatedAt).isEqualTo(updatedDate); + + assertThat(getUserByLogin("julien").updatedAt).isEqualTo(oldDate); + assertThat(getUserByLogin("jb").updatedAt).isEqualTo(oldDate); + assertThat(getUserByLogin("disable").updatedAt).isEqualTo(oldDate); + assertThat(getUserByLogin("teryk").updatedAt).isEqualTo(oldDate); + assertThat(getUserByLogin("teryk2").updatedAt).isEqualTo(oldDate); + } + + @Test + public void nothing_to_migrate_when_no_authors() throws Exception { + db.prepareDbUnit(getClass(), "no_authors.xml"); + Long oldDate = 1500000000000L; + Long updatedDate = 2000000000000L; + when(system.now()).thenReturn(updatedDate); + + migration.execute(); + + assertThat(db.countSql("SELECT count(*) FROM USERS WHERE updated_at=" + updatedDate)).isEqualTo(0); + assertThat(db.countSql("SELECT count(*) FROM USERS WHERE updated_at=" + oldDate)).isEqualTo(7); + } + + private User getUserByLogin(String login) { + return new User(db.selectFirst("SELECT u.scm_Accounts as \"scmAccounts\", u.updated_at as \"updatedAt\" FROM users u WHERE u.login='" + login + "'")); + } + + private static class User { + String scmAccounts; + Long updatedAt; + + User(Map<String, Object> map) { + scmAccounts = (String) map.get("scmAccounts"); + updatedAt = (Long) map.get("updatedAt"); + } + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/DropIssuesColumnsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/DropIssuesColumnsTest.java new file mode 100644 index 00000000000..3971e9c1838 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/DropIssuesColumnsTest.java @@ -0,0 +1,52 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.db.Database; +import org.sonar.db.dialect.PostgreSql; +import org.sonar.db.version.v51.DropIssuesColumns; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DropIssuesColumnsTest { + + DropIssuesColumns migration; + + Database database; + + @Before + public void setUp() { + database = mock(Database.class); + migration = new DropIssuesColumns(database); + } + + @Test + public void generate_sql_on_postgresql() { + when(database.getDialect()).thenReturn(new PostgreSql()); + assertThat(migration.generateSql()).isEqualTo( + "ALTER TABLE issues DROP COLUMN issue_creation_date, DROP COLUMN issue_update_date, DROP COLUMN issue_close_date, DROP COLUMN component_id, DROP COLUMN root_component_id" + ); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedAnalysisReportsLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedAnalysisReportsLongDatesTest.java new file mode 100644 index 00000000000..c3e75f1fc75 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedAnalysisReportsLongDatesTest.java @@ -0,0 +1,53 @@ +/* + * 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.v51; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedAnalysisReportsLongDatesTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedAnalysisReportsLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + System2 system = mock(System2.class); + when(system.now()).thenReturn(1500000000000L); + MigrationStep migration = new FeedAnalysisReportsLongDates(db.database(), system); + migration.execute(); + + int count = db.countSql("select count(*) from analysis_reports where created_at_ms is not null and updated_at_ms is not null"); + assertThat(count).isEqualTo(3); + + int countWithAllDateFieldsNull = db + .countSql("select count(*) from analysis_reports where created_at_ms is not null and updated_at_ms is not null and started_at_ms is not null and finished_at_ms is not null"); + assertThat(countWithAllDateFieldsNull).isEqualTo(2); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedEventsLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedEventsLongDatesTest.java new file mode 100644 index 00000000000..96104aee886 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedEventsLongDatesTest.java @@ -0,0 +1,88 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.utils.DateUtils.parseDate; + +public class FeedEventsLongDatesTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedEventsLongDatesTest.class, "schema.sql"); + + @Before + public void before() { + db.prepareDbUnit(getClass(), "before.xml"); + } + + @Test + public void execute() throws Exception { + MigrationStep migration = newMigration(System2.INSTANCE); + + migration.execute(); + + int count = db + .countSql("select count(*) from events where " + + "created_at_ms is not null " + + "and event_date_ms is not null"); + assertThat(count).isEqualTo(3); + } + + @Test + public void take_now_if_date_in_the_future() throws Exception { + System2 system = mock(System2.class); + when(system.now()).thenReturn(1234L); + + MigrationStep migration = newMigration(system); + + migration.execute(); + + int count = db + .countSql("select count(*) from events where " + + "created_at_ms = 1234"); + assertThat(count).isEqualTo(2); + } + + @Test + public void take_date_if_in_the_past() throws Exception { + MigrationStep migration = newMigration(System2.INSTANCE); + + migration.execute(); + + long time = parseDate("2014-09-25").getTime(); + int count = db + .countSql("select count(*) from events where " + + "created_at_ms=" + time); + assertThat(count).isEqualTo(1); + } + + private MigrationStep newMigration(System2 system) { + return new FeedEventsLongDates(db.database(), system); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedFileSourcesBinaryDataTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedFileSourcesBinaryDataTest.java new file mode 100644 index 00000000000..2bf3bc23004 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedFileSourcesBinaryDataTest.java @@ -0,0 +1,97 @@ +/* + * 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.v51; + +import java.io.InputStream; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.apache.commons.dbutils.DbUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.source.FileSourceDto; +import org.sonar.db.version.MigrationStep; +import org.sonar.server.source.db.FileSourceDb; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FeedFileSourcesBinaryDataTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedFileSourcesBinaryDataTest.class, "schema.sql"); + + @Test + public void convert_csv_to_protobuf() throws Exception { + db.prepareDbUnit(getClass(), "data.xml"); + + MigrationStep migration = new FeedFileSourcesBinaryData(db.database()); + migration.execute(); + + int count = db.countSql("select count(*) from file_sources where binary_data is not null"); + assertThat(count).isEqualTo(3); + + try (Connection connection = db.openConnection()) { + FileSourceDb.Data data = selectData(connection, 1L); + assertThat(data.getLinesCount()).isEqualTo(4); + assertThat(data.getLines(0).getScmRevision()).isEqualTo("aef12a"); + + data = selectData(connection, 2L); + assertThat(data.getLinesCount()).isEqualTo(4); + assertThat(data.getLines(0).hasScmRevision()).isFalse(); + + data = selectData(connection, 3L); + assertThat(data.getLinesCount()).isEqualTo(0); + } + } + + @Test + public void fail_to_parse_csv() throws Exception { + db.prepareDbUnit(getClass(), "bad_data.xml"); + + MigrationStep migration = new FeedFileSourcesBinaryData(db.database()); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Error during processing of row: [id=1,data="); + + migration.execute(); + } + + private FileSourceDb.Data selectData(Connection connection, long fileSourceId) throws SQLException { + PreparedStatement pstmt = connection.prepareStatement("select binary_data from file_sources where id=?"); + ResultSet rs = null; + try { + pstmt.setLong(1, fileSourceId); + rs = pstmt.executeQuery(); + rs.next(); + InputStream data = rs.getBinaryStream(1); + return FileSourceDto.decodeSourceData(data); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(pstmt); + } + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueChangesLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueChangesLongDatesTest.java new file mode 100644 index 00000000000..01690ce52df --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueChangesLongDatesTest.java @@ -0,0 +1,53 @@ +/* + * 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.v51; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedIssueChangesLongDatesTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssueChangesLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + System2 system = mock(System2.class); + when(system.now()).thenReturn(1500000000000L); + MigrationStep migration = new FeedIssueChangesLongDates(db.database(), system); + migration.execute(); + + int count = db.countSql("select count(*) from issue_changes where created_at_ms is not null and updated_at_ms is not null"); + assertThat(count).isEqualTo(3); + + int countWithAllDateFieldsNull = db + .countSql("select count(*) from issue_changes where created_at_ms is not null and updated_at_ms is not null and issue_change_creation_date_ms is not null"); + assertThat(countWithAllDateFieldsNull).isEqualTo(2); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueComponentUuidsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueComponentUuidsTest.java new file mode 100644 index 00000000000..de5f1c94c05 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueComponentUuidsTest.java @@ -0,0 +1,53 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +public class FeedIssueComponentUuidsTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssueComponentUuidsTest.class, "schema.sql"); + + FeedIssueComponentUuids underTest; + + @Before + public void setUp() { + db.truncateTables(); + + underTest = new FeedIssueComponentUuids(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + underTest.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "before.xml"); + underTest.execute(); + db.assertDbUnit(this.getClass(), "after-result.xml", "issues"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueTagsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueTagsTest.java new file mode 100644 index 00000000000..1e630545f94 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssueTagsTest.java @@ -0,0 +1,64 @@ +/* + * 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.v51; + +import java.util.Date; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedIssueTagsTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssueTagsTest.class, "schema.sql"); + + FeedIssueTags migration; + + System2 system; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table rules"); + db.executeUpdateSql("truncate table issues"); + + system = mock(System2.class); + Date now = DateUtils.parseDateTime("2014-12-08T17:33:00+0100"); + when(system.now()).thenReturn(now.getTime()); + migration = new FeedIssueTags(db.database(), system); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate_with_rule_tags() throws Exception { + db.prepareDbUnit(this.getClass(), "before.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "after-result.xml", "issues"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssuesLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssuesLongDatesTest.java new file mode 100644 index 00000000000..b10df31cb5d --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedIssuesLongDatesTest.java @@ -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.v51; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.utils.DateUtils.parseDate; + +public class FeedIssuesLongDatesTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssuesLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + MigrationStep migration = new FeedIssuesLongDates(db.database(), System2.INSTANCE); + migration.execute(); + + int count = db + .countSql("select count(*) from issues where " + + "issue_creation_date_ms is not null " + + "and issue_update_date_ms is not null " + + "and issue_close_date_ms is not null"); + assertThat(count).isEqualTo(2); + } + + @Test + public void take_now_if_date_in_the_future() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + System2 system2 = mock(System2.class); + when(system2.now()).thenReturn(0L); + + MigrationStep migration = new FeedIssuesLongDates(db.database(), mock(System2.class)); + migration.execute(); + + int count = db + .countSql("select count(*) from issues where " + + "issue_creation_date_ms = 0"); + assertThat(count).isEqualTo(1); + } + + @Test + public void take_snapshot_date_if_in_the_past() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + long snapshotTime = parseDate("2014-09-25").getTime(); + + MigrationStep migration = new FeedIssuesLongDates(db.database(), System2.INSTANCE); + migration.execute(); + + int count = db + .countSql("select count(*) from issues where " + + "issue_creation_date_ms=" + snapshotTime); + assertThat(count).isEqualTo(1); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedManualMeasuresLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedManualMeasuresLongDatesTest.java new file mode 100644 index 00000000000..2b4f3f5facb --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedManualMeasuresLongDatesTest.java @@ -0,0 +1,88 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.utils.DateUtils.parseDate; + +public class FeedManualMeasuresLongDatesTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedManualMeasuresLongDatesTest.class, "schema.sql"); + + @Before + public void before() { + db.prepareDbUnit(getClass(), "before.xml"); + } + + @Test + public void execute() throws Exception { + MigrationStep migration = newMigration(System2.INSTANCE); + + migration.execute(); + + int count = db + .countSql("select count(*) from manual_measures where " + + "created_at_ms is not null " + + "and updated_at_ms is not null"); + assertThat(count).isEqualTo(2); + } + + @Test + public void take_now_if_date_in_the_future() throws Exception { + System2 system = mock(System2.class); + when(system.now()).thenReturn(1234L); + + MigrationStep migration = newMigration(system); + + migration.execute(); + + int count = db + .countSql("select count(*) from manual_measures where " + + "created_at_ms = 1234"); + assertThat(count).isEqualTo(1); + } + + @Test + public void take_manual_measure_date_if_in_the_past() throws Exception { + MigrationStep migration = newMigration(System2.INSTANCE); + + migration.execute(); + + long snapshotTime = parseDate("2014-09-25").getTime(); + int count = db + .countSql("select count(*) from manual_measures where " + + "created_at_ms=" + snapshotTime); + assertThat(count).isEqualTo(1); + } + + private MigrationStep newMigration(System2 system) { + return new FeedManualMeasuresLongDates(db.database(), system); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedSnapshotsLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedSnapshotsLongDatesTest.java new file mode 100644 index 00000000000..8eeb8bb588b --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedSnapshotsLongDatesTest.java @@ -0,0 +1,84 @@ +/* + * 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.v51; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.api.utils.DateUtils.parseDate; + +public class FeedSnapshotsLongDatesTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedSnapshotsLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + MigrationStep migration = new FeedSnapshotsLongDates(db.database(), System2.INSTANCE); + migration.execute(); + + int count = db + .countSql("select count(*) from snapshots where created_at_ms is not null " + + "and build_date_ms is not null " + + "and period1_date_ms is not null " + + "and period2_date_ms is not null " + + "and period3_date_ms is not null " + + "and period4_date_ms is not null " + + "and period5_date_ms is not null"); + assertThat(count).isEqualTo(2); + } + + @Test + public void take_now_if_date_in_the_future() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + System2 system2 = mock(System2.class); + when(system2.now()).thenReturn(0L); + + MigrationStep migration = new FeedSnapshotsLongDates(db.database(), mock(System2.class)); + migration.execute(); + + int count = db + .countSql("select count(*) from snapshots where " + + "created_at_ms = 0"); + assertThat(count).isEqualTo(1); + } + + @Test + public void take_snapshot_date_if_in_the_past() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + long snapshotTime = parseDate("2014-09-25").getTime(); + + MigrationStep migration = new FeedSnapshotsLongDates(db.database(), System2.INSTANCE); + migration.execute(); + + int count = db + .countSql("select count(*) from snapshots where " + + "created_at_ms=" + snapshotTime); + assertThat(count).isEqualTo(1); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/FeedUsersLongDatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedUsersLongDatesTest.java new file mode 100644 index 00000000000..88923fbcb6e --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/FeedUsersLongDatesTest.java @@ -0,0 +1,51 @@ +/* + * 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.v51; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FeedUsersLongDatesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedUsersLongDatesTest.class, "schema.sql"); + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + System2 system = mock(System2.class); + when(system.now()).thenReturn(1500000000000L); + MigrationStep migration = new FeedUsersLongDates(db.database(), system); + migration.execute(); + + int count = db.countSql("select count(*) from users where created_at_ms is not null and updated_at_ms is not null"); + assertThat(count).isEqualTo(3); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/RemovePermissionsOnModulesMigrationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/RemovePermissionsOnModulesMigrationTest.java new file mode 100644 index 00000000000..3b41af6846e --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/RemovePermissionsOnModulesMigrationTest.java @@ -0,0 +1,59 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class RemovePermissionsOnModulesMigrationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemovePermissionsOnModulesMigrationTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + migration = new RemovePermissionsOnModulesMigrationStep(db.database()); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "migrate.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "migrate-result.xml", "user_roles", "group_roles"); + } + + @Test + public void nothing_to_do_when_already_migrated() throws Exception { + db.prepareDbUnit(getClass(), "nothing_to_do_when_already_migrated.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "nothing_to_do_when_already_migrated.xml", "user_roles", "group_roles"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/RenameComponentRelatedParamsInIssueFiltersMigrationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/RenameComponentRelatedParamsInIssueFiltersMigrationTest.java new file mode 100644 index 00000000000..b2ecde9fa88 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/RenameComponentRelatedParamsInIssueFiltersMigrationTest.java @@ -0,0 +1,56 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RenameComponentRelatedParamsInIssueFiltersMigrationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, RenameComponentRelatedParamsInIssueFiltersMigrationTest.class, "schema.sql"); + + MigrationStep migration; + System2 system = mock(System2.class); + + @Before + public void setUp() { + db.executeUpdateSql("truncate table issue_filters"); + migration = new RenameComponentRelatedParamsInIssueFilters(db.database(), system); + when(system.now()).thenReturn(DateUtils.parseDate("2014-10-29").getTime()); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "execute.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "execute-result.xml", "issue_filters"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v51/UpdateProjectsModuleUuidPathTest.java b/sonar-db/src/test/java/org/sonar/db/version/v51/UpdateProjectsModuleUuidPathTest.java new file mode 100644 index 00000000000..89a8d7e6c7d --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v51/UpdateProjectsModuleUuidPathTest.java @@ -0,0 +1,65 @@ +/* + * 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.v51; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class UpdateProjectsModuleUuidPathTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, UpdateProjectsModuleUuidPathTest.class, "schema.sql"); + + MigrationStep migration; + + System2 system; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table projects"); + + migration = new UpdateProjectsModuleUuidPath(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate_components.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate_components-result.xml", "projects"); + } + + @Test + public void not_migrate_already_migrated_components() throws Exception { + db.prepareDbUnit(this.getClass(), "not_migrate_already_migrated_components.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "not_migrate_already_migrated_components.xml", "projects"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/AddManualMeasuresComponentUuidColumnTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/AddManualMeasuresComponentUuidColumnTest.java new file mode 100644 index 00000000000..07f48d9a144 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/AddManualMeasuresComponentUuidColumnTest.java @@ -0,0 +1,49 @@ +/* + * 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.v52; + +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +public class AddManualMeasuresComponentUuidColumnTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddManualMeasuresComponentUuidColumnTest.class, "schema.sql"); + + AddManualMeasuresComponentUuidColumn underTest; + + @Before + public void setUp() { + underTest = new AddManualMeasuresComponentUuidColumn(db.database()); + } + + @Test + public void update_columns() throws Exception { + underTest.execute(); + + db.assertColumnDefinition("manual_measures", "component_uuid", Types.VARCHAR, 50); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/FeedEventsComponentUuidTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedEventsComponentUuidTest.java new file mode 100644 index 00000000000..87f156d39be --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedEventsComponentUuidTest.java @@ -0,0 +1,64 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class FeedEventsComponentUuidTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedEventsComponentUuidTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table events"); + db.executeUpdateSql("truncate table projects"); + + migration = new FeedEventsComponentUuid(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "events"); + } + + @Test + public void not_migrate_already_migrated_data() throws Exception { + db.prepareDbUnit(this.getClass(), "not_migrate_already_migrated_data.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "not_migrate_already_migrated_data.xml", "events"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/FeedFileSourcesDataTypeTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedFileSourcesDataTypeTest.java new file mode 100644 index 00000000000..615a41ba117 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedFileSourcesDataTypeTest.java @@ -0,0 +1,55 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class FeedFileSourcesDataTypeTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedFileSourcesDataTypeTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table file_sources"); + + migration = new FeedFileSourcesDataType(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "file_sources"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/FeedManualMeasuresComponentUuidTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedManualMeasuresComponentUuidTest.java new file mode 100644 index 00000000000..cf76d6a4430 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedManualMeasuresComponentUuidTest.java @@ -0,0 +1,55 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +public class FeedManualMeasuresComponentUuidTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedManualMeasuresComponentUuidTest.class, "schema.sql"); + + FeedManualMeasuresComponentUuid underTest; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table manual_measures"); + db.executeUpdateSql("truncate table projects"); + + underTest = new FeedManualMeasuresComponentUuid(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + underTest.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + underTest.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "manual_measures"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/FeedMetricsBooleansTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedMetricsBooleansTest.java new file mode 100644 index 00000000000..b565f04dd34 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedMetricsBooleansTest.java @@ -0,0 +1,54 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class FeedMetricsBooleansTest { + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedMetricsBooleansTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table metrics"); + + migration = new FeedMetricsBooleans(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "metrics"); + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/FeedProjectLinksComponentUuidTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedProjectLinksComponentUuidTest.java new file mode 100644 index 00000000000..4487d000614 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/FeedProjectLinksComponentUuidTest.java @@ -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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class FeedProjectLinksComponentUuidTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedProjectLinksComponentUuidTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table project_links"); + + migration = new FeedProjectLinksComponentUuid(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "project_links"); + } + + @Test + public void not_migrate_already_migrated_data() throws Exception { + db.prepareDbUnit(this.getClass(), "not_migrate_already_migrated_data.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "not_migrate_already_migrated_data.xml", "project_links"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/MoveProjectProfileAssociationTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/MoveProjectProfileAssociationTest.java new file mode 100644 index 00000000000..982859c34ae --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/MoveProjectProfileAssociationTest.java @@ -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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class MoveProjectProfileAssociationTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, MoveProjectProfileAssociationTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table projects"); + db.executeUpdateSql("truncate table project_qprofiles"); + db.executeUpdateSql("truncate table properties"); + db.executeUpdateSql("truncate table rules_profiles"); + + migration = new MoveProjectProfileAssociation(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "rules_profiles", "project_qprofiles"); + } + + @Test + public void not_migrate_already_migrated_data() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate-result.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "rules_profiles", "project_qprofiles"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveComponentLibrariesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveComponentLibrariesTest.java new file mode 100644 index 00000000000..fa1d876f5af --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveComponentLibrariesTest.java @@ -0,0 +1,56 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class RemoveComponentLibrariesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveComponentLibrariesTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table projects"); + + migration = new RemoveComponentLibraries(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void remove_libraries() throws Exception { + db.prepareDbUnit(this.getClass(), "remove_libraries.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "remove_libraries-result.xml", "projects"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveDuplicatedComponentKeysTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveDuplicatedComponentKeysTest.java new file mode 100644 index 00000000000..79d5b1511ab --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveDuplicatedComponentKeysTest.java @@ -0,0 +1,78 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class RemoveDuplicatedComponentKeysTest { + + @ClassRule + public static DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveDuplicatedComponentKeysTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table projects"); + db.executeUpdateSql("truncate table issues"); + + migration = new RemoveDuplicatedComponentKeys(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate_components_and_issues() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "projects", "issues"); + } + + @Test + public void not_migrate_components_and_issues_already_migrated() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate-result.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "migrate-result.xml", "projects", "issues"); + } + + @Test + public void keep_enable_component_when_enabled_component_exists() throws Exception { + db.prepareDbUnit(this.getClass(), "keep_enable_component.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "keep_enable_component-result.xml", "projects"); + } + + @Test + public void keep_last_component_when_no_enabled_components() throws Exception { + db.prepareDbUnit(this.getClass(), "keep_last_component.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "keep_last_component-result.xml", "projects"); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveSnapshotLibrariesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveSnapshotLibrariesTest.java new file mode 100644 index 00000000000..c9163a6c702 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveSnapshotLibrariesTest.java @@ -0,0 +1,56 @@ +/* + * 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.v52; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +public class RemoveSnapshotLibrariesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveSnapshotLibrariesTest.class, "schema.sql"); + + MigrationStep migration; + + @Before + public void setUp() { + db.executeUpdateSql("truncate table snapshots"); + + migration = new RemoveSnapshotLibraries(db.database()); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void remove_libraries() throws Exception { + db.prepareDbUnit(this.getClass(), "remove_libraries.xml"); + migration.execute(); + db.assertDbUnit(this.getClass(), "remove_libraries-result.xml", "snapshots"); + } + +} |