]> source.dussan.org Git - sonarqube.git/blob
4fbe85c0476cbf8b8c9ae936e8d4bb1f146ec15d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.computation.task.projectanalysis.container;
21
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;
28 import java.util.Set;
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;
41
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;
48
49 public class ProjectAnalysisTaskContainerPopulatorTest {
50   private static final String PROJECTANALYSIS_STEP_PACKAGE = "org.sonar.server.computation.task.projectanalysis.step";
51
52   private CeTask task = mock(CeTask.class);
53   private ProjectAnalysisTaskContainerPopulator underTest;
54
55   @Test
56   public void item_is_added_to_the_container() {
57     underTest = new ProjectAnalysisTaskContainerPopulator(task, null);
58     AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
59     underTest.populateContainer(container);
60
61     assertThat(container.added).contains(task);
62   }
63
64   @Test
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);
69
70     Set<String> computationStepClassNames = from(container.added)
71       .transform(new Function<Object, Class<?>>() {
72         @Nullable
73         @Override
74         public Class<?> apply(Object input) {
75           if (input instanceof Class) {
76             return (Class<?>) input;
77           }
78           return null;
79         }
80       })
81       .filter(notNull())
82       .filter(IsComputationStep.INSTANCE)
83       .transform(StepsExplorer.toCanonicalName())
84       .toSet();
85
86     assertThat(difference(StepsExplorer.retrieveStepPackageStepsCanonicalNames(PROJECTANALYSIS_STEP_PACKAGE), computationStepClassNames)).isEmpty();
87   }
88
89   @Test
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);
94
95     assertThat(container.added).contains(PersistComponentsStep.class);
96   }
97
98   @Test
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));
104
105     underTest = new ProjectAnalysisTaskContainerPopulator(task, new ReportAnalysisComponentProvider[] {componentProvider});
106     AddedObjectsRecorderTaskContainer container = new AddedObjectsRecorderTaskContainer();
107     container.add(componentProvider);
108     underTest.populateContainer(container);
109
110     assertThat(container.added).contains(object, clazz);
111   }
112
113   private static final class MyClass {
114
115   }
116
117   private enum IsComputationStep implements Predicate<Class<?>> {
118     INSTANCE;
119
120     @Override
121     public boolean apply(Class<?> input) {
122       return ComputationStep.class.isAssignableFrom(input);
123     }
124   }
125
126   private static class AddedObjectsRecorderTaskContainer implements TaskContainer {
127     private static final DefaultPicoContainer SOME_EMPTY_PICO_CONTAINER = new DefaultPicoContainer();
128
129     private List<Object> added = new ArrayList<>();
130
131     @Override
132     public ComponentContainer getParent() {
133       throw new UnsupportedOperationException("getParent is not implemented");
134     }
135
136     @Override
137     public void cleanup() {
138       throw new UnsupportedOperationException("cleanup is not implemented");
139     }
140
141     @Override
142     public PicoContainer getPicoContainer() {
143       return SOME_EMPTY_PICO_CONTAINER;
144     }
145
146     @Override
147     public ComponentContainer add(Object... objects) {
148       added.addAll(Arrays.asList(objects));
149       return null; // not used anyway
150     }
151
152     @Override
153     public ComponentContainer addSingletons(Iterable<?> components) {
154       for (Object component : components) {
155         added.add(component);
156       }
157       return null; // not used anyway
158     }
159
160     @Override
161     public <T> T getComponentByType(Class<T> type) {
162       for (Object add : added) {
163         if (add.getClass().getSimpleName().contains(type.getSimpleName())) {
164           return (T) add;
165         }
166       }
167       return null;
168     }
169
170     @Override
171     public <T> List<T> getComponentsByType(final Class<T> type) {
172       return from(added)
173         .filter(new Predicate<Object>() {
174           @Override
175           public boolean apply(@Nonnull Object input) {
176             return input.getClass().getSimpleName().contains(type.getSimpleName());
177           }
178         }).transform(new Function<Object, T>() {
179           @Override
180           @Nonnull
181           public T apply(@Nonnull Object input) {
182             return (T) input;
183           }
184         }).toList();
185     }
186   }
187
188 }