Search in sources :

Example 6 with MeasureComputer

use of org.sonar.api.ce.measure.MeasureComputer in project sonarqube by SonarSource.

the class LoadMeasureComputersStepTest method fail_with_ISE_when_output_metric_is_not_define_by_plugin.

@Test
public void fail_with_ISE_when_output_metric_is_not_define_by_plugin() throws Exception {
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("Metric 'unknown' cannot be used as an output metric as no plugin declare this metric");
    MeasureComputer[] computers = new MeasureComputer[] { newMeasureComputer(array(NEW_METRIC_4), array("unknown")) };
    ComputationStep underTest = new LoadMeasureComputersStep(holder, array(new TestMetrics()), computers);
    underTest.execute();
}
Also used : MeasureComputer(org.sonar.api.ce.measure.MeasureComputer) ComputationStep(org.sonar.server.computation.task.step.ComputationStep) Test(org.junit.Test)

Example 7 with MeasureComputer

use of org.sonar.api.ce.measure.MeasureComputer in project sonarqube by SonarSource.

the class ReportMeasureComputersVisitorTest method compute_plugin_measure.

@Test
public void compute_plugin_measure() throws Exception {
    addRawMeasure(FILE_1_REF, NCLOC_KEY, 10);
    addRawMeasure(FILE_1_REF, COMMENT_LINES_KEY, 2);
    addRawMeasure(FILE_2_REF, NCLOC_KEY, 40);
    addRawMeasure(FILE_2_REF, COMMENT_LINES_KEY, 5);
    addRawMeasure(DIRECTORY_REF, NCLOC_KEY, 50);
    addRawMeasure(DIRECTORY_REF, COMMENT_LINES_KEY, 7);
    addRawMeasure(MODULE_REF, NCLOC_KEY, 50);
    addRawMeasure(MODULE_REF, COMMENT_LINES_KEY, 7);
    addRawMeasure(ROOT_REF, NCLOC_KEY, 50);
    addRawMeasure(ROOT_REF, COMMENT_LINES_KEY, 7);
    final MeasureComputer.MeasureComputerDefinition definition = new MeasureComputerDefinitionImpl.BuilderImpl().setInputMetrics(NCLOC_KEY, COMMENT_LINES_KEY).setOutputMetrics(NEW_METRIC_KEY).build();
    measureComputersHolder.setMeasureComputers(newArrayList(new MeasureComputerWrapper(new MeasureComputer() {

        @Override
        public MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext) {
            return definition;
        }

        @Override
        public void compute(MeasureComputerContext context) {
            org.sonar.api.ce.measure.Measure ncloc = context.getMeasure(NCLOC_KEY);
            org.sonar.api.ce.measure.Measure comment = context.getMeasure(COMMENT_LINES_KEY);
            if (ncloc != null && comment != null) {
                context.addMeasure(NEW_METRIC_KEY, ncloc.getIntValue() + comment.getIntValue());
            }
        }
    }, definition)));
    VisitorsCrawler visitorsCrawler = new VisitorsCrawler(Arrays.<ComponentVisitor>asList(new MeasureComputersVisitor(metricRepository, measureRepository, null, measureComputersHolder, componentIssuesRepository)));
    visitorsCrawler.visit(ROOT);
    assertAddedRawMeasure(12, FILE_1_REF, NEW_METRIC_KEY);
    assertAddedRawMeasure(45, FILE_2_REF, NEW_METRIC_KEY);
    assertAddedRawMeasure(57, DIRECTORY_REF, NEW_METRIC_KEY);
    assertAddedRawMeasure(57, MODULE_REF, NEW_METRIC_KEY);
    assertAddedRawMeasure(57, ROOT_REF, NEW_METRIC_KEY);
}
Also used : MeasureComputer(org.sonar.api.ce.measure.MeasureComputer) MeasureComputerWrapper(org.sonar.server.computation.task.projectanalysis.api.measurecomputer.MeasureComputerWrapper) MeasureComputerDefinitionImpl(org.sonar.server.computation.task.projectanalysis.api.measurecomputer.MeasureComputerDefinitionImpl) VisitorsCrawler(org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler) Test(org.junit.Test)

