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.sensors;
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;
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.*;
33 public class ViolationsDensityDecoratorTest {
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));
45 public void decorateDensity() {
46 Resource resource = mock(Resource.class);
47 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
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));
53 ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
54 decorator.decorate(resource, context);
56 verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 75.0);
60 public void noDensityIfNoNcloc() {
61 Resource resource = mock(Resource.class);
62 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
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));
68 ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
69 decorator.decorate(resource, context);
71 verify(context, never()).saveMeasure(eq(CoreMetrics.VIOLATIONS_DENSITY), anyDouble());
75 public void saveDensityIfValueIsZero() {
76 Resource resource = mock(Resource.class);
77 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
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));
83 ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
84 decorator.decorate(resource, context);
86 verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 0.0);
90 public void densityIsHundredWhenNoDebt() {
91 Resource resource = mock(Resource.class);
92 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
94 DecoratorContext context = mock(DecoratorContext.class);
95 when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
97 ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
98 decorator.decorate(resource, context);
100 verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 100.0);
104 public void densityIsHundredWhenDebtIsZero() {
105 Resource resource = mock(Resource.class);
106 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
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));
112 ViolationsDensityDecorator decorator = new ViolationsDensityDecorator();
113 decorator.decorate(resource, context);
115 verify(context).saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, 100.0);