diff --git a/integration-tests/features/test_execution_statistics.feature b/integration-tests/features/test_execution_statistics.feature index 222cccf536..2d38c21828 100644 --- a/integration-tests/features/test_execution_statistics.feature +++ b/integration-tests/features/test_execution_statistics.feature @@ -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: @@ -112,7 +112,7 @@ Feature: Providing test execution numbers """ .*WARN.*cannot find the sources for '#include ' .*WARN.*cannot find the sources for '#include ' - .*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: diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensor.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensor.java index f82e29dac8..91ffc5c84e 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensor.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensor.java @@ -30,6 +30,8 @@ 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; @@ -37,9 +39,11 @@ 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} */ @@ -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)) { @@ -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)) { @@ -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); } } @@ -157,15 +160,13 @@ private Map processReports(final Project projec return measuresTotal; } - private void saveMeasures(Project project, - SensorContext context, + private void saveMeasures(SensorContext context, Map coverageMeasures, CoverageType ctype) { for (Map.Entry 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) { @@ -176,7 +177,7 @@ 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); @@ -184,70 +185,69 @@ private void saveMeasures(Project project, } } - private void zeroMeasuresWithoutReports(Project project, - SensorContext context, + private void zeroMeasuresWithoutReports(SensorContext context, Map coverageMeasures, Map itCoverageMeasures, Map 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 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 lineHitsData = new PropertiesBuilder(hitsDataMetric); - for (int i = 1; i <= context.getMeasure(resource, CoreMetrics.LINES).getIntValue(); ++i) { + PropertiesBuilder 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()); } } @@ -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); } diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/CxxSquidSensor.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/CxxSquidSensor.java index 29b2c5726d..90d770f498 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/CxxSquidSensor.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/CxxSquidSensor.java @@ -177,17 +177,17 @@ private CxxConfiguration createConfiguration(FileSystem fs, Settings conf) { private void save(Collection 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); @@ -196,39 +196,39 @@ private void save(Collection 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 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 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() diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/DependencyAnalyzer.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/DependencyAnalyzer.java index 031662f247..352083652e 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/DependencyAnalyzer.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/squid/DependencyAnalyzer.java @@ -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; @@ -79,7 +80,8 @@ public DependencyAnalyzer(ResourcePerspectives perspectives, Project project, Se this.cycleBetweenPackagesRule = CycleBetweenPackagesCheck.getActiveRule(rules); } - public void addFile(File sonarFile, Collection includedFiles) { + public void addFile(InputFile inputFile, Collection 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); @@ -88,7 +90,7 @@ public void addFile(File sonarFile, Collection included //Build the dependency graph Map firstIncludeLine = new HashMap(); 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) { diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java index b704eef140..67008f9564 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java @@ -30,6 +30,7 @@ import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; @@ -209,13 +210,12 @@ public static List getReports(Settings conf, * Saves code violation only if unique. * Compares file, line, ruleId and msg. */ - public boolean saveUniqueViolation(Project project, SensorContext context, String ruleRepoKey, - String file, String line, String ruleId, String msg) { + public void saveUniqueViolation(Project project, SensorContext context, String ruleRepoKey, + String file, String line, String ruleId, String msg) { if (uniqueIssues.add(file + line + ruleId + msg)) { // StringBuilder is slower - return saveViolation(project, context, ruleRepoKey, file, line, ruleId, msg); + saveViolation(project, context, ruleRepoKey, file, line, ruleId, msg); } - return false; } /** @@ -225,67 +225,60 @@ public boolean saveUniqueViolation(Project project, SensorContext context, Strin * according parameters ('file' = null for project level, 'line' = null for * file-level) */ - private boolean saveViolation(Project project, SensorContext context, String ruleRepoKey, + private void saveViolation(Project project, SensorContext context, String ruleRepoKey, String filename, String line, String ruleId, String msg) { - boolean add = false; - Resource resource = null; + Issuable issuable = null; int lineNr = 0; // handles file="" situation -- file level if ((filename != null) && (filename.length() > 0)) { String root = reactor.getRoot().getBaseDir().getAbsolutePath(); String normalPath = CxxUtils.normalizePathFull(filename, root); if (normalPath != null && !notFoundFiles.contains(normalPath)) { - org.sonar.api.resources.File file - = org.sonar.api.resources.File.fromIOFile(new File(normalPath), project); - if (context.getResource(file) != null) { - lineNr = getLineAsInt(line); - resource = file; - add = true; + InputFile inputFile = fs.inputFile(fs.predicates().is(new File(normalPath))); + if (inputFile != null) { + lineNr = getLineAsInt(line, inputFile.lines()); + issuable = perspectives.as(Issuable.class, inputFile); } else { CxxUtils.LOG.warn("Cannot find the file '{}', skipping violations", normalPath); notFoundFiles.add(normalPath); } } } else { // project level - resource = project; - add = true; + issuable = perspectives.as(Issuable.class, (Resource) project); } - if (add) { - add = contextSaveViolation(resource, lineNr, RuleKey.of(ruleRepoKey, ruleId), msg); + if (issuable != null) { + addIssue(issuable, lineNr, RuleKey.of(ruleRepoKey, ruleId), msg); } - - return add; } - private boolean contextSaveViolation(Resource resource, int lineNr, RuleKey rule, String msg) { - Issuable issuable = perspectives.as(Issuable.class, resource); - boolean result = false; - if (issuable != null) { - Issuable.IssueBuilder issueBuilder = issuable.newIssueBuilder() - .ruleKey(rule) - .message(msg); - if (lineNr > 0) { - issueBuilder = issueBuilder.line(lineNr); - } - Issue issue = issueBuilder.build(); - try{ - result = issuable.addIssue(issue); - } catch (org.sonar.api.utils.MessageException me){ - CxxUtils.LOG.error("Could not add the issue, details: '{}'", me.toString()); - } - if (result) + private void addIssue(Issuable issuable, int lineNr, RuleKey rule, String msg) { + Issuable.IssueBuilder issueBuilder = issuable.newIssueBuilder() + .ruleKey(rule) + .message(msg); + if (lineNr > 0) { + issueBuilder = issueBuilder.line(lineNr); + } + Issue issue = issueBuilder.build(); + try { + if (issuable.addIssue(issue)) { violationsCount++; + } + } catch (org.sonar.api.utils.MessageException me) { + CxxUtils.LOG.error("Could not add the issue, details: '{}'", me.toString()); } - return result; } - private int getLineAsInt(String line) { + private int getLineAsInt(String line, int maxLine) { int lineNr = 0; if (line != null) { try { lineNr = Integer.parseInt(line); - lineNr = lineNr == 0 ? 1 : lineNr; + if (lineNr < 1) { + lineNr = 1; + } else if (lineNr > maxLine) { // https://jira.sonarsource.com/browse/SONAR-6792 + lineNr = maxLine; + } } catch (java.lang.NumberFormatException nfe) { CxxUtils.LOG.warn("Skipping invalid line number: {}", line); lineNr = -1; diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/CxxXunitSensor.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/CxxXunitSensor.java index 1e3288caa2..c135bf64cf 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/CxxXunitSensor.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/CxxXunitSensor.java @@ -64,6 +64,7 @@ import org.sonar.squidbridge.api.SourceFile; import org.sonar.squidbridge.api.SourceFunction; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.fs.InputFile; /** @@ -76,9 +77,8 @@ public class CxxXunitSensor extends CxxReportSensor { private static final double PERCENT_BASE = 100d; private String xsltURL = null; - private Map classDeclTable = new TreeMap(); - private Map classImplTable = new TreeMap(); - private ResourceFinder resourceFinder = null; + private Map classDeclTable = new TreeMap<>(); + private Map classImplTable = new TreeMap<>(); private int tcTotal = 0; private int tcSkipped = 0; @@ -91,11 +91,6 @@ public class CxxXunitSensor extends CxxReportSensor { public CxxXunitSensor(Settings conf, FileSystem fs, ProjectReactor reactor) { super(conf, fs, reactor); xsltURL = conf.getString(XSLT_URL_KEY); - this.resourceFinder = new DefaultResourceFinder(); - } - - void injectResourceFinder(ResourceFinder finder) { - this.resourceFinder = finder; } @Override @@ -223,10 +218,10 @@ private void detailledMode(final Project project, final SensorContext context, L return; } - Collection locatedResources = lookupResources(project, context, testcases); + Collection testFiles = lookupTestFiles(project, context, testcases); - for (TestResource resource : locatedResources) { - saveTestMetrics(context, resource); + for (TestFile testFile : testFiles) { + saveTestMetrics(context, testFile); } CxxUtils.LOG.info("Summary: testcases processed = {}, skipped = {}", tcTotal, tcSkipped); if (tcSkipped > 0){ @@ -236,32 +231,32 @@ private void detailledMode(final Project project, final SensorContext context, L } - private Collection lookupResources(Project project, SensorContext context, List testcases) { - Map resources = new HashMap(); + private Collection lookupTestFiles(Project project, SensorContext context, List testcases) { + HashMap testFileMap = new HashMap<>(); for (TestCase tc : testcases) { tcTotal++; - CxxUtils.LOG.debug("Trying the resource for the testcase '{}' ...", tc.getFullname()); - org.sonar.api.resources.File sonarResource = lookupResource(project, context, tc); - if (sonarResource != null) { - CxxUtils.LOG.debug("... found! The resource is '{}'", sonarResource); + CxxUtils.LOG.debug("Trying the input file for the testcase '{}' ...", tc.getFullname()); + InputFile inputFile = lookupFile(project, context, tc); + if (inputFile != null) { + CxxUtils.LOG.debug("... found! The input file is '{}'", inputFile); - TestResource resource = resources.get(sonarResource.getKey()); - if (resource == null) { - resource = new TestResource(sonarResource); - resources.put(resource.getKey(), resource); + TestFile testFile = testFileMap.get(inputFile.absolutePath()); + if (testFile == null) { + testFile = new TestFile(inputFile); + testFileMap.put(testFile.getKey(), testFile); } - resource.addTestCase(tc); + testFile.addTestCase(tc); } else { tcSkipped++; - CxxUtils.LOG.warn("... no resource found, the testcase '{}' has to be skipped", + CxxUtils.LOG.warn("... no input file found, the testcase '{}' has to be skipped", tc.getFullname()); } } - return resources.values(); + return testFileMap.values(); } File transformReport(File report) @@ -294,38 +289,38 @@ File transformReport(File report) return transformed; } - private void saveTestMetrics(SensorContext context, TestResource resource) { - org.sonar.api.resources.File testfile = resource.getSonarResource(); - double testsRun = resource.getTests() - resource.getSkipped(); + private void saveTestMetrics(SensorContext context, TestFile testFile) { + InputFile inputFile = testFile.getInputFile(); + double testsRun = testFile.getTests() - testFile.getSkipped(); - context.saveMeasure(testfile, CoreMetrics.SKIPPED_TESTS, (double) resource.getSkipped()); - context.saveMeasure(testfile, CoreMetrics.TESTS, testsRun); - context.saveMeasure(testfile, CoreMetrics.TEST_ERRORS, (double) resource.getErrors()); - context.saveMeasure(testfile, CoreMetrics.TEST_FAILURES, (double) resource.getFailures()); - context.saveMeasure(testfile, CoreMetrics.TEST_EXECUTION_TIME, (double) resource.getTime()); + context.saveMeasure(inputFile, CoreMetrics.SKIPPED_TESTS, (double) testFile.getSkipped()); + context.saveMeasure(inputFile, CoreMetrics.TESTS, testsRun); + context.saveMeasure(inputFile, CoreMetrics.TEST_ERRORS, (double) testFile.getErrors()); + context.saveMeasure(inputFile, CoreMetrics.TEST_FAILURES, (double) testFile.getFailures()); + context.saveMeasure(inputFile, CoreMetrics.TEST_EXECUTION_TIME, (double) testFile.getTime()); if (testsRun > 0) { - double testsPassed = testsRun - resource.getErrors() - resource.getFailures(); + double testsPassed = testsRun - testFile.getErrors() - testFile.getFailures(); double successDensity = testsPassed * PERCENT_BASE / testsRun; - context.saveMeasure(testfile, CoreMetrics.TEST_SUCCESS_DENSITY, ParsingUtils.scaleValue(successDensity)); + context.saveMeasure(inputFile, CoreMetrics.TEST_SUCCESS_DENSITY, ParsingUtils.scaleValue(successDensity)); } - context.saveMeasure(testfile, new Measure(CoreMetrics.TEST_DATA, resource.getDetails())); + context.saveMeasure(inputFile, new Measure(CoreMetrics.TEST_DATA, testFile.getDetails())); } - private org.sonar.api.resources.File lookupResource(Project project, SensorContext context, TestCase tc) { - // The lookup of the test resource for a test case is performed as follows: + private InputFile lookupFile(Project project, SensorContext context, TestCase tc) { + // The lookup of the test input files for a test case is performed as follows: // 1. If the testcase carries a filepath just perform the lookup using this data. // As we assume this as the absolute knowledge, we dont want to fallback to other // methods, we want to FAIL. // 2. Use the classname to search in the lookupTable (this is the AST-based lookup) // and redo the lookup in Sonar with the gained value. - org.sonar.api.resources.File sonarResource = null; + InputFile inputFile = null; String filepath = tc.getFilename(); if (filepath != null){ CxxUtils.LOG.debug("Performing the 'filename'-based lookup using the value '{}'", filepath); - return lookupInSonar(filepath, context, project); + return lookupInSonar(filepath); } if(classDeclTable.isEmpty() && classImplTable.isEmpty()){ @@ -336,16 +331,16 @@ private org.sonar.api.resources.File lookupResource(Project project, SensorConte if (classname != null){ filepath = lookupFilePath(classname); CxxUtils.LOG.debug("Performing AST-based lookup, determined file path: '{}'", filepath); - sonarResource = lookupInSonar(filepath, context, project); + inputFile = lookupInSonar(filepath); } else { CxxUtils.LOG.debug("Skipping the AST-based lookup: no classname provided"); } - return sonarResource; + return inputFile; } - private org.sonar.api.resources.File lookupInSonar(String filepath, SensorContext context, Project project) { - return resourceFinder.findInSonar(new File(filepath), context, this.fs, project); + private InputFile lookupInSonar(String filepath) { + return fs.inputFile(fs.predicates().is(new File(filepath))); } String lookupFilePath(String key) { diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/DefaultResourceFinder.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/DefaultResourceFinder.java deleted file mode 100644 index 130446cbe0..0000000000 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/DefaultResourceFinder.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Sonar C++ Plugin (Community) - * Copyright (C) 2010 Neticoa SAS France - * dev@sonar.codehaus.org - * - * 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 02 - */ -package org.sonar.plugins.cxx.xunit; - -import java.io.File; -import java.util.List; - -import com.google.common.collect.Lists; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.Project; -import org.sonar.api.batch.fs.FileSystem; - -public class DefaultResourceFinder implements ResourceFinder { - - public org.sonar.api.resources.File findInSonar(File file, SensorContext context, FileSystem fs, Project project) { - List files = Lists.newArrayList(fs.files(fs.predicates().is(file))); - assert (files.size() <= 1); - return files.size() == 1 - ? org.sonar.api.resources.File.fromIOFile(files.get(0), project) //@todo fromIOFile: deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html - : null; - } -} diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/ResourceFinder.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/ResourceFinder.java deleted file mode 100644 index ae227181fe..0000000000 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/ResourceFinder.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Sonar C++ Plugin (Community) - * Copyright (C) 2010 Neticoa SAS France - * dev@sonar.codehaus.org - * - * 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 02 - */ -package org.sonar.plugins.cxx.xunit; - -import java.io.File; - -import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.resources.Project; - -public interface ResourceFinder { - - public org.sonar.api.resources.File findInSonar(File file, SensorContext context, FileSystem fs, Project project); -} diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/TestResource.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/TestFile.java similarity index 74% rename from sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/TestResource.java rename to sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/TestFile.java index 71078bd635..63e1893069 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/TestResource.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/xunit/TestFile.java @@ -21,33 +21,33 @@ import java.util.ArrayList; import java.util.List; +import org.sonar.api.batch.fs.InputFile; /** - * Represents a test resource in Sonar, i.e. a source code file which - * implements tests. Holds all testcases along with all measures + * Represents a test file in Sonar, i.e. a source code file which + * implements tests. Holds all test cases along with all measures * collected from the reports. */ -public class TestResource { +public class TestFile { private int errors = 0; private int skipped = 0; private int tests = 0; private int time = 0; private int failures = 0; private List testCases; - private org.sonar.api.resources.File sonarResource = null; + private InputFile inputFile = null; /** - * Creates a test resource instance which corresponds and represents the - * passed resources.File instance - * @param sonarResource The resource in SQ which this TestResource proxies + * Creates a test file instance which corresponds and represents the passed InputFile instance + * @param inputFile The InputFile in SQ which this TestFile proxies */ - public TestResource(org.sonar.api.resources.File sonarResource) { - this.sonarResource = sonarResource; + public TestFile(InputFile inputFile) { + this.inputFile = inputFile; this.testCases = new ArrayList(); } public String getKey() { - return sonarResource.getKey(); + return inputFile.absolutePath(); } public int getErrors() { @@ -71,8 +71,7 @@ public int getFailures() { } /** - * Adds the given test case to this testresource maintaining the - * internal statistics + * Adds the given test case to this test file maintaining the internal statistics * @param tc the test case to add */ public void addTestCase(TestCase tc) { @@ -101,7 +100,7 @@ public String getDetails() { return details.toString(); } - public org.sonar.api.resources.File getSonarResource() { - return sonarResource; + public InputFile getInputFile() { + return inputFile; } } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/TestUtils.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/TestUtils.java index d727015095..7e59f2b8db 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/TestUtils.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/TestUtils.java @@ -47,9 +47,11 @@ import org.sonar.api.resources.Resource; import org.sonar.api.rule.RuleKey; - public class TestUtils { + private final static String OS = System.getProperty("os.name").toLowerCase(); + private final static boolean upperCaseRoot = Character.isUpperCase(System.getProperty("java.home").charAt(0)); + public static Issuable mockIssuable() { Issue issue = mock(Issue.class); Issuable.IssueBuilder issueBuilder = mock(Issuable.IssueBuilder.class); @@ -59,6 +61,7 @@ public static Issuable mockIssuable() { when(issueBuilder.message((String)anyObject())).thenReturn(issueBuilder); Issuable issuable = mock(Issuable.class); when(issuable.newIssueBuilder()).thenReturn(issueBuilder); + when(issuable.addIssue((Issue)anyObject())).thenReturn(Boolean.TRUE); return issuable; } @@ -154,7 +157,37 @@ public static DefaultFileSystem mockFileSystem() { public static CxxLanguage mockCxxLanguage() { return new CxxLanguage(new Settings()); } - + + public static void addInputFile(DefaultFileSystem fs, + ResourcePerspectives perspectives, + Issuable issuable, + String path) { + DefaultInputFile inputFile = null; + File file = new File(path); + if (file.isAbsolute()) { + if (upperCaseRoot && isWindows()) { + // workaround: on some Windows system drive letter can be upper or lower case + StringBuilder temp = new StringBuilder(path); + temp.setCharAt(0, Character.toUpperCase(temp.charAt(0))); + path = temp.toString(); + } + inputFile = new DefaultInputFile(path); + inputFile.setAbsolutePath(path); + } else { + inputFile = new DefaultInputFile(path); + inputFile.setAbsolutePath(new java.io.File(fs.baseDir(), path).getAbsolutePath()); + } + inputFile.setType(InputFile.Type.MAIN); + inputFile.setLanguage(CxxLanguage.KEY); + inputFile.setLines(1); + when(perspectives.as(Issuable.class, inputFile)).thenReturn(issuable); + fs.add(inputFile); + } + + public static boolean isWindows() { + return (OS.indexOf("win") >= 0); + } + private static void scanDirs(DefaultFileSystem fs, File baseDir, List dirs, Type ftype) { if (dirs == null){ return; diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/compiler/CxxCompilerSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/compiler/CxxCompilerSensorTest.java index 18d2cc0289..671869b6d2 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/compiler/CxxCompilerSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/compiler/CxxCompilerSensorTest.java @@ -19,31 +19,29 @@ */ package org.sonar.plugins.cxx.compiler; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.plugins.cxx.TestUtils; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + public class CxxCompilerSensorTest { private SensorContext context; private Project project; - private FileSystem fs; + private DefaultFileSystem fs; private RulesProfile profile; private Issuable issuable; private ResourcePerspectives perspectives; @@ -56,8 +54,6 @@ public void setUp() { perspectives = TestUtils.mockPerspectives(issuable); profile = mock(RulesProfile.class); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test @@ -66,6 +62,7 @@ public void shouldReportACorrectVcViolations() { settings.setProperty("sonar.cxx.compiler.parser", CxxCompilerVcParser.KEY); settings.setProperty(CxxCompilerSensor.REPORT_PATH_KEY, "compiler-reports/BuildLog.htm"); settings.setProperty(CxxCompilerSensor.REPORT_CHARSET_DEF, "UTF-16"); + TestUtils.addInputFile(fs, perspectives, issuable, "zipmanager.cpp"); CxxCompilerSensor sensor = new CxxCompilerSensor(perspectives, settings, fs, profile, TestUtils.mockReactor()); sensor.analyse(project, context); verify(issuable, times(9)).addIssue(any(Issue.class)); @@ -77,6 +74,7 @@ public void shouldReportCorrectGccViolations() { settings.setProperty("sonar.cxx.compiler.parser", CxxCompilerGccParser.KEY); settings.setProperty(CxxCompilerSensor.REPORT_PATH_KEY, "compiler-reports/build.log"); settings.setProperty(CxxCompilerSensor.REPORT_CHARSET_DEF, "UTF-8"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/test/src/zip/src/zipmanager.cpp"); CxxCompilerSensor sensor = new CxxCompilerSensor(perspectives, settings, fs, profile, TestUtils.mockReactor()); sensor.analyse(project, context); verify(issuable, times(4)).addIssue(any(Issue.class)); @@ -88,10 +86,11 @@ public void shouldReportBCorrectVcViolations() { settings.setProperty("sonar.cxx.compiler.parser", CxxCompilerVcParser.KEY); settings.setProperty(CxxCompilerSensor.REPORT_PATH_KEY, "compiler-reports/VC-report.log"); settings.setProperty(CxxCompilerSensor.REPORT_CHARSET_DEF, "UTF-8"); - settings.setProperty(CxxCompilerSensor.REPORT_REGEX_DEF, "^.*>(?.*)\\((?\\d+)\\):\\x20warning\\x20(?C\\d+):(?.*)$"); + settings.setProperty(CxxCompilerSensor.REPORT_REGEX_DEF, "^.*>(?.*)\\((?\\d+)\\):\\x20warning\\x20(?C\\d+):(?.*)$"); + TestUtils.addInputFile(fs, perspectives, issuable, "Server/source/zip/zipmanager.cpp"); CxxCompilerSensor sensor = new CxxCompilerSensor(perspectives, settings, fs, profile, TestUtils.mockReactor()); sensor.analyse(project, context); verify(issuable, times(9)).addIssue(any(Issue.class)); } - + } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxBullseyeCoverageSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxBullseyeCoverageSensorTest.java index e4e9ba4d52..b5d51876bc 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxBullseyeCoverageSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxBullseyeCoverageSensorTest.java @@ -19,68 +19,120 @@ */ package org.sonar.plugins.cxx.coverage; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.config.Settings; import org.sonar.api.measures.Measure; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; +import org.sonar.api.component.ResourcePerspectives; +import org.sonar.api.issue.Issuable; import org.sonar.plugins.cxx.TestUtils; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + public class CxxBullseyeCoverageSensorTest { + private CxxCoverageSensor sensor; private SensorContext context; private Project project; - private FileSystem fs; - + private DefaultFileSystem fs; + private Issuable issuable; + private ResourcePerspectives perspectives; + @Before public void setUp() { project = TestUtils.mockProject(); + issuable = TestUtils.mockIssuable(); + perspectives = TestUtils.mockPerspectives(issuable); fs = TestUtils.mockFileSystem(); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectCoverage() { Settings settings = new Settings(); - settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye.xml"); - settings.setProperty(CxxCoverageSensor.IT_REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye.xml"); - settings.setProperty(CxxCoverageSensor.OVERALL_REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye.xml"); + if (TestUtils.isWindows()) { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye-win.xml"); + settings.setProperty(CxxCoverageSensor.IT_REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye-win.xml"); + settings.setProperty(CxxCoverageSensor.OVERALL_REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye-win.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/src/testclass.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/src/testclass.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/testclass.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/testclass.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/source_1.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/src/testclass.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/src/testclass.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "d:/home/path/TESTCOV/testclass.h"); + } else { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye-linux.xml"); + settings.setProperty(CxxCoverageSensor.IT_REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye-linux.xml"); + settings.setProperty(CxxCoverageSensor.OVERALL_REPORT_PATH_KEY, "coverage-reports/bullseye/coverage-result-bullseye-linux.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/src/testclass.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/src/testclass.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/testclass.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/testclass.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/source_1.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/src/testclass.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/src/testclass.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/home/path/TESTCOV/testclass.h"); + } sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - verify(context, times(90)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(90)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } @Test public void shoulParseTopLevelFiles() { Settings settings = new Settings(); - settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node.xml"); + if (TestUtils.isWindows()) { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-win.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/test/anotherincludeattop.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/test/test/test.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/test/test2/test2.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/test/main.c"); + } else { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-linux.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/test/anotherincludeattop.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/test/test/test.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/test/test2/test2.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/test/main.c"); + } sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - verify(context, times(28)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(28)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } - @Test + @Test public void shoulCorrectlyHandleDriveLettersWithoutSlash() { Settings settings = new Settings(); - settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash.xml"); + if (TestUtils.isWindows()) { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-win.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/main.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/test/test.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/test2/test2.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "e:/anotherincludeattop.h"); + } + else { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-linux.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/main.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/test/test.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/test2/test2.c"); + TestUtils.addInputFile(fs, perspectives, issuable, "/e/anotherincludeattop.h"); + } sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - verify(context, times(28)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(28)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } - } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensorTest.java index 51692fe2c6..1f5708fcab 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/coverage/CxxCoverageSensorTest.java @@ -19,50 +19,61 @@ */ package org.sonar.plugins.cxx.coverage; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; import org.sonar.api.measures.Measure; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; +import org.sonar.api.component.ResourcePerspectives; +import org.sonar.api.issue.Issuable; import org.sonar.plugins.cxx.TestUtils; -import org.sonar.api.batch.bootstrap.ProjectReactor; + +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; public class CxxCoverageSensorTest { + private CxxCoverageSensor sensor; private SensorContext context; private Project project; - private FileSystem fs; + private DefaultFileSystem fs; private ProjectReactor reactor; - + private Issuable issuable; + private ResourcePerspectives perspectives; + @Before public void setUp() { project = TestUtils.mockProject(); + issuable = TestUtils.mockIssuable(); + perspectives = TestUtils.mockPerspectives(issuable); fs = TestUtils.mockFileSystem(); reactor = TestUtils.mockReactor(); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectCoverageOnUnitTestCoverage() { Settings settings = new Settings(); settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/coverage-result-cobertura.xml"); - - sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/SAMPLE-test.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/SAMPLE-test.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/utils.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/application/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "builds/Unix Makefiles/COVERAGE/tests/moc_SAMPLE-test.cxx"); + sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); sensor.analyse(project, context); - verify(context, times(33)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(33)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } @Test @@ -71,19 +82,24 @@ public void shouldReportCorrectCoverageForAllTypesOfCoverage() { settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/coverage-result-cobertura.xml"); settings.setProperty(CxxCoverageSensor.IT_REPORT_PATH_KEY, "coverage-reports/cobertura/coverage-result-cobertura.xml"); settings.setProperty(CxxCoverageSensor.OVERALL_REPORT_PATH_KEY, "coverage-reports/cobertura/coverage-result-cobertura.xml"); - + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/SAMPLE-test.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/SAMPLE-test.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/utils.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/application/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "builds/Unix Makefiles/COVERAGE/tests/moc_SAMPLE-test.cxx"); sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - verify(context, times(99)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(99)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } @Test public void shouldReportNoCoverageSaved() { sensor = new CxxCoverageSensor(new Settings(), fs, TestUtils.mockReactor()); - when(context.getResource((File) anyObject())).thenReturn(null); + when(context.getResource((InputFile) anyObject())).thenReturn(null); sensor.analyse(project, context); - verify(context, times(0)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(0)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } @Test @@ -91,7 +107,6 @@ public void shouldNotCrashWhenProcessingReportsContainingBigNumberOfHits() { Settings settings = new Settings(); settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/specific-cases/cobertura-bignumberofhits.xml"); sensor = new CxxCoverageSensor(settings, fs, reactor); - sensor.analyse(project, context); } @@ -100,10 +115,8 @@ public void shouldReportNoCoverageWhenInvalidFilesEmpty() { Settings settings = new Settings(); settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/specific-cases/coverage-result-cobertura-empty.xml"); sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - - verify(context, times(0)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(0)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } @Test @@ -111,20 +124,26 @@ public void shouldReportNoCoverageWhenInvalidFilesInvalid() { Settings settings = new Settings(); settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/specific-cases/coverage-result-invalid.xml"); sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - - verify(context, times(0)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(0)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } @Test public void shouldReportCoverageWhenVisualStudioCase() { Settings settings = new Settings(); - settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/specific-cases/coverage-result-visual-studio.xml"); + if (TestUtils.isWindows()) { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-win.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "x:/coveragetest/project1/source1.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "x:/coveragetest/project2/source1.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "x:/coveragetest/project2/source2.cpp"); + } else { + settings.setProperty(CxxCoverageSensor.REPORT_PATH_KEY, "coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-linux.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "/x/coveragetest/project1/source1.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/x/coveragetest/project2/source1.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "/x/coveragetest/project2/source2.cpp"); + } sensor = new CxxCoverageSensor(settings, fs, TestUtils.mockReactor()); - sensor.analyse(project, context); - - verify(context, times(21)).saveMeasure((File) anyObject(), any(Measure.class)); + verify(context, times(21)).saveMeasure((InputFile) anyObject(), any(Measure.class)); } } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/cppcheck/CxxCppCheckSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/cppcheck/CxxCppCheckSensorTest.java index 4e4e5b9762..04d3cdd48b 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/cppcheck/CxxCppCheckSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/cppcheck/CxxCppCheckSensorTest.java @@ -19,21 +19,13 @@ */ package org.sonar.plugins.cxx.cppcheck; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.Matchers.any; - import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.plugins.cxx.TestUtils; import org.sonar.api.batch.bootstrap.ProjectReactor; @@ -41,6 +33,11 @@ import org.junit.Before; import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Matchers.any; + public class CxxCppCheckSensorTest { private CxxCppCheckSensor sensor; @@ -48,7 +45,7 @@ public class CxxCppCheckSensorTest { private Project project; private RulesProfile profile; private Settings settings; - private FileSystem fs; + private DefaultFileSystem fs; private ProjectReactor reactor; private Issuable issuable; private ResourcePerspectives perspectives; @@ -64,14 +61,14 @@ public void setUp() { settings = new Settings(); sensor = new CxxCppCheckSensor(perspectives, settings, fs, profile, reactor); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectViolations() { settings.setProperty(CxxCppCheckSensor.REPORT_PATH_KEY, - "cppcheck-reports/cppcheck-result-*.xml"); + "cppcheck-reports/cppcheck-result-*.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/utils.cpp"); sensor.analyse(project, context); verify(issuable, times(9)).addIssue(any(Issue.class)); } @@ -99,7 +96,6 @@ public void shouldIgnoreAViolationWhenTheResourceCouldntBeFoundV1() { settings.setProperty(CxxCppCheckSensor.REPORT_PATH_KEY, "cppcheck-reports/cppcheck-result-SAMPLE-V1.xml"); sensor = new CxxCppCheckSensor(perspectives, settings, fs, profile, reactor); - when(context.getResource((File) anyObject())).thenReturn(null); sensor.analyse(project, context); verify(issuable, times(0)).addIssue(any(Issue.class)); } @@ -109,7 +105,6 @@ public void shouldIgnoreAViolationWhenTheResourceCouldntBeFoundV2() { settings.setProperty(CxxCppCheckSensor.REPORT_PATH_KEY, "cppcheck-reports/cppcheck-result-SAMPLE-V2.xml"); sensor = new CxxCppCheckSensor(perspectives, settings, fs, profile, reactor); - when(context.getResource((File) anyObject())).thenReturn(null); sensor.analyse(project, context); verify(issuable, times(0)).addIssue(any(Issue.class)); } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/externalrules/CxxExternalRulesSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/externalrules/CxxExternalRulesSensorTest.java index 5046dd065a..07177f7665 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/externalrules/CxxExternalRulesSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/externalrules/CxxExternalRulesSensorTest.java @@ -19,27 +19,25 @@ */ package org.sonar.plugins.cxx.externalrules; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; //@todo SonarException has been deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html import org.sonar.plugins.cxx.TestUtils; -import org.sonar.api.batch.bootstrap.ProjectReactor; + +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.mock; public class CxxExternalRulesSensorTest { @@ -48,13 +46,13 @@ public class CxxExternalRulesSensorTest { private Project project; private RulesProfile profile; private Settings settings; - private FileSystem fs; + private DefaultFileSystem fs; private Issuable issuable; private ProjectReactor reactor; private ResourcePerspectives perspectives; @Before - public void setUp() { + public void setUp() { project = TestUtils.mockProject(); issuable = TestUtils.mockIssuable(); perspectives = TestUtils.mockPerspectives(issuable); @@ -63,13 +61,13 @@ public void setUp() { profile = mock(RulesProfile.class); context = mock(SensorContext.class); settings = new Settings(); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectViolations() { settings.setProperty(CxxExternalRulesSensor.REPORT_PATH_KEY, "externalrules-reports/externalrules-result-ok.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/utils.cpp"); sensor = new CxxExternalRulesSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); verify(issuable, times(2)).addIssue(any(Issue.class)); @@ -79,6 +77,7 @@ public void shouldReportCorrectViolations() { public void shouldReportFileLevelViolations() { settings.setProperty(CxxExternalRulesSensor.REPORT_PATH_KEY, "externalrules-reports/externalrules-result-filelevelviolation.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); sensor = new CxxExternalRulesSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); verify(issuable, times(1)).addIssue(any(Issue.class)); @@ -122,6 +121,7 @@ public void shouldThrowInCaseOfATrashyReport() { public void shouldReportOnlyOneViolationAndRemoveDuplicates() { settings = new Settings(); settings.setProperty(CxxExternalRulesSensor.REPORT_PATH_KEY, "externalrules-reports/externalrules-with-duplicates.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); sensor = new CxxExternalRulesSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); verify(issuable, times(1)).addIssue(any(Issue.class)); diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/pclint/CxxPCLintSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/pclint/CxxPCLintSensorTest.java index f4b449485c..333d3136b4 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/pclint/CxxPCLintSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/pclint/CxxPCLintSensorTest.java @@ -19,35 +19,34 @@ */ package org.sonar.plugins.cxx.pclint; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; //@todo SonarException has been deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html import org.sonar.plugins.cxx.TestUtils; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + public class CxxPCLintSensorTest { + private SensorContext context; private Project project; private RulesProfile profile; private ResourcePerspectives perspectives; private Issuable issuable; - private FileSystem fs; + private DefaultFileSystem fs; private ProjectReactor reactor; @Before @@ -59,23 +58,25 @@ public void setUp() { perspectives = TestUtils.mockPerspectives(issuable); profile = mock(RulesProfile.class); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectViolations() { Settings settings = new Settings(); settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/pclint-result-SAMPLE.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "FileZip.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "FileZip.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "ZipManager.cpp"); CxxPCLintSensor sensor = new CxxPCLintSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); - verify(issuable, times(15)).addIssue(any(Issue.class)); + verify(issuable, times(14)).addIssue(any(Issue.class)); } @Test public void shouldReportCorrectMisra2004Violations() { Settings settings = new Settings(); settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/pclint-result-MISRA2004-SAMPLE1.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "test.c"); CxxPCLintSensor sensor = new CxxPCLintSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); verify(issuable, times(29)).addIssue(any(Issue.class)); @@ -85,23 +86,26 @@ public void shouldReportCorrectMisra2004Violations() { public void shouldReportCorrectMisra2004PcLint9Violations() { Settings settings = new Settings(); settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/pclint-result-MISRA2004-SAMPLE2.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "test.c"); CxxPCLintSensor sensor = new CxxPCLintSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); verify(issuable, times(1)).addIssue(any(Issue.class)); } - - @Test(expected=SonarException.class) //@todo SonarException has been deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html + + @Test(expected = SonarException.class) //@todo SonarException has been deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html public void shouldThrowExceptionWhenMisra2004DescIsWrong() { Settings settings = new Settings(); settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/incorrect-pclint-MISRA2004-desc.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "test.c"); CxxPCLintSensor sensor = new CxxPCLintSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); } - @Test(expected=SonarException.class) //@todo SonarException has been deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html + @Test(expected = SonarException.class) //@todo SonarException has been deprecated, see http://javadocs.sonarsource.org/4.5.2/apidocs/deprecated-list.html public void shouldThrowExceptionWhenMisra2004RuleDoNotExist() { Settings settings = new Settings(); settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/incorrect-pclint-MISRA2004-rule-do-not-exist.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "test.c"); CxxPCLintSensor sensor = new CxxPCLintSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); } @@ -110,6 +114,7 @@ public void shouldThrowExceptionWhenMisra2004RuleDoNotExist() { public void shouldNotRemapMisra1998Rules() { Settings settings = new Settings(); settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/pclint-result-MISRA1998-SAMPLE.xml"); + TestUtils.addInputFile(fs, perspectives, issuable, "test.c"); CxxPCLintSensor sensor = new CxxPCLintSensor(perspectives, settings, fs, profile, reactor); sensor.analyse(project, context); verify(issuable, times(1)).addIssue(any(Issue.class)); diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/rats/CxxRatsSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/rats/CxxRatsSensorTest.java index da5b1beccc..c703bef424 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/rats/CxxRatsSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/rats/CxxRatsSensorTest.java @@ -19,32 +19,30 @@ */ package org.sonar.plugins.cxx.rats; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.plugins.cxx.TestUtils; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + public class CxxRatsSensorTest { private CxxRatsSensor sensor; private SensorContext context; private Project project; - private FileSystem fs; + private DefaultFileSystem fs; private Issuable issuable; private ResourcePerspectives perspectives; @@ -58,12 +56,12 @@ public void setUp() { settings.setProperty(CxxRatsSensor.REPORT_PATH_KEY, "rats-reports/rats-result-*.xml"); sensor = new CxxRatsSensor(perspectives, settings, fs, mock(RulesProfile.class), TestUtils.mockReactor()); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectViolations() { + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "report.c"); sensor.analyse(project, context); verify(issuable, times(5)).addIssue(any(Issue.class)); } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/squid/CxxSquidSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/squid/CxxSquidSensorTest.java index a67dfc2b30..54f43c8239 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/squid/CxxSquidSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/squid/CxxSquidSensorTest.java @@ -80,14 +80,14 @@ public void testCollectingSquidMetrics() { sensor.analyse(project, context); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.LINES), eq(92.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.NCLOC), eq(54.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.STATEMENTS), eq(50.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(7.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.CLASSES), eq(0.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.COMPLEXITY), eq(19.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.COMMENT_LINES), eq(15.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.LINES), eq(92.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.NCLOC), eq(54.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.STATEMENTS), eq(50.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(7.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.CLASSES), eq(0.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.COMPLEXITY), eq(19.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.COMMENT_LINES), eq(15.0)); } @Test @@ -98,12 +98,12 @@ public void testReplacingOfExtenalMacros() { sensor.analyse(project, context); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.LINES), eq(2.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.NCLOC), eq(1.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.STATEMENTS), eq(0.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(0.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.CLASSES), eq(1.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.LINES), eq(2.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.NCLOC), eq(1.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.STATEMENTS), eq(0.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(0.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.CLASSES), eq(1.0)); } @Test @@ -114,12 +114,12 @@ public void testFindingIncludedFiles() { sensor.analyse(project, context); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.LINES), eq(29.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.NCLOC), eq(9.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.STATEMENTS), eq(0.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(9.0)); - verify(context).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.CLASSES), eq(0.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.LINES), eq(29.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.NCLOC), eq(9.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.STATEMENTS), eq(0.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(9.0)); + verify(context).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.CLASSES), eq(0.0)); } @Test @@ -132,12 +132,12 @@ public void testForceIncludedFiles() { sensor.analyse(project, context); // These checks actually check the force include feature, since only if it works the metric values will be like follows - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.LINES), eq(1.0)); - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.NCLOC), eq(1.0)); - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.STATEMENTS), eq(2.0)); - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(1.0)); - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.CLASSES), eq(0.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FILES), eq(1.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.LINES), eq(1.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.NCLOC), eq(1.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.STATEMENTS), eq(2.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.FUNCTIONS), eq(1.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.CLASSES), eq(0.0)); } @Test @@ -150,7 +150,7 @@ public void testBehaviourOnCircularIncludes() { sensor.analyse(project, context); - verify(context, times(2)).saveMeasure((org.sonar.api.resources.File) anyObject(), eq(CoreMetrics.NCLOC), eq(1.0)); + verify(context, times(2)).saveMeasure((InputFile) anyObject(), eq(CoreMetrics.NCLOC), eq(1.0)); } @Test diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/valgrind/CxxValgrindSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/valgrind/CxxValgrindSensorTest.java index 6272a4baae..d7c2781f27 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/valgrind/CxxValgrindSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/valgrind/CxxValgrindSensorTest.java @@ -32,13 +32,12 @@ import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.plugins.cxx.TestUtils; @@ -46,7 +45,7 @@ public class CxxValgrindSensorTest { private CxxValgrindSensor sensor; private SensorContext context; private Project project; - private FileSystem fs; + private DefaultFileSystem fs; private Issuable issuable; private ResourcePerspectives perspectives; @@ -58,8 +57,6 @@ public void setUp() { perspectives = TestUtils.mockPerspectives(issuable); sensor = new CxxValgrindSensor(perspectives, new Settings(), fs, mock(RulesProfile.class), TestUtils.mockReactor()); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource(any(File.class))).thenReturn(resourceMock); } @Test @@ -71,6 +68,7 @@ public void shouldNotThrowWhenGivenValidData() { public void shouldSaveViolationIfErrorIsInside() { Set valgrindErrors = new HashSet(); valgrindErrors.add(mockValgrindError(true)); + TestUtils.addInputFile(fs, perspectives, issuable, "dir/file"); sensor.saveErrors(project, context, valgrindErrors); verify(issuable, times(1)).addIssue(any(Issue.class)); } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/veraxx/CxxVeraxxSensorTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/veraxx/CxxVeraxxSensorTest.java index fec98e8745..d24e8dbdc9 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/veraxx/CxxVeraxxSensorTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/veraxx/CxxVeraxxSensorTest.java @@ -19,17 +19,8 @@ */ package org.sonar.plugins.cxx.veraxx; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Before; -import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issuable; @@ -38,13 +29,21 @@ import org.sonar.api.resources.Project; import org.sonar.plugins.cxx.TestUtils; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + public class CxxVeraxxSensorTest { private CxxVeraxxSensor sensor; private SensorContext context; private Project project; private Issuable issuable; private ResourcePerspectives perspectives; - private FileSystem fs; + private DefaultFileSystem fs; @Before public void setUp() { @@ -56,12 +55,16 @@ public void setUp() { settings.setProperty(CxxVeraxxSensor.REPORT_PATH_KEY, "vera++-reports/vera++-result-*.xml"); sensor = new CxxVeraxxSensor(perspectives, settings, fs, mock(RulesProfile.class), TestUtils.mockReactor()); context = mock(SensorContext.class); - org.sonar.api.resources.File resourceMock = mock(org.sonar.api.resources.File.class); - when(context.getResource((org.sonar.api.resources.File) anyObject())).thenReturn(resourceMock); } @Test public void shouldReportCorrectViolations() { + TestUtils.addInputFile(fs, perspectives, issuable, "sources/application/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/SAMPLE-test.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/SAMPLE-test.h"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/tests/main.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/code_chunks.cpp"); + TestUtils.addInputFile(fs, perspectives, issuable, "sources/utils/utils.cpp"); sensor.analyse(project, context); verify(issuable, times(10)).addIssue(any(Issue.class)); } diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/MSTestResultsProviderTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/MSTestResultsProviderTest.java index 6658066acc..5e4ae1f521 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/MSTestResultsProviderTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/MSTestResultsProviderTest.java @@ -20,7 +20,6 @@ package org.sonar.plugins.cxx.xunit; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -29,7 +28,6 @@ import org.junit.Test; import org.mockito.Mockito; import org.sonar.api.measures.Metric; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.internal.DefaultFileSystem; @@ -52,8 +50,6 @@ public void setUp() { new DefaultFileSystem(); project = TestUtils.mockProject(); context = mock(SensorContext.class); - File resourceMock = mock(File.class); - when(context.getResource((File) anyObject())).thenReturn(resourceMock); } @Test diff --git a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/TestResourceTest.java b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/TestFileTest.java similarity index 59% rename from sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/TestResourceTest.java rename to sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/TestFileTest.java index a0292d3602..f8981aa4ca 100644 --- a/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/TestResourceTest.java +++ b/sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/xunit/TestFileTest.java @@ -19,76 +19,78 @@ */ package org.sonar.plugins.cxx.xunit; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.junit.Before; import org.junit.Test; -public class TestResourceTest { - TestResource resource; - TestResource equalResource; - TestResource otherResource; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TestFileTest { + TestFile testFile; @Before public void setUp() { - resource = new TestResource(null); + InputFile inputFile = new DefaultInputFile("test.cpp"); + testFile = new TestFile(inputFile); } @Test - public void newBornResourceShouldHaveVirginStatistics() { - assertEquals(resource.getTests(), 0); - assertEquals(resource.getErrors(), 0); - assertEquals(resource.getFailures(), 0); - assertEquals(resource.getSkipped(), 0); - assertEquals(resource.getTime(), 0); - assertEquals(resource.getDetails(), ""); + public void newBornTestFileShouldHaveVirginStatistics() { + assertEquals(testFile.getTests(), 0); + assertEquals(testFile.getErrors(), 0); + assertEquals(testFile.getFailures(), 0); + assertEquals(testFile.getSkipped(), 0); + assertEquals(testFile.getTime(), 0); + assertEquals(testFile.getDetails(), ""); } @Test public void addingTestCaseShouldIncrementStatistics() { - int testBefore = resource.getTests(); - int timeBefore = resource.getTime(); + int testBefore = testFile.getTests(); + int timeBefore = testFile.getTime(); final int EXEC_TIME = 10; - resource.addTestCase(new TestCase("name", EXEC_TIME, "status", "stack", "msg", + testFile.addTestCase(new TestCase("name", EXEC_TIME, "status", "stack", "msg", "classname", "tcfilename", "tsname", "tsfilename")); - assertEquals(resource.getTests(), testBefore + 1); - assertEquals(resource.getTime(), timeBefore + EXEC_TIME); + assertEquals(testFile.getTests(), testBefore + 1); + assertEquals(testFile.getTime(), timeBefore + EXEC_TIME); } @Test public void addingAnErroneousTestCaseShouldIncrementErrorStatistic() { - int errorsBefore = resource.getErrors(); + int errorsBefore = testFile.getErrors(); TestCase error = mock(TestCase.class); when(error.isError()).thenReturn(true); - resource.addTestCase(error); + testFile.addTestCase(error); - assertEquals(resource.getErrors(), errorsBefore + 1); + assertEquals(testFile.getErrors(), errorsBefore + 1); } @Test public void addingAFailedTestCaseShouldIncrementFailedStatistic() { - int failedBefore = resource.getFailures(); + int failedBefore = testFile.getFailures(); TestCase failedTC = mock(TestCase.class); when(failedTC.isFailure()).thenReturn(true); - resource.addTestCase(failedTC); + testFile.addTestCase(failedTC); - assertEquals(resource.getFailures(), failedBefore + 1); + assertEquals(testFile.getFailures(), failedBefore + 1); } @Test public void addingASkippedTestCaseShouldIncrementSkippedStatistic() { - int skippedBefore = resource.getSkipped(); + int skippedBefore = testFile.getSkipped(); TestCase skippedTC = mock(TestCase.class); when(skippedTC.isSkipped()).thenReturn(true); - resource.addTestCase(skippedTC); + testFile.addTestCase(skippedTC); - assertEquals(resource.getSkipped(), skippedBefore + 1); + assertEquals(testFile.getSkipped(), skippedBefore + 1); } } diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-linux.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-linux.xml new file mode 100644 index 0000000000..130118f6e2 --- /dev/null +++ b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-linux.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-win.xml similarity index 100% rename from sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash.xml rename to sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-drive-letter-without-slash-win.xml diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-linux.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-linux.xml new file mode 100644 index 0000000000..357bfcbe70 --- /dev/null +++ b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-linux.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-win.xml similarity index 100% rename from sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node.xml rename to sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/bullseye-coverage-report-data-in-root-node-win.xml diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye-linux.xml similarity index 97% rename from sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye.xml rename to sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye-linux.xml index 91f05b42ca..1106d59242 100644 --- a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye.xml +++ b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye-linux.xml @@ -1,6 +1,6 @@ - + diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye-win.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye-win.xml new file mode 100644 index 0000000000..89804b5377 --- /dev/null +++ b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/bullseye/coverage-result-bullseye-win.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-linux.xml similarity index 90% rename from sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio.xml rename to sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-linux.xml index 26bdcf16e7..1affad1d8e 100644 --- a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio.xml +++ b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-linux.xml @@ -10,7 +10,7 @@ - + @@ -52,9 +52,9 @@ - + - + diff --git a/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-win.xml b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-win.xml new file mode 100644 index 0000000000..6ac2accc36 --- /dev/null +++ b/sonar-cxx-plugin/src/test/resources/org/sonar/plugins/cxx/reports-project/coverage-reports/cobertura/specific-cases/coverage-result-visual-studio-win.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +