Skip to content

Commit

Permalink
SONAR-6993 Rename DuplicationBlock into CpdTextBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Nov 12, 2015
1 parent cefeabc commit dd16b88
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 44 deletions.
Expand Up @@ -42,7 +42,7 @@ public interface BatchReportReader {


CloseableIterator<BatchReport.Duplication> readComponentDuplications(int componentRef); CloseableIterator<BatchReport.Duplication> readComponentDuplications(int componentRef);


CloseableIterator<BatchReport.DuplicationBlock> readComponentDuplicationBlocks(int componentRef); CloseableIterator<BatchReport.CpdTextBlock> readCpdTextBlocks(int componentRef);


CloseableIterator<BatchReport.Symbol> readComponentSymbols(int componentRef); CloseableIterator<BatchReport.Symbol> readComponentSymbols(int componentRef);


Expand Down
Expand Up @@ -100,8 +100,8 @@ public CloseableIterator<BatchReport.Duplication> readComponentDuplications(int
} }


@Override @Override
public CloseableIterator<BatchReport.DuplicationBlock> readComponentDuplicationBlocks(int componentRef) { public CloseableIterator<BatchReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
return delegate.readComponentDuplicationBlocks(componentRef); return delegate.readCpdTextBlocks(componentRef);
} }


@Override @Override
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class BatchReportReaderImplTest {
private static final BatchReport.Component COMPONENT = BatchReport.Component.newBuilder().setRef(COMPONENT_REF).build(); private static final BatchReport.Component COMPONENT = BatchReport.Component.newBuilder().setRef(COMPONENT_REF).build();
private static final BatchReport.Issue ISSUE = BatchReport.Issue.newBuilder().build(); private static final BatchReport.Issue ISSUE = BatchReport.Issue.newBuilder().build();
private static final BatchReport.Duplication DUPLICATION = BatchReport.Duplication.newBuilder().build(); private static final BatchReport.Duplication DUPLICATION = BatchReport.Duplication.newBuilder().build();
private static final BatchReport.DuplicationBlock DUPLICATION_BLOCK = BatchReport.DuplicationBlock.newBuilder().build(); private static final BatchReport.CpdTextBlock DUPLICATION_BLOCK = BatchReport.CpdTextBlock.newBuilder().build();
private static final BatchReport.Symbol SYMBOL = BatchReport.Symbol.newBuilder().build(); private static final BatchReport.Symbol SYMBOL = BatchReport.Symbol.newBuilder().build();
private static final BatchReport.SyntaxHighlighting SYNTAX_HIGHLIGHTING_1 = BatchReport.SyntaxHighlighting.newBuilder().build(); private static final BatchReport.SyntaxHighlighting SYNTAX_HIGHLIGHTING_1 = BatchReport.SyntaxHighlighting.newBuilder().build();
private static final BatchReport.SyntaxHighlighting SYNTAX_HIGHLIGHTING_2 = BatchReport.SyntaxHighlighting.newBuilder().build(); private static final BatchReport.SyntaxHighlighting SYNTAX_HIGHLIGHTING_2 = BatchReport.SyntaxHighlighting.newBuilder().build();
Expand Down Expand Up @@ -203,24 +203,24 @@ public void readComponentDuplications_it_not_cached() {


@Test @Test
public void readComponentDuplicationBlocks_returns_empty_list_if_file_does_not_exist() { public void readComponentDuplicationBlocks_returns_empty_list_if_file_does_not_exist() {
assertThat(underTest.readComponentDuplicationBlocks(COMPONENT_REF)).isEmpty(); assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isEmpty();
} }


@Test @Test
public void verify_readComponentDuplicationBlocks_returns_Issues() { public void verify_readComponentDuplicationBlocks_returns_Issues() {
writer.writeDuplicationBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK)); writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));


try (CloseableIterator<BatchReport.DuplicationBlock> res = underTest.readComponentDuplicationBlocks(COMPONENT_REF)) { try (CloseableIterator<BatchReport.CpdTextBlock> res = underTest.readCpdTextBlocks(COMPONENT_REF)) {
assertThat(res.next()).isEqualTo(DUPLICATION_BLOCK); assertThat(res.next()).isEqualTo(DUPLICATION_BLOCK);
assertThat(res.hasNext()).isFalse(); assertThat(res.hasNext()).isFalse();
} }
} }


@Test @Test
public void readComponentDuplicationBlocks_is_not_cached() { public void readComponentDuplicationBlocks_is_not_cached() {
writer.writeDuplicationBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK)); writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));


assertThat(underTest.readComponentDuplicationBlocks(COMPONENT_REF)).isNotSameAs(underTest.readComponentDuplicationBlocks(COMPONENT_REF)); assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isNotSameAs(underTest.readCpdTextBlocks(COMPONENT_REF));
} }


