import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.core.util.CloseableIterator;
-import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
static final String FILE_KEY = "FILE_KEY";
static final int FILE_REF = 2;
- static final Component FILE = builder(Component.Type.FILE, FILE_REF)
- .setKey(FILE_KEY)
- .setUuid(FILE_UUID)
- .build();
-
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void read_lines_from_report() throws Exception {
- reportReader.putComponent(createFileBatchComponent(2));
reportReader.putFileSourceLines(FILE_REF, "line1", "line2");
- assertThat(underTest.readLines(FILE)).containsOnly("line1", "line2");
+ assertThat(underTest.readLines(createComponent(2))).containsOnly("line1", "line2");
}
@Test
public void read_lines_adds_one_extra_empty_line_when_sourceLine_has_elements_count_equals_to_lineCount_minus_1() throws Exception {
- reportReader.putComponent(createFileBatchComponent(3));
reportReader.putFileSourceLines(FILE_REF, "line1", "line2");
- assertThat(underTest.readLines(FILE)).containsOnly("line1", "line2", "");
+ assertThat(underTest.readLines(createComponent(3))).containsOnly("line1", "line2", "");
}
@Test
public void read_lines_throws_ISE_when_sourceLine_has_less_elements_then_lineCount_minus_1() throws Exception {
- reportReader.putComponent(createFileBatchComponent(10));
reportReader.putFileSourceLines(FILE_REF, "line1", "line2");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Source of file 'ReportComponent{ref=2, key='FILE_KEY', type=FILE}' has less lines (2) than the expected number (10)");
- consume(underTest.readLines(FILE));
+ consume(underTest.readLines(createComponent(10)));
}
@Test
public void read_lines_throws_ISE_when_sourceLines_has_more_elements_then_lineCount() throws Exception {
- reportReader.putComponent(createFileBatchComponent(2));
reportReader.putFileSourceLines(FILE_REF, "line1", "line2", "line3");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Source of file 'ReportComponent{ref=2, key='FILE_KEY', type=FILE}' has at least one more line than the expected number (2)");
- consume(underTest.readLines(FILE));
- }
-
- @Test
- public void not_fail_to_read_lines_on_empty_file_from_report() throws Exception {
- // File exist but there's no line
- reportReader.putComponent(createFileBatchComponent(0));
- reportReader.putFileSourceLines(FILE_REF);
-
- // Should not try to read source file from the db
- assertThat(underTest.readLines(FILE)).isEmpty();
+ consume(underTest.readLines(createComponent(2)));
}
@Test
thrown.expect(IllegalStateException.class);
thrown.expectMessage("File 'ReportComponent{ref=2, key='FILE_KEY', type=FILE}' has no source code");
- underTest.readLines(FILE);
+ underTest.readLines(builder(Component.Type.FILE, FILE_REF)
+ .setKey(FILE_KEY)
+ .setUuid(FILE_UUID)
+ .build());
}
@Test
underTest.readLines(builder(Component.Type.PROJECT, 123).setKey("NotFile").build());
}
- private static ScannerReport.Component createFileBatchComponent(int lineCount) {
- return ScannerReport.Component.newBuilder().setRef(FILE_REF).setLines(lineCount).build();
+ private static Component createComponent(int lineCount) {
+ return builder(Component.Type.FILE, FILE_REF)
+ .setKey(FILE_KEY)
+ .setUuid(FILE_UUID)
+ .setFileAttributes(new FileAttributes(false, null, lineCount))
+ .build();
}
private static void consume(CloseableIterator<String> stringCloseableIterator) {
- try{
+ try {
while (stringCloseableIterator.hasNext()) {
stringCloseableIterator.next();
}
import org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.duplication.Duplicate;
private void initBasicReport(int numberOfLines) {
treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).addChildren(
ReportComponent.builder(Component.Type.MODULE, 2).setUuid("MODULE").setKey("MODULE_KEY").addChildren(
- ReportComponent.builder(Component.Type.FILE, FILE_REF).setUuid(FILE_UUID).setKey("MODULE_KEY:src/Foo.java").build())
+ ReportComponent.builder(Component.Type.FILE, FILE_REF).setUuid(FILE_UUID).setKey("MODULE_KEY:src/Foo.java")
+ .setFileAttributes(new FileAttributes(false, null, numberOfLines)).build())
.build())
.build());
reportReader.putComponent(ScannerReport.Component.newBuilder()
.setRef(FILE_REF)
.setType(ComponentType.FILE)
+ .setLines(numberOfLines)
.build());
for (int i = 1; i <= numberOfLines; i++) {