3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.container;
22 import org.junit.Test;
23 import org.picocontainer.Startable;
24 import org.sonar.core.platform.ComponentContainer;
25 import org.sonar.core.platform.ContainerPopulator;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
31 public class TaskContainerImplTest {
32 private ComponentContainer parent = new ComponentContainer();
33 private ContainerPopulator populator = mock(ContainerPopulator.class);
35 @Test(expected = NullPointerException.class)
36 public void constructor_fails_fast_on_null_container() {
37 new TaskContainerImpl(null, mock(ContainerPopulator.class));
40 @Test(expected = NullPointerException.class)
41 public void constructor_fails_fast_on_null_item() {
42 new TaskContainerImpl(new ComponentContainer(), null);
46 public void calls_method_populateContainer_of_passed_in_populator() {
47 TaskContainerImpl ceContainer = new TaskContainerImpl(parent, populator);
49 verify(populator).populateContainer(ceContainer);
53 public void ce_container_is_not_child_of_specified_container() {
54 TaskContainerImpl ceContainer = new TaskContainerImpl(parent, populator);
56 assertThat(parent.getChildren()).isEmpty();
57 verify(populator).populateContainer(ceContainer);
61 public void components_are_started_lazily_unless_they_are_annotated_with_EagerStart() {
62 final DefaultStartable defaultStartable = new DefaultStartable();
63 final EagerStartable eagerStartable = new EagerStartable();
64 TaskContainerImpl ceContainer = new TaskContainerImpl(parent, new ContainerPopulator<TaskContainer>() {
66 public void populateContainer(TaskContainer container) {
67 container.add(defaultStartable);
68 container.add(eagerStartable);
72 assertThat(defaultStartable.startCalls).isEqualTo(0);
73 assertThat(defaultStartable.stopCalls).isEqualTo(0);
74 assertThat(eagerStartable.startCalls).isEqualTo(1);
75 assertThat(eagerStartable.stopCalls).isEqualTo(0);
78 public static class DefaultStartable implements Startable {
79 protected int startCalls = 0;
80 protected int stopCalls = 0;
94 public static class EagerStartable extends DefaultStartable {