use of org.sonar.api.batch.fs.InputFile 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);
TaskResult result = tester.newTask().properties(builder.put("sonar.sources", "src").put("sonar.cpd.xoo.minimumTokens", "2").put("sonar.cpd.xoo.minimumLines", "2").put("sonar.verbose", "true").build()).start();
InputFile inputFile = result.inputFile("src/sample.xoo");
// One clone group
List<org.sonar.scanner.protocol.output.ScannerReport.Duplication> duplicationGroups = result.duplicationsFor(inputFile);
assertThat(duplicationGroups).hasSize(1);
org.sonar.scanner.protocol.output.ScannerReport.Duplication cloneGroup = duplicationGroups.get(0);
assertThat(cloneGroup.getOriginPosition().getStartLine()).isEqualTo(1);
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.api.batch.fs.InputFile 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);
TaskResult result = tester.newTask().properties(builder.build()).start();
assertThat(result.inputFiles()).hasSize(2);
InputFile inputFile1 = result.inputFile("sample1.xoo");
InputFile inputFile2 = result.inputFile("sample2.xoo");
// One clone group on each file
List<org.sonar.scanner.protocol.output.ScannerReport.Duplication> duplicationGroupsFile1 = result.duplicationsFor(inputFile1);
assertThat(duplicationGroupsFile1).hasSize(1);
org.sonar.scanner.protocol.output.ScannerReport.Duplication cloneGroupFile1 = duplicationGroupsFile1.get(0);
assertThat(cloneGroupFile1.getOriginPosition().getStartLine()).isEqualTo(1);
assertThat(cloneGroupFile1.getOriginPosition().getEndLine()).isEqualTo(17);
assertThat(cloneGroupFile1.getDuplicateList()).hasSize(1);
assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(((DefaultInputFile) inputFile2).key()).getRef());
List<org.sonar.scanner.protocol.output.ScannerReport.Duplication> duplicationGroupsFile2 = result.duplicationsFor(inputFile2);
assertThat(duplicationGroupsFile2).hasSize(1);
org.sonar.scanner.protocol.output.ScannerReport.Duplication cloneGroupFile2 = duplicationGroupsFile2.get(0);
assertThat(cloneGroupFile2.getOriginPosition().getStartLine()).isEqualTo(1);
assertThat(cloneGroupFile2.getOriginPosition().getEndLine()).isEqualTo(17);
assertThat(cloneGroupFile2.getDuplicateList()).hasSize(1);
assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(((DefaultInputFile) inputFile1).key()).getRef());
assertThat(result.duplicationBlocksFor(inputFile1)).isEmpty();
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class DefaultIndexTest method shouldGetHierarchy.
@Test
public void shouldGetHierarchy() {
InputComponent component = new DefaultInputModule("module1");
InputFile file1 = new TestInputFileBuilder("module1", "src/org/foo/Bar.java").build();
when(componentStore.getByKey("module1")).thenReturn(component);
when(componentStore.getByKey("module1:src/org/foo/Bar.java")).thenReturn(file1);
when(componentTree.getParent(file1)).thenReturn(component);
when(componentTree.getChildren(component)).thenReturn(Collections.singleton(file1));
assertThat(index.getParent("module1:src/org/foo/Bar.java").getKey()).isEqualTo("module1");
assertThat(index.getParent("module1")).isNull();
assertThat(index.getChildren("module1")).containsOnly(File.create("src/org/foo/Bar.java"));
assertThat(index.getChildren("module1:src/org/foo/Bar.java")).isEmpty();
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class DefaultIndexTest method shouldTransformToResource.
@Test
public void shouldTransformToResource() {
DefaultInputModule component = new DefaultInputModule(ProjectDefinition.create().setKey("module1").setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "branch1"), 1);
InputFile file1 = new TestInputFileBuilder("module1", "src/org/foo/Bar.java").build();
InputDir dir = new DefaultInputDir("module1", "src");
assertThat(index.toResource(component)).isInstanceOf(Project.class);
assertThat(index.toResource(component).getKey()).isEqualTo("module1");
assertThat(index.toResource(component).getEffectiveKey()).isEqualTo("module1:branch1");
assertThat(index.toResource(file1)).isInstanceOf(File.class);
assertThat(index.toResource(file1).getKey()).isEqualTo("src/org/foo/Bar.java");
assertThat(index.toResource(file1).getPath()).isEqualTo("src/org/foo/Bar.java");
assertThat(index.toResource(dir)).isInstanceOf(Directory.class);
assertThat(index.toResource(dir).getKey()).isEqualTo("src");
assertThat(index.toResource(dir).getPath()).isEqualTo("src");
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class CoveragePerTestMediumTest method coveragePerTestInReport.
@Test
public void coveragePerTestInReport() throws IOException {
File baseDir = createTestFiles();
File testDir = new File(baseDir, "test");
File xooTestExecutionFile = new File(testDir, "sampleTest.xoo.test");
FileUtils.write(xooTestExecutionFile, "some test:4:::OK:UNIT\n" + "another test:10:::OK:UNIT\n" + "test without coverage:10:::OK:UNIT\n");
File xooCoveragePerTestFile = new File(testDir, "sampleTest.xoo.testcoverage");
FileUtils.write(xooCoveragePerTestFile, "some test;src/sample.xoo,10,11;src/sample2.xoo,1,2\n" + "another test;src/sample.xoo,10,20\n");
TaskResult result = runTask(baseDir);
InputFile file = result.inputFile("test/sampleTest.xoo");
org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail someTest = result.coveragePerTestFor(file, "some test");
assertThat(someTest.getCoveredFileList()).hasSize(2);
assertThat(someTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
assertThat(someTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 11);
assertThat(someTest.getCoveredFile(1).getFileRef()).isGreaterThan(0);
assertThat(someTest.getCoveredFile(1).getCoveredLineList()).containsExactly(1, 2);
org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail anotherTest = result.coveragePerTestFor(file, "another test");
assertThat(anotherTest.getCoveredFileList()).hasSize(1);
assertThat(anotherTest.getCoveredFile(0).getFileRef()).isGreaterThan(0);
assertThat(anotherTest.getCoveredFile(0).getCoveredLineList()).containsExactly(10, 20);
}
Aggregations