]> source.dussan.org Git - sonarqube.git/blob
f60f3d0e7d8c8f68696a905785aeb6db2f3484f1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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 import org.sonar.api.CoreProperties;
27 import org.sonar.api.config.Settings;
28 import org.sonar.api.measures.CoreMetrics;
29 import org.sonar.api.utils.MessageException;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.sonar.api.CoreProperties.DEVELOPMENT_COST;
33 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS;
34 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY;
35 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY;
36
37 public class RatingSettingsTest {
38
39   private Settings settings;
40
41   @Rule
42   public ExpectedException throwable = ExpectedException.none();
43
44   @Before
45   public void setUp() {
46     settings = new Settings();
47   }
48
49   @Test
50   public void load_rating_grid() {
51     settings.setProperty(CoreProperties.RATING_GRID, "1,3.4,8,50");
52     RatingSettings configurationLoader = new RatingSettings(settings);
53
54     double[] grid = configurationLoader.getRatingGrid().getGridValues();
55     assertThat(grid).hasSize(4);
56     assertThat(grid[0]).isEqualTo(1.0);
57     assertThat(grid[1]).isEqualTo(3.4);
58     assertThat(grid[2]).isEqualTo(8.0);
59     assertThat(grid[3]).isEqualTo(50.0);
60   }
61
62   @Test
63   public void load_work_units_for_language() {
64     settings.setProperty(DEVELOPMENT_COST, "50");
65     RatingSettings configurationLoader = new RatingSettings(settings);
66
67     assertThat(configurationLoader.getDevCost("defaultLanguage")).isEqualTo(50L);
68   }
69
70   @Test
71   public void load_overridden_values_for_language() {
72
73     String aLanguage = "aLanguage";
74     String anotherLanguage = "anotherLanguage";
75
76     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS, "0,1");
77     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, aLanguage);
78     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "30");
79     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY, CoreMetrics.NCLOC_KEY);
80     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "1" + "." + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, anotherLanguage);
81     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "1" + "." + LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "40");
82     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "1" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY, CoreMetrics.COMPLEXITY_KEY);
83
84     RatingSettings configurationLoader = new RatingSettings(settings);
85
86     assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(30L);
87     assertThat(configurationLoader.getDevCost(anotherLanguage)).isEqualTo(40L);
88   }
89
90   @Test
91   public void fail_on_invalid_rating_grid_configuration() {
92     RatingSettings configurationLoader = new RatingSettings(settings);
93
94     throwable.expect(IllegalArgumentException.class);
95     settings.setProperty(CoreProperties.RATING_GRID, "a b c");
96
97     configurationLoader.getRatingGrid();
98   }
99
100   @Test
101   public void use_generic_value_when_specific_setting_is_missing() {
102     String aLanguage = "aLanguage";
103
104     settings.setProperty(DEVELOPMENT_COST, "30");
105     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS, "0");
106     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, aLanguage);
107     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "40");
108
109     RatingSettings configurationLoader = new RatingSettings(settings);
110
111     assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(40L);
112   }
113
114   @Test
115   public void constructor_fails_with_ME_if_language_specific_parameter_language_is_missing() {
116     settings.setProperty(DEVELOPMENT_COST, "30");
117     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS, "0");
118     settings.setProperty(LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "40");
119
120     throwable.expect(MessageException.class);
121     throwable.expectMessage("Technical debt configuration is corrupted. At least one language specific parameter has no Language key. " +
122       "Contact your administrator to update this configuration in the global administration section of SonarQube.");
123
124     new RatingSettings(settings);
125   }
126 }