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.Before;
23 import org.junit.Test;
24 import org.mockito.Matchers;
25 import org.sonar.api.batch.DecoratorContext;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.resources.Resource;
29 import org.sonar.api.resources.Scopes;
31 import static org.fest.assertions.Assertions.assertThat;
32 import static org.mockito.Mockito.*;
34 public class IssuesDensityDecoratorTest {
36 private IssuesDensityDecorator decorator;
37 private Resource resource;
40 public void before() {
41 resource = mock(Resource.class);
42 when(resource.getScope()).thenReturn(Scopes.PROJECT);
43 decorator = new IssuesDensityDecorator();
47 public void calculate_density() {
48 assertThat(IssuesDensityDecorator.calculate(4000, 200)).isEqualTo(0.0);
49 assertThat(IssuesDensityDecorator.calculate(200, 200)).isEqualTo(0.0);
50 assertThat(IssuesDensityDecorator.calculate(50, 200)).isEqualTo(75.0);
51 assertThat(IssuesDensityDecorator.calculate(0, 200)).isEqualTo(100.0);
55 public void decorate_density() {
56 DecoratorContext context = mock(DecoratorContext.class);
57 when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
58 when(context.getMeasure(CoreMetrics.WEIGHTED_ISSUES)).thenReturn(new Measure(CoreMetrics.WEIGHTED_ISSUES, 50.0));
60 decorator.decorate(resource, context);
62 verify(context).saveMeasure(CoreMetrics.ISSUES_DENSITY, 75.0);
66 public void no_density_if_no_ncloc() {
67 DecoratorContext context = mock(DecoratorContext.class);
68 when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 0.0));
69 when(context.getMeasure(CoreMetrics.WEIGHTED_ISSUES)).thenReturn(new Measure(CoreMetrics.WEIGHTED_ISSUES, 50.0));
71 decorator.decorate(resource, context);
73 verify(context, never()).saveMeasure(Matchers.eq(CoreMetrics.ISSUES_DENSITY), Matchers.anyDouble());
77 public void save_density_if_value_is_zero() {
78 DecoratorContext context = mock(DecoratorContext.class);
79 when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
80 when(context.getMeasure(CoreMetrics.WEIGHTED_ISSUES)).thenReturn(new Measure(CoreMetrics.WEIGHTED_ISSUES, 5000.0));
82 decorator.decorate(resource, context);
84 verify(context).saveMeasure(CoreMetrics.ISSUES_DENSITY, 0.0);
88 public void density_is_hundred_when_no_debt() {
89 DecoratorContext context = mock(DecoratorContext.class);
90 when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
92 decorator.decorate(resource, context);
94 verify(context).saveMeasure(CoreMetrics.ISSUES_DENSITY, 100.0);
98 public void density_is_hundred_when_debt_is_zero() {
99 DecoratorContext context = mock(DecoratorContext.class);
100 when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 200.0));
101 when(context.getMeasure(CoreMetrics.WEIGHTED_ISSUES)).thenReturn(new Measure(CoreMetrics.WEIGHTED_ISSUES, 0.0));
103 decorator.decorate(resource, context);
105 verify(context).saveMeasure(CoreMetrics.ISSUES_DENSITY, 100.0);