@Test @Test
Expand Down
Expand Up @@ -43,7 +43,7 @@ public class BatchReportReaderRule implements TestRule, BatchReportReader {
private Map<Integer, BatchReport.Component> components = new HashMap<>(); private Map<Integer, BatchReport.Component> components = new HashMap<>();
private Map<Integer, List<BatchReport.Issue>> issues = new HashMap<>(); private Map<Integer, List<BatchReport.Issue>> issues = new HashMap<>();
private Map<Integer, List<BatchReport.Duplication>> duplications = new HashMap<>(); private Map<Integer, List<BatchReport.Duplication>> duplications = new HashMap<>();
private Map<Integer, List<BatchReport.DuplicationBlock>> duplicationBlocks = new HashMap<>(); private Map<Integer, List<BatchReport.CpdTextBlock>> duplicationBlocks = new HashMap<>();
private Map<Integer, List<BatchReport.Symbol>> symbols = new HashMap<>(); private Map<Integer, List<BatchReport.Symbol>> symbols = new HashMap<>();
private Map<Integer, List<BatchReport.SyntaxHighlighting>> syntaxHighlightings = new HashMap<>(); private Map<Integer, List<BatchReport.SyntaxHighlighting>> syntaxHighlightings = new HashMap<>();
private Map<Integer, List<BatchReport.Coverage>> coverages = new HashMap<>(); private Map<Integer, List<BatchReport.Coverage>> coverages = new HashMap<>();
Expand Down Expand Up @@ -177,11 +177,11 @@ public BatchReportReaderRule putDuplications(int componentRef, BatchReport.Dupli
} }


@Override @Override
public CloseableIterator<BatchReport.DuplicationBlock> readComponentDuplicationBlocks(int componentRef) { public CloseableIterator<BatchReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
return closeableIterator(this.duplicationBlocks.get(componentRef)); return closeableIterator(this.duplicationBlocks.get(componentRef));
} }


public BatchReportReaderRule putDuplicationBlocks(int componentRef, List<BatchReport.DuplicationBlock> duplicationBlocks) { public BatchReportReaderRule putDuplicationBlocks(int componentRef, List<BatchReport.CpdTextBlock> duplicationBlocks) {
this.duplicationBlocks.put(componentRef, duplicationBlocks); this.duplicationBlocks.put(componentRef, duplicationBlocks);
return this; return this;
} }
Expand Down
Expand Up @@ -91,10 +91,10 @@ public CloseableIterator<BatchReport.Duplication> readComponentDuplications(int
return emptyCloseableIterator(); return emptyCloseableIterator();
} }


public CloseableIterator<BatchReport.DuplicationBlock> readComponentDuplicationBlocks(int componentRef) { public CloseableIterator<BatchReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATION_BLOCKS, componentRef); File file = fileStructure.fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, componentRef);
if (fileExists(file)) { if (fileExists(file)) {
return Protobuf.readStream(file, BatchReport.DuplicationBlock.parser()); return Protobuf.readStream(file, BatchReport.CpdTextBlock.parser());
} }
return emptyCloseableIterator(); return emptyCloseableIterator();
} }
Expand Down
Expand Up @@ -98,9 +98,9 @@ public File writeComponentDuplications(int componentRef, Iterable<BatchReport.Du
return file; return file;
} }


public File writeDuplicationBlocks(int componentRef, Iterable<BatchReport.DuplicationBlock> duplicationBlocks) { public File writeCpdTextBlocks(int componentRef, Iterable<BatchReport.CpdTextBlock> blocks) {
File file = fileStructure.fileFor(FileStructure.Domain.DUPLICATION_BLOCKS, componentRef); File file = fileStructure.fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, componentRef);
Protobuf.writeStream(duplicationBlocks, file, false); Protobuf.writeStream(blocks, file, false);
return file; return file;
} }


Expand Down
Expand Up @@ -31,7 +31,7 @@ public enum Domain {
COMPONENT("component-", Domain.PB), COMPONENT("component-", Domain.PB),
MEASURES("measures-", Domain.PB), MEASURES("measures-", Domain.PB),
DUPLICATIONS("duplications-", Domain.PB), DUPLICATIONS("duplications-", Domain.PB),
DUPLICATION_BLOCKS("duplication-blocks-", Domain.PB), CPD_TEXT_BLOCKS("cpd-text-block-", Domain.PB),
SYNTAX_HIGHLIGHTINGS("syntax-highlightings-", Domain.PB), SYNTAX_HIGHLIGHTINGS("syntax-highlightings-", Domain.PB),
CHANGESETS("changesets-", Domain.PB), CHANGESETS("changesets-", Domain.PB),
SYMBOLS("symbols-", Domain.PB), SYMBOLS("symbols-", Domain.PB),
Expand Down
2 changes: 1 addition & 1 deletion sonar-batch-protocol/src/main/protobuf/batch_report.proto
Expand Up @@ -143,7 +143,7 @@ message Duplication {
} }