Example 8 with MeasureComputer

use of org.sonar.api.ce.measure.MeasureComputer in project sonarqube by SonarSource.

the class LoadMeasureComputersStepTest method fail_with_ISE_when_input_metric_is_unknown.

@Test
public void fail_with_ISE_when_input_metric_is_unknown() throws Exception {
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("Metric 'unknown' cannot be used as an input metric as it's not a core metric and no plugin declare this metric");
    MeasureComputer[] computers = new MeasureComputer[] { newMeasureComputer(array("unknown"), array(NEW_METRIC_4)) };
    ComputationStep underTest = new LoadMeasureComputersStep(holder, array(new TestMetrics()), computers);
    underTest.execute();
}
Also used : MeasureComputer(org.sonar.api.ce.measure.MeasureComputer) ComputationStep(org.sonar.server.computation.task.step.ComputationStep) Test(org.junit.Test)

Example 9 with MeasureComputer

use of org.sonar.api.ce.measure.MeasureComputer in project sonarqube by SonarSource.

the class LoadMeasureComputersStepTest method fail_with_ISE_when_output_metric_is_a_core_metric.

@Test
public void fail_with_ISE_when_output_metric_is_a_core_metric() throws Exception {
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("Metric 'ncloc' cannot be used as an output metric as it's a core metric");
    MeasureComputer[] computers = new MeasureComputer[] { newMeasureComputer(array(NEW_METRIC_4), array(NCLOC_KEY)) };
    ComputationStep underTest = new LoadMeasureComputersStep(holder, array(new TestMetrics()), computers);
    underTest.execute();
}
Also used : MeasureComputer(org.sonar.api.ce.measure.MeasureComputer) ComputationStep(org.sonar.server.computation.task.step.ComputationStep) Test(org.junit.Test)

Example 10 with MeasureComputer

use of org.sonar.api.ce.measure.MeasureComputer in project sonarqube by SonarSource.

the class LoadMeasureComputersStepTest method fail_with_IAE_when_creating_measure_computer_definition_without_using_the_builder_and_with_invalid_output_metrics.

@Test
public void fail_with_IAE_when_creating_measure_computer_definition_without_using_the_builder_and_with_invalid_output_metrics() throws Exception {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("At least one output metric must be defined");
    MeasureComputer measureComputer = new MeasureComputer() {

        @Override
        public MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext) {
            // Create a instance of MeasureComputerDefinition without using the builder
            return new MeasureComputer.MeasureComputerDefinition() {

                @Override
                public Set<String> getInputMetrics() {
                    return ImmutableSet.of(NCLOC_KEY);
                }

                @Override
                public Set<String> getOutputMetrics() {
                    // Empty output metric is not allowed !
                    return Collections.emptySet();
                }
            };
        }

        @Override
        public void compute(MeasureComputerContext context) {
        // Nothing needs to be done as we're only testing metada
        }
    };
    MeasureComputer[] computers = new MeasureComputer[] { measureComputer };
    ComputationStep underTest = new LoadMeasureComputersStep(holder, array(new TestMetrics()), computers);
    underTest.execute();
}
Also used : MeasureComputer(org.sonar.api.ce.measure.MeasureComputer) ComputationStep(org.sonar.server.computation.task.step.ComputationStep) Test(org.junit.Test)

Aggregations

MeasureComputer (org.sonar.api.ce.measure.MeasureComputer)14 Test (org.junit.Test)12 ComputationStep (org.sonar.server.computation.task.step.ComputationStep)11 MeasureComputerWrapper (org.sonar.server.computation.task.projectanalysis.api.measurecomputer.MeasureComputerWrapper)5 MeasureComputerContextImpl (org.sonar.server.computation.task.projectanalysis.api.measurecomputer.MeasureComputerContextImpl)1 MeasureComputerDefinitionImpl (org.sonar.server.computation.task.projectanalysis.api.measurecomputer.MeasureComputerDefinitionImpl)1 VisitorsCrawler (org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler)1