Skip to content

Commit 6fc2980

Browse files
author
Gilles Grousset
committed
API deprecations removal
But still a lot to do...
1 parent d555a39 commit 6fc2980

File tree

30 files changed

+478
-496
lines changed

30 files changed

+478
-496
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* commons - Enables analysis of Swift and Objective-C projects into SonarQube.
3+
* Copyright © 2015 Backelite (${email})
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.backelite.sonarqube.commons;
19+
20+
import org.sonar.api.batch.fs.InputComponent;
21+
import org.sonar.api.batch.sensor.SensorContext;
22+
import org.sonar.api.measures.Metric;
23+
24+
/**
25+
* Created by gillesgrousset on 29/08/2018.
26+
*/
27+
public final class MeasureUtil {
28+
29+
public static void saveMeasure(SensorContext context, InputComponent component, Metric<Integer> metric, int value) {
30+
context.<Integer>newMeasure()
31+
.on(component)
32+
.forMetric(metric)
33+
.withValue(value)
34+
.save();
35+
}
36+
37+
public static void saveMeasure(SensorContext context, InputComponent component, Metric<Long> metric, long value) {
38+
context.<Long>newMeasure()
39+
.on(component)
40+
.forMetric(metric)
41+
.withValue(value)
42+
.save();
43+
}
44+
45+
public static void saveMeasure(SensorContext context, InputComponent component, Metric<Double> metric, double value) {
46+
context.<Double>newMeasure()
47+
.on(component)
48+
.forMetric(metric)
49+
.withValue(value)
50+
.save();
51+
}
52+
}
Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
*/
1818
package com.backelite.sonarqube.commons.surefire;
1919

20+
import com.backelite.sonarqube.commons.MeasureUtil;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
22-
import org.sonar.api.batch.SensorContext;
2323
import org.sonar.api.batch.fs.FileSystem;
24+
import org.sonar.api.batch.fs.InputComponent;
25+
import org.sonar.api.batch.fs.InputFile;
26+
import org.sonar.api.batch.sensor.SensorContext;
2427
import org.sonar.api.component.ResourcePerspectives;
2528
import org.sonar.api.measures.CoreMetrics;
2629
import org.sonar.api.measures.Metric;
27-
import org.sonar.api.resources.Resource;
2830
import org.sonar.api.test.MutableTestPlan;
2931
import org.sonar.api.test.TestCase;
30-
import org.sonar.api.utils.ParsingUtils;
3132
import org.sonar.api.utils.StaxParser;
3233