// Used for cross project duplication // Used for cross project duplication
message DuplicationBlock { message CpdTextBlock {
repeated int32 hash = 1 [packed = true]; repeated int32 hash = 1 [packed = true];
optional int32 start_line = 2; optional int32 start_line = 2;
optional int32 end_line = 3; optional int32 end_line = 3;
Expand Down
Expand Up @@ -180,17 +180,17 @@ public void read_duplication_blocks() {
writer.writeComponent(BatchReport.Component.newBuilder() writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(1).build()); .setRef(1).build());


BatchReport.DuplicationBlock duplicationBlock = BatchReport.DuplicationBlock.newBuilder() BatchReport.CpdTextBlock duplicationBlock = BatchReport.CpdTextBlock.newBuilder()
.addAllHash(asList(1, 2, 3, 5, 7)) .addAllHash(asList(1, 2, 3, 5, 7))
.setStartLine(1) .setStartLine(1)
.setEndLine(2) .setEndLine(2)
.setStartTokenIndex(10) .setStartTokenIndex(10)
.setEndTokenIndex(15) .setEndTokenIndex(15)
.build(); .build();
writer.writeDuplicationBlocks(1, singletonList(duplicationBlock)); writer.writeCpdTextBlocks(1, singletonList(duplicationBlock));


BatchReportReader sut = new BatchReportReader(dir); BatchReportReader sut = new BatchReportReader(dir);
assertThat(sut.readComponentDuplicationBlocks(1)).hasSize(1); assertThat(sut.readCpdTextBlocks(1)).hasSize(1);
} }


