use of org.sonar.ce.task.projectanalysis.component.Component 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);
}
use of org.sonar.ce.task.projectanalysis.component.Component 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);
}
use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.
the class FileMoveDetectionStepTest method execute_detects_no_move_if_content_of_file_is_not_similar_enough.
@Test
public void execute_detects_no_move_if_content_of_file_is_not_similar_enough() {
analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
Component file1 = fileComponent(FILE_1_REF, null);
Component file2 = fileComponent(FILE_2_REF, LESS_CONTENT1);
insertFiles(file1.getDbKey());
insertContentOfFileInDb(file1.getDbKey(), CONTENT1);
setFilesInReport(file2);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
assertThat(movedFilesRepository.getComponentsWithOriginal()).isEmpty();
assertThat(scoreMatrixDumper.scoreMatrix.getMaxScore()).isPositive().isLessThan(MIN_REQUIRED_SCORE);
assertThat(addedFileRepository.getComponents()).contains(file2);
verifyStatistics(context, 1, 1, 1, 0);
}
use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.
the class FileMoveDetectionStepTest method execute_detects_no_move_if_baseSnapshot_has_no_file.
@Test
public void execute_detects_no_move_if_baseSnapshot_has_no_file() {
analysisMetadataHolder.setBaseAnalysis(ANALYSIS);
Component file1 = fileComponent(FILE_1_REF, null);
Component file2 = fileComponent(FILE_2_REF, null);
setFilesInReport(file1, file2);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
assertThat(movedFilesRepository.getComponentsWithOriginal()).isEmpty();
assertThat(addedFileRepository.getComponents()).containsOnly(file1, file2);
verifyStatistics(context, 2, 0, 2, null);
}
use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.
the class MutableMovedFilesRepositoryImplTest method setOriginalFile_throws_IAE_when_type_is_no_FILE.
@Test
public void setOriginalFile_throws_IAE_when_type_is_no_FILE() {
for (Component component : COMPONENTS_EXCEPT_FILE) {
try {
underTest.setOriginalFile(component, SOME_ORIGINAL_FILE);
fail("should have raised a NPE");
} catch (IllegalArgumentException e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("file must be of type FILE");
}
}
}
Aggregations