]> source.dussan.org Git - sonarqube.git/blob
df87cf5ac803f3d29961cc8a75ad6ca1b7691ab3
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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.plugins.core.issue;
21
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;
30
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.*;
35
36
37 public class WeightedIssuesDecoratorTest {
38
39   @Test
40   public void test_weighted_issues() {
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_ISSUES)).thenReturn(new Measure(CoreMetrics.INFO_ISSUES, 50.0));
46     when(context.getMeasure(CoreMetrics.CRITICAL_ISSUES)).thenReturn(new Measure(CoreMetrics.CRITICAL_ISSUES, 80.0));
47     when(context.getMeasure(CoreMetrics.BLOCKER_ISSUES)).thenReturn(new Measure(CoreMetrics.BLOCKER_ISSUES, 100.0));
48
49     decorator.start();
50     decorator.decorate(context);
51
52     verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.WEIGHTED_ISSUES, (double) (100 * 10 + 80 * 5 + 50 * 0))));
53     verify(context).saveMeasure(argThat(new IsMeasure(CoreMetrics.WEIGHTED_ISSUES, "INFO=50;CRITICAL=80;BLOCKER=100")));
54   }
55
56   // SONAR-3092
57   @Test
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);
62
63     WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
64     decorator.start();
65     decorator.decorate(context);
66
67     verify(context).saveMeasure(any(Measure.class));
68   }
69
70   @Test
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");
74
75     WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
76     decorator.start();
77
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);
81   }
82
83   @Test
84   public void weights_setting_should_be_optional() {
85     Settings settings = new Settings();
86     settings.setProperty(CoreProperties.CORE_RULE_WEIGHTS_PROPERTY, "BLOCKER=2");
87
88     WeightedIssuesDecorator decorator = new WeightedIssuesDecorator(settings);
89     decorator.start();
90
91     assertThat(decorator.getWeightsBySeverity().get(RulePriority.MAJOR)).isEqualTo(1);
92   }
93 }