-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
DefaultSensorStorageTest.java
324 lines (269 loc) · 13.4 KB
/
DefaultSensorStorageTest.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.scanner.sensor;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.core.metric.ScannerMetrics;
import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;
import org.sonar.scanner.issue.ModuleIssues;
import org.sonar.scanner.protocol.output.FileStructure;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.report.ReportPublisher;
import org.sonar.scanner.repository.ContextPropertiesCache;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.measure.MeasureCache;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
public class DefaultSensorStorageTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public ExpectedException thrown = ExpectedException.none();
private DefaultSensorStorage underTest;
private MapSettings settings;
private ModuleIssues moduleIssues;
private MeasureCache measureCache;
private ScannerReportWriter reportWriter;
private ContextPropertiesCache contextPropertiesCache = new ContextPropertiesCache();
private BranchConfiguration branchConfiguration;
@Before
public void prepare() throws Exception {
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
when(metricFinder.<Integer>findByKey(CoreMetrics.LINES_TO_COVER_KEY)).thenReturn(CoreMetrics.LINES_TO_COVER);
settings = new MapSettings();
moduleIssues = mock(ModuleIssues.class);
measureCache = mock(MeasureCache.class);
ReportPublisher reportPublisher = mock(ReportPublisher.class);
reportWriter = new ScannerReportWriter(temp.newFolder());
when(reportPublisher.getWriter()).thenReturn(reportWriter);
branchConfiguration = mock(BranchConfiguration.class);
underTest = new DefaultSensorStorage(metricFinder,
moduleIssues, settings.asConfig(), reportPublisher, measureCache,
mock(SonarCpdBlockIndex.class), contextPropertiesCache, new ScannerMetrics(), branchConfiguration);
}
@Test
public void shouldFailIfUnknownMetric() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Unknown metric: lines");
underTest.store(new DefaultMeasure()
.on(file)
.forMetric(CoreMetrics.LINES)
.withValue(10));
}
@Test
public void should_save_issue() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
DefaultIssue issue = new DefaultIssue().at(new DefaultIssueLocation().on(file));
underTest.store(issue);
ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class);
verify(moduleIssues).initAndAddIssue(argumentCaptor.capture());
assertThat(argumentCaptor.getValue()).isEqualTo(issue);
}
@Test
public void should_save_external_issue() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
DefaultExternalIssue externalIssue = new DefaultExternalIssue().at(new DefaultIssueLocation().on(file));
underTest.store(externalIssue);
ArgumentCaptor<ExternalIssue> argumentCaptor = ArgumentCaptor.forClass(ExternalIssue.class);
verify(moduleIssues).initAndAddExternalIssue(argumentCaptor.capture());
assertThat(argumentCaptor.getValue()).isEqualTo(externalIssue);
}
@Test
public void should_skip_issue_on_short_branch_when_file_status_is_SAME() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
DefaultIssue issue = new DefaultIssue().at(new DefaultIssueLocation().on(file));
underTest.store(issue);
verifyZeroInteractions(moduleIssues);
}
@Test
public void should_save_highlighting() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("// comment").build();
DefaultHighlighting highlighting = new DefaultHighlighting(underTest).onFile(file).highlight(0, 1, TypeOfText.KEYWORD);
underTest.store(highlighting);
assertThat(reportWriter.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, file.batchId())).isTrue();
}
@Test
public void should_skip_highlighting_on_short_branch_when_file_status_is_SAME() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("// comment")
.setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
DefaultHighlighting highlighting = new DefaultHighlighting(underTest).onFile(file).highlight(0, 1, TypeOfText.KEYWORD);
underTest.store(highlighting);
assertThat(reportWriter.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, file.batchId())).isFalse();
}
@Test
public void should_save_file_measure() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
when(measureCache.put(eq(file.key()), eq(CoreMetrics.NCLOC_KEY), argumentCaptor.capture())).thenReturn(null);
underTest.store(new DefaultMeasure()
.on(file)
.forMetric(CoreMetrics.NCLOC)
.withValue(10));
DefaultMeasure m = argumentCaptor.getValue();
assertThat(m.value()).isEqualTo(10);
assertThat(m.metric()).isEqualTo(CoreMetrics.NCLOC);
}
@Test
public void should_not_skip_file_measures_on_short_lived_branch_or_pull_request_when_file_status_is_SAME() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
when(measureCache.put(eq(file.key()), eq(CoreMetrics.LINES_TO_COVER_KEY), argumentCaptor.capture())).thenReturn(null);
underTest.store(new DefaultMeasure()
.on(file)
.forMetric(CoreMetrics.LINES_TO_COVER)
.withValue(10));
DefaultMeasure m = argumentCaptor.getValue();
assertThat(m.value()).isEqualTo(10);
assertThat(m.metric()).isEqualTo(CoreMetrics.LINES_TO_COVER);
}
@Test
public void should_skip_significant_code_on_pull_request_when_file_status_is_SAME() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setStatus(InputFile.Status.SAME)
.setContents("foo")
.build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
underTest.store(new DefaultSignificantCode()
.onFile(file)
.addRange(file.selectLine(1)));
assertThat(reportWriter.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, file.batchId())).isFalse();
}
@Test
public void should_save_significant_code() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("foo")
.build();
underTest.store(new DefaultSignificantCode()
.onFile(file)
.addRange(file.selectLine(1)));
assertThat(reportWriter.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, file.batchId())).isTrue();
}
@Test
public void should_save_project_measure() throws IOException {
String projectKey = "myProject";
DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create().setKey(projectKey).setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
when(measureCache.put(eq(module.key()), eq(CoreMetrics.NCLOC_KEY), argumentCaptor.capture())).thenReturn(null);
underTest.store(new DefaultMeasure()
.on(module)
.forMetric(CoreMetrics.NCLOC)
.withValue(10));
DefaultMeasure m = argumentCaptor.getValue();
assertThat(m.value()).isEqualTo(10);
assertThat(m.metric()).isEqualTo(CoreMetrics.NCLOC);
}
@Test(expected = UnsupportedOperationException.class)
public void duplicateHighlighting() throws Exception {
InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
.setModuleBaseDir(temp.newFolder().toPath()).build();
DefaultHighlighting h = new DefaultHighlighting(null)
.onFile(inputFile);
underTest.store(h);
underTest.store(h);
}
@Test(expected = UnsupportedOperationException.class)
public void duplicateSignificantCode() throws Exception {
InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
.setModuleBaseDir(temp.newFolder().toPath()).build();
DefaultSignificantCode h = new DefaultSignificantCode(null)
.onFile(inputFile);
underTest.store(h);
underTest.store(h);
}
@Test(expected = UnsupportedOperationException.class)
public void duplicateSymbolTable() throws Exception {
InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
.setModuleBaseDir(temp.newFolder().toPath()).build();
DefaultSymbolTable st = new DefaultSymbolTable(null)
.onFile(inputFile);
underTest.store(st);
underTest.store(st);
}
@Test
public void shouldStoreContextProperty() {
underTest.storeProperty("foo", "bar");
assertThat(contextPropertiesCache.getAll()).containsOnly(entry("foo", "bar"));
}
@Test
public void shouldValidateStrictlyPositiveLine() throws Exception {
InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build();
Map<Integer, Integer> map = ImmutableMap.of(0, 3);
String data = KeyValueFormat.format(map);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("must be > 0");
underTest.validateCoverageMeasure(data, file);
}
@Test
public void shouldValidateMaxLine() throws Exception {
InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build();
Map<Integer, Integer> map = ImmutableMap.of(11, 3);
String data = KeyValueFormat.format(map);
thrown.expect(IllegalStateException.class);
underTest.validateCoverageMeasure(data, file);
}
@Test
public void mergeCoverageLineMetrics_should_be_sorted() {
assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=1", "1=1")).isEqualTo("1=2");
assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=1", "2=1")).isEqualTo("1=1;2=1");
assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA, "2=1", "1=1")).isEqualTo("1=1;2=1");
assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "1=1", "1=1")).isEqualTo("1=1");
assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "1=1", "2=1")).isEqualTo("1=1;2=1");
assertThat(DefaultSensorStorage.mergeCoverageLineMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "2=1", "1=1")).isEqualTo("1=1;2=1");
}
}