aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java36
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java95
2 files changed, 107 insertions, 24 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
index 3ec41a5cf6e..307baa2e5fc 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
@@ -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) {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java
index 2e5259a4995..8be0edeca61 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java
@@ -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;
@@ -150,56 +151,110 @@ public class SettingsTest {
}
@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