org.sonar.api.measures.Metric Java Examples
The following examples show how to use
org.sonar.api.measures.Metric.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnalysisDetails.java From sonarqube-community-branch-plugin with GNU General Public License v3.0 | 6 votes |
public static String format(QualityGate.Condition condition) { Metric<?> metric = CoreMetrics.getMetric(condition.getMetricKey()); if (metric.getType() == Metric.ValueType.RATING) { return String .format("%s %s (%s %s)", Rating.valueOf(Integer.parseInt(condition.getValue())), metric.getName(), condition.getOperator() == QualityGate.Operator.GREATER_THAN ? "is worse than" : "is better than", Rating.valueOf(Integer.parseInt(condition.getErrorThreshold()))); } else if (metric.getType() == Metric.ValueType.PERCENT) { NumberFormat numberFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); return String.format("%s%% %s (%s %s%%)", numberFormat.format(new BigDecimal(condition.getValue())), metric.getName(), condition.getOperator() == QualityGate.Operator.GREATER_THAN ? "is greater than" : "is less than", numberFormat.format(new BigDecimal(condition.getErrorThreshold()))); } else { return String.format("%s %s (%s %s)", condition.getValue(), metric.getName(), condition.getOperator() == QualityGate.Operator.GREATER_THAN ? "is greater than" : "is less than", condition.getErrorThreshold()); } }
Example #2
Source File: MeasureHolder.java From qualinsight-plugins-sonarqube-badges with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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() ? "%" : ""); }
Example #3
Source File: SmellCountByTypeMeasuresComputerTest.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 6 votes |
@Test @Parameters public void decorate_should_saveExpectedMeasureTotal_when_usingAnyMetric(final Metric<Integer> metric, final Collection<Measure> measures) { Mockito.when(this.context.getComponent()) .thenReturn(this.component); Mockito.when(this.component.getType()) .thenReturn(Type.PROJECT); Mockito.when(this.context.getChildrenMeasures(Matchers.eq(metric.getKey()))) .thenReturn(measures); Mockito.when(this.context.getChildrenMeasures(AdditionalMatchers.not(Matchers.eq(metric.getKey())))) .thenReturn(null); final AbstractSmellMeasureComputer sut = sut(); sut.compute(this.context); Mockito.verify(this.context, Mockito.times(1)) .getComponent(); Mockito.verify(this.context, Mockito.times(1)) .getChildrenMeasures(Matchers.eq(metric.getKey())); Mockito.verify(this.context, Mockito.times(1)) .addMeasure(Matchers.eq(metric.getKey()), Matchers.eq(3)); Mockito.verify(this.context, Mockito.times(sut.getInputMetricsKeys() .size() - 1)) .addMeasure(AdditionalMatchers.not(Matchers.eq(metric.getKey())), Matchers.eq(0)); }
Example #4
Source File: AbstractSmellMeasureComputer.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 6 votes |
/** * {@link Aggregator} contructor. * * @param context {@link MeasureComputerContext} to which aggregated result has to be saved. * @param metricKey key of the {@link Metric} for which {@link Measure}s are aggregated. */ public Aggregator(final MeasureComputerContext context, final String metricKey) { this.context = context; this.metricKey = metricKey; final Metric<Serializable> metric = SmellMetrics.metricFor(metricKey); this.valueType = Preconditions.checkNotNull(metric, "No Metric could be found for metric key '{}'", metricKey) .getType(); switch (this.valueType) { case INT: this.value = Integer.valueOf(0); break; case WORK_DUR: this.value = Long.valueOf(0); break; default: throw new UnsupportedOperationException(); } }
Example #5
Source File: SmellMetricsTest.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 6 votes |
@Test @Parameters public void getMetrics_should_return_correctlyConfiguredMetrics(final Metric<Integer> metric, final String expectedKey, final String expectedName, final ValueType expectedValueType, final Double expectedBestValue, final String expectedDescription, final Integer expectedDirection, final String expectedDomain) { final SoftAssertions softly = new SoftAssertions(); softly.assertThat(metric.getKey()) .isEqualTo(expectedKey); softly.assertThat(metric.getName()) .isEqualTo(expectedName); softly.assertThat(metric.getType()) .isEqualTo(expectedValueType); softly.assertThat(metric.getBestValue()) .isEqualTo(expectedBestValue); softly.assertThat(metric.getDescription()) .isEqualTo(expectedDescription); softly.assertThat(metric.getDirection()) .isEqualTo(expectedDirection); softly.assertThat(metric.getDomain()) .isEqualTo(expectedDomain); softly.assertAll(); }
Example #6
Source File: AbstractMetricsVisitor.java From sonar-css-plugin with GNU Lesser General Public License v3.0 | 5 votes |
protected <T extends Serializable> void saveMetricOnFile(Metric metric, T value) { sensorContext.<T>newMeasure() .withValue(value) .forMetric(metric) .on(inputFile) .save(); }
Example #7
Source File: MetricsVisitor.java From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 | 5 votes |
private <T extends Serializable> void saveMetricOnFile(Metric metric, T value) { sensorContext.<T>newMeasure() .withValue(value) .forMetric(metric) .on(inputFile) .save(); }
Example #8
Source File: MetricsSaver.java From AEM-Rules-for-SonarQube with Apache License 2.0 | 5 votes |
public static void saveMeasures(SensorContext context, HtmlSourceCode sourceCode) { InputFile inputFile = sourceCode.inputFile(); for (Map.Entry<Metric<Integer>, Integer> entry : sourceCode.getMeasures().entrySet()) { context.<Integer>newMeasure() .on(inputFile) .forMetric(entry.getKey()) .withValue(entry.getValue()) .save(); } }
Example #9
Source File: SmellMeasurerTest.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void measure_with_annotatedFile_should_saveExpectedMeasuresThatHaveLineBreaksInAnnotations() { Mockito.when(this.inputFile.file()) .thenReturn(new File("src/test/resources/SmellMeasurerTest_5.java")); Mockito.when(this.sensorContext.newMeasure()) .thenReturn(this.measure); Mockito.when(this.measure.forMetric(Mockito.any())) .thenReturn(this.measure); Mockito.when(this.measure.withValue(Mockito.any())) .thenReturn(this.measure); Mockito.when(this.measure.on(Mockito.any())) .thenReturn(this.measure); final SmellMeasurer sut = new SmellMeasurer(this.sensorContext); sut.measure(this.inputFile); // different metrics should be saved, one for each SmellType final ArgumentCaptor<Metric> metricCaptor = ArgumentCaptor.forClass(Metric.class); final ArgumentCaptor<Serializable> valueCaptor = ArgumentCaptor.forClass(Serializable.class); // 2 because of : SMELL_COUNT, SMELL_DEBT Mockito.verify(this.sensorContext, Mockito.times(2 + SmellType.values().length)) .newMeasure(); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .save(); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .forMetric(metricCaptor.capture()); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .withValue(valueCaptor.capture()); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .on(Mockito.any()); final List<Metric> capturedMetrics = metricCaptor.getAllValues(); final List<Serializable> capturedValues = valueCaptor.getAllValues(); int i = 0; while (i < EXPECTED_SMELL_TYPE_COUNT) { assertEquals(Integer.valueOf(1), capturedValues.get(i++)); } assertEquals(SmellMetrics.SMELL_COUNT, capturedMetrics.get(i)); assertEquals(Integer.valueOf(EXPECTED_SMELL_TYPE_COUNT), capturedValues.get(i++)); assertEquals(SmellMetrics.SMELL_DEBT, capturedMetrics.get(i)); assertEquals(Long.valueOf(EXPECTED_SMELL_TYPE_DEBT), capturedValues.get(i++)); Mockito.verifyNoMoreInteractions(this.sensorContext); }
Example #10
Source File: SmellMeasurerTest.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void measure_with_annotatedFile_should_saveExpectedMeasures() { Mockito.when(this.inputFile.file()) .thenReturn(new File("src/test/resources/SmellMeasurerTest_1.java")); Mockito.when(this.sensorContext.newMeasure()) .thenReturn(this.measure); Mockito.when(this.measure.forMetric(Mockito.any())) .thenReturn(this.measure); Mockito.when(this.measure.withValue(Mockito.any())) .thenReturn(this.measure); Mockito.when(this.measure.on(Mockito.any())) .thenReturn(this.measure); final SmellMeasurer sut = new SmellMeasurer(this.sensorContext); sut.measure(this.inputFile); final ArgumentCaptor<Metric> metricCaptor = ArgumentCaptor.forClass(Metric.class); final ArgumentCaptor<Serializable> valueCaptor = ArgumentCaptor.forClass(Serializable.class); // 2 because of : SMELL_COUNT, SMELL_DEBT Mockito.verify(this.sensorContext, Mockito.times(2 + SmellType.values().length)) .newMeasure(); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .save(); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .forMetric(metricCaptor.capture()); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .withValue(valueCaptor.capture()); Mockito.verify(this.measure, Mockito.times(2 + SmellType.values().length)) .on(Mockito.any()); final List<Metric> capturedMetrics = metricCaptor.getAllValues(); final List<Serializable> capturedValues = valueCaptor.getAllValues(); int i = 0; while (i < EXPECTED_SMELL_TYPE_COUNT) { assertEquals(Integer.valueOf(1), capturedValues.get(i++)); } assertEquals(SmellMetrics.SMELL_COUNT, capturedMetrics.get(i)); assertEquals(Integer.valueOf(EXPECTED_SMELL_TYPE_COUNT), capturedValues.get(i++)); assertEquals(SmellMetrics.SMELL_DEBT, capturedMetrics.get(i)); assertEquals(Long.valueOf(EXPECTED_SMELL_TYPE_DEBT), capturedValues.get(i++)); Mockito.verifyNoMoreInteractions(this.sensorContext); }
Example #11
Source File: MetricsVisitor.java From sonar-esql-plugin with Apache License 2.0 | 4 votes |
private <T extends Serializable> void saveMetricOnFile(Metric metric, T value) { sensorContext.<T>newMeasure().withValue(value).forMetric(metric).on(inputFile).save(); }
Example #12
Source File: EsqlMetrics.java From sonar-esql-plugin with Apache License 2.0 | 4 votes |
@Override public List<Metric> getMetrics() { return metrics; }
Example #13
Source File: SmellMetricsTest.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 4 votes |
@Test @Parameters public void metricFor_should_return_expectedMetric(final SmellType type, final Metric<Integer> expectedMetric) { Assertions.assertThat(SmellMetrics.metricFor(type)) .isEqualTo(expectedMetric); }
Example #14
Source File: SmellMeasuresSensorTest.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 4 votes |
@Test public void analyse_should_saveMeasures() throws IOException { final SmellMeasuresSensor sut = new SmellMeasuresSensor(this.fs); final File testFile = new File("src/test/resources/SmellMeasuresSensorTest_1.java"); Mockito.when(this.fs.inputFiles(this.predicate)) .thenReturn(ImmutableList.<InputFile> of(this.inputFile)); Mockito.when(this.inputFile.file()) .thenReturn(testFile); Mockito.when(this.inputFile.absolutePath()) .thenReturn(testFile.getAbsolutePath()); Mockito.when(this.context.newMeasure()) .thenReturn(this.measure); Mockito.when(this.measure.forMetric(Mockito.any())) .thenReturn(this.measure); Mockito.when(this.measure.withValue(Mockito.any())) .thenReturn(this.measure); Mockito.when(this.measure.on(Mockito.any())) .thenReturn(this.measure); sut.execute(this.context); Mockito.verify(this.inputFile, Mockito.times(1)) .file(); final ArgumentCaptor<Metric> metricCaptor = ArgumentCaptor.forClass(Metric.class); final ArgumentCaptor<Serializable> valueCaptor = ArgumentCaptor.forClass(Serializable.class); // 3 because of : SMELL_COUNT_ANTI_PATTERN, SMELL_COUNT, SMELL_DEBT Mockito.verify(this.context, Mockito.times(3)) .newMeasure(); Mockito.verify(this.measure, Mockito.times(3)) .save(); Mockito.verify(this.measure, Mockito.times(3)) .forMetric(metricCaptor.capture()); final List<Metric> capturedMetrics = metricCaptor.getAllValues(); assertEquals(SmellMetrics.SMELL_COUNT_ANTI_PATTERN, capturedMetrics.get(0)); assertEquals(SmellMetrics.SMELL_COUNT, capturedMetrics.get(1)); assertEquals(SmellMetrics.SMELL_DEBT, capturedMetrics.get(2)); Mockito.verify(this.measure, Mockito.times(3)) .withValue(valueCaptor.capture()); final List<Serializable> capturedValues = valueCaptor.getAllValues(); assertEquals(Integer.valueOf(7), capturedValues.get(0)); assertEquals(Integer.valueOf(7), capturedValues.get(1)); assertEquals(Long.valueOf(70), capturedValues.get(2)); Mockito.verify(this.measure, Mockito.times(3)) .on(Mockito.any()); Mockito.verifyNoMoreInteractions(this.context); }
Example #15
Source File: SmellMetrics.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 4 votes |
@SuppressWarnings("rawtypes") @Override public List<Metric> getMetrics() { return ImmutableList.<Metric> copyOf(SMELL_METRICS); }
Example #16
Source File: LicenseCheckMetrics.java From sonarqube-licensecheck with Apache License 2.0 | 4 votes |
@Override public List<Metric> getMetrics() { return Arrays.asList(DEPENDENCY, LICENSE); }
Example #17
Source File: SmellMetrics.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 2 votes |
/** * Returns a {@link List} of all {@link Metric}s handled by the Smells plugin. * * @return {@link List} of {@link Metric} */ @CheckForNull public static final List<Metric<Serializable>> metrics() { return ImmutableList.<Metric<Serializable>> copyOf(SMELL_METRICS); }
Example #18
Source File: SmellMetrics.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 2 votes |
/** * Returns a {@link Metric} from a {@link SmellType} * * @param metricKey Key to convert to a metric * @return a {@link Metric} corresponding to the key */ @CheckForNull public static final Metric<Serializable> metricFor(final String metricKey) { return SMELL_METRICS_BY_KEY.get(metricKey); }
Example #19
Source File: SmellMetrics.java From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 | 2 votes |
/** * Returns a {@link Metric} from a {@link SmellType} * * @param type {@link SmellType} to convert to a metric * @return a {@link Metric} corresponding to the {@link SmellType} */ @CheckForNull public static final Metric<Serializable> metricFor(final SmellType type) { return SMELL_METRICS_BY_TYPE.get(type); }