use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class CpdMediumTest method testCrossFileDuplications.
@Test
public void testCrossFileDuplications() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
String duplicatedStuff = "Sample xoo\ncontent\n" + "foo\nbar\ntoto\ntiti\n" + "foo\nbar\ntoto\ntiti\n" + "bar\ntoto\ntiti\n" + "foo\nbar\ntoto\ntiti";
File xooFile1 = new File(srcDir, "sample1.xoo");
FileUtils.write(xooFile1, duplicatedStuff, StandardCharsets.UTF_8);
File xooFile2 = new File(srcDir, "sample2.xoo");
FileUtils.write(xooFile2, duplicatedStuff, StandardCharsets.UTF_8);
AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").put("sonar.cpd.xoo.minimumTokens", "10").put("sonar.verbose", "true").build()).execute();
assertThat(result.inputFiles()).hasSize(2);
InputFile inputFile1 = result.inputFile("src/sample1.xoo");
InputFile inputFile2 = result.inputFile("src/sample2.xoo");
// One clone group on each file
List<ScannerReport.Duplication> duplicationGroupsFile1 = result.duplicationsFor(inputFile1);
assertThat(duplicationGroupsFile1).hasSize(1);
ScannerReport.Duplication cloneGroupFile1 = duplicationGroupsFile1.get(0);
assertThat(cloneGroupFile1.getOriginPosition().getStartLine()).isOne();
assertThat(cloneGroupFile1.getOriginPosition().getEndLine()).isEqualTo(17);
assertThat(cloneGroupFile1.getDuplicateList()).hasSize(1);
assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile2).getRef());
List<ScannerReport.Duplication> duplicationGroupsFile2 = result.duplicationsFor(inputFile2);
assertThat(duplicationGroupsFile2).hasSize(1);
ScannerReport.Duplication cloneGroupFile2 = duplicationGroupsFile2.get(0);
assertThat(cloneGroupFile2.getOriginPosition().getStartLine()).isOne();
assertThat(cloneGroupFile2.getOriginPosition().getEndLine()).isEqualTo(17);
assertThat(cloneGroupFile2.getDuplicateList()).hasSize(1);
assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile1).getRef());
assertThat(result.duplicationBlocksFor(inputFile1)).isEmpty();
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class CpdMediumTest method testExclusions.
@Test
public void testExclusions() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
String duplicatedStuff = "Sample xoo\ncontent\n" + "foo\nbar\ntoto\ntiti\n" + "foo\nbar\ntoto\ntiti\n" + "bar\ntoto\ntiti\n" + "foo\nbar\ntoto\ntiti";
File xooFile1 = new File(srcDir, "sample1.xoo");
FileUtils.write(xooFile1, duplicatedStuff);
File xooFile2 = new File(srcDir, "sample2.xoo");
FileUtils.write(xooFile2, duplicatedStuff);
AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").put("sonar.cpd.xoo.minimumTokens", "10").put("sonar.cpd.exclusions", "src/sample1.xoo").build()).execute();
assertThat(result.inputFiles()).hasSize(2);
InputFile inputFile1 = result.inputFile("src/sample1.xoo");
InputFile inputFile2 = result.inputFile("src/sample2.xoo");
List<ScannerReport.Duplication> duplicationGroupsFile1 = result.duplicationsFor(inputFile1);
assertThat(duplicationGroupsFile1).isEmpty();
List<ScannerReport.Duplication> duplicationGroupsFile2 = result.duplicationsFor(inputFile2);
assertThat(duplicationGroupsFile2).isEmpty();
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class CpdMediumTest method enableCrossProjectDuplication.
@Test
public void enableCrossProjectDuplication() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
String duplicatedStuff = "Sample xoo\ncontent\nfoo\nbar\ntoto\ntiti\nfoo";
File xooFile1 = new File(srcDir, "sample1.xoo");
FileUtils.write(xooFile1, duplicatedStuff);
AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").put("sonar.cpd.xoo.minimumTokens", "1").put("sonar.cpd.xoo.minimumLines", "5").put("sonar.verbose", "true").put("sonar.cpd.cross_project", "true").build()).execute();
InputFile inputFile1 = result.inputFile("src/sample1.xoo");
List<ScannerReport.CpdTextBlock> duplicationBlocks = result.duplicationBlocksFor(inputFile1);
assertThat(duplicationBlocks).hasSize(3);
assertThat(duplicationBlocks.get(0).getStartLine()).isOne();
assertThat(duplicationBlocks.get(0).getEndLine()).isEqualTo(5);
assertThat(duplicationBlocks.get(0).getStartTokenIndex()).isOne();
assertThat(duplicationBlocks.get(0).getEndTokenIndex()).isEqualTo(6);
assertThat(duplicationBlocks.get(0).getHash()).isNotEmpty();
assertThat(duplicationBlocks.get(1).getStartLine()).isEqualTo(2);
assertThat(duplicationBlocks.get(1).getEndLine()).isEqualTo(6);
assertThat(duplicationBlocks.get(1).getStartTokenIndex()).isEqualTo(3);
assertThat(duplicationBlocks.get(1).getEndTokenIndex()).isEqualTo(7);
assertThat(duplicationBlocks.get(0).getHash()).isNotEmpty();
assertThat(duplicationBlocks.get(2).getStartLine()).isEqualTo(3);
assertThat(duplicationBlocks.get(2).getEndLine()).isEqualTo(7);
assertThat(duplicationBlocks.get(2).getStartTokenIndex()).isEqualTo(4);
assertThat(duplicationBlocks.get(2).getEndTokenIndex()).isEqualTo(8);
assertThat(duplicationBlocks.get(0).getHash()).isNotEmpty();
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class ExternalIssuesMediumTest method testLoadIssuesFromJsonReport.
@Test
public void testLoadIssuesFromJsonReport() throws URISyntaxException, IOException {
File projectDir = new File("test-resources/mediumtest/xoo/sample");
File tmpDir = temp.newFolder();
FileUtils.copyDirectory(projectDir, tmpDir);
AnalysisResult result = tester.newAnalysis(new File(tmpDir, "sonar-project.properties")).property("sonar.externalIssuesReportPaths", "externalIssues.json").execute();
List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
assertThat(issues).isEmpty();
List<ExternalIssue> externalIssues = result.externalIssuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
assertThat(externalIssues).hasSize(2);
// precise issue location
ExternalIssue issue = externalIssues.get(0);
assertPreciseIssueLocation(issue);
// location on a line
issue = externalIssues.get(1);
assertIssueLocationLine(issue);
// One file-level issue in helloscala, with secondary location
List<ExternalIssue> externalIssues2 = result.externalIssuesFor(result.inputFile("xources/hello/helloscala.xoo"));
assertThat(externalIssues2).hasSize(1);
issue = externalIssues2.iterator().next();
assertSecondaryLocation(issue);
// one issue is located in a non-existing file
assertThat(logs.logs()).contains("External issues ignored for 1 unknown files, including: invalidFile");
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class ExternalIssuesMediumTest method testOneIssuePerLine.
@Test
public void testOneIssuePerLine() throws Exception {
File projectDir = new File("test-resources/mediumtest/xoo/sample");
File tmpDir = temp.newFolder();
FileUtils.copyDirectory(projectDir, tmpDir);
AnalysisResult result = tester.newAnalysis(new File(tmpDir, "sonar-project.properties")).property(OneExternalIssuePerLineSensor.ACTIVATE, "true").execute();
List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
assertThat(issues).isEmpty();
List<ExternalIssue> externalIssues = result.externalIssuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
assertThat(externalIssues).hasSize(8);
ExternalIssue issue = externalIssues.get(0);
assertThat(issue.getTextRange().getStartLine()).isEqualTo(issue.getTextRange().getStartLine());
assertThat(result.adHocRules()).isEmpty();
}
Aggregations