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.

MigrationContainerPopulatorImplTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.engine;
  21. import java.util.stream.Stream;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.sonar.server.platform.db.migration.history.MigrationHistory;
  25. import org.sonar.server.platform.db.migration.step.MigrationStep;
  26. import org.sonar.server.platform.db.migration.step.MigrationStepRegistry;
  27. import org.sonar.server.platform.db.migration.step.MigrationSteps;
  28. import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl;
  29. import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep;
  30. import org.sonar.server.platform.db.migration.version.DbVersion;
  31. import static java.util.Arrays.asList;
  32. import static java.util.Collections.emptyList;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.when;
  36. public class MigrationContainerPopulatorImplTest {
  37. private MigrationContainer migrationContainer = new SimpleMigrationContainer();
  38. private MigrationSteps migrationSteps = mock(MigrationSteps.class);
  39. private MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl();
  40. @Before
  41. public void setUp() {
  42. migrationContainer.add(migrationSteps);
  43. }
  44. @Test
  45. public void populateContainer_adds_components_of_DbVersion_getSupportComponents() {
  46. MigrationContainerPopulatorImpl underTest = new MigrationContainerPopulatorImpl(
  47. new NoRegistryDbVersion() {
  48. @Override
  49. public Stream<Object> getSupportComponents() {
  50. return Stream.of(Clazz2.class);
  51. }
  52. },
  53. new NoRegistryDbVersion(),
  54. new NoRegistryDbVersion() {
  55. @Override
  56. public Stream<Object> getSupportComponents() {
  57. return Stream.of(Clazz1.class, Clazz3.class);
  58. }
  59. });
  60. when(migrationSteps.readAll()).thenReturn(emptyList());
  61. underTest.populateContainer(migrationContainer);
  62. assertThat(migrationContainer.getComponentsByType(Clazz1.class)).isNotNull();
  63. assertThat(migrationContainer.getComponentsByType(Clazz2.class)).isNotNull();
  64. assertThat(migrationContainer.getComponentsByType(Clazz3.class)).isNotNull();
  65. }
  66. @Test
  67. public void populateContainer_adds_MigrationStepsExecutorImpl() {
  68. when(migrationSteps.readAll()).thenReturn(emptyList());
  69. // add MigrationStepsExecutorImpl's dependencies
  70. migrationContainer.add(mock(MigrationHistory.class));
  71. underTest.populateContainer(migrationContainer);
  72. assertThat(migrationContainer.getComponentByType(MigrationStepsExecutorImpl.class)).isNotNull();
  73. }
  74. @Test
  75. public void populateContainer_adds_classes_of_all_steps_defined_in_MigrationSteps() {
  76. when(migrationSteps.readAll()).thenReturn(asList(
  77. new RegisteredMigrationStep(1, "foo", MigrationStep1.class),
  78. new RegisteredMigrationStep(2, "bar", MigrationStep2.class),
  79. new RegisteredMigrationStep(3, "dor", MigrationStep3.class)));
  80. underTest.populateContainer(migrationContainer);
  81. assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull();
  82. assertThat(migrationContainer.getComponentsByType(MigrationStep2.class)).isNotNull();
  83. assertThat(migrationContainer.getComponentsByType(MigrationStep3.class)).isNotNull();
  84. }
  85. @Test
  86. public void populateCotnainer_does_not_fail_if_same_class_is_used_for_more_than_one_migration() {
  87. when(migrationSteps.readAll()).thenReturn(asList(
  88. new RegisteredMigrationStep(1, "foo", MigrationStep1.class),
  89. new RegisteredMigrationStep(2, "bar", MigrationStep2.class),
  90. new RegisteredMigrationStep(3, "bar2", MigrationStep2.class),
  91. new RegisteredMigrationStep(4, "foo2", MigrationStep1.class),
  92. new RegisteredMigrationStep(5, "dor", MigrationStep3.class)));
  93. underTest.populateContainer(migrationContainer);
  94. assertThat(migrationContainer.getComponentsByType(MigrationStep1.class)).isNotNull();
  95. assertThat(migrationContainer.getComponentsByType(MigrationStep2.class)).isNotNull();
  96. assertThat(migrationContainer.getComponentsByType(MigrationStep3.class)).isNotNull();
  97. }
  98. private static abstract class NoopMigrationStep implements MigrationStep {
  99. @Override
  100. public void execute() {
  101. throw new UnsupportedOperationException("execute not implemented");
  102. }
  103. }
  104. public static final class MigrationStep1 extends NoopMigrationStep {
  105. }
  106. public static final class MigrationStep2 extends NoopMigrationStep {
  107. }
  108. public static final class MigrationStep3 extends NoopMigrationStep {
  109. }
  110. public static final class Clazz1 {
  111. }
  112. public static final class Clazz2 {
  113. }
  114. public static final class Clazz3 {
  115. }
  116. /**
  117. * An implementation of DbVersion to be passed to {@link MigrationContainerPopulatorImpl}'s constructor which is
  118. * not supposed to call the {@link DbVersion#addSteps(MigrationStepRegistry)} method and therefor has an
  119. * implementation that throws a {@link UnsupportedOperationException} when called.
  120. */
  121. private static class NoRegistryDbVersion implements DbVersion {
  122. @Override
  123. public void addSteps(MigrationStepRegistry registry) {
  124. throw new UnsupportedOperationException("addSteps is not supposed to be called");
  125. }
  126. }
  127. }