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.

TaskContainerImplTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.ce.task.container;
  21. import org.junit.Test;
  22. import org.picocontainer.Startable;
  23. import org.sonar.core.platform.ComponentContainer;
  24. import org.sonar.core.platform.ContainerPopulator;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  27. import static org.mockito.Mockito.spy;
  28. import static org.mockito.Mockito.verify;
  29. public class TaskContainerImplTest {
  30. private ComponentContainer parent = new ComponentContainer();
  31. private ContainerPopulator<TaskContainer> populator = spy(new DummyContainerPopulator());
  32. @Test
  33. public void constructor_fails_fast_on_null_container() {
  34. assertThatThrownBy(() -> new TaskContainerImpl(null, populator))
  35. .isInstanceOf(NullPointerException.class);
  36. }
  37. @Test
  38. public void constructor_fails_fast_on_null_item() {
  39. assertThatThrownBy(() -> new TaskContainerImpl(new ComponentContainer(), null))
  40. .isInstanceOf(NullPointerException.class);
  41. }
  42. @Test
  43. public void calls_method_populateContainer_of_passed_in_populator() {
  44. TaskContainerImpl ceContainer = new TaskContainerImpl(parent, populator);
  45. verify(populator).populateContainer(ceContainer);
  46. }
  47. @Test
  48. public void ce_container_is_not_child_of_specified_container() {
  49. TaskContainerImpl ceContainer = new TaskContainerImpl(parent, populator);
  50. assertThat(parent.getChildren()).isEmpty();
  51. verify(populator).populateContainer(ceContainer);
  52. }
  53. @Test
  54. public void bootup_starts_components_lazily_unless_they_are_annotated_with_EagerStart() {
  55. final DefaultStartable defaultStartable = new DefaultStartable();
  56. final EagerStartable eagerStartable = new EagerStartable();
  57. TaskContainerImpl ceContainer = new TaskContainerImpl(parent, container -> {
  58. container.add(defaultStartable);
  59. container.add(eagerStartable);
  60. });
  61. ceContainer.bootup();
  62. assertThat(defaultStartable.startCalls).isZero();
  63. assertThat(defaultStartable.stopCalls).isZero();
  64. assertThat(eagerStartable.startCalls).isOne();
  65. assertThat(eagerStartable.stopCalls).isZero();
  66. }
  67. @Test
  68. public void close_stops_started_components() {
  69. final DefaultStartable defaultStartable = new DefaultStartable();
  70. final EagerStartable eagerStartable = new EagerStartable();
  71. TaskContainerImpl ceContainer = new TaskContainerImpl(parent, container -> {
  72. container.add(defaultStartable);
  73. container.add(eagerStartable);
  74. });
  75. ceContainer.bootup();
  76. ceContainer.close();
  77. assertThat(defaultStartable.startCalls).isZero();
  78. assertThat(defaultStartable.stopCalls).isZero();
  79. assertThat(eagerStartable.startCalls).isOne();
  80. assertThat(eagerStartable.stopCalls).isOne();
  81. }
  82. public static class DefaultStartable implements Startable {
  83. protected int startCalls = 0;
  84. protected int stopCalls = 0;
  85. @Override
  86. public void start() {
  87. startCalls++;
  88. }
  89. @Override
  90. public void stop() {
  91. stopCalls++;
  92. }
  93. }
  94. @EagerStart
  95. public static class EagerStartable extends DefaultStartable {
  96. }
  97. private static class DummyContainerPopulator implements ContainerPopulator<TaskContainer> {
  98. @Override
  99. public void populateContainer(TaskContainer container) {
  100. }
  101. }
  102. }