@Override public int compare(@Nullable Block b1, @Nullable Block b2) { if (b1 == null || b2 == null) { return -1; } List<Duplication> duplications1 = b1.getDuplications(); List<Duplication> duplications2 = b2.getDuplications(); if (duplications1.isEmpty() || duplications2.isEmpty()) { return -1; } return duplications1.get(0).from().compareTo(duplications2.get(0).from()); } }
@Override public int compare(@Nullable Duplication d1, @Nullable Duplication d2) { if (d1 == null || d2 == null) { return -1; } ComponentDto file1 = d1.componentDto(); ComponentDto file2 = d2.componentDto(); if (file1 != null && file1.equals(file2)) { // if duplication on same file => order by starting line return d1.from().compareTo(d2.from()); } if (sameFile(file1) && !sameFile(file2)) { // the current resource must be displayed first return -1; } if (sameFile(file2) && !sameFile(file1)) { // the current resource must be displayed first return 1; } if (sameProject(file1) && !sameProject(file2)) { // if resource is in the same project, this it must be displayed first return -1; } if (sameProject(file2) && !sameProject(file1)) { // if resource is in the same project, this it must be displayed first return 1; } return d1.from().compareTo(d2.from()); }
private static Block.Builder toWsBlock(Duplication duplication, Map<String, Reference> refByComponentKey) { Block.Builder block = Block.newBuilder(); if (!duplication.removed()) { Reference ref = refByComponentKey.computeIfAbsent(duplication.componentDbKey(), k -> new Reference( Integer.toString(refByComponentKey.size() + 1), duplication.componentDto(), duplication.componentDbKey())); block.setRef(ref.getId()); } block.setFrom(duplication.from()); block.setSize(duplication.size()); return block; }
@Test public void duplication_on_same_file() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file, null, null, format("<duplications>\n" + " <g>\n" + " <b s=\"31\" l=\"5\" r=\"%s\"/>\n" + " <b s=\"20\" l=\"5\" r=\"%s\"/>\n" + " </g>\n" + "</duplications>", file.getDbKey(), file.getDbKey())); assertThat(blocks).hasSize(1); List<Duplication> duplications = blocks.get(0).getDuplications(); assertThat(duplications).hasSize(2); // Smallest line comes first Duplication duplication1 = duplications.get(0); assertThat(duplication1.componentDto()).isEqualTo(file); assertThat(duplication1.from()).isEqualTo(20); assertThat(duplication1.size()).isEqualTo(5); Duplication duplication2 = duplications.get(1); assertThat(duplication2.componentDto()).isEqualTo(file); assertThat(duplication2.from()).isEqualTo(31); assertThat(duplication2.size()).isEqualTo(5); }
@Test public void duplication_on_same_project() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file1 = db.components().insertComponent(newFileDto(project)); ComponentDto file2 = db.components().insertComponent(newFileDto(project)); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, null, null, format("<duplications>\n" + " <g>\n" + " <b s=\"20\" l=\"5\" r=\"%s\"/>\n" + " <b s=\"31\" l=\"5\" r=\"%s\"/>\n" + " </g>\n" + "</duplications>", file2.getDbKey(), file1.getDbKey())); assertThat(blocks).hasSize(1); List<Duplication> duplications = blocks.get(0).getDuplications(); assertThat(duplications).hasSize(2); // Current file comes first Duplication duplication1 = duplications.get(0); assertThat(duplication1.componentDto()).isEqualTo(file1); assertThat(duplication1.from()).isEqualTo(31); assertThat(duplication1.size()).isEqualTo(5); Duplication duplication2 = duplications.get(1); assertThat(duplication2.componentDto()).isEqualTo(file2); assertThat(duplication2.from()).isEqualTo(20); assertThat(duplication2.size()).isEqualTo(5); }
@Test public void duplication_on_not_existing_file() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file, null, null, format("<duplications>\n" + " <g>\n" + " <b s=\"20\" l=\"5\" r=\"%s\"/>\n" + " <b s=\"31\" l=\"5\" r=\"%s\"/>\n" + " </g>\n" + "</duplications>", file.getDbKey(), "not_existing")); assertThat(blocks).hasSize(1); List<Duplication> duplications = blocks.get(0).getDuplications(); assertThat(duplications).hasSize(2); // Duplications on removed file Duplication duplication1 = duplication(duplications, null); assertThat(duplication1.componentDto()).isNull(); assertThat(duplication1.from()).isEqualTo(31); assertThat(duplication1.size()).isEqualTo(5); Duplication duplication2 = duplication(duplications, file.getDbKey()); assertThat(duplication2.componentDto()).isEqualTo(file); assertThat(duplication2.from()).isEqualTo(20); assertThat(duplication2.size()).isEqualTo(5); }
@Test public void duplications_on_many_blocks() { ComponentDto project1 = db.components().insertPrivateProject(); ComponentDto file1 = db.components().insertComponent(newFileDto(project1) .setDbKey("org.codehaus.sonar:sonar-plugin-api:src/main/java/org/sonar/api/utils/command/CommandExecutor.java") .setLongName("CommandExecutor")); ComponentDto project2 = db.components().insertPrivateProject(); ComponentDto file2 = db.components().insertComponent(newFileDto(project2) .setDbKey("com.sonarsource.orchestrator:sonar-orchestrator:src/main/java/com/sonar/orchestrator/util/CommandExecutor.java") .setLongName("CommandExecutor")); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, null, null, format("<duplications>\n" + " <g>\n" + " <b s=\"94\" l=\"101\" r=\"%s\"/>\n" + " <b s=\"83\" l=\"101\" r=\"%s\"/>\n" + " </g>\n" + " <g>\n" + " <b s=\"38\" l=\"40\" r=\"%s\"/>\n" + " <b s=\"29\" l=\"39\" r=\"%s\"/>\n" + " </g>\n" + "</duplications>\n", file2.getDbKey(), file1.getDbKey(), file2.getDbKey(), file1.getDbKey())); assertThat(blocks).hasSize(2); // Block with smaller line should come first assertThat(blocks.get(0).getDuplications().get(0).from()).isEqualTo(29); assertThat(blocks.get(0).getDuplications().get(1).from()).isEqualTo(38); assertThat(blocks.get(1).getDuplications().get(0).from()).isEqualTo(83); assertThat(blocks.get(1).getDuplications().get(1).from()).isEqualTo(94); }
@Test public void duplication_on_branch() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file1 = db.components().insertComponent(newFileDto(branch)); ComponentDto file2 = db.components().insertComponent(newFileDto(branch)); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, branch.getBranch(), null, format("<duplications>\n" + " <g>\n" + " <b s=\"20\" l=\"5\" r=\"%s\"/>\n" + " <b s=\"31\" l=\"5\" r=\"%s\"/>\n" + " </g>\n" + "</duplications>", file2.getDbKey(), file1.getDbKey())); assertThat(blocks).hasSize(1); List<Duplication> duplications = blocks.get(0).getDuplications(); assertThat(duplications).hasSize(2); // Current file comes first Duplication duplication1 = duplications.get(0); assertThat(duplication1.componentDto()).isEqualTo(file1); assertThat(duplication1.componentDto().getKey()).isEqualTo(file1.getKey()); assertThat(duplication1.from()).isEqualTo(31); assertThat(duplication1.size()).isEqualTo(5); Duplication duplication2 = duplications.get(1); assertThat(duplication2.componentDto()).isEqualTo(file2); assertThat(duplication2.componentDto().getKey()).isEqualTo(file2.getKey()); assertThat(duplication2.from()).isEqualTo(20); assertThat(duplication2.size()).isEqualTo(5); }
@Test public void duplication_on_pull_request() { ComponentDto project = db.components().insertMainBranch(); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); ComponentDto file1 = db.components().insertComponent(newFileDto(pullRequest)); ComponentDto file2 = db.components().insertComponent(newFileDto(pullRequest)); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, null, pullRequest.getPullRequest(), format("<duplications>\n" + " <g>\n" + " <b s=\"20\" l=\"5\" r=\"%s\"/>\n" + " <b s=\"31\" l=\"5\" r=\"%s\"/>\n" + " </g>\n" + "</duplications>", file2.getDbKey(), file1.getDbKey())); assertThat(blocks).hasSize(1); List<Duplication> duplications = blocks.get(0).getDuplications(); assertThat(duplications).hasSize(2); // Current file comes first Duplication duplication1 = duplications.get(0); assertThat(duplication1.componentDto()).isEqualTo(file1); assertThat(duplication1.componentDto().getKey()).isEqualTo(file1.getKey()); assertThat(duplication1.from()).isEqualTo(31); assertThat(duplication1.size()).isEqualTo(5); Duplication duplication2 = duplications.get(1); assertThat(duplication2.componentDto()).isEqualTo(file2); assertThat(duplication2.componentDto().getKey()).isEqualTo(file2.getKey()); assertThat(duplication2.from()).isEqualTo(20); assertThat(duplication2.size()).isEqualTo(5); }