Search in sources :

Example 26 with TestComputationStepContext

use of org.sonar.ce.task.step.TestComputationStepContext in project sonarqube by SonarSource.

the class FileMoveDetectionStepTest method execute_detects_no_move_if_two_files_are_empty_in_DB.

@Test
public void execute_detects_no_move_if_two_files_are_empty_in_DB() {
    analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
    Component file1 = fileComponent(FILE_1_REF, null);
    Component file2 = fileComponent(FILE_2_REF, null);
    insertFiles(file1.getUuid(), file2.getUuid());
    insertContentOfFileInDb(file1.getUuid(), null);
    insertContentOfFileInDb(file2.getUuid(), null);
    setFilesInReport(file1, file2);
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    assertThat(movedFilesRepository.getComponentsWithOriginal()).isEmpty();
    assertThat(scoreMatrixDumper.scoreMatrix).isNull();
    assertThat(addedFileRepository.getComponents()).isEmpty();
    verifyStatistics(context, 2, 2, 0, null);
}
Also used : Component(org.sonar.ce.task.projectanalysis.component.Component) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Example 27 with TestComputationStepContext

use of org.sonar.ce.task.step.TestComputationStepContext in project sonarqube by SonarSource.

the class FileMoveDetectionStepTest method execute_detects_move_if_content_of_file_is_same_in_DB_and_report.

@Test
public void execute_detects_move_if_content_of_file_is_same_in_DB_and_report() {
    analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
    Component file1 = fileComponent(FILE_1_REF, null);
    Component file2 = fileComponent(FILE_2_REF, CONTENT1);
    ComponentDto[] dtos = insertFiles(file1.getUuid());
    insertContentOfFileInDb(file1.getUuid(), CONTENT1);
    setFilesInReport(file2);
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    assertThat(movedFilesRepository.getComponentsWithOriginal()).containsExactly(file2);
    MovedFilesRepository.OriginalFile originalFile = movedFilesRepository.getOriginalFile(file2).get();
    assertThat(originalFile.getKey()).isEqualTo(dtos[0].getDbKey());
    assertThat(originalFile.getUuid()).isEqualTo(dtos[0].uuid());
    assertThat(addedFileRepository.getComponents()).isEmpty();
    verifyStatistics(context, 1, 1, 1, 1);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) Component(org.sonar.ce.task.projectanalysis.component.Component) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Example 28 with TestComputationStepContext

use of org.sonar.ce.task.step.TestComputationStepContext in project sonarqube by SonarSource.

the class FileMoveDetectionStepTest method execute_does_not_compute_any_distance_if_all_files_sizes_are_all_too_different.

@Test
public void execute_does_not_compute_any_distance_if_all_files_sizes_are_all_too_different() {
    analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
    Component file1 = fileComponent(FILE_1_REF, null);
    Component file2 = fileComponent(FILE_2_REF, null);
    Component file3 = fileComponent(FILE_3_REF, arrayOf(118));
    Component file4 = fileComponent(5, arrayOf(25));
    insertFiles(file1.getDbKey(), file2.getDbKey());
    insertContentOfFileInDb(file1.getDbKey(), arrayOf(100));
    insertContentOfFileInDb(file2.getDbKey(), arrayOf(30));
    setFilesInReport(file3, file4);
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    assertThat(movedFilesRepository.getComponentsWithOriginal()).isEmpty();
    assertThat(scoreMatrixDumper.scoreMatrix.getMaxScore()).isZero();
    verifyStatistics(context, 2, 2, 2, 0);
}
Also used : Component(org.sonar.ce.task.projectanalysis.component.Component) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Example 29 with TestComputationStepContext

use of org.sonar.ce.task.step.TestComputationStepContext in project sonarqube by SonarSource.

the class FileMoveDetectionStepTest method real_life_use_case.

/**
 * JH: A bug was encountered in the algorithm and I didn't manage to forge a simpler test case.
 */
