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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/*
* 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.mediumtest;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarPlugin;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.batch.debt.internal.DefaultDebtModel;
import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.duplication.DuplicationGroup;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.batch.sensor.symbol.Symbol;
import org.sonar.api.batch.sensor.test.TestCase;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.batch.bootstrap.PluginsReferential;
import org.sonar.batch.bootstrap.TaskProperties;
import org.sonar.batch.bootstrapper.Batch;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.batch.dependency.DependencyCache;
import org.sonar.batch.dependency.OutgoingDependency;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.highlighting.SyntaxHighlightingData;
import org.sonar.batch.highlighting.SyntaxHighlightingRule;
import org.sonar.batch.index.Cache.Entry;
import org.sonar.batch.index.ComponentDataCache;
import org.sonar.batch.protocol.input.ActiveRule;
import org.sonar.batch.protocol.input.GlobalReferentials;
import org.sonar.batch.protocol.input.ProjectReferentials;
import org.sonar.batch.referential.GlobalReferentialsLoader;
import org.sonar.batch.referential.ProjectReferentialsLoader;
import org.sonar.batch.scan.filesystem.InputPathCache;
import org.sonar.batch.scan2.IssueCache;
import org.sonar.batch.scan2.MeasureCache;
import org.sonar.batch.scan2.ProjectScanContainer;
import org.sonar.batch.scan2.ScanTaskObserver;
import org.sonar.batch.symbol.SymbolData;
import org.sonar.batch.test.CoveragePerTestCache;
import org.sonar.batch.test.TestCaseCache;
import org.sonar.core.plugins.DefaultPluginMetadata;
import org.sonar.core.plugins.RemotePlugin;
import org.sonar.core.source.SnapshotDataTypes;
import javax.annotation.CheckForNull;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Main utility class for writing batch medium tests.
*
*/
public class BatchMediumTester {
private Batch batch;
public static BatchMediumTesterBuilder builder() {
return new BatchMediumTesterBuilder().registerCoreMetrics();
}
public static class BatchMediumTesterBuilder {
private final FakeGlobalReferentialsLoader globalRefProvider = new FakeGlobalReferentialsLoader();
private final FakeProjectReferentialsLoader projectRefProvider = new FakeProjectReferentialsLoader();
private final FakePluginsReferential pluginsReferential = new FakePluginsReferential();
private final Map<String, String> bootstrapProperties = new HashMap<String, String>();
public BatchMediumTester build() {
return new BatchMediumTester(this);
}
public BatchMediumTesterBuilder registerPlugin(String pluginKey, File location) {
pluginsReferential.addPlugin(pluginKey, location);
return this;
}
public BatchMediumTesterBuilder registerPlugin(String pluginKey, SonarPlugin instance) {
pluginsReferential.addPlugin(pluginKey, instance);
return this;
}
public BatchMediumTesterBuilder registerCoreMetrics() {
for (Metric<?> m : CoreMetrics.getMetrics()) {
registerMetric(m);
}
return this;
}
public BatchMediumTesterBuilder registerMetric(Metric<?> metric) {
globalRefProvider.add(metric);
return this;
}
public BatchMediumTesterBuilder addQProfile(String language, String name) {
projectRefProvider.addQProfile(language, name);
return this;
}
public BatchMediumTesterBuilder addDefaultQProfile(String language, String name) {
addQProfile(language, name);
globalRefProvider.globalSettings().put("sonar.profile." + language, name);
return this;
}
public BatchMediumTesterBuilder bootstrapProperties(Map<String, String> props) {
bootstrapProperties.putAll(props);
return this;
}
public BatchMediumTesterBuilder activateRule(ActiveRule activeRule) {
projectRefProvider.addActiveRule(activeRule);
return this;
}
}
public void start() {
batch.start();
}
public void stop() {
batch.stop();
}
private BatchMediumTester(BatchMediumTesterBuilder builder) {
batch = Batch.builder()
.setEnableLoggingConfiguration(true)
.addComponents(
new EnvironmentInformation("mediumTest", "1.0"),
builder.pluginsReferential,
builder.globalRefProvider,
builder.projectRefProvider,
new DefaultDebtModel())
.setBootstrapProperties(builder.bootstrapProperties)
.build();
}
public TaskBuilder newTask() {
return new TaskBuilder(this);
}
public TaskBuilder newScanTask(File sonarProps) {
Properties prop = new Properties();
FileReader reader = null;
try {
reader = new FileReader(sonarProps);
prop.load(reader);
} catch (Exception e) {
throw new IllegalStateException("Unable to read configuration file", e);
} finally {
if (reader != null) {
IOUtils.closeQuietly(reader);
}
}
TaskBuilder builder = new TaskBuilder(this);
builder.property("sonar.task", "scan");
builder.property("sonar.projectBaseDir", sonarProps.getParentFile().getAbsolutePath());
for (Map.Entry entry : prop.entrySet()) {
builder.property(entry.getKey().toString(), entry.getValue().toString());
}
return builder;
}
public static class TaskBuilder {
private final Map<String, String> taskProperties = new HashMap<String, String>();
private BatchMediumTester tester;
public TaskBuilder(BatchMediumTester tester) {
this.tester = tester;
}
public TaskResult start() {
TaskResult result = new TaskResult();
tester.batch.executeTask(taskProperties,
result
);
return result;
}
public TaskBuilder properties(Map<String, String> props) {
taskProperties.putAll(props);
return this;
}
public TaskBuilder property(String key, String value) {
taskProperties.put(key, value);
return this;
}
}
public static class TaskResult implements ScanTaskObserver {
private static final Logger LOG = LoggerFactory.getLogger(BatchMediumTester.TaskResult.class);
private List<Issue> issues = new ArrayList<Issue>();
private List<Measure> measures = new ArrayList<Measure>();
private Map<String, List<DuplicationGroup>> duplications = new HashMap<String, List<DuplicationGroup>>();
private List<InputFile> inputFiles = new ArrayList<InputFile>();
private List<InputDir> inputDirs = new ArrayList<InputDir>();
private Map<InputFile, SyntaxHighlightingData> highlightingPerFile = new HashMap<InputFile, SyntaxHighlightingData>();
private Map<InputFile, SymbolData> symbolTablePerFile = new HashMap<InputFile, SymbolData>();
private Map<String, Map<String, TestCase>> testCasesPerFile = new HashMap<String, Map<String, TestCase>>();
private Map<String, Map<String, Map<String, List<Integer>>>> coveragePerTest = new HashMap<String, Map<String, Map<String, List<Integer>>>>();
private Map<String, Map<String, Integer>> dependencies = new HashMap<String, Map<String, Integer>>();
@Override
public void scanTaskCompleted(ProjectScanContainer container) {
LOG.info("Store analysis results in memory for later assertions in medium test");
for (Issue issue : container.getComponentByType(IssueCache.class).all()) {
issues.add(issue);
}
for (DefaultMeasure<?> measure : container.getComponentByType(MeasureCache.class).all()) {
measures.add(measure);
}
storeFs(container);
storeComponentData(container);
storeDuplication(container);
storeTestCases(container);
storeCoveragePerTest(container);
storeDependencies(container);
}
private void storeCoveragePerTest(ProjectScanContainer container) {
CoveragePerTestCache coveragePerTestCache = container.getComponentByType(CoveragePerTestCache.class);
for (Entry<List<Integer>> entry : coveragePerTestCache.entries()) {
String testFileKey = entry.key()[0].toString();
if (!coveragePerTest.containsKey(testFileKey)) {
coveragePerTest.put(testFileKey, new HashMap<String, Map<String, List<Integer>>>());
}
String testName = entry.key()[1].toString();
if (!coveragePerTest.get(testFileKey).containsKey(testName)) {
coveragePerTest.get(testFileKey).put(testName, new HashMap<String, List<Integer>>());
}
coveragePerTest.get(testFileKey).get(testName).put(entry.key()[2].toString(), entry.value());
}
}
private void storeTestCases(ProjectScanContainer container) {
TestCaseCache testCaseCache = container.getComponentByType(TestCaseCache.class);
for (Entry<TestCase> entry : testCaseCache.entries()) {
String effectiveKey = entry.key()[0].toString();
if (!testCasesPerFile.containsKey(effectiveKey)) {
testCasesPerFile.put(effectiveKey, new HashMap<String, TestCase>());
}
testCasesPerFile.get(effectiveKey).put(entry.value().name(), entry.value());
}
}
private void storeDuplication(ProjectScanContainer container) {
DuplicationCache duplicationCache = container.getComponentByType(DuplicationCache.class);
for (Entry<List<DuplicationGroup>> entry : duplicationCache.entries()) {
String effectiveKey = entry.key()[0].toString();
duplications.put(effectiveKey, entry.value());
}
}
private void storeComponentData(ProjectScanContainer container) {
ComponentDataCache componentDataCache = container.getComponentByType(ComponentDataCache.class);
for (InputFile file : inputFiles) {
SyntaxHighlightingData highlighting = componentDataCache.getData(((DefaultInputFile) file).key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING);
if (highlighting != null) {
highlightingPerFile.put(file, highlighting);
}
SymbolData symbolTable = componentDataCache.getData(((DefaultInputFile) file).key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING);
if (symbolTable != null) {
symbolTablePerFile.put(file, symbolTable);
}
}
}
private void storeFs(ProjectScanContainer container) {
InputPathCache inputFileCache = container.getComponentByType(InputPathCache.class);
for (InputPath inputPath : inputFileCache.all()) {
if (inputPath instanceof InputFile) {
inputFiles.add((InputFile) inputPath);
} else {
inputDirs.add((InputDir) inputPath);
}
}
}
private void storeDependencies(ProjectScanContainer container) {
DependencyCache dependencyCache = container.getComponentByType(DependencyCache.class);
for (Entry<OutgoingDependency> entry : dependencyCache.entries()) {
String fromKey = entry.key()[1].toString();
String toKey = entry.key()[2].toString();
if (!dependencies.containsKey(fromKey)) {
dependencies.put(fromKey, new HashMap<String, Integer>());
}
dependencies.get(fromKey).put(toKey, entry.value().weight());
}
}
public List<Issue> issues() {
return issues;
}
public List<Measure> measures() {
return measures;
}
public List<InputFile> inputFiles() {
return inputFiles;
}
public List<InputDir> inputDirs() {
return inputDirs;
}
public List<DuplicationGroup> duplicationsFor(InputFile inputFile) {
return duplications.get(((DefaultInputFile) inputFile).key());
}
public Collection<TestCase> testCasesFor(InputFile inputFile) {
String key = ((DefaultInputFile) inputFile).key();
if (testCasesPerFile.containsKey(key)) {
return testCasesPerFile.get(key).values();
} else {
return Collections.emptyList();
}
}
public TestCase testCase(InputFile inputFile, String testCaseName) {
return testCasesPerFile.get(((DefaultInputFile) inputFile).key()).get(testCaseName);
}
public List<Integer> coveragePerTest(InputFile testFile, String testCaseName, InputFile mainFile) {
String testKey = ((DefaultInputFile) testFile).key();
String mainKey = ((DefaultInputFile) mainFile).key();
if (coveragePerTest.containsKey(testKey) && coveragePerTest.get(testKey).containsKey(testCaseName) && coveragePerTest.get(testKey).get(testCaseName).containsKey(mainKey)) {
return coveragePerTest.get(testKey).get(testCaseName).get(mainKey);
} else {
return Collections.emptyList();
}
}
/**
* Get highlighting types at a given position in an inputfile
* @param charIndex 0-based offset in file
*/
public List<TypeOfText> highlightingTypeFor(InputFile file, int charIndex) {
SyntaxHighlightingData syntaxHighlightingData = highlightingPerFile.get(file);
if (syntaxHighlightingData == null) {
return Collections.emptyList();
}
List<TypeOfText> result = new ArrayList<TypeOfText>();
for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.syntaxHighlightingRuleSet()) {
if (sortedRule.getStartPosition() <= charIndex && sortedRule.getEndPosition() > charIndex) {
result.add(sortedRule.getTextType());
}
}
return result;
}
/**
* Get list of all positions of a symbol in an inputfile
* @param symbolStartOffset 0-based start offset for the symbol in file
* @param symbolEndOffset 0-based end offset for the symbol in file
*/
@CheckForNull
public Set<Integer> symbolReferencesFor(InputFile file, int symbolStartOffset, int symbolEndOffset) {
SymbolData data = symbolTablePerFile.get(file);
if (data == null) {
return null;
}
for (Symbol symbol : data.referencesBySymbol().keySet()) {
if (symbol.getDeclarationStartOffset() == symbolStartOffset && symbol.getDeclarationEndOffset() == symbolEndOffset) {
return data.referencesBySymbol().get(symbol);
}
}
return null;
}
/**
* @return null if no dependency else return dependency weight.
*/
@CheckForNull
public Integer dependencyWeight(InputFile from, InputFile to) {
String fromKey = ((DefaultInputFile) from).key();
String toKey = ((DefaultInputFile) to).key();
return dependencies.containsKey(fromKey) ? dependencies.get(fromKey).get(toKey) : null;
}
}
private static class FakeGlobalReferentialsLoader implements GlobalReferentialsLoader {
private int metricId = 1;
private GlobalReferentials ref = new GlobalReferentials();
@Override
public GlobalReferentials load() {
return ref;
}
public Map<String, String> globalSettings() {
return ref.globalSettings();
}
public FakeGlobalReferentialsLoader add(Metric metric) {
Boolean optimizedBestValue = metric.isOptimizedBestValue();
ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metricId,
metric.key(),
metric.getType().name(),
metric.getDescription(),
metric.getDirection(),
metric.getName(),
metric.getQualitative(),
metric.getUserManaged(),
metric.getWorstValue(),
metric.getBestValue(),
optimizedBestValue != null ? optimizedBestValue : false));
metricId++;
return this;
}
}
private static class FakeProjectReferentialsLoader implements ProjectReferentialsLoader {
private ProjectReferentials ref = new ProjectReferentials();
@Override
public ProjectReferentials load(ProjectReactor reactor, TaskProperties taskProperties) {
return ref;
}
public FakeProjectReferentialsLoader addQProfile(String language, String name) {
ref.addQProfile(new org.sonar.batch.protocol.input.QProfile(name, name, language, new Date()));
return this;
}
public FakeProjectReferentialsLoader addActiveRule(ActiveRule activeRule) {
ref.addActiveRule(activeRule);
return this;
}
}
private static class FakePluginsReferential implements PluginsReferential {
private List<RemotePlugin> pluginList = new ArrayList<RemotePlugin>();
private Map<RemotePlugin, File> pluginFiles = new HashMap<RemotePlugin, File>();
Map<PluginMetadata, SonarPlugin> localPlugins = new HashMap<PluginMetadata, SonarPlugin>();
@Override
public List<RemotePlugin> pluginList() {
return pluginList;
}
@Override
public File pluginFile(RemotePlugin remote) {
return pluginFiles.get(remote);
}
public FakePluginsReferential addPlugin(String pluginKey, File location) {
RemotePlugin plugin = new RemotePlugin(pluginKey, false);
pluginList.add(plugin);
pluginFiles.put(plugin, location);
return this;
}
public FakePluginsReferential addPlugin(String pluginKey, SonarPlugin pluginInstance) {
localPlugins.put(DefaultPluginMetadata.create(pluginKey), pluginInstance);
return this;
}
@Override
public Map<PluginMetadata, SonarPlugin> localPlugins() {
return localPlugins;
}
}
}
|