3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.server.computation.task.projectanalysis.qualitymodel;
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;
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;
38 public class RatingSettingsTest {
40 private Settings settings;
43 public ExpectedException throwable = ExpectedException.none();
47 settings = new MapSettings();
51 public void load_rating_grid() {
52 settings.setProperty(CoreProperties.RATING_GRID, "1,3.4,8,50");
53 RatingSettings configurationLoader = new RatingSettings(settings);
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);
64 public void load_work_units_for_language() {
65 settings.setProperty(DEVELOPMENT_COST, "50");
66 RatingSettings configurationLoader = new RatingSettings(settings);
68 assertThat(configurationLoader.getDevCost("defaultLanguage")).isEqualTo(50L);
72 public void load_overridden_values_for_language() {
74 String aLanguage = "aLanguage";
75 String anotherLanguage = "anotherLanguage";
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);
85 RatingSettings configurationLoader = new RatingSettings(settings);
87 assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(30L);
88 assertThat(configurationLoader.getDevCost(anotherLanguage)).isEqualTo(40L);
92 public void fail_on_invalid_rating_grid_configuration() {
93 RatingSettings configurationLoader = new RatingSettings(settings);
95 throwable.expect(IllegalArgumentException.class);
96 settings.setProperty(CoreProperties.RATING_GRID, "a b c");
98 configurationLoader.getRatingGrid();
102 public void use_generic_value_when_specific_setting_is_missing() {
103 String aLanguage = "aLanguage";
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");
110 RatingSettings configurationLoader = new RatingSettings(settings);
112 assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(40L);
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");
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.");
125 new RatingSettings(settings);