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