diff options
author | Freddy Mallet <freddy.mallet@gmail.com> | 2011-03-11 12:20:15 +0100 |
---|---|---|
committer | Freddy Mallet <freddy.mallet@gmail.com> | 2011-03-11 12:26:02 +0100 |
commit | e04578cf0d52ba18f2902ad353443e6035d388fd (patch) | |
tree | 77fb79d1947699bb6359f8b46c1816763e5bbb56 /plugins | |
parent | 26803e55544df9e5c40998224c3b9f55247bc0d8 (diff) | |
download | sonarqube-e04578cf0d52ba18f2902ad353443e6035d388fd.tar.gz sonarqube-e04578cf0d52ba18f2902ad353443e6035d388fd.zip |
fix SONAR-2267 : The "duplicated blocks" measure on each file is not correct when a duplication involved more than two blocks
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java | 29 | ||||
-rw-r--r-- | plugins/sonar-cpd-plugin/src/test/java/org/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<Resource, DuplicationsData> 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<Resource, DuplicationsData> 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<Integer> duplicatedLines = new HashSet<Integer>(); - protected Set<Match> duplicatedBlocks = new HashSet<Match>(); + protected double duplicatedBlocks = 0; protected Resource resource; private SensorContext context; private List<StringBuilder> duplicationXMLEntries = new ArrayList<StringBuilder>(); @@ -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, "<duplications>" |