3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.measures.CoreMetrics;
29 import org.sonar.api.utils.MessageException;
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;
37 public class RatingSettingsTest {
39 private Settings settings;
42 public ExpectedException throwable = ExpectedException.none();
46 settings = new Settings();
50 public void load_rating_grid() {
51 settings.setProperty(CoreProperties.RATING_GRID, "1,3.4,8,50");
52 RatingSettings configurationLoader = new RatingSettings(settings);
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);
63 public void load_work_units_for_language() {
64 settings.setProperty(DEVELOPMENT_COST, "50");
65 RatingSettings configurationLoader = new RatingSettings(settings);
67 assertThat(configurationLoader.getDevCost("defaultLanguage")).isEqualTo(50L);
71 public void load_overridden_values_for_language() {
73 String aLanguage = "aLanguage";
74 String anotherLanguage = "anotherLanguage";
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);
84 RatingSettings configurationLoader = new RatingSettings(settings);
86 assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(30L);
87 assertThat(configurationLoader.getDevCost(anotherLanguage)).isEqualTo(40L);
91 public void fail_on_invalid_rating_grid_configuration() {
92 RatingSettings configurationLoader = new RatingSettings(settings);
94 throwable.expect(IllegalArgumentException.class);
95 settings.setProperty(CoreProperties.RATING_GRID, "a b c");
97 configurationLoader.getRatingGrid();
101 public void use_generic_value_when_specific_setting_is_missing() {
102 String aLanguage = "aLanguage";
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");
109 RatingSettings configurationLoader = new RatingSettings(settings);
111 assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(40L);
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");
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.");
124 new RatingSettings(settings);