import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
-import java.util.HashSet;
import java.util.List;
-import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.duplication.NewDuplication;
import org.sonar.api.batch.sensor.duplication.internal.DefaultDuplication;
-import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.config.Settings;
-import org.sonar.api.measures.CoreMetrics;
import org.sonar.batch.cpd.index.SonarDuplicationsIndex;
import org.sonar.batch.index.BatchComponentCache;
import org.sonar.batch.report.ReportPublisher;
if (duplications == null || Iterables.isEmpty(duplications)) {
return;
}
- computeBlockAndLineCount(context, inputFile, duplications);
saveDuplications(context, inputFile, duplications);
}
- private static void saveMeasures(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, int duplicatedLines, int duplicatedBlocks) {
- // Save
- ((DefaultMeasure<Integer>) context.<Integer>newMeasure()
- .forMetric(CoreMetrics.DUPLICATED_FILES)
- .on(inputFile)
- .withValue(1))
- .setFromCore()
- .save();
- ((DefaultMeasure<Integer>) context.<Integer>newMeasure()
- .forMetric(CoreMetrics.DUPLICATED_LINES)
- .on(inputFile)
- .withValue(duplicatedLines))
- .setFromCore()
- .save();
- ((DefaultMeasure<Integer>) context.<Integer>newMeasure()
- .forMetric(CoreMetrics.DUPLICATED_BLOCKS)
- .on(inputFile)
- .withValue(duplicatedBlocks))
- .setFromCore()
- .save();
- }
-
private static void saveDuplications(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, Iterable<CloneGroup> duplications) {
int cloneGroupCount = 0;
for (CloneGroup duplication : duplications) {
}
}
- private static void computeBlockAndLineCount(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, Iterable<CloneGroup> duplications) {
- int duplicatedBlocks = 0;
- Set<Integer> duplicatedLines = new HashSet<>();
- for (CloneGroup clone : duplications) {
- ClonePart origin = clone.getOriginPart();
- for (ClonePart part : clone.getCloneParts()) {
- if (part.getResourceId().equals(origin.getResourceId())) {
- duplicatedBlocks++;
- for (int duplicatedLine = part.getStartLine(); duplicatedLine < part.getStartLine() + part.getLines(); duplicatedLine++) {
- duplicatedLines.add(duplicatedLine);
- }
- }
- }
- }
- saveMeasures(context, inputFile, duplicatedLines.size(), duplicatedBlocks);
- }
-
}
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
-import org.sonar.api.measures.CoreMetrics;
import org.sonar.duplications.index.CloneGroup;
import org.sonar.duplications.index.ClonePart;
List<CloneGroup> groups = Arrays.asList(newCloneGroup(new ClonePart("key1", 0, 2, 4), new ClonePart("key2", 0, 15, 17)));
JavaCpdEngine.save(context, inputFile, groups);
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_FILES).on(inputFile).withValue(1));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_BLOCKS).on(inputFile).withValue(1));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_LINES).on(inputFile).withValue(3));
-
verify(storage).store(new DefaultDuplication()
.originBlock(inputFile, 2, 4)
.isDuplicatedBy("key2", 15, 17));
List<CloneGroup> groups = Arrays.asList(newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key1", 0, 215, 414)));
JavaCpdEngine.save(context, inputFile, groups);
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_FILES).on(inputFile).withValue(1));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_BLOCKS).on(inputFile).withValue(2));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_LINES).on(inputFile).withValue(400));
-
verify(storage).store(new DefaultDuplication()
.originBlock(inputFile, 5, 204)
.isDuplicatedBy("key1", 215, 414));
List<CloneGroup> groups = Arrays.asList(newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key2", 0, 15, 214), new ClonePart("key3", 0, 25, 224)));
JavaCpdEngine.save(context, inputFile, groups);
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_FILES).on(inputFile).withValue(1));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_BLOCKS).on(inputFile).withValue(1));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_LINES).on(inputFile).withValue(200));
-
verify(storage).store(new DefaultDuplication()
.originBlock(inputFile, 5, 204)
.isDuplicatedBy("key2", 15, 214)
newCloneGroup(new ClonePart("key1", 0, 15, 214), new ClonePart("key3", 0, 15, 214)));
JavaCpdEngine.save(context, inputFile, groups);
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_FILES).on(inputFile).withValue(1));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_BLOCKS).on(inputFile).withValue(2));
- verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.DUPLICATED_LINES).on(inputFile).withValue(210));
-
verify(storage).store(new DefaultDuplication()
.originBlock(inputFile, 5, 204)
.isDuplicatedBy("key2", 15, 214));
import org.sonar.xoo.XooPlugin;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.tuple;
public class CpdMediumTest {
assertThat(result.inputFiles()).hasSize(2);
Map<String, List<org.sonar.batch.protocol.output.BatchReport.Measure>> allMeasures = result.allMeasures();
- assertThat(allMeasures.get("com.foo.project:src/sample1.xoo")).extracting("metricKey", "intValue")
- .contains(tuple("duplicated_blocks", 1),
- tuple("duplicated_files", 1),
- tuple("duplicated_lines", 17));
InputFile inputFile1 = result.inputFile("src/sample1.xoo");
InputFile inputFile2 = result.inputFile("src/sample2.xoo");
"[{\"key\":\"Sonar Way\",\"language\":\"xoo\",\"name\":\"Sonar Way\",\"rulesUpdatedAt\":\"2009-02-13T23:31:31+0000\"}]"));
assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue").containsOnly(
- Tuple.tuple(CoreMetrics.LINES_KEY, blockCount * 2 + 1),
- Tuple.tuple(CoreMetrics.DUPLICATED_FILES_KEY, 1),
- Tuple.tuple(CoreMetrics.DUPLICATED_BLOCKS_KEY, blockCount),
- Tuple.tuple(CoreMetrics.DUPLICATED_LINES_KEY, blockCount));
+ Tuple.tuple(CoreMetrics.LINES_KEY, blockCount * 2 + 1));
List<org.sonar.batch.protocol.output.BatchReport.Duplication> duplicationGroups = result.duplicationsFor(result.inputFile("src/sample.xoo"));
assertThat(duplicationGroups).hasSize(1);
.build())
.start();
- Map<String, List<org.sonar.batch.protocol.output.BatchReport.Measure>> allMeasures = result.allMeasures();
- assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue")
- .contains(tuple("duplicated_blocks", 2),
- tuple("duplicated_files", 1),
- tuple("duplicated_lines", 4));
-
InputFile inputFile = result.inputFile("src/sample.xoo");
// One clone group
List<org.sonar.batch.protocol.output.BatchReport.Duplication> duplicationGroups = result.duplicationsFor(inputFile);