From e04578cf0d52ba18f2902ad353443e6035d388fd Mon Sep 17 00:00:00 2001 From: Freddy Mallet Date: Fri, 11 Mar 2011 12:20:15 +0100 Subject: [PATCH] fix SONAR-2267 : The "duplicated blocks" measure on each file is not correct when a duplication involved more than two blocks --- .../org/sonar/plugins/cpd/CpdAnalyser.java | 29 ++++++++++--------- .../sonar/plugins/cpd/CpdAnalyserTest.java | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java index aef9524bd93..a185eed64af 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java @@ -69,6 +69,9 @@ public class CpdAnalyser { continue; } + DuplicationsData firstFileData = getDuplicationsData(duplicationsData, firstFile); + firstFileData.incrementDuplicatedBlock(); + for (TokenEntry tokenEntry : match.getMarkSet()) { String secondAbsolutePath = tokenEntry.getTokenSrcID(); int secondLine = tokenEntry.getBeginLine(); @@ -81,7 +84,7 @@ public class CpdAnalyser { continue; } - processClassMeasure(duplicationsData, firstFile, firstLine, secondFile, secondLine, match.getLineCount(), match); + firstFileData.cumulate(secondFile, secondLine, firstLine, match.getLineCount(), match); } } } @@ -91,22 +94,19 @@ public class CpdAnalyser { } } - private void processClassMeasure(Map fileContainer, Resource file, int duplicationStartLine, - Resource targetFile, int targetDuplicationStartLine, int duplicatedLines, Match match) { - if (file != null && targetFile != null) { - DuplicationsData data = fileContainer.get(file); - if (data == null) { - data = new DuplicationsData(file, context); - fileContainer.put(file, data); - } - data.cumulate(targetFile, targetDuplicationStartLine, duplicationStartLine, duplicatedLines, match); + private DuplicationsData getDuplicationsData(Map fileContainer, Resource file) { + DuplicationsData data = fileContainer.get(file); + if (data == null) { + data = new DuplicationsData(file, context); + fileContainer.put(file, data); } + return data; } private static final class DuplicationsData { protected Set duplicatedLines = new HashSet(); - protected Set duplicatedBlocks = new HashSet(); + protected double duplicatedBlocks = 0; protected Resource resource; private SensorContext context; private List duplicationXMLEntries = new ArrayList(); @@ -129,13 +129,16 @@ public class CpdAnalyser { for (int duplicatedLine = duplicationStartLine; duplicatedLine < duplicationStartLine + duplicatedLines; duplicatedLine++) { this.duplicatedLines.add(duplicatedLine); } - duplicatedBlocks.add(match); + } + + protected void incrementDuplicatedBlock() { + duplicatedBlocks++; } protected void saveUsing(SensorContext context) { context.saveMeasure(resource, CoreMetrics.DUPLICATED_FILES, 1d); context.saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, (double) duplicatedLines.size()); - context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, (double) duplicatedBlocks.size()); + context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, duplicatedBlocks); context.saveMeasure(resource, new Measure(CoreMetrics.DUPLICATIONS_DATA, getDuplicationXMLData())); } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java index a31b826a483..827c266119a 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java @@ -262,7 +262,7 @@ public class CpdAnalyserTest { verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_FILES, 1d); verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_LINES, 400d); - verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_BLOCKS, 1d); + verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_BLOCKS, 2d); verify(context).saveMeasure( eq(resource1), argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "" -- 2.39.5