Search in sources :

Example 1 with Metrics

use of org.sonar.api.measures.Metrics in project sonarqube by SonarSource.

the class MetricProviderTest method should_provide_plugin_metrics.

@Test
public void should_provide_plugin_metrics() {
    Metrics factory = new Metrics() {

        public List<Metric> getMetrics() {
            return Arrays.<Metric>asList(new Metric.Builder("custom", "Custom", Metric.ValueType.FLOAT).create());
        }
    };
    MetricProvider provider = new MetricProvider(new Metrics[] { factory });
    List<Metric> metrics = provider.provide();
    assertThat(metrics.size()).isEqualTo(1 + CoreMetrics.getMetrics().size());
    assertThat(metrics).extracting("key").contains("custom");
}
Also used : Metrics(org.sonar.api.measures.Metrics) CoreMetrics(org.sonar.api.measures.CoreMetrics) MetricProvider(org.sonar.scanner.bootstrap.MetricProvider) Metric(org.sonar.api.measures.Metric) Test(org.junit.Test)

Example 2 with Metrics

use of org.sonar.api.measures.Metrics in project sonarqube by SonarSource.

the class RegisterMetrics method getPluginMetrics.

@VisibleForTesting
List<Metric> getPluginMetrics() {
    List<Metric> metricsToRegister = newArrayList();
    Map<String, Metrics> metricsByRepository = Maps.newHashMap();
    for (Metrics metrics : metricsRepositories) {
        checkMetrics(metricsByRepository, metrics);
        metricsToRegister.addAll(metrics.getMetrics());
    }
    return metricsToRegister;
}
Also used : Metrics(org.sonar.api.measures.Metrics) CoreMetrics(org.sonar.api.measures.CoreMetrics) Metric(org.sonar.api.measures.Metric) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Metrics

use of org.sonar.api.measures.Metrics in project sonarqube by SonarSource.

the class RegisterMetrics method checkMetrics.

private void checkMetrics(Map<String, Metrics> metricsByRepository, Metrics metrics) {
    for (Metric metric : metrics.getMetrics()) {
        String metricKey = metric.getKey();
        if (CoreMetrics.getMetrics().contains(metric)) {
            throw new IllegalStateException(String.format("Metric [%s] is already defined by SonarQube", metricKey));
        }
        Metrics anotherRepository = metricsByRepository.get(metricKey);
        if (anotherRepository != null) {
            throw new IllegalStateException(String.format("Metric [%s] is already defined by the repository [%s]", metricKey, anotherRepository));
        }
        metricsByRepository.put(metricKey, metrics);
    }
}
Also used : Metrics(org.sonar.api.measures.Metrics) CoreMetrics(org.sonar.api.measures.CoreMetrics) Metric(org.sonar.api.measures.Metric)

Example 4 with Metrics

use of org.sonar.api.measures.Metrics in project sonarqube by SonarSource.

the class RegisterMetricsTest method fail_if_plugin_duplicates_core_metric.

@Test(expected = IllegalStateException.class)
public void fail_if_plugin_duplicates_core_metric() {
    Metrics plugin = new TestMetrics(new Metric.Builder("ncloc", "In plugin", Metric.ValueType.FLOAT).create());
    new RegisterMetrics(dbClient, new Metrics[] { plugin }).start();
}
Also used : Metrics(org.sonar.api.measures.Metrics) CoreMetrics(org.sonar.api.measures.CoreMetrics) Test(org.junit.Test)

Example 5 with Metrics

use of org.sonar.api.measures.Metrics in project sonarqube by SonarSource.

the class RegisterMetricsTest method fail_if_duplicated_plugin_metrics.

@Test(expected = IllegalStateException.class)
public void fail_if_duplicated_plugin_metrics() {
    Metrics plugin1 = new TestMetrics(new Metric.Builder("m1", "In first plugin", Metric.ValueType.FLOAT).create());
    Metrics plugin2 = new TestMetrics(new Metric.Builder("m1", "In second plugin", Metric.ValueType.FLOAT).create());
    new RegisterMetrics(dbClient, new Metrics[] { plugin1, plugin2 }).start();
}
Also used : Metrics(org.sonar.api.measures.Metrics) CoreMetrics(org.sonar.api.measures.CoreMetrics) Test(org.junit.Test)

Aggregations

CoreMetrics (org.sonar.api.measures.CoreMetrics)5 Metrics (org.sonar.api.measures.Metrics)5 Test (org.junit.Test)3 Metric (org.sonar.api.measures.Metric)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MetricProvider (org.sonar.scanner.bootstrap.MetricProvider)1