3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.api.measurecomputer;
22 import com.google.common.base.Optional;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.ce.measure.Component;
30 import org.sonar.api.ce.measure.MeasureComputer;
31 import org.sonar.api.measures.CoreMetrics;
32 import org.sonar.api.rule.RuleKey;
33 import org.sonar.api.utils.Duration;
34 import org.sonar.core.issue.DefaultIssue;
35 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
36 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
37 import org.sonar.server.computation.task.projectanalysis.issue.ComponentIssuesRepositoryRule;
38 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
39 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
40 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
41 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
42 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.assertj.guava.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48 import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY;
49 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
50 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
51 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
53 public class MeasureComputerContextImplTest {
56 public ExpectedException thrown = ExpectedException.none();
58 private static final String INT_METRIC_KEY = "int_metric_key";
59 private static final String DOUBLE_METRIC_KEY = "double_metric_key";
60 private static final String LONG_METRIC_KEY = "long_metric_key";
61 private static final String STRING_METRIC_KEY = "string_metric_key";
62 private static final String BOOLEAN_METRIC_KEY = "boolean_metric_key";
64 private static final int PROJECT_REF = 1;
65 private static final int FILE_1_REF = 12341;
66 private static final String FILE_1_KEY = "fileKey";
67 private static final int FILE_2_REF = 12342;
69 private static final org.sonar.server.computation.task.projectanalysis.component.Component FILE_1 = builder(org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE, FILE_1_REF)
74 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
75 .setRoot(builder(org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT, PROJECT_REF).setKey("project")
78 builder(org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE, FILE_2_REF).setKey("fileKey2").build()
82 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
83 .add(1, CoreMetrics.NCLOC)
84 .add(new MetricImpl(2, INT_METRIC_KEY, "int metric", Metric.MetricType.INT))
85 .add(new MetricImpl(3, DOUBLE_METRIC_KEY, "double metric", Metric.MetricType.FLOAT))
86 .add(new MetricImpl(4, LONG_METRIC_KEY, "long metric", Metric.MetricType.MILLISEC))
87 .add(new MetricImpl(5, STRING_METRIC_KEY, "string metric", Metric.MetricType.STRING))
88 .add(new MetricImpl(6, BOOLEAN_METRIC_KEY, "boolean metric", Metric.MetricType.BOOL))
92 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
95 public ComponentIssuesRepositoryRule componentIssuesRepository = new ComponentIssuesRepositoryRule(treeRootHolder);
97 SettingsRepository settingsRepository = mock(SettingsRepository.class);
100 public void get_component() throws Exception {
101 MeasureComputerContextImpl underTest = newContext(FILE_1_REF);
102 assertThat(underTest.getComponent().getType()).isEqualTo(Component.Type.FILE);
106 public void get_string_settings() throws Exception {
107 org.sonar.api.config.Settings serverSettings = new org.sonar.api.config.Settings();
108 serverSettings.setProperty("prop", "value");
109 when(settingsRepository.getSettings(FILE_1)).thenReturn(serverSettings);
111 MeasureComputerContextImpl underTest = newContext(FILE_1_REF);
112 assertThat(underTest.getSettings().getString("prop")).isEqualTo("value");
113 assertThat(underTest.getSettings().getString("unknown")).isNull();
117 public void get_string_array_settings() throws Exception {
118 org.sonar.api.config.Settings serverSettings = new org.sonar.api.config.Settings();
119 serverSettings.setProperty("prop", "1,3.4,8,50");
120 when(settingsRepository.getSettings(FILE_1)).thenReturn(serverSettings);
122 MeasureComputerContextImpl underTest = newContext(FILE_1_REF);
123 assertThat(underTest.getSettings().getStringArray("prop")).containsExactly("1", "3.4", "8", "50");
124 assertThat(underTest.getSettings().getStringArray("unknown")).isEmpty();
128 public void get_measure() throws Exception {
129 measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
131 MeasureComputerContextImpl underTest = newContext(FILE_1_REF, NCLOC_KEY, COMMENT_LINES_KEY);
132 assertThat(underTest.getMeasure(NCLOC_KEY).getIntValue()).isEqualTo(10);
136 public void fail_with_IAE_when_get_measure_is_called_on_metric_not_in_input_list() throws Exception {
137 thrown.expect(IllegalArgumentException.class);
138 thrown.expectMessage("Only metrics in [another metric] can be used to load measures");
140 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt");
141 underTest.getMeasure(NCLOC_KEY);
145 public void get_children_measures() throws Exception {
146 measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
147 measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(12));
149 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, COMMENT_LINES_KEY);
150 assertThat(underTest.getChildrenMeasures(NCLOC_KEY)).hasSize(2);
151 assertThat(underTest.getChildrenMeasures(NCLOC_KEY)).extracting("intValue").containsOnly(10, 12);
155 public void get_children_measures_when_one_child_has_no_value() throws Exception {
156 measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
159 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, COMMENT_LINES_KEY);
160 assertThat(underTest.getChildrenMeasures(NCLOC_KEY)).extracting("intValue").containsOnly(10);
164 public void not_fail_to_get_children_measures_on_output_metric() throws Exception {
165 measureRepository.addRawMeasure(FILE_1_REF, INT_METRIC_KEY, newMeasureBuilder().create(10));
167 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY);
168 assertThat(underTest.getChildrenMeasures(INT_METRIC_KEY)).hasSize(1);
169 assertThat(underTest.getChildrenMeasures(INT_METRIC_KEY)).extracting("intValue").containsOnly(10);
173 public void fail_with_IAE_when_get_children_measures_is_called_on_metric_not_in_input_list() throws Exception {
174 thrown.expect(IllegalArgumentException.class);
175 thrown.expectMessage("Only metrics in [another metric] can be used to load measures");
177 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt");
178 underTest.getChildrenMeasures(NCLOC_KEY);
182 public void add_int_measure_create_measure_of_type_int_with_right_value() throws Exception {
183 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY);
184 underTest.addMeasure(INT_METRIC_KEY, 10);
186 Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_KEY);
187 assertThat(measure).isPresent();
188 assertThat(measure.get().getIntValue()).isEqualTo(10);
192 public void add_double_measure_create_measure_of_type_double_with_right_value() throws Exception {
193 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, DOUBLE_METRIC_KEY);
194 underTest.addMeasure(DOUBLE_METRIC_KEY, 10d);
196 Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, DOUBLE_METRIC_KEY);
197 assertThat(measure).isPresent();
198 assertThat(measure.get().getDoubleValue()).isEqualTo(10d);
202 public void add_long_measure_create_measure_of_type_long_with_right_value() throws Exception {
203 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, LONG_METRIC_KEY);
204 underTest.addMeasure(LONG_METRIC_KEY, 10L);
206 Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, LONG_METRIC_KEY);
207 assertThat(measure).isPresent();
208 assertThat(measure.get().getLongValue()).isEqualTo(10L);
212 public void add_string_measure_create_measure_of_type_string_with_right_value() throws Exception {
213 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, STRING_METRIC_KEY);
214 underTest.addMeasure(STRING_METRIC_KEY, "data");
216 Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, STRING_METRIC_KEY);
217 assertThat(measure).isPresent();
218 assertThat(measure.get().getStringValue()).isEqualTo("data");
222 public void add_boolean_measure_create_measure_of_type_boolean_with_right_value() throws Exception {
223 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, BOOLEAN_METRIC_KEY);
224 underTest.addMeasure(BOOLEAN_METRIC_KEY, true);
226 Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, BOOLEAN_METRIC_KEY);
227 assertThat(measure).isPresent();
228 assertThat(measure.get().getBooleanValue()).isTrue();
232 public void fail_with_IAE_when_add_measure_is_called_on_metric_not_in_output_list() throws Exception {
233 thrown.expect(IllegalArgumentException.class);
234 thrown.expectMessage("Only metrics in [int_metric_key] can be used to add measures. Metric 'double_metric_key' is not allowed.");
236 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY);
237 underTest.addMeasure(DOUBLE_METRIC_KEY, 10);
241 public void fail_with_unsupported_operation_when_adding_measure_that_already_exists() throws Exception {
242 thrown.expect(UnsupportedOperationException.class);
243 thrown.expectMessage("A measure on metric 'int_metric_key' already exists on component 'fileKey'");
245 measureRepository.addRawMeasure(FILE_1_REF, INT_METRIC_KEY, newMeasureBuilder().create(20));
247 MeasureComputerContextImpl underTest = newContext(FILE_1_REF, NCLOC_KEY, INT_METRIC_KEY);
248 underTest.addMeasure(INT_METRIC_KEY, 10);
252 public void get_issues() throws Exception {
253 DefaultIssue issue = new DefaultIssue()
255 .setRuleKey(RuleKey.of("xoo", "S01"))
256 .setSeverity("MAJOR")
258 .setResolution("FIXED")
259 .setEffort(Duration.create(10l));
261 MeasureComputerContextImpl underTest = newContext(PROJECT_REF, Arrays.asList(issue));
263 assertThat(underTest.getIssues()).hasSize(1);
264 org.sonar.api.ce.measure.Issue result = underTest.getIssues().get(0);
265 assertThat(result.key()).isEqualTo("KEY");
266 assertThat(result.ruleKey()).isEqualTo(RuleKey.of("xoo", "S01"));
267 assertThat(result.severity()).isEqualTo("MAJOR");
268 assertThat(result.status()).isEqualTo("CLOSED");
269 assertThat(result.resolution()).isEqualTo("FIXED");
270 assertThat(result.debt()).isEqualTo(Duration.create(10l));
273 private MeasureComputerContextImpl newContext(int componentRef) {
274 return newContext(componentRef, NCLOC_KEY, COMMENT_LINES_KEY, Collections.<DefaultIssue>emptyList());
277 private MeasureComputerContextImpl newContext(int componentRef, List<DefaultIssue> issues) {
278 return newContext(componentRef, NCLOC_KEY, COMMENT_LINES_KEY, issues);
281 private MeasureComputerContextImpl newContext(int componentRef, String inputMetric, String outputMetric) {
282 return newContext(componentRef, inputMetric, outputMetric, Collections.<DefaultIssue>emptyList());
285 private MeasureComputerContextImpl newContext(int componentRef, String inputMetric, String outputMetric, List<DefaultIssue> issues) {
286 componentIssuesRepository.setIssues(componentRef, issues);
288 MeasureComputer.MeasureComputerDefinition definition = new MeasureComputerDefinitionImpl.BuilderImpl()
289 .setInputMetrics(new String[] {inputMetric})
290 .setOutputMetrics(new String[] {outputMetric})
293 MeasureComputerContextImpl context = new MeasureComputerContextImpl(treeRootHolder.getComponentByRef(componentRef),
294 settingsRepository, measureRepository, metricRepository, componentIssuesRepository);
295 context.setDefinition(definition);