]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5513 Deprecate measure duplication_data and provide alternative to dev cockpit
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 31 Jul 2014 13:47:08 +0000 (15:47 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 31 Jul 2014 13:56:00 +0000 (15:56 +0200)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DefaultCpdEngine.java
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/DefaultCpdEngineTest.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/JavaCpdEngineTest.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/medium/CpdMediumTest.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/CoreMetricsTest.java

index 33558ec898a242089e25bb722a517c7f80e8d40e..887c886ac1e212834a6d68acce20f64ec82e0055 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
 import org.sonar.api.batch.sensor.SensorContext;
 import org.sonar.api.config.Settings;
+import org.sonar.api.measures.FileLinesContextFactory;
 import org.sonar.api.resources.Project;
 import org.sonar.api.utils.SonarException;
 import org.sonar.batch.duplication.BlockCache;
@@ -69,18 +70,21 @@ public class DefaultCpdEngine extends CpdEngine {
   private final Settings settings;
   private final BlockCache duplicationCache;
   private final Project project;
+  private final FileLinesContextFactory contextFactory;
 
-  public DefaultCpdEngine(@Nullable Project project, IndexFactory indexFactory, CpdMappings mappings, FileSystem fs, Settings settings, BlockCache duplicationCache) {
+  public DefaultCpdEngine(@Nullable Project project, IndexFactory indexFactory, CpdMappings mappings, FileSystem fs, Settings settings, BlockCache duplicationCache,
+    FileLinesContextFactory contextFactory) {
     this.project = project;
     this.indexFactory = indexFactory;
     this.mappings = mappings;
     this.fs = fs;
     this.settings = settings;
     this.duplicationCache = duplicationCache;
+    this.contextFactory = contextFactory;
   }
 
-  public DefaultCpdEngine(IndexFactory indexFactory, CpdMappings mappings, FileSystem fs, Settings settings, BlockCache duplicationCache) {
-    this(null, indexFactory, mappings, fs, settings, duplicationCache);
+  public DefaultCpdEngine(IndexFactory indexFactory, CpdMappings mappings, FileSystem fs, Settings settings, BlockCache duplicationCache, FileLinesContextFactory contextFactory) {
+    this(null, indexFactory, mappings, fs, settings, duplicationCache, contextFactory);
   }
 
   @Override
@@ -146,7 +150,7 @@ public class DefaultCpdEngine extends CpdEngine {
           throw new SonarException("Fail during detection of duplication for " + inputFile, e);
         }
 
-        JavaCpdEngine.save(context, inputFile, filtered);
+        JavaCpdEngine.save(context, inputFile, filtered, contextFactory);
       }
     } finally {
       executorService.shutdown();
index 2ea908dbb2764f5029ac6b54bf38bbd26868641f..c74f41bccc9cfa810fdf99f34aefe0b38807baf7 100644 (file)
@@ -34,6 +34,8 @@ import org.sonar.api.batch.sensor.SensorContext;
 import org.sonar.api.batch.sensor.duplication.DuplicationBuilder;
 import org.sonar.api.config.Settings;
 import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.FileLinesContext;
+import org.sonar.api.measures.FileLinesContextFactory;
 import org.sonar.api.resources.Project;
 import org.sonar.api.utils.SonarException;
 import org.sonar.batch.duplication.DefaultDuplicationBuilder;
@@ -83,16 +85,18 @@ public class JavaCpdEngine extends CpdEngine {
   private final FileSystem fs;
   private final Settings settings;
   private final Project project;
+  private final FileLinesContextFactory contextFactory;
 
-  public JavaCpdEngine(@Nullable Project project, IndexFactory indexFactory, FileSystem fs, Settings settings) {
+  public JavaCpdEngine(@Nullable Project project, IndexFactory indexFactory, FileSystem fs, Settings settings, FileLinesContextFactory contextFactory) {
     this.project = project;
     this.indexFactory = indexFactory;
     this.fs = fs;
     this.settings = settings;
+    this.contextFactory = contextFactory;
   }
 
-  public JavaCpdEngine(IndexFactory indexFactory, FileSystem fs, Settings settings) {
-    this(null, indexFactory, fs, settings);
+  public JavaCpdEngine(IndexFactory indexFactory, FileSystem fs, Settings settings, FileLinesContextFactory contextFactory) {
+    this(null, indexFactory, fs, settings, contextFactory);
   }
 
   @Override
@@ -168,7 +172,7 @@ public class JavaCpdEngine extends CpdEngine {
           throw new SonarException("Fail during detection of duplication for " + inputFile, e);
         }
 
-        save(context, inputFile, clones);
+        save(context, inputFile, clones, contextFactory);
       }
     } finally {
       executorService.shutdown();
@@ -189,7 +193,8 @@ public class JavaCpdEngine extends CpdEngine {
     }
   }
 
-  static void save(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, @Nullable Iterable<CloneGroup> duplications) {
+  static void save(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, @Nullable Iterable<CloneGroup> duplications,
+    FileLinesContextFactory contextFactory) {
     if (duplications == null || Iterables.isEmpty(duplications)) {
       return;
     }
@@ -207,6 +212,11 @@ public class JavaCpdEngine extends CpdEngine {
         }
       }
     }
+    FileLinesContext linesContext = contextFactory.createFor(inputFile);
+    for (int i = 1; i <= inputFile.lines(); i++) {
+      linesContext.setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, i, duplicatedLines.contains(i) ? 1 : 0);
+    }
+    linesContext.save();
     // Save
     context.addMeasure(context.<Integer>measureBuilder()
       .forMetric(CoreMetrics.DUPLICATED_FILES)
index 7724fa6f725943fc3cce05edc7e86678e13676cd..a307f859edf30f945529a58a5c3a858d7b23510e 100644 (file)
@@ -41,8 +41,8 @@ public class CpdSensorTest {
   @Before
   public void setUp() {
     IndexFactory indexFactory = mock(IndexFactory.class);
-    sonarEngine = new JavaCpdEngine(indexFactory, null, null);
-    sonarBridgeEngine = new DefaultCpdEngine(indexFactory, new CpdMappings(), null, null, mock(BlockCache.class));
+    sonarEngine = new JavaCpdEngine(indexFactory, null, null, null);
+    sonarBridgeEngine = new DefaultCpdEngine(indexFactory, new CpdMappings(), null, null, mock(BlockCache.class), null);
     settings = new Settings(new PropertyDefinitions(CpdPlugin.class));
 
     DefaultFileSystem fs = new DefaultFileSystem();
index 924aa9193c835f33d520819e7b1af7f7768a2b16..6410d5ddcf0efb851931cefc3966f9ec838be0b4 100644 (file)
@@ -42,7 +42,7 @@ public class DefaultCpdEngineTest {
   @Before
   public void init() {
     settings = new Settings();
-    engine = new DefaultCpdEngine(null, null, null, settings, mock(BlockCache.class));
+    engine = new DefaultCpdEngine(null, null, null, settings, mock(BlockCache.class), null);
   }
 
   @Test
index 770688d24a925a2f9201cd84f69755a95f61bd14..5a0e6f658152973d2df98b7b41a47e3264f4a107 100644 (file)
@@ -30,6 +30,8 @@ import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
 import org.sonar.api.batch.sensor.SensorContext;
 import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder;
 import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.FileLinesContext;
+import org.sonar.api.measures.FileLinesContextFactory;
 import org.sonar.batch.duplication.DefaultDuplicationBuilder;
 import org.sonar.batch.duplication.DuplicationCache;
 import org.sonar.duplications.index.CloneGroup;
@@ -55,6 +57,8 @@ public class JavaCpdEngineTest {
   SensorContext context = mock(SensorContext.class);
   DeprecatedDefaultInputFile inputFile;
   private DefaultDuplicationBuilder duplicationBuilder;
+  private FileLinesContextFactory contextFactory;
+  private FileLinesContext linesContext;
 
   @Before
   public void before() throws IOException {
@@ -64,21 +68,25 @@ public class JavaCpdEngineTest {
     duplicationBuilder = spy(new DefaultDuplicationBuilder(inputFile, duplicationCache));
     when(context.duplicationBuilder(any(InputFile.class))).thenReturn(duplicationBuilder);
     inputFile.setFile(temp.newFile("Foo.java"));
+    contextFactory = mock(FileLinesContextFactory.class);
+    linesContext = mock(FileLinesContext.class);
+    when(contextFactory.createFor(inputFile)).thenReturn(linesContext);
   }
 
   @SuppressWarnings("unchecked")
   @Test
   public void testNothingToSave() {
-    JavaCpdEngine.save(context, inputFile, null);
-    JavaCpdEngine.save(context, inputFile, Collections.EMPTY_LIST);
+    JavaCpdEngine.save(context, inputFile, null, contextFactory);
+    JavaCpdEngine.save(context, inputFile, Collections.EMPTY_LIST, contextFactory);
 
     verifyZeroInteractions(context);
   }
 
   @Test
   public void testOneSimpleDuplicationBetweenTwoFiles() {
+    inputFile.setLines(300);
     List<CloneGroup> groups = Arrays.asList(newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key2", 0, 15, 214)));
-    JavaCpdEngine.save(context, inputFile, groups);
+    JavaCpdEngine.save(context, inputFile, groups, contextFactory);
 
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_FILES).onFile(inputFile).withValue(1).build());
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_BLOCKS).onFile(inputFile).withValue(1).build());
@@ -88,12 +96,18 @@ public class JavaCpdEngineTest {
     inOrder.verify(duplicationBuilder).originBlock(5, 204);
     inOrder.verify(duplicationBuilder).isDuplicatedBy("key2", 15, 214);
     inOrder.verify(duplicationBuilder).done();
+
+    verify(linesContext).setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, 1, 0);
+    verify(linesContext).setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, 4, 0);
+    verify(linesContext).setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, 5, 1);
+    verify(linesContext).setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, 204, 1);
+    verify(linesContext).setIntValue(CoreMetrics.DUPLICATION_LINES_DATA_KEY, 205, 0);
   }
 
   @Test
   public void testDuplicationOnSameFile() throws Exception {
     List<CloneGroup> groups = Arrays.asList(newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key1", 0, 215, 414)));
-    JavaCpdEngine.save(context, inputFile, groups);
+    JavaCpdEngine.save(context, inputFile, groups, contextFactory);
 
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_FILES).onFile(inputFile).withValue(1).build());
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_BLOCKS).onFile(inputFile).withValue(2).build());
@@ -108,7 +122,7 @@ public class JavaCpdEngineTest {
   @Test
   public void testOneDuplicatedGroupInvolvingMoreThanTwoFiles() throws Exception {
     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);
+    JavaCpdEngine.save(context, inputFile, groups, contextFactory);
 
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_FILES).onFile(inputFile).withValue(1).build());
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_BLOCKS).onFile(inputFile).withValue(1).build());
@@ -126,7 +140,7 @@ public class JavaCpdEngineTest {
     List<CloneGroup> groups = Arrays.asList(
       newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key2", 0, 15, 214)),
       newCloneGroup(new ClonePart("key1", 0, 15, 214), new ClonePart("key3", 0, 15, 214)));
-    JavaCpdEngine.save(context, inputFile, groups);
+    JavaCpdEngine.save(context, inputFile, groups, contextFactory);
 
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_FILES).onFile(inputFile).withValue(1).build());
     verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.DUPLICATED_BLOCKS).onFile(inputFile).withValue(2).build());