@Test
public void real_life_use_case() throws Exception {
    analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
    for (File f : FileUtils.listFiles(new File("src/test/resources/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest/v1"), null, false)) {
        insertFiles("uuid_" + f.getName().hashCode());
        insertContentOfFileInDb("uuid_" + f.getName().hashCode(), readLines(f));
    }
    Map<String, Component> comps = new HashMap<>();
    int i = 1;
    for (File f : FileUtils.listFiles(new File("src/test/resources/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest/v2"), null, false)) {
        String[] lines = readLines(f);
        Component c = builder(Component.Type.FILE, i++).setUuid("uuid_" + f.getName().hashCode()).setKey(f.getName()).setName(f.getName()).setFileAttributes(new FileAttributes(false, null, lines.length)).build();
        comps.put(f.getName(), c);
        setFileLineHashesInReport(c, lines);
    }
    setFilesInReport(comps.values().toArray(new Component[0]));
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    Component makeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndex = comps.get("MakeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndex.java");
    Component migrationRb1238 = comps.get("1238_make_component_uuid_and_analysis_uuid_not_null_on_duplications_index.rb");
    Component addComponentUuidAndAnalysisUuidColumnToDuplicationsIndex = comps.get("AddComponentUuidAndAnalysisUuidColumnToDuplicationsIndex.java");
    assertThat(movedFilesRepository.getComponentsWithOriginal()).containsOnly(makeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndex, migrationRb1238, addComponentUuidAndAnalysisUuidColumnToDuplicationsIndex);
    assertThat(movedFilesRepository.getOriginalFile(makeComponentUuidAndAnalysisUuidNotNullOnDuplicationsIndex).get().getUuid()).isEqualTo("uuid_" + "MakeComponentUuidNotNullOnDuplicationsIndex.java".hashCode());
    assertThat(movedFilesRepository.getOriginalFile(migrationRb1238).get().getUuid()).isEqualTo("uuid_" + "1242_make_analysis_uuid_not_null_on_duplications_index.rb".hashCode());
    assertThat(movedFilesRepository.getOriginalFile(addComponentUuidAndAnalysisUuidColumnToDuplicationsIndex).get().getUuid()).isEqualTo("uuid_" + "AddComponentUuidColumnToDuplicationsIndex.java".hashCode());
    verifyStatistics(context, comps.values().size(), 12, 6, 3);
}
Also used : HashMap(java.util.HashMap) Component(org.sonar.ce.task.projectanalysis.component.Component) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) File(java.io.File) FileAttributes(org.sonar.ce.task.projectanalysis.component.FileAttributes) Test(org.junit.Test)

Example 30 with TestComputationStepContext

use of org.sonar.ce.task.step.TestComputationStepContext in project sonarqube by SonarSource.

the class FileMoveDetectionStepTest method execute_detects_several_moves.

@Test
public void execute_detects_several_moves() {
    // testing:
    // - file1 renamed to file3
    // - file2 deleted
    // - file4 untouched
    // - file5 renamed to file6 with a small change
    analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
    Component file1 = fileComponent(FILE_1_REF, null);
    Component file2 = fileComponent(FILE_2_REF, null);
    Component file3 = fileComponent(FILE_3_REF, CONTENT1);
    Component file4 = fileComponent(5, new String[] { "a", "b" });
    Component file5 = fileComponent(6, null);
    Component file6 = fileComponent(7, LESS_CONTENT2);
    ComponentDto[] dtos = insertFiles(file1.getUuid(), file2.getUuid(), file4.getUuid(), file5.getUuid());
    insertContentOfFileInDb(file1.getUuid(), CONTENT1);
    insertContentOfFileInDb(file2.getUuid(), LESS_CONTENT1);
    insertContentOfFileInDb(file4.getUuid(), new String[] { "e", "f", "g", "h", "i" });
    insertContentOfFileInDb(file5.getUuid(), CONTENT2);
    setFilesInReport(file3, file4, file6);
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    assertThat(movedFilesRepository.getComponentsWithOriginal()).containsOnly(file3, file6);
    MovedFilesRepository.OriginalFile originalFile2 = movedFilesRepository.getOriginalFile(file3).get();
    assertThat(originalFile2.getKey()).isEqualTo(dtos[0].getDbKey());
    assertThat(originalFile2.getUuid()).isEqualTo(dtos[0].uuid());
    MovedFilesRepository.OriginalFile originalFile5 = movedFilesRepository.getOriginalFile(file6).get();
    assertThat(originalFile5.getKey()).isEqualTo(dtos[3].getDbKey());
    assertThat(originalFile5.getUuid()).isEqualTo(dtos[3].uuid());
    assertThat(scoreMatrixDumper.scoreMatrix.getMaxScore()).isGreaterThan(MIN_REQUIRED_SCORE);
    assertThat(addedFileRepository.getComponents()).isEmpty();
    verifyStatistics(context, 3, 4, 2, 2);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) Component(org.sonar.ce.task.projectanalysis.component.Component) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Aggregations

TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)458 Test (org.junit.Test)431 ComponentDto (org.sonar.db.component.ComponentDto)91 ProjectDump (com.sonarsource.governance.projectdump.protobuf.ProjectDump)38 Component (org.sonar.ce.task.projectanalysis.component.Component)38 SnapshotDto (org.sonar.db.component.SnapshotDto)31 Date (java.util.Date)30 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)28 ComputationStep (org.sonar.ce.task.step.ComputationStep)24 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)17 FileSourceDto (org.sonar.db.source.FileSourceDto)15 BaseStepTest (org.sonar.ce.task.projectanalysis.step.BaseStepTest)14 Project (org.sonar.server.project.Project)14 TextBlock (org.sonar.ce.task.projectanalysis.duplication.TextBlock)13 QualityProfile (org.sonar.server.qualityprofile.QualityProfile)13 Notification (org.sonar.api.notifications.Notification)12 DefaultIssue (org.sonar.core.issue.DefaultIssue)12 MeasureComputer (org.sonar.api.ce.measure.MeasureComputer)11 IssueDto (org.sonar.db.issue.IssueDto)11 IssuesChangesNotification (org.sonar.server.issue.notification.IssuesChangesNotification)11