You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetRuleScopeToMainTest.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform.db.migration.version.v71;
  21. import java.sql.SQLException;
  22. import javax.annotation.Nullable;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.sonar.api.rule.RuleScope;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.db.CoreDbTester;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. public class SetRuleScopeToMainTest {
  31. @Rule
  32. public final CoreDbTester dbTester = CoreDbTester.createForSchema(SetRuleScopeToMainTest.class, "rules.sql");
  33. @Rule
  34. public ExpectedException expectedException = ExpectedException.none();
  35. private System2 system = new System2();
  36. private SetRuleScopeToMain underTest = new SetRuleScopeToMain(dbTester.database(), system);
  37. @Test
  38. public void has_no_effect_if_table_rules_is_empty() throws SQLException {
  39. underTest.execute();
  40. assertThat(dbTester.countRowsOfTable("rules")).isEqualTo(0);
  41. }
  42. @Test
  43. public void updates_rows_with_null_is_build_in_column_to_false() throws SQLException {
  44. insertRow(1, null);
  45. insertRow(2, null);
  46. assertThat(countRowsWithValue(null)).isEqualTo(2);
  47. assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(0);
  48. underTest.execute();
  49. assertThat(countRowsWithValue(null)).isEqualTo(0);
  50. assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(2);
  51. }
  52. @Test
  53. public void execute_is_reentreant() throws SQLException {
  54. insertRow(1, null);
  55. insertRow(2, RuleScope.MAIN);
  56. underTest.execute();
  57. underTest.execute();
  58. assertThat(countRowsWithValue(null)).isEqualTo(0);
  59. assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(2);
  60. }
  61. private int countRowsWithValue(@Nullable RuleScope value) {
  62. if (value == null) {
  63. return dbTester.countSql("select count(1) from rules where scope is null");
  64. }
  65. return dbTester.countSql("select count(1) from rules where scope='" + value + "'");
  66. }
  67. private void insertRow(int id, @Nullable RuleScope scope) {
  68. dbTester.executeInsert(
  69. "RULES",
  70. "PLUGIN_RULE_KEY", "key_" + id,
  71. "PLUGIN_NAME", "name_" + id,
  72. "SCOPE", scope == null ? null : scope.name());
  73. }
  74. }