1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch.debt;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import static org.assertj.core.api.Assertions.assertThat;
public class SqaleRatingSettingsTest {
private static final Metric[] metrics = {CoreMetrics.NCLOC, CoreMetrics.COMPLEXITY};
private Settings settings;
@Rule
public ExpectedException throwable = ExpectedException.none();
@Before
public void setUp() {
settings = new Settings();
}
@Test
public void load_rating_grid() {
settings.setProperty(CoreProperties.RATING_GRID, "1,3.4,8,50");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
double[] grid = configurationLoader.getRatingGrid();
assertThat(grid).hasSize(4);
assertThat(grid[0]).isEqualTo(1.0);
assertThat(grid[1]).isEqualTo(3.4);
assertThat(grid[2]).isEqualTo(8.0);
assertThat(grid[3]).isEqualTo(50.0);
}
@Test
public void load_work_units_for_language() {
settings.setProperty(CoreProperties.DEVELOPMENT_COST, "50");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
assertThat(configurationLoader.getDevCost("defaultLanguage")).isEqualTo(50L);
}
@Test
public void load_size_metric_for_language() {
settings.setProperty(CoreProperties.SIZE_METRIC, "complexity");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
assertThat(configurationLoader.getSizeMetric("defaultLanguage", metrics)).isEqualTo(CoreMetrics.COMPLEXITY);
}
@Test
public void load_overridden_values_for_language() {
String aLanguage = "aLanguage";
String anotherLanguage = "anotherLanguage";
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS, "0,1");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, aLanguage);
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "30");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY, CoreMetrics.NCLOC_KEY);
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "1" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, anotherLanguage);
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "1" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "40");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "1" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY, CoreMetrics.COMPLEXITY_KEY);
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
assertThat(configurationLoader.getSizeMetric(aLanguage, metrics)).isEqualTo(CoreMetrics.NCLOC);
assertThat(configurationLoader.getSizeMetric(anotherLanguage, metrics)).isEqualTo(CoreMetrics.COMPLEXITY);
assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(30L);
assertThat(configurationLoader.getDevCost(anotherLanguage)).isEqualTo(40L);
}
@Test
public void fail_on_invalid_rating_grid_configuration() {
throwable.expect(IllegalArgumentException.class);
settings.setProperty(CoreProperties.RATING_GRID, "a b c");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
configurationLoader.getRatingGrid();
}
@Test
public void fail_on_invalid_work_unit_value() {
throwable.expect(IllegalArgumentException.class);
settings.setProperty(CoreProperties.DEVELOPMENT_COST, "a");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
try {
configurationLoader.getSizeMetric("aLanguage", metrics);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
@Test
public void fail_on_unknown_metric_key() {
throwable.expect(IllegalArgumentException.class);
settings.setProperty(CoreProperties.SIZE_METRIC, "unknown");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
configurationLoader.getSizeMetric("aLanguage", metrics);
}
@Test
public void use_generic_value_when_specific_setting_is_missing() {
String aLanguage = "aLanguage";
settings.setProperty(CoreProperties.SIZE_METRIC, "complexity");
settings.setProperty(CoreProperties.DEVELOPMENT_COST, "30");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS, "0");
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY, aLanguage);
settings.setProperty(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + "0" + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY, "40");
SqaleRatingSettings configurationLoader = new SqaleRatingSettings(settings);
assertThat(configurationLoader.getSizeMetric(aLanguage, metrics)).isEqualTo(CoreMetrics.COMPLEXITY);
assertThat(configurationLoader.getDevCost(aLanguage)).isEqualTo(40L);
}
}
|