3334
import javax.annotation.Nullable;
@@ -39,21 +40,21 @@
3940
/**
4041
* Created by gillesgrousset on 28/08/2018.
4142
*/
42-
public abstract class BaseSurefireParser {
43+
public class SurefireParser {
4344

44-
protected static final Logger LOGGER = LoggerFactory.getLogger(BaseSurefireParser.class);
45+
protected static final Logger LOGGER = LoggerFactory.getLogger(SurefireParser.class);
4546

4647
protected final FileSystem fileSystem;
4748
protected final SensorContext context;
4849
protected final ResourcePerspectives perspectives;
4950

50-
protected BaseSurefireParser(FileSystem fileSystem, ResourcePerspectives perspectives, SensorContext context) {
51+
protected SurefireParser(FileSystem fileSystem, ResourcePerspectives perspectives, SensorContext context) {
5152
this.fileSystem = fileSystem;
5253
this.perspectives = perspectives;
5354
this.context = context;
5455
}
5556

56-
private static void parseFiles(File[] reports, UnitTestIndex index) {
57+
private void parseFiles(File[] reports, UnitTestIndex index) {
5758
SurefireStaxHandler staxParser = new SurefireStaxHandler(index);
5859
StaxParser parser = new StaxParser(staxParser, false);
5960
for (File report : reports) {
@@ -63,16 +64,15 @@ private static void parseFiles(File[] reports, UnitTestIndex index) {
6364
throw new IllegalStateException("Fail to parse the Surefire report: " + report, e);
6465
}
6566
}
67+
6668
}
6769

6870
public void collect(File reportsDir) {
6971

7072

7173
File[] xmlFiles = getReports(reportsDir);
7274

73-
if (xmlFiles.length == 0) {
74-
insertZeroWhenNoReports();
75-
} else {
75+
if (xmlFiles.length > 0) {
7676
parseFiles(xmlFiles);
7777
}
7878
}
@@ -91,9 +91,6 @@ public boolean accept(File dir, String name) {
9191
});
9292
}
9393

94-
private void insertZeroWhenNoReports() {
95-
context.saveMeasure(CoreMetrics.TESTS, 0.0);
96-
}
9794

9895
private void parseFiles(File[] reports) {
9996
UnitTestIndex index = new UnitTestIndex();
@@ -102,41 +99,55 @@ private void parseFiles(File[] reports) {
10299
}
103100

104101
private void save(UnitTestIndex index) {
102+
103+
105104
long negativeTimeTestNumber = 0;
105+
int testsCount = 0;
106+
int testsSkipped = 0;
107+
int testsErrors = 0;
108+
int testsFailures = 0;
109+
long testsTime = 0;
106110

107111
for (Map.Entry<String, UnitTestClassReport> entry : index.getIndexByClassname().entrySet()) {
112+
108113
UnitTestClassReport report = entry.getValue();
114+
115+
testsCount += report.getTests() - report.getSkipped();
116+
testsSkipped += report.getSkipped();
117+
testsErrors += report.getErrors();
118+
testsFailures += report.getFailures();
119+
109120
if (report.getTests() > 0) {
121+
110122
negativeTimeTestNumber += report.getNegativeTimeTestNumber();
111-
Resource resource = getUnitTestResource(entry.getKey());
112-
if (resource != null) {
113-
save(report, resource);
123+
InputFile inputFile = getUnitTestResource(entry.getKey());
124+
if (inputFile != null) {
125+
saveResults(inputFile, report);
114126
} else {
115127
LOGGER.warn("Resource not found: {}", entry.getKey());
116128
}
129+
117130
}
131+
118132
}
133+
134+
119135
if (negativeTimeTestNumber > 0) {
120136
LOGGER.warn("There is {} test(s) reported with negative time by data, total duration may not be accurate.", negativeTimeTestNumber);
121137
}
122-
}
123138

124-
private void save(UnitTestClassReport report, Resource resource) {
125-
double testsCount = report.getTests() - report.getSkipped();
126-
saveMeasure(resource, CoreMetrics.SKIPPED_TESTS, report.getSkipped());
127-
saveMeasure(resource, CoreMetrics.TESTS, testsCount);
128-
saveMeasure(resource, CoreMetrics.TEST_ERRORS, report.getErrors());
129-
saveMeasure(resource, CoreMetrics.TEST_FAILURES, report.getFailures());
130-
saveMeasure(resource, CoreMetrics.TEST_EXECUTION_TIME, report.getDurationMilliseconds());
131-
double passedTests = testsCount - report.getErrors() - report.getFailures();
132139
if (testsCount > 0) {
133-
double percentage = passedTests * 100d / testsCount;
134-
saveMeasure(resource, CoreMetrics.TEST_SUCCESS_DENSITY, ParsingUtils.scaleValue(percentage));
140+
InputComponent module = context.module();
141+
MeasureUtil.saveMeasure(context, module, CoreMetrics.TESTS, testsCount);
142+
MeasureUtil.saveMeasure(context, module, CoreMetrics.SKIPPED_TESTS, testsSkipped);
143+
MeasureUtil.saveMeasure(context, module, CoreMetrics.TEST_ERRORS, testsErrors);
144+
MeasureUtil.saveMeasure(context, module, CoreMetrics.TEST_FAILURES, testsFailures);
145+
MeasureUtil.saveMeasure(context, module, CoreMetrics.TEST_EXECUTION_TIME, testsTime);
135146
}
136-
saveResults(resource, report);
137147
}
138148

139-
protected void saveResults(Resource testFile, UnitTestClassReport report) {
149+
150+
protected void saveResults(InputFile testFile, UnitTestClassReport report) {
140151
for (UnitTestResult unitTestResult : report.getResults()) {
141152
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, testFile);
142153
if (testPlan != null) {
@@ -151,11 +162,9 @@ protected void saveResults(Resource testFile, UnitTestClassReport report) {
151162
}
152163

153164
@Nullable
154-
public abstract Resource getUnitTestResource(String classname);
155-
156-
private void saveMeasure(Resource resource, Metric metric, double value) {
157-
if (!Double.isNaN(value)) {
158-
context.saveMeasure(resource, metric, value);
159-
}
165+
public InputFile getUnitTestResource(String classname) {
166+
return TestFileFinders.getInstance().getUnitTestResource(fileSystem, classname);
160167
}
168+
169+
161170
}
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,47 @@
2020
import com.backelite.sonarqube.commons.Constants;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
23-
import org.sonar.api.batch.Sensor;
24-
import org.sonar.api.batch.SensorContext;
2523
import org.sonar.api.batch.fs.FileSystem;
24+
import org.sonar.api.batch.fs.InputFile;
25+
import org.sonar.api.batch.sensor.Sensor;
26+
import org.sonar.api.batch.sensor.SensorContext;
27+
import org.sonar.api.batch.sensor.SensorDescriptor;
2628
import org.sonar.api.component.ResourcePerspectives;
2729
import org.sonar.api.config.Settings;
28-
import org.sonar.api.resources.Project;
2930
import org.sonar.api.scan.filesystem.PathResolver;
3031

3132
import java.io.File;
3233

3334
/**
3435
* Created by gillesgrousset on 28/08/2018.
3536
*/
36-
public abstract class BaseSurefireSensor implements Sensor {
37+
public class SurefireSensor implements Sensor {
3738

3839
public static final String REPORTS_PATH_KEY = Constants.PROPERTY_PREFIX + ".surefire.junit.reportsPath";
3940
public static final String DEFAULT_REPORTS_PATH = "sonar-reports/";
40-
protected static final Logger LOGGER = LoggerFactory.getLogger(BaseSurefireSensor.class);
41-
protected final FileSystem fileSystem;
42-
protected final PathResolver pathResolver;
43-
protected final ResourcePerspectives resourcePerspectives;
44-
protected final Settings settings;
4541

46-
protected BaseSurefireSensor(FileSystem fileSystem, PathResolver pathResolver, ResourcePerspectives resourcePerspectives, Settings settings) {
42+
private static final Logger LOGGER = LoggerFactory.getLogger(SurefireSensor.class);
43+
private final FileSystem fileSystem;
44+
private final PathResolver pathResolver;
45+
private final ResourcePerspectives resourcePerspectives;
46+
private final Settings settings;
47+
48+
public SurefireSensor(FileSystem fileSystem, PathResolver pathResolver, ResourcePerspectives resourcePerspectives, Settings settings) {
4749
this.fileSystem = fileSystem;
4850
this.pathResolver = pathResolver;
4951
this.resourcePerspectives = resourcePerspectives;
5052
this.settings = settings;
5153
}
5254

5355
@Override
54-
public abstract boolean shouldExecuteOnProject(Project project);
56+
public void describe(SensorDescriptor descriptor) {
57+
descriptor
58+
.name("Surefire")
59+
.onlyOnFileType(InputFile.Type.MAIN);
60+
}
5561

5662
@Override
57-
public void analyse(Project project, SensorContext context) {
63+
public void execute(SensorContext context) {
5864

5965
String path = this.reportPath();
6066
File reportsDir = pathResolver.relativeFile(fileSystem.baseDir(), path);
@@ -69,7 +75,10 @@ public void analyse(Project project, SensorContext context) {
6975
collect(context, reportsDir);
7076
}
7177

72-
protected abstract void collect(SensorContext context, File reportsDir);
78+
protected void collect(SensorContext context, File reportsDir) {
79+
LOGGER.info("parsing {}", reportsDir);
80+
new SurefireParser(fileSystem, resourcePerspectives, context).collect(reportsDir);
81+
}
7382

7483
protected String reportPath() {
7584
String reportPath = settings.getString(REPORTS_PATH_KEY);

commons/src/main/java/com/backelite/sonarqube/commons/surefire/SurefireStaxHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
121121
if (testSuiteEvent.compareTo(SMEvent.START_ELEMENT) == 0) {
122122
String testSuiteClassName = testSuite.getAttrValue("name");
123123
if (StringUtils.contains(testSuiteClassName, "$")) {
124-
// test suites for inner classes are ignored
124+
// Test suites for inner classes are ignored
125125
return;
126126
}
127127
SMInputCursor testCase = testSuite.childCursor(new ElementFilter("testcase"));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* commons - Enables analysis of Swift and Objective-C projects into SonarQube.
3+
* Copyright © 2015 Backelite (${email})
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.backelite.sonarqube.commons.surefire;
19+
20+
import org.sonar.api.batch.fs.FileSystem;
21+
import org.sonar.api.batch.fs.InputFile;
22+
23+
import javax.annotation.Nullable;
24+
25+
/**
26+
* Created by gillesgrousset on 28/08/2018.
27+
*/
28+
public interface TestFileFinder {
29+
30+
@Nullable
31+
InputFile getUnitTestResource(FileSystem fileSystem, String classname);
32+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* commons - Enables analysis of Swift and Objective-C projects into SonarQube.
3+
* Copyright © 2015 Backelite (${email})
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.backelite.sonarqube.commons.surefire;
19+
20+
import org.sonar.api.batch.fs.FileSystem;
21+
import org.sonar.api.batch.fs.InputFile;
22+
23+
import javax.annotation.Nullable;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class TestFileFinders {
28+
29+
private static TestFileFinders instance;
30+
31+
32+
private final List<TestFileFinder> finders = new ArrayList<>();
33+
34+
private TestFileFinders() {
35+
36+
}
37+
38+
public static synchronized TestFileFinders getInstance() {
39+
40+
if (instance == null) {
41+
instance = new TestFileFinders();
42+
}
43+
return instance;
44+
}
45+
46+
public void addFinder(TestFileFinder finder) {
47+
finders.add(finder);
48+
}
49+
50+
@Nullable
51+
InputFile getUnitTestResource(FileSystem fileSystem, String classname) {
52+
53+
for (TestFileFinder finder : finders) {
54+
InputFile result = finder.getUnitTestResource(fileSystem, classname);
55+
if (result != null) {
56+
return result;
57+
}
58+
}
59+
60+
return null;
61+
}
62+
63+
64+
}

0 commit comments

Comments
 (0)