Search in sources :

Example 6 with FileAttributes

use of org.sonar.ce.task.projectanalysis.component.FileAttributes in project sonarqube by SonarSource.

the class NewMaintainabilityMeasuresVisitorTest method compute_new_maintainability_rating.

@Test
public void compute_new_maintainability_rating() {
    when(ratingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
    ReportComponent file = builder(FILE, LANGUAGE_1_FILE_REF).setFileAttributes(new FileAttributes(false, LANGUAGE_1_KEY, 1)).build();
    treeRootHolder.setRoot(builder(PROJECT, ROOT_REF).addChildren(builder(DIRECTORY, 111).addChildren(file).build()).build());
    Measure newDebtMeasure = createNewDebtMeasure(50);
    measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, newDebtMeasure);
    measureRepository.addRawMeasure(111, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(150));
    measureRepository.addRawMeasure(ROOT_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(250));
    // 4 lines file, only first one is not ncloc
    measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
    // first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
    setNewLines(file, 3, 4);
    underTest.visit(treeRootHolder.getRoot());
    assertNewMaintainability(LANGUAGE_1_FILE_REF, D);
    assertNewMaintainability(111, D);
    assertNewMaintainability(ROOT_REF, D);
}
Also used : Measure(org.sonar.ce.task.projectanalysis.measure.Measure) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) FileAttributes(org.sonar.ce.task.projectanalysis.component.FileAttributes) Test(org.junit.Test)

Example 7 with FileAttributes

use of org.sonar.ce.task.projectanalysis.component.FileAttributes in project sonarqube by SonarSource.

the class MaintainabilityMeasuresVisitorTest method compute_development_cost.

@Test
public void compute_development_cost() {
    ReportComponent root = builder(PROJECT, 1).addChildren(builder(DIRECTORY, 111).addChildren(createFileComponent(LANGUAGE_KEY_1, 1111), createFileComponent(LANGUAGE_KEY_2, 1112), // Unit test should not be ignored
    builder(FILE, 1113).setFileAttributes(new FileAttributes(true, LANGUAGE_KEY_1, 1)).build()).build(), builder(DIRECTORY, 112).addChildren(createFileComponent(LANGUAGE_KEY_2, 1121)).build(), builder(DIRECTORY, 121).addChildren(createFileComponent(LANGUAGE_KEY_1, 1211)).build(), builder(DIRECTORY, 122).build()).build();
    treeRootHolder.setRoot(root);
    int ncloc1112 = 12;
    addRawMeasure(NCLOC_KEY, 1112, ncloc1112);
    int ncloc1113 = 15;
    addRawMeasure(NCLOC_KEY, 1113, ncloc1113);
    int nclocValue1121 = 30;
    addRawMeasure(NCLOC_KEY, 1121, nclocValue1121);
    int ncloc1211 = 20;
    addRawMeasure(NCLOC_KEY, 1211, ncloc1211);
    underTest.visit(root);
    // verify measures on files
    verifyAddedRawMeasure(1112, DEVELOPMENT_COST_KEY, Long.toString(ncloc1112 * DEV_COST_LANGUAGE_2));
    verifyAddedRawMeasure(1113, DEVELOPMENT_COST_KEY, Long.toString(ncloc1113 * DEV_COST_LANGUAGE_1));
    verifyAddedRawMeasure(1121, DEVELOPMENT_COST_KEY, Long.toString(nclocValue1121 * DEV_COST_LANGUAGE_2));
    verifyAddedRawMeasure(1211, DEVELOPMENT_COST_KEY, Long.toString(ncloc1211 * DEV_COST_LANGUAGE_1));
    // directory has no children => no file => 0 everywhere and A rating
    verifyAddedRawMeasure(122, DEVELOPMENT_COST_KEY, "0");
    // directory has children => dev cost is aggregated
    verifyAddedRawMeasure(111, DEVELOPMENT_COST_KEY, Long.toString(ncloc1112 * DEV_COST_LANGUAGE_2 + ncloc1113 * DEV_COST_LANGUAGE_1));
    verifyAddedRawMeasure(112, DEVELOPMENT_COST_KEY, Long.toString(nclocValue1121 * DEV_COST_LANGUAGE_2));
    verifyAddedRawMeasure(121, DEVELOPMENT_COST_KEY, Long.toString(ncloc1211 * DEV_COST_LANGUAGE_1));
    verifyAddedRawMeasure(1, DEVELOPMENT_COST_KEY, Long.toString(ncloc1112 * DEV_COST_LANGUAGE_2 + ncloc1113 * DEV_COST_LANGUAGE_1 + nclocValue1121 * DEV_COST_LANGUAGE_2 + ncloc1211 * DEV_COST_LANGUAGE_1));
}
Also used : ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) FileAttributes(org.sonar.ce.task.projectanalysis.component.FileAttributes) Test(org.junit.Test)

