use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class CpdMediumTest method testCrossModuleDuplications.
@Test
public void testCrossModuleDuplications() throws IOException {
builder.put("sonar.modules", "module1,module2").put("sonar.cpd.xoo.minimumTokens", "10").put("sonar.verbose", "true");
// module 1
builder.put("module1.sonar.projectKey", "module1");
builder.put("module1.sonar.projectName", "Module 1");
builder.put("module1.sonar.sources", ".");
// module2
builder.put("module2.sonar.projectKey", "module2");
builder.put("module2.sonar.projectName", "Module 2");
builder.put("module2.sonar.sources", ".");
File module1Dir = new File(baseDir, "module1");
File module2Dir = new File(baseDir, "module2");
module1Dir.mkdir();
module2Dir.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";
// create duplicated file in both modules
File xooFile1 = new File(module1Dir, "sample1.xoo");
FileUtils.write(xooFile1, duplicatedStuff);
File xooFile2 = new File(module2Dir, "sample2.xoo");
FileUtils.write(xooFile2, duplicatedStuff);
AnalysisResult result = tester.newAnalysis().properties(builder.build()).execute();
assertThat(result.inputFiles()).hasSize(2);
InputFile inputFile1 = result.inputFile("module1/sample1.xoo");
InputFile inputFile2 = result.inputFile("module2/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 testIntraFileDuplications.
@Test
public void testIntraFileDuplications() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
String content = "Sample xoo\ncontent\nfoo\nbar\nSample xoo\ncontent\n";
File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, content);
AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").put("sonar.cpd.xoo.minimumTokens", "2").put("sonar.cpd.xoo.minimumLines", "2").put("sonar.verbose", "true").build()).execute();
InputFile inputFile = result.inputFile("src/sample.xoo");
// One clone group
List<ScannerReport.Duplication> duplicationGroups = result.duplicationsFor(inputFile);
assertThat(duplicationGroups).hasSize(1);
ScannerReport.Duplication cloneGroup = duplicationGroups.get(0);
assertThat(cloneGroup.getOriginPosition().getStartLine()).isOne();
assertThat(cloneGroup.getOriginPosition().getEndLine()).isEqualTo(2);
assertThat(cloneGroup.getDuplicateList()).hasSize(1);
assertThat(cloneGroup.getDuplicate(0).getRange().getStartLine()).isEqualTo(5);
assertThat(cloneGroup.getDuplicate(0).getRange().getEndLine()).isEqualTo(6);
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class HighlightingMediumTest method computeSyntaxHighlightingOnTempProject.
@Test
public void computeSyntaxHighlightingOnTempProject() throws IOException {
File baseDir = temp.newFolder();
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
File xooFile = new File(srcDir, "sample.xoo");
File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
FileUtils.write(xooFile, "Sample xoo\ncontent plop");
FileUtils.write(xoohighlightingFile, "1:0:2:0:s\n2:0:2:8:k");
AnalysisResult result = tester.newAnalysis().properties(ImmutableMap.<String, String>builder().put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.projectName", "Foo Project").put("sonar.projectVersion", "1.0-SNAPSHOT").put("sonar.projectDescription", "Description of Foo Project").put("sonar.sources", "src").build()).execute();
InputFile file = result.inputFile("src/sample.xoo");
assertThat(result.highlightingTypeFor(file, 1, 0)).containsExactly(TypeOfText.STRING);
assertThat(result.highlightingTypeFor(file, 1, 9)).containsExactly(TypeOfText.STRING);
assertThat(result.highlightingTypeFor(file, 2, 0)).containsExactly(TypeOfText.KEYWORD);
assertThat(result.highlightingTypeFor(file, 2, 8)).isEmpty();
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class FileSystemMediumTest method dont_log_warn_about_files_out_of_basedir_if_they_arent_included.
@Test
public void dont_log_warn_about_files_out_of_basedir_if_they_arent_included() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
writeFile(srcDir, "sample1.xoo", "Sample xoo\ncontent");
File outsideBaseDir = temp.newFolder().getCanonicalFile();
File xooFile2 = new File(outsideBaseDir, "another.xoo");
FileUtils.write(xooFile2, "Sample xoo 2\ncontent", StandardCharsets.UTF_8);
AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src," + PathUtils.canonicalPath(xooFile2)).put("sonar.inclusions", "**/sample1.xoo").build()).execute();
assertThat(result.inputFiles()).hasSize(1);
assertThat(logTester.logs(LoggerLevel.WARN)).doesNotContain("File '" + xooFile2.getAbsolutePath() + "' is ignored. It is not located in project basedir '" + baseDir + "'.");
}
use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.
the class FileSystemMediumTest method warn_user_for_outdated_scanner_side_inherited_exclusions_for_multi_module_project.
@Test
public void warn_user_for_outdated_scanner_side_inherited_exclusions_for_multi_module_project() throws IOException {
File baseDir = temp.getRoot();
File baseDirModuleA = new File(baseDir, "moduleA");
File baseDirModuleB = new File(baseDir, "moduleB");
File srcDirA = new File(baseDirModuleA, "src");
srcDirA.mkdirs();
File srcDirB = new File(baseDirModuleB, "src");
srcDirB.mkdirs();
writeFile(srcDirA, "sample.xoo", "Sample xoo\ncontent");
writeFile(srcDirB, "sample.xoo", "Sample xoo\ncontent");
AnalysisResult result = tester.newAnalysis().properties(ImmutableMap.<String, String>builder().put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.sources", "src").put("sonar.modules", "moduleA,moduleB").put("sonar.exclusions", "src/sample.xoo").build()).execute();
InputFile fileA = result.inputFile("moduleA/src/sample.xoo");
assertThat(fileA).isNull();
InputFile fileB = result.inputFile("moduleB/src/sample.xoo");
assertThat(fileB).isNull();
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Specifying module-relative paths at project level in the property 'sonar.exclusions' is deprecated. " + "To continue matching files like 'moduleA/src/sample.xoo', update this property so that patterns refer to project-relative paths.");
}
Aggregations