*/
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;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import java.util.Collection;
import java.util.Date;
import java.util.Map;
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);
+ }
}
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";
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);
}
}
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;
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);
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()) {
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();
}
*/
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;
import java.io.FileInputStream;
import java.util.Date;
+import java.util.List;
import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
.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")
.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());
.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"),
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)
);
}
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);
*/
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;
.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()
.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()
.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(
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)
<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 { 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 { aef12a,alice,2014-04-25T12:34:56+0100,,,,,,,,,,polop,palap,"1,2",}" data_hash="THE_HASH" />
</dataset>
"scmRevision": "cafebabe",
"lineHits": 3,
"conditions": 2,
- "coveredConditions": 1
+ "coveredConditions": 1,
+ "duplicated": true
},
{
"code": "<span class=\"h3 sym-pylyp\">}</span>",