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.projectanalysis.container;
22 import com.google.common.base.Function;
23 import com.google.common.base.Predicate;
24 import com.google.common.collect.ImmutableList;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
31 import org.junit.Test;
32 import org.picocontainer.DefaultPicoContainer;
33 import org.picocontainer.PicoContainer;
34 import org.sonar.ce.queue.CeTask;
35 import org.sonar.core.platform.ComponentContainer;
36 import org.sonar.plugin.ce.ReportAnalysisComponentProvider;
37 import org.sonar.server.computation.task.container.TaskContainer;
38 import org.sonar.server.computation.task.step.StepsExplorer;
39 import org.sonar.server.computation.task.projectanalysis.step.PersistComponentsStep;
40 import org.sonar.server.computation.task.step.ComputationStep;
42 import static com.google.common.base.Predicates.notNull;
43 import static com.google.common.collect.FluentIterable.from;
44 import static com.google.common.collect.Sets.difference;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
49 public class ProjectAnalysisTaskContainerPopulatorTest {
50 private static final String PROJECTANALYSIS_STEP_PACKAGE = "org.sonar.server.computation.task.projectanalysis.step";
52 private CeTask task = mock(CeTask.class);
53 private ProjectAnalysisTaskContainerPopulator underTest;
56 public void item_is_added_to_the_container() {
57 underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
58 AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
59 underTest.populateContainer(container);
61 assertThat(container.added).contains(task);
65 public void all_computation_steps_are_added_in_order_to_the_container() {
66 underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
67 AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
68 underTest.populateContainer(container);
70 Set<String> computationStepClassNames = from(container.added)
71 .transform(new Function<Object, Class<?>>() {
74 public Class<?> apply(Object input) {
75 if (input instanceof Class) {
76 return (Class<?>) input;
82 .filter(IsComputationStep.INSTANCE)
83 .transform(StepsExplorer.toCanonicalName())
86 assertThat(difference(StepsExplorer.retrieveStepPackageStepsCanonicalNames(PROJECTANALYSIS_STEP_PACKAGE), computationStepClassNames)).isEmpty();
90 public void at_least_one_core_step_is_added_to_the_container() {
91 underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
92 AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
93 underTest.populateContainer(container);
95 assertThat(container.added).contains(PersistComponentsStep.class);
99 public void Components_of_ReportAnalysisComponentProvider_are_added_to_the_container() {
100 Object object = new Object();
101 Class<MyClass> clazz = MyClass.class;
102 ReportAnalysisComponentProvider componentProvider = mock(ReportAnalysisComponentProvider.class);
103 when(componentProvider.getComponents()).thenReturn(ImmutableList.of(object, clazz));
105 underTest = new ProjectAnalysisTaskContainerPopulator(task, new ReportAnalysisComponentProvider[] {componentProvider});
106 AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
107 container.add(componentProvider);
108 underTest.populateContainer(container);
110 assertThat(container.added).contains(object, clazz);
113 private static final class MyClass {
117 private enum IsComputationStep implements Predicate<Class<?>> {
121 public boolean apply(Class<?> input) {
122 return ComputationStep.class.isAssignableFrom(input);
126 private static class AddedObjectsRecorderTaskContainer implements TaskContainer {
127 private static final DefaultPicoContainer SOME_EMPTY_PICO_CONTAINER = new DefaultPicoContainer();
129 private List<Object> added = new ArrayList<>();
132 public ComponentContainer getParent() {
133 throw new UnsupportedOperationException("getParent is not implemented");
137 public void cleanup() {
138 throw new UnsupportedOperationException("cleanup is not implemented");
142 public PicoContainer getPicoContainer() {
143 return SOME_EMPTY_PICO_CONTAINER;
147 public ComponentContainer add(Object... objects) {
148 added.addAll(Arrays.asList(objects));
149 return null; // not used anyway
153 public ComponentContainer addSingletons(Iterable<?> components) {
154 for (Object component : components) {
155 added.add(component);
157 return null; // not used anyway
161 public <T> T getComponentByType(Class<T> type) {
162 for (Object add : added) {
163 if (add.getClass().getSimpleName().contains(type.getSimpleName())) {
171 public <T> List<T> getComponentsByType(final Class<T> type) {
173 .filter(new Predicate<Object>() {
175 public boolean apply(@Nonnull Object input) {
176 return input.getClass().getSimpleName().contains(type.getSimpleName());
178 }).transform(new Function<Object, T>() {
181 public T apply(@Nonnull Object input) {