Skip to content

Commit

Permalink
Merge pull request #631 from guwirth/enhancement/629_remove_fromIOFile
Browse files Browse the repository at this point in the history
remove deprecated fromIOFile
  • Loading branch information
guwirth committed Sep 16, 2015
2 parents 11f256d + b9167a5 commit 64bbf0a
Show file tree
Hide file tree
Showing 30 changed files with 710 additions and 475 deletions.
6 changes: 3 additions & 3 deletions integration-tests/features/test_execution_statistics.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Feature: Providing test execution numbers
# | mode it just aggregates the measures contained in the reports
# | and saves the result in the project, skipping all the testcase
# | details. In detailed mode, the plugin tries to find the
# | resources (=test source files) where the testcases are
# | implemented in and saves the measures to those resources.
# | test source files where the testcases are implemented in
# | and saves the measures to those input files.
# |
# | * To locate the test source file for assigning, there are
# | two strategies:
Expand Down Expand Up @@ -112,7 +112,7 @@ Feature: Providing test execution numbers
"""
.*WARN.*cannot find the sources for '#include <gtest/gtest\.h>'
.*WARN.*cannot find the sources for '#include <unistd\.h>'
.*WARN.*no resource found, the testcase '.*' has to be skipped
.*WARN.*no input file found, the testcase '.*' has to be skipped
.*WARN.*Some testcases had to be skipped, check the relevant parts of your setup.*
"""
AND the test related metrics have following values: <values>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.CoverageMeasuresBuilder;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.PropertiesBuilder;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.plugins.cxx.CxxLanguage;
import org.sonar.plugins.cxx.utils.CxxReportSensor;
import org.sonar.plugins.cxx.utils.CxxUtils;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -96,7 +100,7 @@ public void analyse(Project project, SensorContext context) {
fs.baseDir().getPath(),
REPORT_PATH_KEY);
coverageMeasures = processReports(project, context, reports);
saveMeasures(project, context, coverageMeasures, CoverageType.UT_COVERAGE);
saveMeasures(context, coverageMeasures, CoverageType.UT_COVERAGE);
}

if (conf.hasKey(IT_REPORT_PATH_KEY)) {
Expand All @@ -106,7 +110,7 @@ public void analyse(Project project, SensorContext context) {
fs.baseDir().getPath(),
IT_REPORT_PATH_KEY);
itCoverageMeasures = processReports(project, context, itReports);
saveMeasures(project, context, itCoverageMeasures, CoverageType.IT_COVERAGE);
saveMeasures(context, itCoverageMeasures, CoverageType.IT_COVERAGE);
}

if (conf.hasKey(OVERALL_REPORT_PATH_KEY)) {
Expand All @@ -116,13 +120,12 @@ public void analyse(Project project, SensorContext context) {
fs.baseDir().getPath(),
OVERALL_REPORT_PATH_KEY);
overallCoverageMeasures = processReports(project, context, overallReports);
saveMeasures(project, context, overallCoverageMeasures, CoverageType.OVERALL_COVERAGE);
saveMeasures(context, overallCoverageMeasures, CoverageType.OVERALL_COVERAGE);
}

if (isForceZeroCoverageActivated()) {
CxxUtils.LOG.debug("Zeroing coverage information for untouched files");
zeroMeasuresWithoutReports(project, context, coverageMeasures,
itCoverageMeasures, overallCoverageMeasures);
zeroMeasuresWithoutReports(context, coverageMeasures, itCoverageMeasures, overallCoverageMeasures);
}
}

Expand Down Expand Up @@ -157,15 +160,13 @@ private Map<String, CoverageMeasuresBuilder> processReports(final Project projec
return measuresTotal;
}

