aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-12-04 11:41:15 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-12-07 17:26:43 +0100
commit6cf62f0adc9677404df3ff457135591c17ad89a5 (patch)
tree08b31db10e52e92575a6343768f93e46b13266b7 /sonar-plugin-api
parent541c15654fc024534c45710b30b9cfc8ec551d96 (diff)
downloadsonarqube-6cf62f0adc9677404df3ff457135591c17ad89a5.tar.gz
sonarqube-6cf62f0adc9677404df3ff457135591c17ad89a5.zip
SONAR-6939 ability for plugins to override the decimal scale of float
measures
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaContext.java21
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java33
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java82
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaContext.java3
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaData.java3
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/MeanAggregationFormula.java32
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java19
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java68
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildDistributionFormula.java34
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildValuesFormula.java22
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/WeightedMeanAggregationFormula.java39
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/DefaultFormulaDataTest.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java170
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/MeanAggregationFormulaTest.java52
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java15
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java98
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java119
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildValuesFormulaTest.java74
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/WeightedMeanAggregationFormulaTest.java36
19 files changed, 287 insertions, 675 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaContext.java
index 766652028ed..50b1f151b7d 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaContext.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaContext.java
@@ -19,37 +19,42 @@
*/
package org.sonar.api.batch;
+import org.sonar.api.measures.Formula;
import org.sonar.api.measures.FormulaContext;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Resource;
/**
* @since 1.11
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
+@Deprecated
public class DefaultFormulaContext implements FormulaContext {
- private Metric metric;
- private DecoratorContext decoratorContext;
-
public DefaultFormulaContext(Metric metric) {
- this.metric = metric;
}
@Override
public Metric getTargetMetric() {
- return metric;
+ throw fail();
}
@Override
public Resource getResource() {
- return decoratorContext.getResource();
+ throw fail();
}
public void setMetric(Metric metric) {
- this.metric = metric;
+ throw fail();
}
public void setDecoratorContext(DecoratorContext decoratorContext) {
- this.decoratorContext = decoratorContext;
+ throw fail();
+ }
+
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException(
+ "Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java
index 1aa4bc24193..8e218a00c9c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java
@@ -19,52 +19,51 @@
*/
package org.sonar.api.batch;
+import java.util.Collection;
+import org.sonar.api.measures.Formula;
import org.sonar.api.measures.FormulaData;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
/**
* @since 1.11
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
+@Deprecated
public class DefaultFormulaData implements FormulaData {
- private DecoratorContext decoratorContext;
-
- public DefaultFormulaData(DecoratorContext decoratorContext) {
- this.decoratorContext = decoratorContext;
+ public DefaultFormulaData(DecoratorContext unused) {
}
@Override
public Measure getMeasure(Metric metric) {
- return decoratorContext.getMeasure(metric);
+ throw fail();
}
@Override
public <M> M getMeasures(MeasuresFilter<M> filter) {
- return decoratorContext.getMeasures(filter);
+ throw fail();
}
@Override
public Collection<Measure> getChildrenMeasures(MeasuresFilter filter) {
- return decoratorContext.getChildrenMeasures(filter);
+ throw fail();
}
@Override
public Collection<Measure> getChildrenMeasures(Metric metric) {
- return decoratorContext.getChildrenMeasures(metric);
+ throw fail();
}
@Override
public Collection<FormulaData> getChildren() {
- List<FormulaData> result = new ArrayList<>();
- for (DecoratorContext childContext : decoratorContext.getChildren()) {
- result.add(new DefaultFormulaData(childContext));
- }
- return result;
+ throw fail();
+ }
+
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException(
+ "Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java
index f029f438918..2c3af7f5b95 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java
@@ -21,33 +21,15 @@
package org.sonar.api.measures;
import java.util.List;
-import org.sonar.api.resources.ResourceUtils;
-
-import static com.google.common.collect.Lists.newArrayList;
/**
- * Formula used to compute an average for a given metric A, which is the result of the sum of measures of this metric (A) divided by another metric (B).
- * <p/>
- * For example: to compute the metric "complexity by file", the main metric (A) is "complexity" and the other metric (B) is "file".
- *
* @since 3.0
- * @deprecated since 5.2 decorators are no more executed on batch side
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
@Deprecated
public class AverageFormula implements Formula {
- private Metric mainMetric;
- private Metric byMetric;
- private Metric fallbackMetric;
-
- /**
- * This method should be private but it kep package-protected because of AverageComplexityFormula.
- */
- AverageFormula(Metric mainMetric, Metric byMetric) {
- this.mainMetric = mainMetric;
- this.byMetric = byMetric;
- }
-
/**
* Creates a new {@link AverageFormula} class.
*
@@ -55,7 +37,7 @@ public class AverageFormula implements Formula {
* @param by The metric used to divide the main metric to compute average (ex.: "file" for "complexity by file")
*/
public static AverageFormula create(Metric main, Metric by) {
- return new AverageFormula(main, by);
+ return new AverageFormula();
}
/**
@@ -65,8 +47,7 @@ public class AverageFormula implements Formula {
* @since 3.6
*/
public AverageFormula setFallbackForMainMetric(Metric fallbackMetric) {
- this.fallbackMetric = fallbackMetric;
- return this;
+ throw fail();
}
/**
@@ -74,7 +55,7 @@ public class AverageFormula implements Formula {
*/
@Override
public List<Metric> dependsUponMetrics() {
- return fallbackMetric != null ? newArrayList(mainMetric, fallbackMetric, byMetric) : newArrayList(mainMetric, byMetric);
+ throw fail();
}
/**
@@ -82,56 +63,11 @@ public class AverageFormula implements Formula {
*/
@Override
public Measure calculate(FormulaData data, FormulaContext context) {
- if (!shouldDecorateResource(data, context)) {
- return null;
- }
-
- Measure result;
- if (ResourceUtils.isFile(context.getResource())) {
- result = calculateForFile(data, context);
- } else {
- result = calculateOnChildren(data, context);
- }
- return result;
- }
-
- private Measure calculateOnChildren(FormulaData data, FormulaContext context) {
- Measure result = null;
-
- double totalByMeasure = 0;
- double totalMainMeasure = 0;
- boolean hasApplicableChildren = false;
-
- for (FormulaData childrenData : data.getChildren()) {
- Double fallbackMeasure = fallbackMetric != null ? MeasureUtils.getValue(childrenData.getMeasure(fallbackMetric), null) : null;
- Double childrenByMeasure = MeasureUtils.getValue(childrenData.getMeasure(byMetric), null);
- Double childrenMainMeasure = MeasureUtils.getValue(childrenData.getMeasure(mainMetric), fallbackMeasure);
- if (childrenMainMeasure != null && childrenByMeasure != null && childrenByMeasure > 0.0) {
- totalByMeasure += childrenByMeasure;
- totalMainMeasure += childrenMainMeasure;
- hasApplicableChildren = true;
- }
- }
- if (hasApplicableChildren) {
- result = new Measure(context.getTargetMetric(), totalMainMeasure / totalByMeasure);
- }
- return result;
- }
-
- private Measure calculateForFile(FormulaData data, FormulaContext context) {
- Measure result = null;
-
- Double fallbackMeasure = fallbackMetric != null ? MeasureUtils.getValue(data.getMeasure(fallbackMetric), null) : null;
- Double byMeasure = MeasureUtils.getValue(data.getMeasure(byMetric), null);
- Double mainMeasure = MeasureUtils.getValue(data.getMeasure(mainMetric), fallbackMeasure);
- if (mainMeasure != null && byMeasure != null && byMeasure > 0.0) {
- result = new Measure(context.getTargetMetric(), mainMeasure / byMeasure);
- }
-
- return result;
+ throw fail();
}
- private static boolean shouldDecorateResource(FormulaData data, FormulaContext context) {
- return !MeasureUtils.hasValue(data.getMeasure(context.getTargetMetric()));
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException("Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. " +
+ "Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaContext.java
index b3c2fcb524b..2011af669f9 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaContext.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaContext.java
@@ -23,7 +23,10 @@ import org.sonar.api.resources.Resource;
/**
* @since 1.11
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
+@Deprecated
public interface FormulaContext {
Metric getTargetMetric();
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaData.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaData.java
index 8cf5bb54307..459aec61695 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaData.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/FormulaData.java
@@ -23,7 +23,10 @@ import java.util.Collection;
/**
* @since 1.11
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
+@Deprecated
public interface FormulaData {
Measure getMeasure(Metric metric);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeanAggregationFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeanAggregationFormula.java
index f66bf51009d..22647cca1d5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeanAggregationFormula.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeanAggregationFormula.java
@@ -19,21 +19,17 @@
*/
package org.sonar.api.measures;
-import java.util.Collection;
-import java.util.Collections;
import java.util.List;
/**
* @since 2.0
- * @deprecated since 5.2 decorators are no more executed on batch side
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
@Deprecated
public class MeanAggregationFormula implements Formula {
- private boolean forceZeroIfMissingData=false;
-
- public MeanAggregationFormula(boolean forceZeroIfMissingData) {
- this.forceZeroIfMissingData = forceZeroIfMissingData;
+ public MeanAggregationFormula(boolean unused) {
}
public MeanAggregationFormula() {
@@ -42,26 +38,16 @@ public class MeanAggregationFormula implements Formula {
@Override
public List<Metric> dependsUponMetrics() {
- return Collections.emptyList();
+ throw fail();
}
@Override
public Measure calculate(FormulaData data, FormulaContext context) {
- double sum=0.0;
- int count=0;
- boolean hasValue=false;
- Collection<Measure> measures = data.getChildrenMeasures(context.getTargetMetric());
- for (Measure measure : measures) {
- if (MeasureUtils.hasValue(measure)) {
- sum+=measure.getValue();
- count++;
- hasValue=true;
- }
- }
+ throw fail();
+ }
- if (!hasValue && !forceZeroIfMissingData) {
- return null;
- }
- return new Measure(context.getTargetMetric(), (count==0) ? 0.0 : (sum/count));
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException(
+ "Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
index 4f2bef50a9f..adeae251213 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java
@@ -21,8 +21,6 @@ package org.sonar.api.measures;
import com.google.common.annotations.Beta;
import java.io.Serializable;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
import java.util.Date;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
@@ -43,7 +41,9 @@ public class Measure<G extends Serializable> implements Serializable {
/**
* Default precision when saving a float type metric
+ * @deprecated in 5.3. Decimal scale is provided by metric, not by measure.
*/
+ @Deprecated
public static final int DEFAULT_PRECISION = 1;
protected String metricKey;
@@ -322,27 +322,22 @@ public class Measure<G extends Serializable> implements Serializable {
/**
* Sets the measure value with a given precision
*
- * @param v the measure value
- * @param precision the measure value precision
- * @return the measure object instance
+ * @return {@code this}
+ * @deprecated in 5.3. The decimal scale is given by the metric, not by the measure. Anyway this parameter was enforced to 1 before version 5.3.
*/
- public Measure<G> setValue(@Nullable Double v, int precision) {
+ @Deprecated
+ public Measure<G> setValue(@Nullable Double v, int decimalScale) {
if (v != null) {
if (Double.isNaN(v)) {
throw new IllegalArgumentException("Measure value can not be NaN");
}
- this.value = scaleValue(v, precision);
+ this.value = v;
} else {
this.value = null;
}
return this;
}
- private static double scaleValue(double value, int scale) {
- BigDecimal bd = BigDecimal.valueOf(value);
- return bd.setScale(scale, RoundingMode.HALF_UP).doubleValue();
- }
-
/**
* @return the data field of the measure
*/
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java
index 949e0ab603c..8fdb94f44a6 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java
@@ -27,19 +27,33 @@ import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.server.ServerSide;
+import static com.google.common.base.Objects.firstNonNull;
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+
@BatchSide
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ServerSide
public class Metric<G extends Serializable> implements Serializable, org.sonar.api.batch.measure.Metric<G> {
/**
+ * @since 5.3
+ */
+ public static final int DEFAULT_DECIMAL_SCALE = 1;
+
+ /**
+ * The maximum supported value of scale for decimal metrics
+ * @since 5.3
+ */
+ public static final int MAX_DECIMAL_SCALE = 20;
+
+ /**
* A metric bigger value means a degradation
*/
public static final int DIRECTION_WORST = -1;
@@ -126,6 +140,7 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
private Boolean optimizedBestValue;
private Boolean hidden = Boolean.FALSE;
private Boolean deleteHistoricalData;
+ private Integer decimalScale;
private Metric(Builder builder) {
this.key = builder.key;
@@ -143,6 +158,7 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
this.formula = builder.formula;
this.userManaged = builder.userManaged;
this.deleteHistoricalData = builder.deleteHistoricalData;
+ this.decimalScale = builder.decimalScale;
}
/**
@@ -208,9 +224,12 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
this.name = name;
this.qualitative = qualitative;
this.userManaged = userManaged;
- if (ValueType.PERCENT.equals(this.type)) {
+ if (ValueType.PERCENT == this.type) {
this.bestValue = (direction == DIRECTION_BETTER) ? 100.0 : 0.0;
this.worstValue = (direction == DIRECTION_BETTER) ? 0.0 : 100.0;
+ this.decimalScale = DEFAULT_DECIMAL_SCALE;
+ } else if (ValueType.FLOAT == this.type) {
+ this.decimalScale = DEFAULT_DECIMAL_SCALE;
}
}
@@ -492,6 +511,15 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
return deleteHistoricalData;
}
+ /**
+ * Return the number scale if metric type is {@link ValueType#FLOAT}, else {@code null}
+ * @since 5.3
+ */
+ @CheckForNull
+ public Integer getDecimalScale() {
+ return decimalScale;
+ }
+
@Override
public int hashCode() {
return key.hashCode();
@@ -557,6 +585,7 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
private boolean hidden = false;
private boolean userManaged = false;
private boolean deleteHistoricalData = false;
+ private Integer decimalScale = null;
/**
* Creates a new {@link Builder} object.
@@ -566,15 +595,9 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
* @param type the metric type
*/
public Builder(String key, String name, ValueType type) {
- if (StringUtils.isBlank(key)) {
- throw new IllegalArgumentException("Metric key can not be blank");
- }
- if (StringUtils.isBlank(name)) {
- throw new IllegalArgumentException("Metric name can not be blank");
- }
- if (type == null) {
- throw new IllegalArgumentException("Metric type can not be null");
- }
+ checkArgument(isNotBlank(key), "Metric key can not be blank");
+ checkArgument(isNotBlank(name), "Name of metric %s must be set", key);
+ checkArgument(type != null, "Type of metric %s must be set", key);
this.key = key;
this.name = name;
this.type = type;
@@ -647,11 +670,6 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
* When a formula is set, sensors/decorators just need to store measures at a specific level and let Sonar run the formula to store
* measures on the remaining levels.
*
- * @see SumChildDistributionFormula
- * @see SumChildValuesFormula
- * @see MeanAggregationFormula
- * @see WeightedMeanAggregationFormula
- *
* @param f the formula
* @return the builder
*
@@ -741,14 +759,30 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a
}
/**
+ * Scale to be used if the metric has decimal type ({@link ValueType#FLOAT} or {@link ValueType#PERCENT}).
+ * Default is 1. It is not set (({@code null}) on non-decimal metrics.
+ * @since 5.3
+ */
+ public Builder setDecimalScale(int scale) {
+ checkArgument(scale >= 0, "Scale of decimal metric %s must be positive: %d", key, scale);
+ checkArgument(scale <= MAX_DECIMAL_SCALE, "Scale of decimal metric [%s] must be less than or equal %s: %s", key, MAX_DECIMAL_SCALE, scale);
+ this.decimalScale = scale;
+ return this;
+ }
+
+ /**
* Creates a new metric definition based on the properties set on this metric builder.
*
* @return a new {@link Metric} object
*/
public <G extends Serializable> Metric<G> create() {
- if (ValueType.PERCENT.equals(this.type)) {
+ if (ValueType.PERCENT == this.type) {
this.bestValue = (direction == DIRECTION_BETTER) ? 100.0 : 0.0;
this.worstValue = (direction == DIRECTION_BETTER) ? 0.0 : 100.0;
+ this.decimalScale = firstNonNull(decimalScale, DEFAULT_DECIMAL_SCALE);
+
+ } else if (ValueType.FLOAT == this.type) {
+ this.decimalScale = firstNonNull(decimalScale, DEFAULT_DECIMAL_SCALE);
}
return new Metric<>(this);
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildDistributionFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildDistributionFormula.java
index e75a2f3c119..616eb2cbff7 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildDistributionFormula.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildDistributionFormula.java
@@ -19,50 +19,38 @@
*/
package org.sonar.api.measures;
-import java.util.Collection;
-import java.util.Collections;
import java.util.List;
-import org.sonar.api.resources.Scopes;
/**
* @since 2.0
*
* Used to consolidate a distribution measure throughout the resource tree
- * @deprecated since 5.2 decorators are no more executed on batch side
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
@Deprecated
public class SumChildDistributionFormula implements Formula {
- private String minimumScopeToPersist= Scopes.FILE;
-
@Override
public List<Metric> dependsUponMetrics() {
- return Collections.emptyList();
+ throw fail();
}
public String getMinimumScopeToPersist() {
- return minimumScopeToPersist;
+ throw fail();
}
public SumChildDistributionFormula setMinimumScopeToPersist(String s) {
- this.minimumScopeToPersist = s;
- return this;
+ throw fail();
}
@Override
public Measure calculate(FormulaData data, FormulaContext context) {
- Collection<Measure> measures = data.getChildrenMeasures(context.getTargetMetric());
- if (measures == null || measures.isEmpty()) {
- return null;
- }
- RangeDistributionBuilder distribution = new RangeDistributionBuilder(context.getTargetMetric());
- for (Measure measure : measures) {
- distribution.add(measure);
- }
- Measure measure = distribution.build();
- if (!Scopes.isHigherThanOrEquals(context.getResource().getScope(), minimumScopeToPersist)) {
- measure.setPersistenceMode(PersistenceMode.MEMORY);
- }
- return measure;
+ throw fail();
+ }
+
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException(
+ "Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildValuesFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildValuesFormula.java
index 37e7abf6a25..450befdee5a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildValuesFormula.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/SumChildValuesFormula.java
@@ -19,33 +19,31 @@
*/
package org.sonar.api.measures;
-import java.util.Collections;
import java.util.List;
/**
* @since 1.11
- * @deprecated since 5.2 decorators are no more executed on batch side
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
@Deprecated
public class SumChildValuesFormula implements Formula {
- private boolean saveZeroIfNoChildValues;
-
- public SumChildValuesFormula(boolean saveZeroIfNoChildValues) {
- this.saveZeroIfNoChildValues = saveZeroIfNoChildValues;
+ public SumChildValuesFormula(boolean unused) {
}
@Override
public List<Metric> dependsUponMetrics() {
- return Collections.emptyList();
+ throw fail();
}
@Override
public Measure calculate(FormulaData data, FormulaContext context) {
- Double sum = MeasureUtils.sum(saveZeroIfNoChildValues, data.getChildrenMeasures(context.getTargetMetric()));
- if (sum != null) {
- return new Measure(context.getTargetMetric(), sum);
- }
- return null;
+ throw fail();
+ }
+
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException(
+ "Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/WeightedMeanAggregationFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/WeightedMeanAggregationFormula.java
index 7be604edaf6..360eb01ea9e 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/WeightedMeanAggregationFormula.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/WeightedMeanAggregationFormula.java
@@ -19,54 +19,31 @@
*/
package org.sonar.api.measures;
-import java.util.Collections;
import java.util.List;
/**
* @since 2.0
- * @deprecated since 5.2 decorators are no more executed on batch side
+ * @deprecated since 5.2. Aggregation of measures is provided by {@link org.sonar.api.ce.measure.MeasureComputer}. {@link org.sonar.api.batch.Decorator}
+ * and {@link Formula} are no more supported.
*/
@Deprecated
public class WeightedMeanAggregationFormula implements Formula {
- private Metric weightingMetric;
- private boolean zeroIfNoValues=false;
-
public WeightedMeanAggregationFormula(Metric weightingMetric, boolean zeroIfNoValues) {
- this.weightingMetric = weightingMetric;
- if (weightingMetric==null) {
- throw new IllegalArgumentException("Metric can not be null");
- }
- this.zeroIfNoValues = zeroIfNoValues;
}
@Override
public List<Metric> dependsUponMetrics() {
- return Collections.emptyList();
+ throw fail();
}
@Override
public Measure calculate(FormulaData data, FormulaContext context) {
- double sum=0.0;
- double count=0.0;
- boolean hasValue=false;
-
- for (FormulaData child : data.getChildren()) {
- Measure measure = child.getMeasure(context.getTargetMetric());
- Measure weightingMeasure = child.getMeasure(weightingMetric);
- if (MeasureUtils.haveValues(measure, weightingMeasure)) {
- sum += measure.getValue() * weightingMeasure.getValue();
- count += weightingMeasure.getValue();
- hasValue=true;
- }
- }
-
- if (!hasValue && !zeroIfNoValues) {
- return null;
- }
+ throw fail();
+ }
- double result = (Double.doubleToRawLongBits(count)==0L) ? 0.0 : (sum/count);
- return new Measure(context.getTargetMetric(), result);
+ private static RuntimeException fail() {
+ throw new UnsupportedOperationException(
+ "Unsupported since version 5.2. Decorators and formulas are not used anymore for aggregation measures. Please use org.sonar.api.ce.measure.MeasureComputer.");
}
}
-
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/DefaultFormulaDataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/DefaultFormulaDataTest.java
index 5139aa75edb..03f6188eea6 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/DefaultFormulaDataTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/DefaultFormulaDataTest.java
@@ -23,42 +23,22 @@ import org.junit.Test;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
-import java.util.Arrays;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
public class DefaultFormulaDataTest {
- @Test
- public void isDecoratorContextProxy() {
- DecoratorContext context = mock(DecoratorContext.class);
- DefaultFormulaData data = new DefaultFormulaData(context);
-
- data.getChildrenMeasures(any(MeasuresFilter.class));
- verify(context).getChildrenMeasures(any(MeasuresFilter.class));
+ DefaultFormulaData underTest = new DefaultFormulaData(null);
- data.getChildrenMeasures(any(Metric.class));
- verify(context).getChildrenMeasures(any(Metric.class));
-
- data.getMeasures(any(MeasuresFilter.class));
- verify(context).getMeasures(any(MeasuresFilter.class));
-
- data.getMeasure(any(Metric.class));
- verify(context).getMeasure(any(Metric.class));
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_1() {
+ underTest.getChildren();
}
- @Test
- public void getChildren() {
- DecoratorContext context = mock(DecoratorContext.class);
- DecoratorContext child1 = mock(DecoratorContext.class);
- DecoratorContext child2 = mock(DecoratorContext.class);
- when(context.getChildren()).thenReturn(Arrays.asList(child1, child2));
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_2() {
+ underTest.getChildrenMeasures((MeasuresFilter) null);
+ }
- DefaultFormulaData data = new DefaultFormulaData(context);
- assertThat(data.getChildren()).hasSize(2);
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_3() {
+ underTest.getChildrenMeasures((Metric) null);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java
index e4a9c96f57c..cc05ee44bbf 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java
@@ -19,174 +19,24 @@
*/
package org.sonar.api.measures;
-import org.junit.Before;
import org.junit.Test;
-import org.sonar.api.resources.File;
-
-import java.util.List;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class AverageFormulaTest {
- private FormulaContext context;
- private FormulaData data;
+ AverageFormula underTest = new AverageFormula();
- @Before
- public void before() {
- context = mock(FormulaContext.class);
- when(context.getTargetMetric()).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY);
- data = mock(FormulaData.class);
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_1() {
+ underTest.calculate(null, null);
}
- @Test
- public void test_depends_upon_metrics() throws Exception {
- AverageFormula formula = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS);
- assertThat(formula.dependsUponMetrics()).containsOnly(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS);
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_2() {
+ underTest.dependsUponMetrics();
}
- @Test
- public void test_depends_upon_fallback_metric() throws Exception {
- AverageFormula formula = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS).setFallbackForMainMetric(CoreMetrics.COMPLEXITY);
- assertThat(formula.dependsUponMetrics()).containsOnly(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS);
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_3() {
+ underTest.setFallbackForMainMetric(null);
}
-
- @Test
- public void test_average_calculation() {
- List<FormulaData> childrenData = newArrayList();
- FormulaData data1 = mock(FormulaData.class);
- childrenData.add(data1);
- when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0));
- when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 107.0));
-
- FormulaData data2 = mock(FormulaData.class);
- childrenData.add(data2);
- when(data2.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 127.0));
- when(data2.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 233.0));
-
- when(data.getChildren()).thenReturn(childrenData);
-
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
-
- assertThat(measure.getValue()).isEqualTo(2.0);
- }
-
- @Test
- public void should_not_compute_if_not_target_metric() {
- when(data.getMeasure(CoreMetrics.FUNCTION_COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTION_COMPLEXITY, 2.0));
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
- assertThat(measure).isNull();
- }
-
- @Test
- public void test_when_no_children_measures() {
- List<FormulaData> childrenData = newArrayList();
- when(data.getChildren()).thenReturn(childrenData);
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
- assertThat(measure).isNull();
- }
-
- @Test
- public void test_when_no_complexity_measures() {
- List<FormulaData> childrenData = newArrayList();
- FormulaData data1 = mock(FormulaData.class);
- childrenData.add(data1);
- when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0));
-
- when(data.getChildren()).thenReturn(childrenData);
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
-
- assertThat(measure).isNull();
- }
-
- @Test
- public void test_when_no_by_metric_measures() {
- List<FormulaData> childrenData = newArrayList();
- FormulaData data1 = mock(FormulaData.class);
- childrenData.add(data1);
- when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 43.0));
-
- when(data.getChildren()).thenReturn(childrenData);
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
-
- assertThat(measure).isNull();
- }
-
- @Test
- public void test_when_mixed_metrics() {
- List<FormulaData> childrenData = newArrayList();
- FormulaData data1 = mock(FormulaData.class);
- childrenData.add(data1);
- when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0));
- when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 107.0));
-
- FormulaData data2 = mock(FormulaData.class);
- childrenData.add(data2);
- when(data2.getMeasure(CoreMetrics.STATEMENTS)).thenReturn(new Measure(CoreMetrics.STATEMENTS, 127.0));
- when(data2.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 233.0));
-
- when(data.getChildren()).thenReturn(childrenData);
-
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
-
- assertThat(measure.getValue()).isEqualTo(2.5);
- }
-
- @Test
- public void test_calculation_for_file() {
- when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0));
- when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
- when(context.getResource()).thenReturn(File.create("foo"));
-
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
- assertThat(measure.getValue()).isEqualTo(3.0);
- }
-
- @Test
- public void should_use_fallback_metric_when_no_data_on_main_metric_for_file() {
- when(data.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(null);
- when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0));
- when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
- when(context.getResource()).thenReturn(File.create("foo"));
-
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS)
- .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
- .calculate(data, context);
- assertThat(measure.getValue()).isEqualTo(3.0);
- }
-
- @Test
- public void should_use_main_metric_even_if_fallback_metric_provided() {
- when(data.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0));
- when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 42.0));
- when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
- when(context.getResource()).thenReturn(File.create("foo"));
-
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS)
- .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
- .calculate(data, context);
- assertThat(measure.getValue()).isEqualTo(3.0);
- }
-
- @Test
- public void should_use_fallback_metric_when_no_data_on_main_metric_for_children() {
- List<FormulaData> childrenData = newArrayList();
- FormulaData data1 = mock(FormulaData.class);
- childrenData.add(data1);
- when(data1.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(null);
- when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 107.0));
- when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0));
-
- when(data.getChildren()).thenReturn(childrenData);
-
- Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS)
- .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
- .calculate(data, context);
-
- assertThat(measure.getValue()).isEqualTo(2.5);
- }
-
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeanAggregationFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeanAggregationFormulaTest.java
index c1443832c7b..2beec78cdd1 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeanAggregationFormulaTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeanAggregationFormulaTest.java
@@ -19,57 +19,19 @@
*/
package org.sonar.api.measures;
-import org.junit.Before;
import org.junit.Test;
-import java.util.Arrays;
-import java.util.Collections;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
public class MeanAggregationFormulaTest {
- private FormulaContext context;
- private FormulaData data;
-
- @Before
- public void before() {
- context = mock(FormulaContext.class);
- data = mock(FormulaData.class);
- }
-
- @Test
- public void calculateChildrenMean() {
- when(context.getTargetMetric()).thenReturn(CoreMetrics.COVERAGE);
- when(data.getChildrenMeasures(CoreMetrics.COVERAGE)).thenReturn(Arrays.<Measure>asList(newCoverage(100.0), newCoverage(50.0), newCoverage(30.0)));
-
- Measure measure = new MeanAggregationFormula().calculate(data, context);
- assertThat(measure.getValue(), is(60.0));
- }
-
- @Test
- public void doNotForceZero() {
- when(context.getTargetMetric()).thenReturn(CoreMetrics.COVERAGE);
- when(data.getChildrenMeasures(CoreMetrics.COVERAGE)).thenReturn(Collections.<Measure>emptyList());
-
- Measure measure = new MeanAggregationFormula(false).calculate(data, context);
- assertNull(measure);
- }
-
- @Test
- public void forceZero() {
- when(context.getTargetMetric()).thenReturn(CoreMetrics.COVERAGE);
- when(data.getChildrenMeasures(CoreMetrics.COVERAGE)).thenReturn(Collections.<Measure>emptyList());
+ MeanAggregationFormula underTest = new MeanAggregationFormula();
- Measure measure = new MeanAggregationFormula(true).calculate(data, context);
- assertThat(measure.getValue(), is(0.0));
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_1() {
+ underTest.calculate(null, null);
}
- private Measure newCoverage(double val) {
- return new Measure(CoreMetrics.COVERAGE, val);
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_2() {
+ underTest.dependsUponMetrics();
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java
index 9a33b01caab..b671a63e9c7 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java
@@ -46,12 +46,6 @@ public class MeasureTest {
}
@Test
- public void scaleValue() {
- assertThat(new Measure(CoreMetrics.COVERAGE, 80.666666).getValue()).isEqualTo(80.7);
- assertThat(new Measure(CoreMetrics.COVERAGE, 80.666666, 2).getValue()).isEqualTo(80.67);
- }
-
- @Test
public void defaultPersistenceModeIsFull() {
assertThat(new Measure(CoreMetrics.LINES, 32.0).getPersistenceMode()).isEqualTo(PersistenceMode.FULL);
}
@@ -81,15 +75,6 @@ public class MeasureTest {
assertThat(new Measure(CoreMetrics.LINES).setValue(3.6).getIntValue()).isEqualTo(3);
}
- @Test
- public void valuesAreRoundUp() {
- assertThat(new Measure(CoreMetrics.COVERAGE, 5.22222222).getValue()).isEqualTo(5.2);
- assertThat(new Measure(CoreMetrics.COVERAGE, 5.7777777).getValue()).isEqualTo(5.8);
-
- assertThat(new Measure(CoreMetrics.COVERAGE, 5.22222222, 3).getValue()).isEqualTo(5.222);
- assertThat(new Measure(CoreMetrics.COVERAGE, 5.7777777, 3).getValue()).isEqualTo(5.778);
- }
-
/**
* Proper definition of equality for measures is important, because used to store them.
*/
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java
index 24ae4b69be7..0a088f53d3a 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java
@@ -19,55 +19,97 @@
*/
package org.sonar.api.measures;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
-import static org.hamcrest.core.Is.is;
-import static org.hamcrest.core.IsNull.nullValue;
-import static org.junit.Assert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
public class MetricTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Test
public void shouldCreateMetric() {
Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.INT)
- .setDomain("my domain")
- .create();
+ .setDomain("my domain")
+ .create();
- assertThat(metric.getKey(), is("foo"));
- assertThat(metric.getName(), is("Foo"));
- assertThat(metric.getDomain(), is("my domain"));
+ assertThat(metric.getKey()).isEqualTo("foo");
+ assertThat(metric.getName()).isEqualTo("Foo");
+ assertThat(metric.getDomain()).isEqualTo("my domain");
}
@Test
public void shouldCreateMetricWithDefaultValues() {
Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.INT)
- .create();
-
- assertThat(metric.getBestValue(), nullValue());
- assertThat(metric.getDescription(), nullValue());
- assertThat(metric.getWorstValue(), nullValue());
- assertThat(metric.getDirection(), is(Metric.DIRECTION_NONE));
- assertThat(metric.getEnabled(), is(true));
- assertThat(metric.getFormula(), nullValue());
- assertThat(metric.getId(), nullValue());
- assertThat(metric.getUserManaged(), is(false));
- assertThat(metric.isHidden(), is(false));
- assertThat(metric.isOptimizedBestValue(), is(false));
+ .create();
+
+ assertThat(metric.getBestValue()).isNull();
+ assertThat(metric.getDescription()).isNull();
+ assertThat(metric.getWorstValue()).isNull();
+ assertThat(metric.getDirection()).isEqualTo(Metric.DIRECTION_NONE);
+ assertThat(metric.getEnabled()).isTrue();
+ assertThat(metric.getFormula()).isNull();
+ assertThat(metric.getId()).isNull();
+ assertThat(metric.getUserManaged()).isFalse();
+ assertThat(metric.isHidden()).isFalse();
+ assertThat(metric.isOptimizedBestValue()).isFalse();
}
@Test
public void shouldCreatePercentMetricWithDefaultValues() {
Metric better = new Metric.Builder("foo", "Foo", Metric.ValueType.PERCENT)
- .setDirection(Metric.DIRECTION_BETTER)
- .create();
+ .setDirection(Metric.DIRECTION_BETTER)
+ .create();
Metric worst = new Metric.Builder("foo", "Foo", Metric.ValueType.PERCENT)
- .setDirection(Metric.DIRECTION_WORST)
- .create();
+ .setDirection(Metric.DIRECTION_WORST)
+ .create();
+
+ assertThat(better.getBestValue()).isEqualTo(100.0);
+ assertThat(better.getWorstValue()).isEqualTo(0.0);
+ assertThat(worst.getBestValue()).isEqualTo(0.0);
+ assertThat(worst.getWorstValue()).isEqualTo(100.0);
+ }
+
+ @Test
+ public void override_decimal_scale_of_float_metric() {
+ Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.FLOAT)
+ .setDecimalScale(3)
+ .create();
+ assertThat(metric.getDecimalScale()).isEqualTo(3);
+ }
+
+ @Test
+ public void fail_if_decimal_scale_is_greater_than_max_supported_value() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Scale of decimal metric [foo] must be less than or equal 20: 21");
- assertThat(better.getBestValue(), is(100.0));
- assertThat(better.getWorstValue(), is(0.0));
- assertThat(worst.getBestValue(), is(0.0));
- assertThat(worst.getWorstValue(), is(100.0));
+ new Metric.Builder("foo", "Foo", Metric.ValueType.FLOAT)
+ .setDecimalScale(Metric.MAX_DECIMAL_SCALE + 1)
+ .create();
}
+ @Test
+ public void override_decimal_scale_of_percent_metric() {
+ Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.PERCENT)
+ .setDecimalScale(3)
+ .create();
+ assertThat(metric.getDecimalScale()).isEqualTo(3);
+ }
+
+ @Test
+ public void default_decimal_scale_is_1() {
+ Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.FLOAT)
+ .create();
+ assertThat(metric.getDecimalScale()).isEqualTo(1);
+ }
+
+ @Test
+ public void non_decimal_metric_has_no_scale() {
+ Metric metric = new Metric.Builder("foo", "Foo", Metric.ValueType.INT)
+ .create();
+ assertThat(metric.getDecimalScale()).isNull();
+ }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java
index b4d64fd8551..e712da3f312 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java
@@ -19,122 +19,29 @@
*/
package org.sonar.api.measures;
-import com.google.common.collect.Lists;
-import org.junit.Before;
import org.junit.Test;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Scopes;
-
-import java.util.Collections;
-import java.util.List;
-
-import static junit.framework.Assert.assertNull;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class SumChildDistributionFormulaTest {
- SumChildDistributionFormula formula;
- FormulaContext context;
- FormulaData data;
-
- @Before
- public void init() {
- formula = new SumChildDistributionFormula();
- context = mock(FormulaContext.class);
- when(context.getResource()).thenReturn(File.create("foo"));
- data = mock(FormulaData.class);
- }
-
- @Test
- public void testWhenGetChildrenReturnsNull() {
- when(context.getTargetMetric()).thenReturn(new Metric("foo"));
- when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(null);
- assertNull(formula.calculate(data, context));
- }
-
- @Test
- public void testWhenGetChildrenReturnsEmpty() {
- when(context.getTargetMetric()).thenReturn(new Metric("foo"));
- when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(Collections.<Measure>emptyList());
- assertNull(formula.calculate(data, context));
- }
-
- @Test
- public void shouldNotSumDifferentRanges() {
- Metric m = new Metric("foo", Metric.ValueType.DATA);
- when(context.getTargetMetric()).thenReturn(m);
-
- List<Measure> list = Lists.newArrayList(
- new Measure(m, "1=0;2=2;5=0;10=10;20=2"),
- new Measure(m, "1=0;2=2;5=0;10=10;30=3")
- );
- when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
- assertThat(formula.calculate(data, context), nullValue());
- }
- @Test
- public void shouldSumSameIntRanges() {
- Metric m = new Metric("foo", Metric.ValueType.DATA);
- when(context.getTargetMetric()).thenReturn(m);
+ SumChildDistributionFormula underTest = new SumChildDistributionFormula();
- List<Measure> list = Lists.newArrayList(
- new Measure(m, "1=0;2=2;5=0;10=10;20=2"),
- new Measure(m, "1=3;2=2;5=3;10=12;20=0")
- );
- when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
- assertThat(formula.calculate(data, context).getData(), is("1=3;2=4;5=3;10=22;20=2"));
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_1() {
+ underTest.calculate(null, null);
}
- @Test
- public void shouldSumSameDoubleRanges() {
- initContextWithChildren();
- assertThat(formula.calculate(data, context).getData(), is("0.5=3;2.5=6"));
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_2() {
+ underTest.dependsUponMetrics();
}
- @Test
- public void shouldNotPersistWhenScopeLowerThanMinimun() {
- when(context.getResource()).thenReturn(File.create("org/Foo.java"));
-
- initContextWithChildren();
- formula.setMinimumScopeToPersist(Scopes.DIRECTORY);
-
- Measure distribution = formula.calculate(data, context);
- assertThat(distribution.getPersistenceMode().useDatabase(), is(false));
- }
-
- @Test
- public void shouldPersistWhenScopeEqualsMinimun() {
- when(context.getResource()).thenReturn(File.create("org/Foo.java"));
-
- initContextWithChildren();
- formula.setMinimumScopeToPersist(Scopes.FILE);
-
- Measure distribution = formula.calculate(data, context);
- assertThat(distribution.getPersistenceMode().useDatabase(), is(true));
- }
-
- @Test
- public void shouldPersistWhenScopeHigherThanMinimun() {
- when(context.getResource()).thenReturn(Directory.create("org/foo"));
-
- initContextWithChildren();
- formula.setMinimumScopeToPersist(Scopes.FILE);
-
- Measure distribution = formula.calculate(data, context);
- assertThat(distribution.getPersistenceMode().useDatabase(), is(true));
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_3() {
+ underTest.getMinimumScopeToPersist();
}
- private void initContextWithChildren() {
- Metric m = new Metric("foo", Metric.ValueType.DATA);
- when(context.getTargetMetric()).thenReturn(m);
- List<Measure> list = Lists.newArrayList(
- new Measure(m, "0.5=0;2.5=2"),
- new Measure(m, "0.5=3;2.5=4")
- );
- when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_4() {
+ underTest.setMinimumScopeToPersist(null);
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildValuesFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildValuesFormulaTest.java
deleted file mode 100644
index 5bc69ab6684..00000000000
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildValuesFormulaTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.measures;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.Collections;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class SumChildValuesFormulaTest {
- private FormulaContext context;
- private FormulaData data;
-
- @Before
- public void before() {
- context = mock(FormulaContext.class);
- data = mock(FormulaData.class);
- }
-
- @Test
- public void sumChildValues() {
- when(context.getTargetMetric()).thenReturn(CoreMetrics.NCLOC);
- when(data.getChildrenMeasures(CoreMetrics.NCLOC)).thenReturn(
- Arrays.<Measure>asList(new Measure(CoreMetrics.NCLOC, 100.0), new Measure(CoreMetrics.NCLOC, 50.0)));
-
- Measure measure = new SumChildValuesFormula(true).calculate(data, context);
-
- assertThat(measure.getMetric()).isEqualTo(CoreMetrics.NCLOC);
- assertThat(measure.getValue()).isEqualTo(150.0);
- }
-
- @Test
- public void doNotInsertZero() {
- when(context.getTargetMetric()).thenReturn(CoreMetrics.NCLOC);
- when(data.getChildrenMeasures(CoreMetrics.NCLOC)).thenReturn(Collections.<Measure>emptyList());
-
- Measure measure = new SumChildValuesFormula(false).calculate(data, context);
-
- assertThat(measure).isNull();
- }
-
- @Test
- public void doInsertZero() {
- when(context.getTargetMetric()).thenReturn(CoreMetrics.NCLOC);
- when(data.getChildrenMeasures(CoreMetrics.NCLOC)).thenReturn(Collections.<Measure>emptyList());
-
- Measure measure = new SumChildValuesFormula(true).calculate(data, context);
-
- assertThat(measure.getMetric()).isEqualTo(CoreMetrics.NCLOC);
- assertThat(measure.getValue()).isEqualTo(0.0);
- }
-}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/WeightedMeanAggregationFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/WeightedMeanAggregationFormulaTest.java
new file mode 100644
index 00000000000..3aa8d6d50af
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/WeightedMeanAggregationFormulaTest.java
@@ -0,0 +1,36 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.measures;
+
+import org.junit.Test;
+
+public class WeightedMeanAggregationFormulaTest {
+ WeightedMeanAggregationFormula underTest = new WeightedMeanAggregationFormula(null, false);
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_1() {
+ underTest.calculate(null, null);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void fail_if_used_2() {
+ underTest.dependsUponMetrics();
+ }
+}