]> source.dussan.org Git - sonarqube.git/blob
fbcadb3009e31ff03f329e15572ae070507ced40
[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.sensors;
21
22 import org.junit.Test;
23 import org.sonar.api.batch.DecoratorContext;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.api.measures.Measure;
26 import org.sonar.api.resources.Resource;
27
28 import static org.hamcrest.CoreMatchers.is;
29 import static org.junit.Assert.assertThat;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.*;
32
33 public class ViolationsDensityDecoratorTest {
34
35   @Test
36   public void calculateDensity() {
37     assertThat(ViolationsDensityDecorator.calculate(4000, 200), is(0.0));
38     assertThat(ViolationsDensityDecorator.calculate(200, 200), is(0.0));
39     assertThat(ViolationsDensityDecorator.calculate(50, 200), is(75.0));
40     assertThat(ViolationsDensityDecorator.calculate(0, 200), is(100.0));
41   }
42
43
44   @Test
45   public void decorateDensity() {
46     Resource resource = mock(Resource.class);
47     when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
48
49     DecoratorContext context = mock(DecoratorContext.class);
50     when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
51     when(context.getMeasure(CoreMetrics.WEIGHTED_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.WEIGHTED_VIOLATIONS, 50.0));
52
53     ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
54     decorator.decorate(resource, context);
55
56     verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 75.0);
57   }
58
59   @Test
60   public void noDensityIfNoNcloc() {
61     Resource resource = mock(Resource.class);
62     when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
63
64     DecoratorContext context = mock(DecoratorContext.class);
65     when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 0.0));
66     when(context.getMeasure(CoreMetrics.WEIGHTED_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.WEIGHTED_VIOLATIONS, 50.0));
67
68     ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
69     decorator.decorate(resource, context);
70
71     verify(context, never()).saveMeasure(eq(CoreMetrics.VIOLATIONS_DENSITY), anyDouble());
72   }
73
74   @Test
75   public void saveDensityIfValueIsZero() {
76     Resource resource = mock(Resource.class);
77     when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
78
79     DecoratorContext context = mock(DecoratorContext.class);
80     when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
81     when(context.getMeasure(CoreMetrics.WEIGHTED_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.WEIGHTED_VIOLATIONS, 5000.0));
82
83     ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
84     decorator.decorate(resource, context);
85
86     verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 0.0);
87   }
88
89   @Test
90   public void densityIsHundredWhenNoDebt() {
91     Resource resource = mock(Resource.class);
92     when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
93
94     DecoratorContext context = mock(DecoratorContext.class);
95     when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
96
97     ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
98     decorator.decorate(resource, context);
99
100     verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 100.0);
101   }
102
103   @Test
104   public void densityIsHundredWhenDebtIsZero() {
105     Resource resource = mock(Resource.class);
106     when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
107
108     DecoratorContext context = mock(DecoratorContext.class);
109     when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
110     when(context.getMeasure(CoreMetrics.WEIGHTED_VIOLATIONS)).thenReturn(new Measure(CoreMetrics.WEIGHTED_VIOLATIONS, 0.0));
111
112     ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
113     decorator.decorate(resource, context);
114
115     verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 100.0);
116   }
117 }