diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2013-12-03 17:57:54 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2013-12-03 17:57:54 +0100 |
commit | 1ff7b7c9b2302379519e18a32309f002a3f66415 (patch) | |
tree | 229484d4d2f7d5fb31ac4bdbabf2ba6b7522397c /sonar-plugin-api | |
parent | bd532fb46ebe51e65e0b5d169a39066665749e51 (diff) | |
download | sonarqube-1ff7b7c9b2302379519e18a32309f002a3f66415.tar.gz sonarqube-1ff7b7c9b2302379519e18a32309f002a3f66415.zip |
Fix quality flaws
Diffstat (limited to 'sonar-plugin-api')
3 files changed, 86 insertions, 9 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasuresFilters.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasuresFilters.java index d42f3c329f3..510806b5c45 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasuresFilters.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasuresFilters.java @@ -76,12 +76,10 @@ public final class MeasuresFilters { return null; } for (Measure measure : measures) { - Characteristic measureCharacteristic = measure.getCharacteristic(); if (measure.getClass().equals(Measure.class) && measure.getMetric().equals(metric) && measure.getPersonId() == null && - measureCharacteristic != null && - measureCharacteristic.equals(characteristic)) { + isSameCharacteristic(measure, characteristic)) { return measure; } } @@ -90,6 +88,12 @@ public final class MeasuresFilters { }; } + private static boolean isSameCharacteristic(Measure measure, final Characteristic characteristic){ + Characteristic measureCharacteristic = measure.getCharacteristic(); + return measureCharacteristic != null && + measureCharacteristic.equals(characteristic); + } + public static MeasuresFilter<Measure> requirement(final Metric metric, final Requirement requirement) { return new MetricFilter<Measure>(metric) { @@ -101,8 +105,7 @@ public final class MeasuresFilters { if (measure.getClass().equals(Measure.class) && measure.getMetric().equals(metric) && measure.getPersonId() == null && - measure.getRequirement() != null && - measure.getRequirement().equals(requirement)) { + isSameRequirement(measure, requirement)) { return measure; } } @@ -111,6 +114,12 @@ public final class MeasuresFilters { }; } + private static boolean isSameRequirement(Measure measure, final Requirement requirement){ + Requirement measureRequirement = measure.getRequirement(); + return measureRequirement != null && + measureRequirement.equals(requirement); + } + /** * @since 2.0 */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkUnit.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkUnit.java index 417666ce282..61a5aeec2a7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkUnit.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/WorkUnit.java @@ -53,15 +53,15 @@ public final class WorkUnit { } public static WorkUnit create(@Nullable Double value, @Nullable String unit) { - unit = StringUtils.defaultIfEmpty(unit, DEFAULT_UNIT); - if (!ArrayUtils.contains(UNITS, unit)) { - throw new IllegalArgumentException("Unit can not be: " + unit + ". Possible values are " + ArrayUtils.toString(UNITS)); + String defaultIfEmptyUnit = StringUtils.defaultIfEmpty(unit, DEFAULT_UNIT); + if (!ArrayUtils.contains(UNITS, defaultIfEmptyUnit)) { + throw new IllegalArgumentException("Unit can not be: " + defaultIfEmptyUnit + ". Possible values are " + ArrayUtils.toString(UNITS)); } double d = value != null ? value : DEFAULT_VALUE; if (d < 0.0) { throw new IllegalArgumentException("Value can not be negative: " + d); } - return new WorkUnit(d, unit); + return new WorkUnit(d, defaultIfEmptyUnit); } public static WorkUnit create() { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkUnitTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkUnitTest.java new file mode 100644 index 00000000000..108f7aa57c2 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/WorkUnitTest.java @@ -0,0 +1,68 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.utils; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class WorkUnitTest { + + @Test + public void create() throws Exception { + WorkUnit workUnit = WorkUnit.create(2.0, "mn"); + assertThat(workUnit.getUnit()).isEqualTo("mn"); + assertThat(workUnit.getValue()).isEqualTo(2.0); + } + + @Test + public void create_default() throws Exception { + WorkUnit workUnit = WorkUnit.create(); + assertThat(workUnit.getUnit()).isEqualTo("d"); + assertThat(workUnit.getValue()).isEqualTo(0.0); + } + + @Test + public void test_equals() throws Exception { + assertThat(WorkUnit.create(2.0, "mn")).isEqualTo(WorkUnit.create(2.0, "mn")); + assertThat(WorkUnit.create(3.0, "mn")).isNotEqualTo(WorkUnit.create(2.0, "mn")); + assertThat(WorkUnit.create(2.0, "h")).isNotEqualTo(WorkUnit.create(2.0, "mn")); + } + + @Test + public void fail_with_bad_unit() throws Exception { + try { + WorkUnit.create(2.0, "z"); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class); + } + } + + @Test + public void fail_with_bad_value() throws Exception { + try { + WorkUnit.create(-2.0, "mn"); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class); + } + } + +} |