2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.plugins.core.issue;
22 import org.junit.Test;
23 import org.sonar.api.CoreProperties;
24 import org.sonar.api.batch.DecoratorContext;
25 import org.sonar.api.config.Settings;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.rules.RulePriority;
29 import org.sonar.api.test.IsMeasure;
31 import static org.fest.assertions.Assertions.assertThat;
32 import static org.mockito.Matchers.any;
33 import static org.mockito.Matchers.argThat;
34 import static org.mockito.Mockito.*;
37 public class WeightedIssuesDecoratorTest {
40 public void test_weighted_VIOLATIONS() {
41 Settings settings = new Settings();
42 settings.setProperty(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY, "BLOCKER=10;CRITICAL=5;MAJOR=2;MINOR=1;INFO=0");
43 WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
44 DecoratorContext context = mock(DecoratorContext.class);
45 when(context.getMeasure(CoreMetrics.INFO_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.INFO_VIOLATIONS, 50.0));
46 when(context.getMeasure(CoreMetrics.CRITICAL_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.CRITICAL_VIOLATIONS, 80.0));
47 when(context.getMeasure(CoreMetrics.BLOCKER_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.BLOCKER_VIOLATIONS, 100.0));
50 decorator.decorate(context);
52 verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.WEIGHTED_VIOLATIONS, (double) (100 * 10 + 80 * 5 + 50 * 0))));
53 verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.WEIGHTED_VIOLATIONS, "INFO=50;CRITICAL=80;BLOCKER=100")));
58 public void do_save_zero() {
59 Settings settings = new Settings();
60 settings.setProperty(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY, "BLOCKER=10;CRITICAL=5;MAJOR=2;MINOR=1;INFO=0");
61 DecoratorContext context = mock(DecoratorContext.class);
63 WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
65 decorator.decorate(context);
67 verify(context).saveMeasure(any(Measure.class));
71 public void should_load_severity_weights_at_startup() {
72 Settings settings = new Settings();
73 settings.setProperty(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY, "BLOCKER=2;CRITICAL=1;MAJOR=0;MINOR=0;INFO=0");
75 WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
78 assertThat(decorator.getWeightsBySeverity().get(RulePriority.BLOCKER)).isEqualTo(2);
79 assertThat(decorator.getWeightsBySeverity().get(RulePriority.CRITICAL)).isEqualTo(1);
80 assertThat(decorator.getWeightsBySeverity().get(RulePriority.MAJOR)).isEqualTo(0);
84 public void weights_setting_should_be_optional() {
85 Settings settings = new Settings();
86 settings.setProperty(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY, "BLOCKER=2");
88 WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
91 assertThat(decorator.getWeightsBySeverity().get(RulePriority.MAJOR)).isEqualTo(1);