import org.sonar.core.persistence.DbSession;
import org.sonar.server.component.persistence.ComponentDao;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
if (d1 == null || d2 == null) {
return -1;
}
-
ComponentDto file1 = d1.file();
ComponentDto file2 = d2.file();
+
+ if (file1 == null || file2 == null) {
+ return -1;
+ }
+
if (file1.equals(d2.file())) {
// if duplication on same file => order by starting line
return d1.from().compareTo(d2.from());
private final ComponentDto file;
private final Integer from, size;
- Duplication(ComponentDto file, Integer from, Integer size) {
+ Duplication(@Nullable ComponentDto file, Integer from, Integer size) {
this.file = file;
this.from = from;
this.size = size;
}
+ /**
+ * File can be null when duplication is linked on a removed file
+ */
+ @CheckForNull
ComponentDto file() {
return file;
}
}
private void writeDuplication(Map<String, String> refByComponentKey, DuplicationsParser.Duplication duplication, JsonWriter json) {
- String componentKey = duplication.file().key();
- String ref = refByComponentKey.get(componentKey);
- if (ref == null) {
- ref = Integer.toString(refByComponentKey.size() + 1);
- refByComponentKey.put(componentKey, ref);
+ String ref = null;
+ ComponentDto componentDto = duplication.file();
+ if (componentDto != null) {
+ String componentKey = componentDto.key();
+ ref = refByComponentKey.get(componentKey);
+ if (ref == null) {
+ ref = Integer.toString(refByComponentKey.size() + 1);
+ refByComponentKey.put(componentKey, ref);
+ }
}
json.beginObject();
assertThat(blocks.get(1).duplications().get(1).from()).isEqualTo(83);
}
+ @Test
+ public void duplication_on_removed_file() throws Exception {
+ List<DuplicationsParser.Block> blocks = parser.parse(currentFile, getData("duplication_on_removed_file.xml"), session);
+ assertThat(blocks).hasSize(1);
+
+ List<DuplicationsParser.Duplication> duplications = blocks.get(0).duplications();
+ assertThat(duplications).hasSize(2);
+
+ // Duplications on removed file
+ DuplicationsParser.Duplication duplication1 = duplications.get(0);
+ assertThat(duplication1.file()).isNull();
+ assertThat(duplication1.from()).isEqualTo(31);
+ assertThat(duplication1.size()).isEqualTo(5);
+
+ DuplicationsParser.Duplication duplication2 = duplications.get(1);
+ assertThat(duplication2.file()).isEqualTo(fileOnSameProject);
+ assertThat(duplication2.from()).isEqualTo(20);
+ assertThat(duplication2.size()).isEqualTo(5);
+ }
+
@Test
public void compare_duplications() throws Exception {
ComponentDto currentFile = new ComponentDto().setId(11L).setProjectId(1L);
assertThat(comparator.compare(new DuplicationsParser.Duplication(fileOnDifferentProject, 5, 2),
new DuplicationsParser.Duplication(new ComponentDto().setId(30L).setProjectId(3L), 2, 2))).isEqualTo(1);
- // At leat one null files
+ // With null duplications
assertThat(comparator.compare(null, new DuplicationsParser.Duplication(fileOnSameProject, 2, 2))).isEqualTo(-1);
assertThat(comparator.compare(new DuplicationsParser.Duplication(fileOnSameProject, 2, 2), null)).isEqualTo(-1);
assertThat(comparator.compare(null, null)).isEqualTo(-1);
+
+ // On some removed file
+ assertThat(comparator.compare(new DuplicationsParser.Duplication(currentFile, 2, 2), new DuplicationsParser.Duplication(null, 5, 2))).isEqualTo(-1);
+ assertThat(comparator.compare(new DuplicationsParser.Duplication(null, 2, 2), new DuplicationsParser.Duplication(currentFile, 5, 2))).isEqualTo(-1);
}
private String getData(String file) throws IOException {
List<DuplicationsParser.Block> blocks = newArrayList();
blocks.add(new DuplicationsParser.Block(newArrayList(
new DuplicationsParser.Duplication(file1, 57, 12),
- new DuplicationsParser.Duplication(file2, 73, 12)
+ new DuplicationsParser.Duplication(null, 73, 12)
)));
test(blocks,
" \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
" },\n" +
" {\n" +
- " \"from\": 73, \"size\": 12, \"_ref\": \"2\"\n" +
+ " \"from\": 73, \"size\": 12\n" +
" }\n" +
" ]\n" +
" }," +
"}"
);
- verify(componentDao, times(2)).getNullableByKey(eq(session), anyString());
+ verify(componentDao, times(1)).getNullableByKey(eq(session), anyString());
verify(componentDao, times(1)).getById(anyLong(), eq(session));
}
--- /dev/null
+<duplications>
+ <g>
+ <b s="20" l="5" r="org.codehaus.sonar:sonar-plugin-api:src/main/java/com/sonar/orchestrator/util/CommandExecutor.java"/>
+ <b s="31" l="5" r="org.codehaus.sonar:sonar-plugin-api:src/main/java/com/sonar/orchestrator/util/RemovedFile.java"/>
+ </g>
+</duplications>