}
private static Comparable getValueForComparison(Metric metric, String value) {
- if (isADouble(metric)) {
- return Double.parseDouble(value);
- }
- if (isAInteger(metric)) {
- return parseInteger(value);
- }
- if (isAString(metric)) {
- return value;
- }
- if (isABoolean(metric)) {
- return Integer.parseInt(value);
- }
- if (isAWorkDuration(metric)) {
- return Long.parseLong(value);
- }
- throw new NotImplementedException(metric.getType().toString());
+ Comparable valueToCompare = null;
+ try {
+ if (isADouble(metric)) {
+ valueToCompare = Double.parseDouble(value);
+ } else if (isAInteger(metric)) {
+ valueToCompare = parseInteger(value);
+ } else if (isAString(metric)) {
+ valueToCompare = value;
+ } else if (isABoolean(metric)) {
+ valueToCompare = Integer.parseInt(value);
+ } else if (isAWorkDuration(metric)) {
+ valueToCompare = Long.parseLong(value);
+ } else {
+ throw new NotImplementedException(metric.getType().toString());
+ }
+ } catch (NumberFormatException badValueFormat) {
+ throw new IllegalArgumentException(String.format("Unable to parse value '%s' to compare against %s", value, metric.getName()));
+ }
+ return valueToCompare;
}
private static Comparable<Integer> parseInteger(String value) {
}
private static Double getValue(ResolvedCondition condition, Measure measure) {
- if (condition.period() == null) {
+ Integer period = condition.period();
+ if (period == null) {
return measure.getValue();
- } else if (condition.period() == 1) {
+ } else if (period == 1) {
return measure.getVariation1();
- } else if (condition.period() == 2) {
+ } else if (period == 2) {
return measure.getVariation2();
- } else if (condition.period() == 3) {
+ } else if (period == 3) {
return measure.getVariation3();
} else {
- throw new IllegalStateException("Following index period is not allowed : " + Double.toString(condition.period()));
+ throw new IllegalStateException("Following index period is not allowed : " + Double.toString(period));
}
}
}
package org.sonar.batch.qualitygate;
-import org.junit.Assert;
+import org.apache.commons.lang.NotImplementedException;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.core.qualitygate.db.QualityGateConditionDto;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
when(condition.errorThreshold()).thenReturn("20");
ConditionUtils.getLevel(condition, measure);
} catch (NumberFormatException ex) {
- Assert.fail();
+ fail();
}
try {
when(condition.errorThreshold()).thenReturn("20.1");
ConditionUtils.getLevel(condition, measure);
} catch (NumberFormatException ex) {
- Assert.fail();
+ fail();
}
try {
when(condition.errorThreshold()).thenReturn("20.1");
ConditionUtils.getLevel(condition, measure);
} catch (NumberFormatException ex) {
- Assert.fail();
+ fail();
}
}
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn("10.1");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
metric.setType(Metric.ValueType.STRING);
measure.setData("TEST");
measure.setValue(null);
when(condition.errorThreshold()).thenReturn("TEST");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn("TEST2");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
}
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
when(condition.errorThreshold()).thenReturn("10.1");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
metric.setType(Metric.ValueType.STRING);
measure.setData("TEST");
measure.setValue(null);
when(condition.errorThreshold()).thenReturn("TEST");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
when(condition.errorThreshold()).thenReturn("TEST2");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
}
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.1");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn("10.3");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.1");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
when(condition.errorThreshold()).thenReturn("10.3");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn(Metric.Level.ERROR.toString());
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn(Metric.Level.OK.toString());
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
when(condition.operator()).thenReturn(QualityGateConditionDto.OPERATOR_NOT_EQUALS);
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("1");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
when(condition.errorThreshold()).thenReturn("0");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.operator()).thenReturn(QualityGateConditionDto.OPERATOR_NOT_EQUALS);
when(condition.errorThreshold()).thenReturn("1");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn("0");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
+
+ when(condition.errorThreshold()).thenReturn("polop");
+ try {
+ ConditionUtils.getLevel(condition, measure);
+ fail();
+ } catch(Exception expected) {
+ assertThat(expected).isInstanceOf(IllegalArgumentException.class).hasMessage("Unable to parse value 'polop' to compare against name");
+ }
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("60");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
+
+ when(condition.errorThreshold()).thenReturn("polop");
+ try {
+ ConditionUtils.getLevel(condition, measure);
+ fail();
+ } catch(Exception expected) {
+ assertThat(expected).isInstanceOf(IllegalArgumentException.class).hasMessage("Unable to parse value 'polop' to compare against name");
+ }
}
@Test
when(condition.metric()).thenReturn(metric);
when(condition.errorThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.ERROR, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.ERROR);
when(condition.errorThreshold()).thenReturn("10.1");
- Assert.assertEquals(Metric.Level.OK, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.OK);
when(condition.errorThreshold()).thenReturn("10.3");
when(condition.warningThreshold()).thenReturn("10.2");
- Assert.assertEquals(Metric.Level.WARN, ConditionUtils.getLevel(condition, measure));
+ assertThat(ConditionUtils.getLevel(condition, measure)).isEqualTo(Metric.Level.WARN);
+ }
+
+ @Test
+ public void testUnsupportedType() {
+ metric.setType(Metric.ValueType.DATA);
+ measure.setValue(3.14159265358);
+ when(condition.operator()).thenReturn(QualityGateConditionDto.OPERATOR_EQUALS);
+ when(condition.metric()).thenReturn(metric);
+
+ when(condition.errorThreshold()).thenReturn("1.60217657");
+ try {
+ ConditionUtils.getLevel(condition, measure);
+ fail();
+ } catch (Exception expected) {
+ assertThat(expected).isInstanceOf(NotImplementedException.class);
+ }
}
+
}