]> source.dussan.org Git - sonarqube.git/commitdiff
fix SONAR-2267 : The "duplicated blocks" measure on each file is not correct when...
authorFreddy Mallet <freddy.mallet@gmail.com>
Fri, 11 Mar 2011 11:20:15 +0000 (12:20 +0100)
committerFreddy Mallet <freddy.mallet@gmail.com>
Fri, 11 Mar 2011 11:26:02 +0000 (12:26 +0100)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java

index aef9524bd938eddb82d70ad0760d836e537b1927..a185eed64affb9e31a764e0273a6429aac2dd1a5 100644 (file)
@@ -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()));
     }
 
index a31b826a483933a456d8d3e53a4f593e675ef2eb..827c266119a2d2471a96281ace4dac20438e9571 100644 (file)
@@ -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>"