Skip to content

Commit

Permalink
Fix quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed May 18, 2016
1 parent b1a9efa commit 26883ae
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 29 deletions.
Expand Up @@ -19,9 +19,8 @@
*/
package org.sonar.server.computation.measure.api;

import com.google.common.collect.ImmutableSet;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import javax.annotation.concurrent.Immutable;
import org.sonar.api.ce.measure.Measure;

Expand All @@ -36,7 +35,7 @@
@Immutable
public class MeasureImpl implements Measure {

private static final Set<org.sonar.server.computation.measure.Measure.ValueType> ALLOWED_VALUE_TYPES = ImmutableSet.of(INT, LONG, DOUBLE, STRING, BOOLEAN);
private static final EnumSet<org.sonar.server.computation.measure.Measure.ValueType> ALLOWED_VALUE_TYPES = EnumSet.of(INT, LONG, DOUBLE, STRING, BOOLEAN);

private final org.sonar.server.computation.measure.Measure measure;

Expand Down Expand Up @@ -79,8 +78,7 @@ private void checkValueType(org.sonar.server.computation.measure.Measure.ValueTy
checkState(measure.getValueType() == expected, String.format(
"Value can not be converted to %s because current value type is a %s",
expected.toString().toLowerCase(Locale.US),
measure.getValueType()
));
measure.getValueType()));
}

@Override
Expand Down
Expand Up @@ -22,8 +22,6 @@
import com.google.common.base.Optional;
import org.sonar.api.ce.measure.Issue;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.Severity;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.server.computation.component.Component;
Expand Down Expand Up @@ -270,14 +268,6 @@ private void addSecurityRating(Rating rating) {
}
}

private static long getEffortForNotMinorIssue(Issue issue) {
Duration effort = issue.effort();
if (!issue.severity().equals(Severity.INFO) && effort != null) {
return effort.toMinutes();
}
return 0L;
}

private static Rating getRatingFromSeverity(String severity) {
switch (severity) {
case BLOCKER:
Expand Down
Expand Up @@ -21,7 +21,6 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.io.Resources;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -240,8 +239,4 @@ private static void appendContext(String context, StringBuilder res) {
}
}

private String get(String key, String defaultValue) {
return Objects.firstNonNull(settings.getString(key), defaultValue);
}

}
Expand Up @@ -288,7 +288,7 @@ private void validateCondition(Metric metric, String operator, @Nullable String
}
}

private void checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(Collection<QualityGateConditionDto> conditions, Metric metric, @Nullable final Integer period) {
private static void checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(Collection<QualityGateConditionDto> conditions, Metric metric, @Nullable final Integer period) {
if (conditions.isEmpty()) {
return;
}
Expand All @@ -302,32 +302,32 @@ private void checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(Collection<Q
}
}

private void checkPeriod(Metric metric, @Nullable Integer period, Errors errors) {
private static void checkPeriod(Metric metric, @Nullable Integer period, Errors errors) {
if (period == null) {
errors.check(!metric.getKey().startsWith("new_"), "A period must be selected for differential metrics.");
} else {
errors.check(period == 1, "The only valid quality gate period is 1, the leak period.");
}
}

private void checkThresholds(@Nullable String warningThreshold, @Nullable String errorThreshold, Errors errors) {
private static void checkThresholds(@Nullable String warningThreshold, @Nullable String errorThreshold, Errors errors) {
errors.check(warningThreshold != null || errorThreshold != null, "At least one threshold (warning, error) must be set.");
}

private void checkOperator(Metric metric, String operator, Errors errors) {
private static void checkOperator(Metric metric, String operator, Errors errors) {
errors
.check(QualityGateConditionDto.isOperatorAllowed(operator, metric.getType()), format("Operator %s is not allowed for metric type %s.", operator, metric.getType()));
}

private void validateMetric(Metric metric, Errors errors) {
private static void validateMetric(Metric metric, Errors errors) {
errors.check(isAlertable(metric), format("Metric '%s' cannot be used to define a condition.", metric.getKey()));
}

private boolean isAvailableForInit(Metric metric) {
private static boolean isAvailableForInit(Metric metric) {
return !metric.isDataType() && !CoreMetrics.ALERT_STATUS.equals(metric) && ValueType.RATING != metric.getType();
}

private boolean isAlertable(Metric metric) {
private static boolean isAlertable(Metric metric) {
return isAvailableForInit(metric) && BooleanUtils.isFalse(metric.isHidden());
}

Expand Down
Expand Up @@ -109,15 +109,15 @@ public void fail_with_ISE_when_not_boolean_value() throws Exception {
@Test
public void fail_with_ISE_when_creating_measure_with_no_value() throws Exception {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Only following types are allowed [INT, LONG, DOUBLE, STRING, BOOLEAN]");
thrown.expectMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]");

new MeasureImpl(Measure.newMeasureBuilder().createNoValue());
}

@Test
public void fail_with_ISE_when_creating_measure_with_not_allowed_value() throws Exception {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Only following types are allowed [INT, LONG, DOUBLE, STRING, BOOLEAN]");
thrown.expectMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]");

new MeasureImpl(Measure.newMeasureBuilder().create(Measure.Level.ERROR));
}
Expand Down

0 comments on commit 26883ae

Please sign in to comment.