aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/measures/CountDistributionBuilderTest.java
blob: 2a832c77bce9c3c7a5d0f29f05bf6f9bded1acd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.sonar.api.measures;

import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class CountDistributionBuilderTest {
  @Test
  public void buildDistribution() {
    CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION);
    Measure measure = builder
        .add("foo")
        .add("bar")
        .add("foo")
        .add("hello")
        .build();

    assertThat(measure.getData(), is("bar=1;foo=2;hello=1"));
  }

  @Test
  public void addZeroValues() {
    CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION);
    Measure measure = builder
        .addZero("foo")
        .add("bar")
        .add("foo")
        .addZero("hello")
        .build();

    assertThat(measure.getData(), is("bar=1;foo=1;hello=0"));
  }

  @Test
  public void addDistributionMeasureAsStrings() {
    Measure measureToAdd = mock(Measure.class);
    when(measureToAdd.getData()).thenReturn("foo=3;hello=5;none=0");

    CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION);
    Measure measure = builder
        .add("bar")
        .add("foo")
        .add(measureToAdd)
        .build();

    assertThat(measure.getData(), is("bar=1;foo=4;hello=5;none=0"));
  }

  @Test
  public void intervalsAreSorted() {
    Measure measureToAdd = mock(Measure.class);
    when(measureToAdd.getData()).thenReturn("10=5;3=2;1=3");

    CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION);
    Measure measure = builder
        .add(10)
        .add(measureToAdd)
        .add(1)
        .build();

    assertThat(measure.getData(), is("1=4;3=2;10=6"));
  }

}