private void saveMeasures(Project project,
SensorContext context,
private void saveMeasures(SensorContext context,
Map<String, CoverageMeasuresBuilder> coverageMeasures,
CoverageType ctype) {
for (Map.Entry<String, CoverageMeasuresBuilder> entry : coverageMeasures.entrySet()) {
String filePath = entry.getKey();
org.sonar.api.resources.File cxxfile
= org.sonar.api.resources.File.fromIOFile(new File(filePath), project);
if (fileExist(context, cxxfile)) {
InputFile cxxFile = fs.inputFile(fs.predicates().hasPath(filePath));
if (cxxFile != null) {
CxxUtils.LOG.debug("Saving coverage measures for file '{}'", filePath);
for (Measure measure : entry.getValue().createMeasures()) {
switch (ctype) {
Expand All @@ -176,78 +177,77 @@ private void saveMeasures(Project project,
measure = convertForOverall(measure);
break;
}
context.saveMeasure(cxxfile, measure);
context.saveMeasure(cxxFile, measure);
}
} else {
CxxUtils.LOG.warn("Cannot find the file '{}', ignoring coverage measures", filePath);
}
}
}

private void zeroMeasuresWithoutReports(Project project,
SensorContext context,
private void zeroMeasuresWithoutReports(SensorContext context,
Map<String, CoverageMeasuresBuilder> coverageMeasures,
Map<String, CoverageMeasuresBuilder> itCoverageMeasures,
Map<String, CoverageMeasuresBuilder> overallCoverageMeasures
) {
for (File file : fs.files(fs.predicates().hasLanguage(CxxLanguage.KEY))) {
org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, project); //@todo: fromIOFile is deprecated
if (fileExist(context, resource)) {
String filePath = CxxUtils.normalizePath(file.getAbsolutePath());
FilePredicates p = fs.predicates();
Iterable<InputFile> inputFiles = fs.inputFiles(p.and(p.hasType(InputFile.Type.MAIN), p.hasLanguage(CxxLanguage.KEY)));
for (InputFile inputFile : inputFiles) {
String filePath = CxxUtils.normalizePath(inputFile.absolutePath());

if (coverageMeasures == null || coverageMeasures.get(filePath) == null) {
saveZeroValueForResource(resource, filePath, context, CoverageType.UT_COVERAGE);
}
if (coverageMeasures == null || coverageMeasures.get(filePath) == null) {
saveZeroValueForResource(inputFile, filePath, context, CoverageType.UT_COVERAGE);
}

if (itCoverageMeasures == null || itCoverageMeasures.get(filePath) == null) {
saveZeroValueForResource(resource, filePath, context, CoverageType.IT_COVERAGE);
}
if (itCoverageMeasures == null || itCoverageMeasures.get(filePath) == null) {
saveZeroValueForResource(inputFile, filePath, context, CoverageType.IT_COVERAGE);
}

if (overallCoverageMeasures == null || overallCoverageMeasures.get(filePath) == null) {
saveZeroValueForResource(resource, filePath, context, CoverageType.OVERALL_COVERAGE);
}
if (overallCoverageMeasures == null || overallCoverageMeasures.get(filePath) == null) {
saveZeroValueForResource(inputFile, filePath, context, CoverageType.OVERALL_COVERAGE);
}
}
}

private void saveZeroValueForResource(org.sonar.api.resources.File resource,
String filePath,
SensorContext context,
CoverageType ctype) {
private void saveZeroValueForResource(InputFile inputFile,
String filePath,
SensorContext context,
CoverageType ctype) {

Resource resource = context.getResource(inputFile);
Measure ncloc = context.getMeasure(resource, CoreMetrics.NCLOC);
Measure stmts = context.getMeasure(resource, CoreMetrics.STATEMENTS);
if (ncloc != null && stmts != null
&& ncloc.getValue() > 0 && stmts.getValue() > 0) {
&& ncloc.getValue() > 0 && stmts.getValue() > 0) {
String coverageKind = "unit test ";
Metric hitsDataMetric = CoreMetrics.COVERAGE_LINE_HITS_DATA;
Metric linesToCoverMetric = CoreMetrics.LINES_TO_COVER;
Metric uncoveredLinesMetric = CoreMetrics.UNCOVERED_LINES;

switch(ctype){
case IT_COVERAGE:
coverageKind = "integration test ";
hitsDataMetric = CoreMetrics.IT_COVERAGE_LINE_HITS_DATA;
linesToCoverMetric = CoreMetrics.IT_LINES_TO_COVER;
uncoveredLinesMetric = CoreMetrics.IT_UNCOVERED_LINES;
break;
case OVERALL_COVERAGE:
coverageKind = "overall ";
hitsDataMetric = CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA;
linesToCoverMetric = CoreMetrics.OVERALL_LINES_TO_COVER;
uncoveredLinesMetric = CoreMetrics.OVERALL_UNCOVERED_LINES;
default:
switch (ctype) {
case IT_COVERAGE:
coverageKind = "integration test ";
hitsDataMetric = CoreMetrics.IT_COVERAGE_LINE_HITS_DATA;
linesToCoverMetric = CoreMetrics.IT_LINES_TO_COVER;
uncoveredLinesMetric = CoreMetrics.IT_UNCOVERED_LINES;
break;
case OVERALL_COVERAGE:
coverageKind = "overall ";
hitsDataMetric = CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA;
linesToCoverMetric = CoreMetrics.OVERALL_LINES_TO_COVER;
uncoveredLinesMetric = CoreMetrics.OVERALL_UNCOVERED_LINES;
default:
}

CxxUtils.LOG.debug("Zeroing {}coverage measures for file '{}'", coverageKind, filePath);

PropertiesBuilder<Integer, Integer> lineHitsData = new PropertiesBuilder<Integer, Integer>(hitsDataMetric);
for (int i = 1; i <= context.getMeasure(resource, CoreMetrics.LINES).getIntValue(); ++i) {
PropertiesBuilder<Integer, Integer> lineHitsData = new PropertiesBuilder<>(hitsDataMetric);
for (int i = 1; i <= inputFile.lines(); ++i) {
lineHitsData.add(i, 0);
}
context.saveMeasure(resource, lineHitsData.build());
context.saveMeasure(resource, linesToCoverMetric, ncloc.getValue());
context.saveMeasure(resource, uncoveredLinesMetric, ncloc.getValue());
context.saveMeasure(inputFile, lineHitsData.build());
context.saveMeasure(inputFile, linesToCoverMetric, ncloc.getValue());
context.saveMeasure(inputFile, uncoveredLinesMetric, ncloc.getValue());
}
}

Expand Down Expand Up @@ -297,10 +297,6 @@ private Measure convertForOverall(Measure measure) {
return itMeasure;
}

private boolean fileExist(SensorContext context, org.sonar.api.resources.File file) {
return context.getResource(file) != null;
}

private boolean isForceZeroCoverageActivated() {
return conf.getBoolean(FORCE_ZERO_COVERAGE_KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ private CxxConfiguration createConfiguration(FileSystem fs, Settings conf) {
private void save(Collection<SourceCode> squidSourceFiles) {
int violationsCount = 0;
DependencyAnalyzer dependencyAnalyzer = new DependencyAnalyzer(resourcePerspectives, project, context, rules);

for (SourceCode squidSourceFile : squidSourceFiles) {
SourceFile squidFile = (SourceFile) squidSourceFile;
File ioFile = new File(squidFile.getKey());
InputFile inputFile = fs.inputFile(fs.predicates().is(ioFile));

org.sonar.api.resources.File sonarFile = org.sonar.api.resources.File.fromIOFile(ioFile, project); //@todo fromIOFile: deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html

saveMeasures(sonarFile, squidFile);
saveFilesComplexityDistribution(sonarFile, squidFile);
saveFunctionsComplexityDistribution(sonarFile, squidFile);
violationsCount += saveViolations(sonarFile, squidFile);
dependencyAnalyzer.addFile(sonarFile, CxxParser.getIncludedFiles(ioFile));
saveMeasures(inputFile, squidFile);
saveFilesComplexityDistribution(inputFile, squidFile);
saveFunctionsComplexityDistribution(inputFile, squidFile);
violationsCount += saveViolations(inputFile, squidFile);
dependencyAnalyzer.addFile(inputFile, CxxParser.getIncludedFiles(ioFile));
}

Measure measure = new Measure(CxxMetrics.SQUID);
Expand All @@ -196,39 +196,39 @@ private void save(Collection<SourceCode> squidSourceFiles) {
dependencyAnalyzer.save();
}

private void saveMeasures(org.sonar.api.resources.File sonarFile, SourceFile squidFile) {
context.saveMeasure(sonarFile, CoreMetrics.FILES, squidFile.getDouble(CxxMetric.FILES));
context.saveMeasure(sonarFile, CoreMetrics.LINES, squidFile.getDouble(CxxMetric.LINES));
context.saveMeasure(sonarFile, CoreMetrics.NCLOC, squidFile.getDouble(CxxMetric.LINES_OF_CODE));
context.saveMeasure(sonarFile, CoreMetrics.STATEMENTS, squidFile.getDouble(CxxMetric.STATEMENTS));
context.saveMeasure(sonarFile, CoreMetrics.FUNCTIONS, squidFile.getDouble(CxxMetric.FUNCTIONS));
context.saveMeasure(sonarFile, CoreMetrics.CLASSES, squidFile.getDouble(CxxMetric.CLASSES));
context.saveMeasure(sonarFile, CoreMetrics.COMPLEXITY, squidFile.getDouble(CxxMetric.COMPLEXITY));
context.saveMeasure(sonarFile, CoreMetrics.COMMENT_LINES, squidFile.getDouble(CxxMetric.COMMENT_LINES));
context.saveMeasure(sonarFile, CoreMetrics.PUBLIC_API, squidFile.getDouble(CxxMetric.PUBLIC_API));
context.saveMeasure(sonarFile, CoreMetrics.PUBLIC_UNDOCUMENTED_API, squidFile.getDouble(CxxMetric.PUBLIC_UNDOCUMENTED_API));
private void saveMeasures(InputFile inputFile, SourceFile squidFile) {
context.saveMeasure(inputFile, CoreMetrics.FILES, squidFile.getDouble(CxxMetric.FILES));
context.saveMeasure(inputFile, CoreMetrics.LINES, squidFile.getDouble(CxxMetric.LINES));
context.saveMeasure(inputFile, CoreMetrics.NCLOC, squidFile.getDouble(CxxMetric.LINES_OF_CODE));
context.saveMeasure(inputFile, CoreMetrics.STATEMENTS, squidFile.getDouble(CxxMetric.STATEMENTS));
context.saveMeasure(inputFile, CoreMetrics.FUNCTIONS, squidFile.getDouble(CxxMetric.FUNCTIONS));
context.saveMeasure(inputFile, CoreMetrics.CLASSES, squidFile.getDouble(CxxMetric.CLASSES));
context.saveMeasure(inputFile, CoreMetrics.COMPLEXITY, squidFile.getDouble(CxxMetric.COMPLEXITY));
context.saveMeasure(inputFile, CoreMetrics.COMMENT_LINES, squidFile.getDouble(CxxMetric.COMMENT_LINES));
context.saveMeasure(inputFile, CoreMetrics.PUBLIC_API, squidFile.getDouble(CxxMetric.PUBLIC_API));
context.saveMeasure(inputFile, CoreMetrics.PUBLIC_UNDOCUMENTED_API, squidFile.getDouble(CxxMetric.PUBLIC_UNDOCUMENTED_API));
}

private void saveFunctionsComplexityDistribution(org.sonar.api.resources.File sonarFile, SourceFile squidFile) {
private void saveFunctionsComplexityDistribution(InputFile inputFile, SourceFile squidFile) {
Collection<SourceCode> squidFunctionsInFile = scanner.getIndex().search(new QueryByParent(squidFile), new QueryByType(SourceFunction.class));
RangeDistributionBuilder complexityDistribution = new RangeDistributionBuilder(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, FUNCTIONS_DISTRIB_BOTTOM_LIMITS);
for (SourceCode squidFunction : squidFunctionsInFile) {
complexityDistribution.add(squidFunction.getDouble(CxxMetric.COMPLEXITY));
}
context.saveMeasure(sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY));
context.saveMeasure(inputFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY));
}

private void saveFilesComplexityDistribution(org.sonar.api.resources.File sonarFile, SourceFile squidFile) {
private void saveFilesComplexityDistribution(InputFile inputFile, SourceFile squidFile) {
RangeDistributionBuilder complexityDistribution = new RangeDistributionBuilder(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, FILES_DISTRIB_BOTTOM_LIMITS);
complexityDistribution.add(squidFile.getDouble(CxxMetric.COMPLEXITY));
context.saveMeasure(sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY));
context.saveMeasure(inputFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY));
}

private int saveViolations(org.sonar.api.resources.File sonarFile, SourceFile squidFile) {
private int saveViolations(InputFile inputFile, SourceFile squidFile) {
Collection<CheckMessage> messages = squidFile.getCheckMessages();
int violationsCount = 0;
if (messages != null) {
Issuable issuable = resourcePerspectives.as(Issuable.class, sonarFile);
Issuable issuable = resourcePerspectives.as(Issuable.class, inputFile);
if (issuable != null) {
for (CheckMessage message : messages) {
Issue issue = issuable.newIssueBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.design.Dependency;
import org.sonar.api.issue.Issuable;
Expand Down Expand Up @@ -79,7 +80,8 @@ public DependencyAnalyzer(ResourcePerspectives perspectives, Project project, Se
this.cycleBetweenPackagesRule = CycleBetweenPackagesCheck.getActiveRule(rules);
}

public void addFile(File sonarFile, Collection<CxxPreprocessor.Include> includedFiles) {
public void addFile(InputFile inputFile, Collection<CxxPreprocessor.Include> includedFiles) {
File sonarFile = File.fromIOFile(inputFile.file(), project); //@todo fromIOFile: deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html
//Store the directory and file
Directory sonarDir = sonarFile.getParent();
packagesGraph.addVertex(sonarDir);
Expand All @@ -88,7 +90,7 @@ public void addFile(File sonarFile, Collection<CxxPreprocessor.Include> included
//Build the dependency graph
Map<String, Integer> firstIncludeLine = new HashMap<String, Integer>();
for (CxxPreprocessor.Include include : includedFiles) {
File includedFile = File.fromIOFile(new java.io.File(include.getPath()), project);
File includedFile = File.fromIOFile(new java.io.File(include.getPath()), project); //@todo fromIOFile: deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html
String includedFilePath = includedFile != null ? includedFile.getPath() : include.getPath();
Integer prevIncludeLine = firstIncludeLine.put(includedFilePath, include.getLine());
if (prevIncludeLine != null) {
Expand Down
Loading

0 comments on commit 64bbf0a

Please sign in to comment.