]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5871 Add duplication data to lines WS
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 1 Dec 2014 16:48:01 +0000 (17:48 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 1 Dec 2014 17:08:49 +0000 (18:08 +0100)
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineDoc.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/source/index/SourceLineResultSetIterator.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineResultSetIteratorTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/source/index/SourceLineIndexerTest/db.xml
server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json

index e845c1926fc3191f202d588b5ebb6ec534903b04..f7235215a53a543292365e643321d661dbce59fc 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.source.index;
 
+import com.google.common.collect.ImmutableList;
 import org.sonar.server.search.BaseDoc;
 import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexUtils;
@@ -26,6 +27,7 @@ import org.sonar.server.search.IndexUtils;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
+import java.util.Collection;
 import java.util.Date;
 import java.util.Map;
 
@@ -214,4 +216,12 @@ public class SourceLineDoc extends BaseDoc {
     setField(SourceLineIndexDefinition.FIELD_SYMBOLS, s);
   }
 
+  public Collection<Integer> duplications() {
+    Collection<Integer> duplications = getNullableField(SourceLineIndexDefinition.FIELD_DUPLICATIONS);
+    return duplications == null ? ImmutableList.<Integer>of() : duplications;
+  }
+
+  public void setDuplications(@Nullable Collection<Integer> dups) {
+    setField(SourceLineIndexDefinition.FIELD_DUPLICATIONS, dups == null ? ImmutableList.<Integer>of() : dups);
+  }
 }
index ce10ac551bc5a16dbd48e7ff89bcd09dda15aef7..7bc5a70e887fa6344bb1d4252175ea38fd5524fc 100644 (file)
@@ -46,6 +46,7 @@ public class SourceLineIndexDefinition implements IndexDefinition {
   public static final String FIELD_OVERALL_CONDITIONS = "overallConditions";
   public static final String FIELD_OVERALL_COVERED_CONDITIONS = "overallCoveredConditions";
   public static final String FIELD_SYMBOLS = "symbols";
+  public static final String FIELD_DUPLICATIONS = "duplications";
 
   public static final String INDEX = "sourcelines";
 
@@ -90,6 +91,7 @@ public class SourceLineIndexDefinition implements IndexDefinition {
     sourceLineMapping.createShortField(FIELD_OVERALL_CONDITIONS);
     sourceLineMapping.createShortField(FIELD_OVERALL_COVERED_CONDITIONS);
     sourceLineMapping.stringFieldBuilder(FIELD_SYMBOLS).build();
+    sourceLineMapping.createShortField(FIELD_DUPLICATIONS);
     sourceLineMapping.createDateTimeField(BaseNormalizer.UPDATED_AT_FIELD);
   }
 }
index f2cd6030096da75d80e4417b68e116ea77e67bdc..ac0c80311a4b3278ea4a994c6e695b0397e47cff 100644 (file)
@@ -32,6 +32,8 @@ import org.sonar.server.db.DbClient;
 import org.sonar.server.db.ResultSetIterator;
 import org.sonar.server.db.migrations.SqlUtil;
 
+import javax.annotation.Nullable;
+
 import java.io.IOException;
 import java.io.StringReader;
 import java.sql.Connection;
@@ -146,6 +148,7 @@ public class SourceLineResultSetIterator extends ResultSetIterator<SourceLineRes
           doc.setOverallCoveredConditions(parseIntegerFromRecord(csvRecord, 11));
           doc.setHighlighting(csvRecord.get(12));
           doc.setSymbols(csvRecord.get(13));
+          doc.setDuplications(parseDuplications(csvRecord.get(14)));
           doc.setSource(csvRecord.get(csvRecord.size() - 1));
 
           result.addLine(doc);
@@ -165,6 +168,17 @@ public class SourceLineResultSetIterator extends ResultSetIterator<SourceLineRes
     return result;
   }
 
