]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3898 API: add methods getFloat() and getDouble() to org.sonar.api.config.Settings
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 22 Oct 2012 16:19:05 +0000 (18:19 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 22 Oct 2012 16:19:05 +0000 (18:19 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java

index 3ec41a5cf6e79594bb8f44c867db3635778902fc..307baa2e5fc639ac9b10d16e0b67a3e42de597a8 100644 (file)
@@ -156,6 +156,30 @@ public class Settings implements BatchComponent, ServerComponent {
     return null;
   }
 
+  public final Float getFloat(String key) {
+    String value = getString(key);
+    if (StringUtils.isNotEmpty(value)) {
+      try {
+        return Float.valueOf(value);
+      } catch (NumberFormatException e) {
+        throw new IllegalStateException(String.format("The property '%s' is not a float value", key));
+      }
+    }
+    return null;
+  }
+
+  public final Double getDouble(String key) {
+    String value = getString(key);
+    if (StringUtils.isNotEmpty(value)) {
+      try {
+        return Double.valueOf(value);
+      } catch (NumberFormatException e) {
+        throw new IllegalStateException(String.format("The property '%s' is not a double value", key));
+      }
+    }
+    return null;
+  }
+
   /**
    * Value is split by comma and trimmed.
    * <p/>
@@ -272,19 +296,23 @@ public class Settings implements BatchComponent, ServerComponent {
   }
 
   public final Settings setProperty(String key, @Nullable Boolean value) {
-    return setProperty(key, String.valueOf(value));
+    return setProperty(key, value==null ? null : String.valueOf(value));
   }
 
   public final Settings setProperty(String key, @Nullable Integer value) {
-    return setProperty(key, String.valueOf(value));
+    return setProperty(key, value==null ? null : String.valueOf(value));
   }
 
   public final Settings setProperty(String key, @Nullable Long value) {
-    return setProperty(key, String.valueOf(value));
+    return setProperty(key, value==null ? null : String.valueOf(value));
   }
 
   public final Settings setProperty(String key, @Nullable Double value) {
-    return setProperty(key, String.valueOf(value));
+    return setProperty(key, value==null ? null : String.valueOf(value));
+  }
+
+  public final Settings setProperty(String key, @Nullable Float value) {
+    return setProperty(key, value==null ? null : String.valueOf(value));
   }
 
   public final Settings setProperty(String key, @Nullable Date date) {
index 2e5259a4995e81f164244bea3e1cac4b6b398425..8be0edeca61b2837edb5ad6f151024b66b2a979c 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.config;
 
 import com.google.common.collect.ImmutableMap;
+import org.fest.assertions.Delta;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -149,57 +150,111 @@ public class SettingsTest {
     assertThat(settings.getDateTime("datetime").getMinutes()).isEqualTo(50);
   }
 
+  @Test
+  public void testGetDouble() {
+    Settings settings = new Settings();
+    settings.setProperty("from_double", 3.14159);
+    settings.setProperty("from_string", "3.14159");
+    assertThat(settings.getDouble("from_double")).isEqualTo(3.14159, Delta.delta(0.00001));
+    assertThat(settings.getDouble("from_string")).isEqualTo(3.14159, Delta.delta(0.00001));
+    assertThat(settings.getDouble("unknown")).isNull();
+  }
+
+  @Test
+  public void testGetFloat() {
+    Settings settings = new Settings();
+    settings.setProperty("from_float", 3.14159f);
+    settings.setProperty("from_string", "3.14159");
+    assertThat(settings.getDouble("from_float")).isEqualTo(3.14159f, Delta.delta(0.00001));
+    assertThat(settings.getDouble("from_string")).isEqualTo(3.14159f, Delta.delta(0.00001));
+    assertThat(settings.getDouble("unknown")).isNull();
+  }
+
+  @Test
+  public void testGetBadFloat() {
+    Settings settings = new Settings();
+    settings.setProperty("foo", "bar");
+
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("The property 'foo' is not a float value");
+    settings.getFloat("foo");
+  }
+
+  @Test
+  public void testGetBadDouble() {
+    Settings settings = new Settings();
+    settings.setProperty("foo", "bar");
+
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("The property 'foo' is not a double value");
+    settings.getDouble("foo");
+  }
+
+  @Test
+  public void testSetNullFloat() {
+    Settings settings = new Settings();
+    settings.setProperty("foo", (Float) null);
+    assertThat(settings.getFloat("foo")).isNull();
+  }
+
+  @Test
+  public void testSetNullDouble() {
+    Settings settings = new Settings();
+    settings.setProperty("foo", (Double) null);
+    assertThat(settings.getDouble("foo")).isNull();
+  }
+
   @Test
   public void getStringArray() {
     Settings settings = new Settings(definitions);
     String[] array = settings.getStringArray("array");
-    assertThat(array).isEqualTo(new String[] {"one", "two", "three"});
+    assertThat(array).isEqualTo(new String[]{"one", "two", "three"});
   }
 
   @Test
   public void setStringArray() {
     Settings settings = new Settings(definitions);
-    settings.setProperty("multi_values", new String[] {"A", "B"});
+    settings.setProperty("multi_values", new String[]{"A", "B"});
     String[] array = settings.getStringArray("multi_values");
-    assertThat(array).isEqualTo(new String[] {"A", "B"});
+    assertThat(array).isEqualTo(new String[]{"A", "B"});
   }
 
   @Test
   public void setStringArrayTrimValues() {
     Settings settings = new Settings(definitions);
-    settings.setProperty("multi_values", new String[] {" A ", " B "});
+    settings.setProperty("multi_values", new String[]{" A ", " B "});
     String[] array = settings.getStringArray("multi_values");
-    assertThat(array).isEqualTo(new String[] {"A", "B"});
+    assertThat(array).isEqualTo(new String[]{"A", "B"});
   }
 
   @Test
   public void setStringArrayEscapeCommas() {
     Settings settings = new Settings(definitions);
-    settings.setProperty("multi_values", new String[] {"A,B", "C,D"});
+    settings.setProperty("multi_values", new String[]{"A,B", "C,D"});
     String[] array = settings.getStringArray("multi_values");
-    assertThat(array).isEqualTo(new String[] {"A,B", "C,D"});
+    assertThat(array).isEqualTo(new String[]{"A,B", "C,D"});
   }
 
   @Test
   public void setStringArrayWithEmptyValues() {
     Settings settings = new Settings(definitions);
-    settings.setProperty("multi_values", new String[] {"A,B", "", "C,D"});
+    settings.setProperty("multi_values", new String[]{"A,B", "", "C,D"});
     String[] array = settings.getStringArray("multi_values");
-    assertThat(array).isEqualTo(new String[] {"A,B", "", "C,D"});
+    assertThat(array).isEqualTo(new String[]{"A,B", "", "C,D"});
   }
 
   @Test
   public void setStringArrayWithNullValues() {
     Settings settings = new Settings(definitions);
-    settings.setProperty("multi_values", new String[] {"A,B", null, "C,D"});
+    settings.setProperty("multi_values", new String[]{"A,B", null, "C,D"});
     String[] array = settings.getStringArray("multi_values");
-    assertThat(array).isEqualTo(new String[] {"A,B", "", "C,D"});
+    assertThat(array).isEqualTo(new String[]{"A,B", "", "C,D"});
   }
 
   @Test(expected = IllegalStateException.class)
   public void shouldFailToSetArrayValueOnSingleValueProperty() {
     Settings settings = new Settings(definitions);
-    settings.setProperty("array", new String[] {"A", "B", "C"});
+    settings.setProperty("array", new String[]{"A", "B", "C"});
   }
 
   @Test
@@ -214,7 +269,7 @@ public class SettingsTest {
     Settings settings = new Settings();
     settings.setProperty("foo", "  one,  two, three  ");
     String[] array = settings.getStringArray("foo");
-    assertThat(array).isEqualTo(new String[] {"one", "two", "three"});
+    assertThat(array).isEqualTo(new String[]{"one", "two", "three"});
   }
 
   @Test
@@ -222,7 +277,7 @@ public class SettingsTest {
     Settings settings = new Settings();
     settings.setProperty("foo", "  one,  , two");
     String[] array = settings.getStringArray("foo");
-    assertThat(array).isEqualTo(new String[] {"one", "", "two"});
+    assertThat(array).isEqualTo(new String[]{"one", "", "two"});
   }
 
   @Test
@@ -280,34 +335,34 @@ public class SettingsTest {
   public void getStringLines_single_line() {
     Settings settings = new Settings();
     settings.setProperty("foo", "the line");
-    assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"the line"});
+    assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"the line"});
   }
 
   @Test
   public void getStringLines_linux() {
     Settings settings = new Settings();
     settings.setProperty("foo", "one\ntwo");
-    assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+    assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
 
     settings.setProperty("foo", "one\ntwo\n");
-    assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+    assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
   }
 
   @Test
   public void getStringLines_windows() {
     Settings settings = new Settings();
     settings.setProperty("foo", "one\r\ntwo");
-    assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+    assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
 
     settings.setProperty("foo", "one\r\ntwo\r\n");
-    assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+    assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
   }
 
   @Test
   public void getStringLines_mix() {
     Settings settings = new Settings();
     settings.setProperty("foo", "one\r\ntwo\nthree");
-    assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two", "three"});
+    assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two", "three"});
   }
 
   @Test