]> source.dussan.org Git - sonarqube.git/blob
596697900d9837666c3f1a08b833eed45f6989e1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2018 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program 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  * This program 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.server.computation.task.projectanalysis.qualitymodel;
21
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28
29 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rating.A;
30 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rating.B;
31 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rating.C;
32 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rating.D;
33 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rating.E;
34
35 public class DebtRatingGridTest {
36
37   private DebtRatingGrid ratingGrid;
38
39   @Rule
40   public ExpectedException throwable = ExpectedException.none();
41
42   @Before
43   public void setUp() {
44     double[] gridValues = new double[] {0.1, 0.2, 0.5, 1};
45     ratingGrid = new DebtRatingGrid(gridValues);
46   }
47
48   @Test
49   public void return_rating_matching_density() {
50     assertThat(ratingGrid.getRatingForDensity(0)).isEqualTo(A);
51     assertThat(ratingGrid.getRatingForDensity(0.05)).isEqualTo(A);
52     assertThat(ratingGrid.getRatingForDensity(0.09999999)).isEqualTo(A);
53     assertThat(ratingGrid.getRatingForDensity(0.1)).isEqualTo(B);
54     assertThat(ratingGrid.getRatingForDensity(0.15)).isEqualTo(B);
55     assertThat(ratingGrid.getRatingForDensity(0.2)).isEqualTo(C);
56     assertThat(ratingGrid.getRatingForDensity(0.25)).isEqualTo(C);
57     assertThat(ratingGrid.getRatingForDensity(0.5)).isEqualTo(D);
58     assertThat(ratingGrid.getRatingForDensity(0.65)).isEqualTo(D);
59     assertThat(ratingGrid.getRatingForDensity(1)).isEqualTo(E);
60     assertThat(ratingGrid.getRatingForDensity(1.01)).isEqualTo(E);
61   }
62
63   @Test
64   public void fail_on_invalid_density() {
65     throwable.expect(RuntimeException.class);
66
67     ratingGrid.getRatingForDensity(-1);
68   }
69
70   @Test
71   public void convert_int_to_rating() throws Exception {
72     assertThat(Rating.valueOf(1)).isEqualTo(A);
73     assertThat(Rating.valueOf(2)).isEqualTo(B);
74     assertThat(Rating.valueOf(3)).isEqualTo(C);
75     assertThat(Rating.valueOf(4)).isEqualTo(D);
76     assertThat(Rating.valueOf(5)).isEqualTo(E);
77   }
78
79   @Test
80   public void fail_to_concert_invalid_value() throws Exception {
81     throwable.expect(IllegalArgumentException.class);
82     Rating.valueOf(10);
83   }
84 }