index 21876b8f3d5e5254abef99546e776d97bdfdecf2..71b5288c6b38ca9fef51d6d882f5b2b1261da931 100644 (file)
@@ -103,7 +103,7 @@ public class CpdMediumTest {
     assertThat(result.inputFiles()).hasSize(2);
 
     // 4 measures per file
-    assertThat(result.measures()).hasSize(6);
+    assertThat(result.measures()).hasSize(8);
 
     InputFile inputFile = result.inputFiles().get(0);
     // One clone group
index dc72077af4145d83e2b989584fb2bcdf1aaeeac1..c88a2c189747f99ead21ec011ce52ec8cbe6a4a5 100644 (file)
@@ -1318,6 +1318,10 @@ public final class CoreMetrics {
     .setOptimizedBestValue(true)
     .create();
 
+  /**
+   * @deprecated since 4.5. Internal storage of duplication is not an API. No more available on batch side.
+   */
+  @Deprecated
   public static final String DUPLICATIONS_DATA_KEY = "duplications_data";
 
   /**
@@ -1336,7 +1340,9 @@ public final class CoreMetrics {
    *   </duplications>
    * </pre>
    * </p>
+   * @deprecated since 4.5. Internal storage of duplication is not an API. No more available on batch side.
    */
+  @Deprecated
   public static final Metric<String> DUPLICATIONS_DATA = new Metric.Builder(DUPLICATIONS_DATA_KEY, "Duplications details", Metric.ValueType.DATA)
     .setDescription("Duplications details")
     .setDirection(Metric.DIRECTION_NONE)
@@ -1345,6 +1351,25 @@ public final class CoreMetrics {
     .setDeleteHistoricalData(true)
     .create();
 
+  /**
+   * @since 4.5 used by dev cockpit.
+   */
+  @Beta
+  public static final String DUPLICATION_LINES_DATA_KEY = "comment_lines_data";
+
+  /**
+   * Information about duplication in file.
+   * Key-value pairs, where key - is a number of line, and value - is an indicator of whether line is duplicated somewhere (1) or not (0).
+   *
+   * @see org.sonar.api.measures.FileLinesContext
+   * @since 4.5 used by dev cockpit
+   */
+  @Beta
+  public static final Metric<String> DUPLICATION_LINES_DATA = new Metric.Builder(DUPLICATION_LINES_DATA_KEY, "duplication_lines_data", Metric.ValueType.DATA)
+    .setHidden(true)
+    .setDomain(DOMAIN_DUPLICATION)
+    .create();
+
   // --------------------------------------------------------------------------------------------------------------------
   //
   // CODING RULES
index 6d4156dc66861ca969005ecb0422b525559e4f65..204fcfc71e68e07bb8681661c1811e3aab00026b 100644 (file)
@@ -32,7 +32,7 @@ public class CoreMetricsTest {
   @Test
   public void read_metrics_from_class_reflection() {
     List<Metric> metrics = CoreMetrics.getMetrics();
-    assertThat(metrics).hasSize(153);
+    assertThat(metrics).hasSize(154);
     assertThat(metrics).contains(CoreMetrics.NCLOC, CoreMetrics.DIRECTORIES);
   }