@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.CODEVIEWER, component); String duplications = findDataFromComponent(dbSession, component); String branch = component.getBranch(); String pullRequest = component.getPullRequest(); List<DuplicationsParser.Block> blocks = parser.parse(dbSession, component, branch, pullRequest, duplications); writeProtobuf(responseBuilder.build(dbSession, blocks, branch, pullRequest), request, response); } }
@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 empty_list_when_no_data() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); assertThat(parser.parse(db.getSession(), file, null, null, null)).isEmpty(); }
@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); }
ComponentDto project2 = db.components().insertPrivateProject(); ComponentDto fileOnProject2 = db.components().insertComponent(newFileDto(project2)); List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, null, null, format("<duplications>\n" + " <g>\n" +
@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); }
@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); }
@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.CODEVIEWER, component); String duplications = findDataFromComponent(dbSession, component); String branch = component.getBranch(); String pullRequest = component.getPullRequest(); List<DuplicationsParser.Block> blocks = parser.parse(dbSession, component, branch, pullRequest, duplications); writeProtobuf(responseBuilder.build(dbSession, blocks, branch, pullRequest), request, response); } }