@Test public void get_metric_by_key() { Metric metric = getMetric("ncloc"); assertThat(metric.getKey()).isEqualTo("ncloc"); }
@Test public void fail_get_unknown_metric_by_key() { expectedException.expect(NoSuchElementException.class); getMetric("unknown"); } }
/** * Constructs a MeasureHolder from a metric key. * * @param metricKey key used to retrieve the metric name for which the MeasureHolder is built */ public MeasureHolder(final String metricKey) { try { this.metricName = CoreMetrics.getMetric(metricKey) .getName() .replace(" (%)", "") .toLowerCase(); } catch (final NoSuchElementException e) { LOGGER.debug("Metric '{}' is not referenced in CoreMetrics.", metricKey, e); this.metricName = metricKey; } this.value = NA; }
@VisibleForTesting String getMetricName(String metricKey) { try { Metric metric = CoreMetrics.getMetric(metricKey); return metric.getName(); } catch (NoSuchElementException e) { LOG.trace("Using key as name for custom metric '{}' due to '{}'", metricKey, e); } return metricKey; }
private static String getMetricName(String metricKey) { try { Metric metric = CoreMetrics.getMetric(metricKey); return metric.getName(); } catch (NoSuchElementException e) { LOGGER.trace("Using key as name for custom metric '{}' due to '{}'", metricKey, e); } return metricKey; }
/** * Constructs a MeasureHolder from a Measure object. * * @param measure used to retrieve the metric name for which the MeasureHolder is built */ @SuppressWarnings("unchecked") public MeasureHolder(final Measure measure) { final Metric<Serializable> metric = CoreMetrics.getMetric(measure.getMetric()); this.metricName = metric.getName() .replace(" (%)", "") .toLowerCase(); String tempValue = null; if (!measure.hasValue()) { if (measure.hasPeriods()) { final PeriodsValue periods = measure.getPeriods(); final PeriodValue periodValue = periods.getPeriodsValue(0); tempValue = periodValue.getValue(); } } else { tempValue = measure.getValue(); } this.value = tempValue == null ? NA : tempValue + (metric.isPercentageType() ? "%" : ""); }