Example 8 with FileAttributes

use of org.sonar.ce.task.projectanalysis.component.FileAttributes 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 9 with FileAttributes

use of org.sonar.ce.task.projectanalysis.component.FileAttributes in project sonarqube by SonarSource.

the class NewCoverageMeasuresStepTest method no_measure_for_unit_test_FILE_component.

@Test
public void no_measure_for_unit_test_FILE_component() {
    treeRootHolder.setRoot(ReportComponent.builder(Component.Type.FILE, FILE_1_REF).setFileAttributes(new FileAttributes(true, null, 1)).build());
    underTest.execute(new TestComputationStepContext());
    assertThat(measureRepository.isEmpty()).isTrue();
}
Also used : TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) FileAttributes(org.sonar.ce.task.projectanalysis.component.FileAttributes) Test(org.junit.Test)

Example 10 with FileAttributes

use of org.sonar.ce.task.projectanalysis.component.FileAttributes in project sonarqube by SonarSource.

the class ReportPersistComponentsStepTest method persist_components.

@Test
public void persist_components() {
    ComponentDto projectDto = prepareProject();
    Component file = builder(FILE, 4).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java").setName("src/main/java/dir/Foo.java").setShortName("Foo.java").setFileAttributes(new FileAttributes(false, "java", 1)).build();
    Component directory = builder(DIRECTORY, 3).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir").setName("src/main/java/dir").setShortName("dir").addChildren(file).build();
    Component treeRoot = asTreeRoot(projectDto).addChildren(directory).build();
    treeRootHolder.setRoot(treeRoot);
    underTest.execute(new TestComputationStepContext());
    assertThat(db.countRowsOfTable("components")).isEqualTo(3);
    ComponentDto directoryDto = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get();
    assertThat(directoryDto.name()).isEqualTo("dir");
    assertThat(directoryDto.longName()).isEqualTo("src/main/java/dir");
    assertThat(directoryDto.description()).isNull();
    assertThat(directoryDto.path()).isEqualTo("src/main/java/dir");
    assertThat(directoryDto.uuid()).isEqualTo("CDEF");
    assertThat(directoryDto.getUuidPath()).isEqualTo(UUID_PATH_SEPARATOR + projectDto.uuid() + UUID_PATH_SEPARATOR);
    assertThat(directoryDto.moduleUuid()).isEqualTo(projectDto.uuid());
    assertThat(directoryDto.moduleUuidPath()).isEqualTo(projectDto.moduleUuidPath());
    assertThat(directoryDto.getMainBranchProjectUuid()).isNull();
    assertThat(directoryDto.projectUuid()).isEqualTo(projectDto.uuid());
    assertThat(directoryDto.qualifier()).isEqualTo("DIR");
    assertThat(directoryDto.scope()).isEqualTo("DIR");
    assertThat(directoryDto.getRootUuid()).isEqualTo(projectDto.uuid());
    assertThat(directoryDto.getCreatedAt()).isEqualTo(now);
    ComponentDto fileDto = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get();
    assertThat(fileDto.name()).isEqualTo("Foo.java");
    assertThat(fileDto.longName()).isEqualTo("src/main/java/dir/Foo.java");
    assertThat(fileDto.description()).isNull();
    assertThat(fileDto.path()).isEqualTo("src/main/java/dir/Foo.java");
    assertThat(fileDto.language()).isEqualTo("java");
    assertThat(fileDto.uuid()).isEqualTo("DEFG");
    assertThat(fileDto.getUuidPath()).isEqualTo(directoryDto.getUuidPath() + directoryDto.uuid() + UUID_PATH_SEPARATOR);
    assertThat(fileDto.moduleUuid()).isEqualTo(projectDto.uuid());
    assertThat(fileDto.moduleUuidPath()).isEqualTo(projectDto.moduleUuidPath());
    assertThat(fileDto.getMainBranchProjectUuid()).isNull();
    assertThat(fileDto.projectUuid()).isEqualTo(projectDto.uuid());
    assertThat(fileDto.qualifier()).isEqualTo("FIL");
    assertThat(fileDto.scope()).isEqualTo("FIL");
    assertThat(fileDto.getRootUuid()).isEqualTo(projectDto.uuid());
    assertThat(fileDto.getCreatedAt()).isEqualTo(now);
}
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) FileAttributes(org.sonar.ce.task.projectanalysis.component.FileAttributes) Test(org.junit.Test)

Aggregations

FileAttributes (org.sonar.ce.task.projectanalysis.component.FileAttributes)20 Test (org.junit.Test)17 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)12 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)8 Component (org.sonar.ce.task.projectanalysis.component.Component)6 ComponentDto (org.sonar.db.component.ComponentDto)5 Measure (org.sonar.ce.task.projectanalysis.measure.Measure)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Block (org.sonar.duplications.block.Block)1 ByteArray (org.sonar.duplications.block.ByteArray)1