@Override public String apply(@Nonnull CpdTextBlock duplicationBlock) { return duplicationBlock.getHash(); } }
@Test public void persist_cpd_text_block() { when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true); reportReader.putDuplicationBlocks(FILE_1_REF, singletonList(CPD_TEXT_BLOCK)); TestComputationStepContext context = new TestComputationStepContext(); underTest.execute(context); Map<String, Object> dto = dbTester.selectFirst("select HASH, START_LINE, END_LINE, INDEX_IN_FILE, COMPONENT_UUID, ANALYSIS_UUID from duplications_index"); assertThat(dto.get("HASH")).isEqualTo(CPD_TEXT_BLOCK.getHash()); assertThat(dto.get("START_LINE")).isEqualTo(30L); assertThat(dto.get("END_LINE")).isEqualTo(45L); assertThat(dto.get("INDEX_IN_FILE")).isEqualTo(0L); assertThat(dto.get("COMPONENT_UUID")).isEqualTo(FILE_1.getUuid()); assertThat(dto.get("ANALYSIS_UUID")).isEqualTo(ANALYSIS_UUID); context.getStatistics().assertValue("inserts", 1); }
assertThat(duplicationBlocks.get(0).getStartTokenIndex()).isEqualTo(1); assertThat(duplicationBlocks.get(0).getEndTokenIndex()).isEqualTo(6); assertThat(duplicationBlocks.get(0).getHash()).isNotEmpty(); assertThat(duplicationBlocks.get(1).getStartTokenIndex()).isEqualTo(3); assertThat(duplicationBlocks.get(1).getEndTokenIndex()).isEqualTo(7); assertThat(duplicationBlocks.get(0).getHash()).isNotEmpty(); assertThat(duplicationBlocks.get(2).getStartTokenIndex()).isEqualTo(4); assertThat(duplicationBlocks.get(2).getEndTokenIndex()).isEqualTo(8); assertThat(duplicationBlocks.get(0).getHash()).isNotEmpty();
private void readFromReport(Component component) { int indexInFile = 0; try (CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) { while (blocks.hasNext()) { ScannerReport.CpdTextBlock block = blocks.next(); dbClient.duplicationDao().insert( session, new DuplicationUnitDto() .setHash(block.getHash()) .setStartLine(block.getStartLine()) .setEndLine(block.getEndLine()) .setIndexInFile(indexInFile) .setAnalysisUuid(analysisUuid) .setComponentUuid(component.getUuid())); indexInFile++; } } }
@Override public String apply(@Nonnull CpdTextBlock duplicationBlock) { return duplicationBlock.getHash(); } }
@Override public Block apply(@Nonnull CpdTextBlock duplicationBlock) { Block block = Block.builder() .setResourceId(fileKey) .setBlockHash(new ByteArray(duplicationBlock.getHash())) .setIndexInFile(indexInFile) .setLines(duplicationBlock.getStartLine(), duplicationBlock.getEndLine()) .setUnit(duplicationBlock.getStartTokenIndex(), duplicationBlock.getEndTokenIndex()) .build(); indexInFile++; return block; } }
.setHash(originBlock1.getHash()) .setStartLine(40) .setEndLine(55) .setHash(originBlock2.getHash()) .setStartLine(20) .setEndLine(35) new Block.Builder() .setResourceId(CURRENT_FILE_KEY) .setBlockHash(new ByteArray(originBlock1.getHash())) .setIndexInFile(0) .setLines(originBlock1.getStartLine(), originBlock1.getEndLine()) new Block.Builder() .setResourceId(CURRENT_FILE_KEY) .setBlockHash(new ByteArray(originBlock2.getHash())) .setIndexInFile(1) .setLines(originBlock2.getStartLine(), originBlock2.getEndLine()) new Block.Builder() .setResourceId(otherFile.getDbKey()) .setBlockHash(new ByteArray(originBlock1.getHash())) .setIndexInFile(duplicate1.getIndexInFile()) .setLines(duplicate1.getStartLine(), duplicate1.getEndLine()) new Block.Builder() .setResourceId(otherFile.getDbKey()) .setBlockHash(new ByteArray(originBlock2.getHash()))
@Test public void write_duplication_blocks() { assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isFalse(); ScannerReport.CpdTextBlock duplicationBlock = ScannerReport.CpdTextBlock.newBuilder() .setHash("abcdefghijklmnop") .setStartLine(1) .setEndLine(2) .setStartTokenIndex(10) .setEndTokenIndex(15) .build(); underTest.writeCpdTextBlocks(1, asList(duplicationBlock)); assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, 1); assertThat(file).exists().isFile(); try (CloseableIterator<ScannerReport.CpdTextBlock> duplicationBlocks = Protobuf.readStream(file, ScannerReport.CpdTextBlock.parser())) { ScannerReport.CpdTextBlock duplicationBlockResult = duplicationBlocks.next(); assertThat(duplicationBlockResult.getHash()).isEqualTo("abcdefghijklmnop"); assertThat(duplicationBlockResult.getStartLine()).isEqualTo(1); assertThat(duplicationBlockResult.getEndLine()).isEqualTo(2); assertThat(duplicationBlockResult.getStartTokenIndex()).isEqualTo(10); assertThat(duplicationBlockResult.getEndTokenIndex()).isEqualTo(15); } }
@Test public void persist_many_cpd_text_blocks() { when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true); reportReader.putDuplicationBlocks(FILE_1_REF, Arrays.asList( CPD_TEXT_BLOCK, ScannerReport.CpdTextBlock.newBuilder() .setHash("b1234353e96320ff") .setStartLine(20) .setEndLine(15) .build())); TestComputationStepContext context = new TestComputationStepContext(); underTest.execute(context); List<Map<String, Object>> dtos = dbTester.select("select HASH, START_LINE, END_LINE, INDEX_IN_FILE, COMPONENT_UUID, ANALYSIS_UUID from duplications_index"); assertThat(dtos).extracting("HASH").containsOnly(CPD_TEXT_BLOCK.getHash(), "b1234353e96320ff"); assertThat(dtos).extracting("START_LINE").containsOnly(30L, 20L); assertThat(dtos).extracting("END_LINE").containsOnly(45L, 15L); assertThat(dtos).extracting("INDEX_IN_FILE").containsOnly(0L, 1L); assertThat(dtos).extracting("COMPONENT_UUID").containsOnly(FILE_1.getUuid()); assertThat(dtos).extracting("ANALYSIS_UUID").containsOnly(ANALYSIS_UUID); context.getStatistics().assertValue("inserts", 2); }
private void readFromReport(Component component) { int indexInFile = 0; try (CloseableIterator<ScannerReport.CpdTextBlock> blocks = reportReader.readCpdTextBlocks(component.getReportAttributes().getRef())) { while (blocks.hasNext()) { ScannerReport.CpdTextBlock block = blocks.next(); dbClient.duplicationDao().insert( session, new DuplicationUnitDto() .setHash(block.getHash()) .setStartLine(block.getStartLine()) .setEndLine(block.getEndLine()) .setIndexInFile(indexInFile) .setAnalysisUuid(analysisUuid) .setComponentUuid(component.getUuid())); indexInFile++; } } count += indexInFile; }
@Override public Block apply(@Nonnull CpdTextBlock duplicationBlock) { Block block = Block.builder() .setResourceId(fileKey) .setBlockHash(new ByteArray(duplicationBlock.getHash())) .setIndexInFile(indexInFile) .setLines(duplicationBlock.getStartLine(), duplicationBlock.getEndLine()) .setUnit(duplicationBlock.getStartTokenIndex(), duplicationBlock.getEndTokenIndex()) .build(); indexInFile++; return block; } }