+  private List<Integer> parseDuplications(@Nullable String duplications) {
+    List<Integer> dups = Lists.newArrayList();
+    if (duplications != null && !duplications.isEmpty()) {
+      String[] dupsStrings = duplications.split(",");
+      for (String dup: dupsStrings) {
+        dups.add(NumberUtils.toInt(dup, -1));
+      }
+    }
+    return dups;
+  }
+
   private Integer parseIntegerFromRecord(CSVRecord record, int column) {
     String cellValue = record.get(column);
     if (cellValue == null || cellValue.isEmpty()) {
index 1b7e12ed408b5d4abc31b700f1bf57bf0f7576ce..3d7808019769449affe2ad1f79038e6d9bf1bbe2 100644 (file)
@@ -108,8 +108,11 @@ public class LinesAction implements RequestHandler {
       json.prop("scmDate", scmDate == null ? null : DateUtils.formatDateTime(scmDate));
       json.prop("lineHits", line.overallLineHits())
         .prop("conditions", line.overallConditions())
-        .prop("coveredConditions", line.overallCoveredConditions())
-        .endObject();
+        .prop("coveredConditions", line.overallCoveredConditions());
+      if (! line.duplications().isEmpty()) {
+        json.prop("duplicated", true);
+      }
+      json.endObject();
     }
     json.endArray();
   }
index b4c16ec448c3a772462beca1d1a606daf5b9d839..db7f9b5370186c97fcc69e421ff66f5361adb18e 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.source.index;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterators;
 import org.apache.commons.io.IOUtils;
@@ -38,6 +39,7 @@ import org.sonar.test.TestUtils;
 
 import java.io.FileInputStream;
 import java.util.Date;
+import java.util.List;
 import java.util.Map;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -74,6 +76,7 @@ public class SourceLineIndexerTest {
       .setRefresh(true)
       .get();
 
+    List<Integer> duplications = ImmutableList.of(1, 2, 3);
     SourceLineDoc line1 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
       .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
       .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
@@ -82,6 +85,7 @@ public class SourceLineIndexerTest {
       .put(SourceLineIndexDefinition.FIELD_SCM_DATE, DateUtils.parseDateTime("2014-01-01T12:34:56+0100"))
       .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
       .put(SourceLineIndexDefinition.FIELD_SOURCE, "package org.sonar.server.source;")
+      .put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, duplications)
       .put(BaseNormalizer.UPDATED_AT_FIELD, new Date())
       .build());
     SourceLineResultSetIterator.SourceFile file = new SourceLineResultSetIterator.SourceFile("efgh", System.currentTimeMillis());
@@ -96,7 +100,7 @@ public class SourceLineIndexerTest {
       .get();
     assertThat(fileSearch.getHits().getTotalHits()).isEqualTo(1L);
     Map<String, Object> fields = fileSearch.getHits().getHits()[0].sourceAsMap();
-    assertThat(fields).hasSize(8);
+    assertThat(fields).hasSize(9);
     assertThat(fields).includes(
       MapAssert.entry(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd"),
       MapAssert.entry(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh"),
@@ -104,7 +108,8 @@ public class SourceLineIndexerTest {
       MapAssert.entry(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe"),
       MapAssert.entry(SourceLineIndexDefinition.FIELD_SCM_DATE, "2014-01-01T11:34:56.000Z"),
       MapAssert.entry(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop"),
-      MapAssert.entry(SourceLineIndexDefinition.FIELD_SOURCE, "package org.sonar.server.source;")
+      MapAssert.entry(SourceLineIndexDefinition.FIELD_SOURCE, "package org.sonar.server.source;"),
+      MapAssert.entry(SourceLineIndexDefinition.FIELD_DUPLICATIONS, duplications)
       );
   }
 
index e86cc45328df59f1075d5fb39bdf7e19ee245cad..ccb2584b7d473c834e4a44f918b15be60ae8308f 100644 (file)
@@ -56,10 +56,10 @@ public class SourceLineResultSetIteratorTest {
   public void should_generate_source_line_documents() throws Exception {
     db.prepareDbUnit(getClass(), "shared.xml");
     PreparedStatement stmt = connection.prepareStatement("UPDATE file_sources SET data = ? WHERE id=1");
-    stmt.setString(1, "aef12a,alice,2014-04-25T12:34:56+0100,1,0,0,2,0,0,3,0,0,polop,palap,class Foo {\r\n" +
-      "abe465,bob,2014-07-25T12:34:56+0100,,,,,,,,,,,,  // Empty\r\n" +
-      "afb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,}\r\n" +
-      "afb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,\r\n");
+    stmt.setString(1, "aef12a,alice,2014-04-25T12:34:56+0100,1,0,0,2,0,0,3,0,0,polop,palap,,class Foo {\r\n" +
+      "abe465,bob,2014-07-25T12:34:56+0100,,,,,,,,,,,,,  // Empty\r\n" +
+      "afb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,,}\r\n" +
+      "afb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,,\r\n");
     stmt.executeUpdate();
 
     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
index 94639b2088461ed93d316a652d80de89635179f7..9059a316e18bcb3f5429f87c34fcf2eddc2eeab9 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.source.ws;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import org.apache.commons.lang.StringEscapeUtils;
 import org.elasticsearch.common.collect.Lists;
@@ -100,6 +101,7 @@ public class LinesActionTest {
       .put(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS, 3)
       .put(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS, 2)
       .put(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS, 1)
+      .put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, ImmutableList.of())
       .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
       .build());
     SourceLineDoc line2 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
@@ -115,6 +117,7 @@ public class LinesActionTest {
       .put(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS, 3)
       .put(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS, 2)
       .put(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS, 1)
+      .put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, ImmutableList.of(1))
       .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
       .build());
     SourceLineDoc line3 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
@@ -130,6 +133,7 @@ public class LinesActionTest {
       .put(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS, 3)
       .put(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS, 2)
       .put(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS, 1)
+      .put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, ImmutableList.of())
       .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
       .build());
     when(sourceLineIndex.getLines(eq(componentUuid), anyInt(), anyInt())).thenReturn(newArrayList(
@@ -173,6 +177,7 @@ public class LinesActionTest {
     fieldMap.put(SourceLineIndexDefinition.FIELD_OVERALL_LINE_HITS, null);
     fieldMap.put(SourceLineIndexDefinition.FIELD_OVERALL_CONDITIONS, null);
     fieldMap.put(SourceLineIndexDefinition.FIELD_OVERALL_COVERED_CONDITIONS, null);
+    fieldMap.put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, null);
     fieldMap.put(BaseNormalizer.UPDATED_AT_FIELD, new Date());
     when(sourceLineIndex.getLines(fileKey, 3, 3)).thenReturn(newArrayList(
       new SourceLineDoc(fieldMap)
index 9ce30d4f8e5c0f7cf0d51a46271f670ad08a5a48..d8d7eed7662179f080baf977c7bc4aee803f2ea8 100644 (file)
@@ -1,6 +1,6 @@
 <dataset>
 
   <file_sources id="1" project_uuid="uuid-MyProject" file_uuid="uuid-MyFile.xoo" created_at="1416238020000" updated_at="1416239042000"
-                data="aef12a,alice,2014-04-25T12:34:56+0100,,,,,,,,,,polop,class Foo {&#10;aef12a,alice,2014-04-25T12:34:56+0100,,,,,,,,,,polop,}" data_hash="THE_HASH" />
+                data="aef12a,alice,2014-04-25T12:34:56+0100,,,,,,,,,,polop,palap,1,class Foo {&#10;aef12a,alice,2014-04-25T12:34:56+0100,,,,,,,,,,polop,palap,&quot;1,2&quot;,}" data_hash="THE_HASH" />
 
 </dataset>
index 1ab70956af5c91b36ea162b33b11affcbe7a25cd..123ccde38e21b653bb85f5c9b7d90f248e532486 100644 (file)
@@ -16,7 +16,8 @@
       "scmRevision": "cafebabe",
       "lineHits": 3,
       "conditions": 2,
-      "coveredConditions": 1
+      "coveredConditions": 1,
+      "duplicated": true
     },
     {
       "code": "<span class=\"h3 sym-pylyp\">}</span>",