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.

MigrationStepRegistryImplTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.step;
  21. import java.util.List;
  22. import java.util.Random;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. public class MigrationStepRegistryImplTest {
  28. @Rule
  29. public ExpectedException expectedException = ExpectedException.none();
  30. private MigrationStepRegistryImpl underTest = new MigrationStepRegistryImpl();
  31. @Test
  32. public void add_fails_with_IAE_if_migrationNumber_is_less_than_0() {
  33. expectedException.expect(IllegalArgumentException.class);
  34. expectedException.expectMessage("Migration number must be >= 0");
  35. underTest.add(-Math.abs(new Random().nextLong() + 1), "sdsd", MigrationStep.class);
  36. }
  37. @Test
  38. public void add_fails_with_NPE_if_description_is_null() {
  39. expectedException.expect(NullPointerException.class);
  40. expectedException.expectMessage("description can't be null");
  41. underTest.add(12, null, MigrationStep.class);
  42. }
  43. @Test
  44. public void add_fails_with_IAE_if_description_is_empty() {
  45. expectedException.expect(IllegalArgumentException.class);
  46. expectedException.expectMessage("description can't be empty");
  47. underTest.add(12, "", MigrationStep.class);
  48. }
  49. @Test
  50. public void add_fails_with_NPE_is_migrationstep_class_is_null() {
  51. expectedException.expect(NullPointerException.class);
  52. expectedException.expectMessage("MigrationStep class can't be null");
  53. underTest.add(12, "sdsd", null);
  54. }
  55. @Test
  56. public void add_fails_with_ISE_when_called_twice_with_same_migration_number() {
  57. underTest.add(12, "dsd", MigrationStep.class);
  58. expectedException.expect(IllegalStateException.class);
  59. expectedException.expectMessage("A migration is already registered for migration number '12'");
  60. underTest.add(12, "dfsdf", MigrationStep.class);
  61. }
  62. @Test
  63. public void build_fails_with_ISE_if_registry_is_empty() {
  64. expectedException.expect(IllegalStateException.class);
  65. expectedException.expectMessage("Registry is empty");
  66. underTest.build();
  67. }
  68. @Test
  69. public void build_returns_a_MigrationStepsImpl_with_all_steps_added_to_registry_ordered_by_migration_number() {
  70. underTest.add(343, "sss", MigrationStep2.class);
  71. underTest.add(5, "aazsa", MigrationStep1.class);
  72. underTest.add(66, "bbb", MigrationStep3.class);
  73. underTest.add(2, "aaaa", MigrationStep4.class);
  74. MigrationSteps migrationSteps = underTest.build();
  75. assertThat(migrationSteps).isInstanceOf(MigrationStepsImpl.class);
  76. List<RegisteredMigrationStep> registeredMigrationSteps = migrationSteps.readAll();
  77. assertThat(registeredMigrationSteps).hasSize(4);
  78. verify(registeredMigrationSteps.get(0), 2, "aaaa", MigrationStep4.class);
  79. verify(registeredMigrationSteps.get(1), 5, "aazsa", MigrationStep1.class);
  80. verify(registeredMigrationSteps.get(2), 66, "bbb", MigrationStep3.class);
  81. verify(registeredMigrationSteps.get(3), 343, "sss", MigrationStep2.class);
  82. }
  83. private static void verify(RegisteredMigrationStep step, int migrationNUmber, String description, Class<? extends MigrationStep> stepClass) {
  84. assertThat(step.getMigrationNumber()).isEqualTo(migrationNUmber);
  85. assertThat(step.getDescription()).isEqualTo(description);
  86. assertThat(step.getStepClass()).isEqualTo(stepClass);
  87. }
  88. private static abstract class NoopMigrationStep implements MigrationStep {
  89. @Override
  90. public void execute() {
  91. throw new IllegalStateException("execute is not implemented");
  92. }
  93. }
  94. private static class MigrationStep1 extends NoopMigrationStep {
  95. }
  96. private static class MigrationStep2 extends NoopMigrationStep {
  97. }
  98. private static class MigrationStep3 extends NoopMigrationStep {
  99. }
  100. private static class MigrationStep4 extends NoopMigrationStep {
  101. }
  102. }