@Test @Test
Expand Down
Expand Up @@ -191,22 +191,22 @@ public void write_duplications() {


@Test @Test
public void write_duplication_blocks() { public void write_duplication_blocks() {
assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATION_BLOCKS, 1)).isFalse(); assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isFalse();


BatchReport.DuplicationBlock duplicationBlock = BatchReport.DuplicationBlock.newBuilder() BatchReport.CpdTextBlock duplicationBlock = BatchReport.CpdTextBlock.newBuilder()
.addAllHash(asList(1, 2, 3, 5, 7)) .addAllHash(asList(1, 2, 3, 5, 7))
.setStartLine(1) .setStartLine(1)
.setEndLine(2) .setEndLine(2)
.setStartTokenIndex(10) .setStartTokenIndex(10)
.setEndTokenIndex(15) .setEndTokenIndex(15)
.build(); .build();
underTest.writeDuplicationBlocks(1, asList(duplicationBlock)); underTest.writeCpdTextBlocks(1, asList(duplicationBlock));


assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATION_BLOCKS, 1)).isTrue(); assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isTrue();
File file = underTest.getFileStructure().fileFor(FileStructure.Domain.DUPLICATION_BLOCKS, 1); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, 1);
assertThat(file).exists().isFile(); assertThat(file).exists().isFile();
try (CloseableIterator<BatchReport.DuplicationBlock> duplicationBlocks = Protobuf.readStream(file, BatchReport.DuplicationBlock.parser())) { try (CloseableIterator<BatchReport.CpdTextBlock> duplicationBlocks = Protobuf.readStream(file, BatchReport.CpdTextBlock.parser())) {
BatchReport.DuplicationBlock duplicationBlockResult = duplicationBlocks.next(); BatchReport.CpdTextBlock duplicationBlockResult = duplicationBlocks.next();
assertThat(duplicationBlockResult.getHashList()).containsOnly(1, 2, 3, 5, 7); assertThat(duplicationBlockResult.getHashList()).containsOnly(1, 2, 3, 5, 7);
assertThat(duplicationBlockResult.getStartLine()).isEqualTo(1); assertThat(duplicationBlockResult.getStartLine()).isEqualTo(1);
assertThat(duplicationBlockResult.getEndLine()).isEqualTo(2); assertThat(duplicationBlockResult.getEndLine()).isEqualTo(2);
Expand Down
Expand Up @@ -28,7 +28,6 @@
import org.sonar.api.config.Settings; import org.sonar.api.config.Settings;
import org.sonar.batch.index.BatchComponentCache; import org.sonar.batch.index.BatchComponentCache;
import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.DuplicationBlock;
import org.sonar.batch.report.ReportPublisher; import org.sonar.batch.report.ReportPublisher;
import org.sonar.duplications.block.Block; import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray; import org.sonar.duplications.block.ByteArray;
Expand All @@ -52,10 +51,10 @@ public SonarDuplicationsIndex(ReportPublisher publisher, BatchComponentCache bat
public void insert(InputFile inputFile, Collection<Block> blocks) { public void insert(InputFile inputFile, Collection<Block> blocks) {
if (isCrossProjectDuplicationEnabled(settings)) { if (isCrossProjectDuplicationEnabled(settings)) {
int id = batchComponentCache.get(inputFile).batchId(); int id = batchComponentCache.get(inputFile).batchId();
final BatchReport.DuplicationBlock.Builder builder = BatchReport.DuplicationBlock.newBuilder(); final BatchReport.CpdTextBlock.Builder builder = BatchReport.CpdTextBlock.newBuilder();
publisher.getWriter().writeDuplicationBlocks(id, Iterables.transform(blocks, new Function<Block, BatchReport.DuplicationBlock>() { publisher.getWriter().writeCpdTextBlocks(id, Iterables.transform(blocks, new Function<Block, BatchReport.CpdTextBlock>() {
@Override @Override
public DuplicationBlock apply(Block input) { public BatchReport.CpdTextBlock apply(Block input) {
builder.clear(); builder.clear();
builder.setStartLine(input.getStartLine()); builder.setStartLine(input.getStartLine());
builder.setEndLine(input.getEndLine()); builder.setEndLine(input.getEndLine());
Expand Down
Expand Up @@ -19,22 +19,17 @@
*/ */
package org.sonar.batch.mediumtest; package org.sonar.batch.mediumtest;


import org.sonar.batch.issue.tracking.TrackedIssue;

import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;

import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;

import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;

import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
Expand All @@ -48,6 +43,7 @@
import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.highlighting.TypeOfText; import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.batch.issue.IssueCache; import org.sonar.batch.issue.IssueCache;
import org.sonar.batch.issue.tracking.TrackedIssue;
import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.Component; import org.sonar.batch.protocol.output.BatchReport.Component;
import org.sonar.batch.protocol.output.BatchReport.Metadata; import org.sonar.batch.protocol.output.BatchReport.Metadata;
Expand Down Expand Up @@ -231,10 +227,10 @@ public List<BatchReport.Duplication> duplicationsFor(InputFile file) {
return result; return result;
} }


public List<BatchReport.DuplicationBlock> duplicationBlocksFor(InputFile file) { public List<BatchReport.CpdTextBlock> duplicationBlocksFor(InputFile file) {
List<BatchReport.DuplicationBlock> result = new ArrayList<>(); List<BatchReport.CpdTextBlock> result = new ArrayList<>();
int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef();
try (CloseableIterator<BatchReport.DuplicationBlock> it = getReportReader().readComponentDuplicationBlocks(ref)) { try (CloseableIterator<BatchReport.CpdTextBlock> it = getReportReader().readCpdTextBlocks(ref)) {
while (it.hasNext()) { while (it.hasNext()) {
result.add(it.next()); result.add(it.next());
} }
Expand Down
Expand Up @@ -37,7 +37,7 @@
import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.CoreMetrics;
import org.sonar.batch.mediumtest.BatchMediumTester; import org.sonar.batch.mediumtest.BatchMediumTester;
import org.sonar.batch.mediumtest.TaskResult; import org.sonar.batch.mediumtest.TaskResult;
import org.sonar.batch.protocol.output.BatchReport.DuplicationBlock; import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.Measure; import org.sonar.batch.protocol.output.BatchReport.Measure;
import org.sonar.xoo.XooPlugin; import org.sonar.xoo.XooPlugin;


Expand Down Expand Up @@ -152,7 +152,7 @@ public void enableCrossProjectDuplication() throws IOException {


InputFile inputFile1 = result.inputFile("src/sample1.xoo"); InputFile inputFile1 = result.inputFile("src/sample1.xoo");


List<DuplicationBlock> duplicationBlocks = result.duplicationBlocksFor(inputFile1); List<BatchReport.CpdTextBlock> duplicationBlocks = result.duplicationBlocksFor(inputFile1);
assertThat(duplicationBlocks).hasSize(3); assertThat(duplicationBlocks).hasSize(3);
assertThat(duplicationBlocks.get(0).getStartLine()).isEqualTo(1); assertThat(duplicationBlocks.get(0).getStartLine()).isEqualTo(1);
assertThat(duplicationBlocks.get(0).getEndLine()).isEqualTo(5); assertThat(duplicationBlocks.get(0).getEndLine()).isEqualTo(5);
Expand Down

0 comments on commit dd16b88

Please sign in to comment.