aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-07-06 18:55:46 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-07-06 18:56:12 +0200
commit612c68f47947000e22fb0c09d73b10561702926e (patch)
tree0c7672f61b250272a4cf2dc91e6c3725724a2e31 /sonar-plugin-api
parent2351d69d0f72e566bfa0b99450e0ed7bced090ae (diff)
downloadsonarqube-612c68f47947000e22fb0c09d73b10561702926e.tar.gz
sonarqube-612c68f47947000e22fb0c09d73b10561702926e.zip
Add some tests to org.sonar.api.config.Settings
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java3
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java53
2 files changed, 54 insertions, 2 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 584e00d74b7..2b8423a67d7 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
@@ -114,6 +114,9 @@ public class Settings implements BatchComponent, ServerComponent {
return StringUtils.isNotEmpty(value) && Boolean.parseBoolean(value);
}
+ /**
+ * @return the value as int. If the property does not exist and has no default value, then 0 is returned.
+ */
public final int getInt(String key) {
String value = getString(key);
if (StringUtils.isNotEmpty(value)) {
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 e821ff6a8c0..00757d04646 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
@@ -21,7 +21,9 @@ package org.sonar.api.config;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.Properties;
import org.sonar.api.Property;
@@ -43,6 +45,9 @@ public class SettingsTest {
static class Init {
}
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Before
public void initDefinitions() {
definitions = new PropertyDefinitions();
@@ -56,6 +61,42 @@ public class SettingsTest {
}
@Test
+ public void setProperty_int() {
+ Settings settings = new Settings();
+ settings.setProperty("foo", 123);
+ assertThat(settings.getInt("foo")).isEqualTo(123);
+ assertThat(settings.getString("foo")).isEqualTo("123");
+ assertThat(settings.getBoolean("foo")).isFalse();
+ }
+
+ @Test
+ public void setProperty_boolean() {
+ Settings settings = new Settings();
+ settings.setProperty("foo", true);
+ settings.setProperty("bar", false);
+ assertThat(settings.getBoolean("foo")).isTrue();
+ assertThat(settings.getBoolean("bar")).isFalse();
+ assertThat(settings.getString("foo")).isEqualTo("true");
+ assertThat(settings.getString("bar")).isEqualTo("false");
+ }
+
+ @Test
+ public void default_number_values_are_zero() {
+ Settings settings = new Settings();
+ assertThat(settings.getInt("foo")).isEqualTo(0);
+ assertThat(settings.getLong("foo")).isEqualTo(0L);
+ }
+
+ @Test
+ public void getInt_value_must_be_valid() {
+ thrown.expect(NumberFormatException.class);
+
+ Settings settings = new Settings();
+ settings.setProperty("foo", "not a number");
+ settings.getInt("foo");
+ }
+
+ @Test
public void allValuesShouldBeTrimmed_set_property() {
Settings settings = new Settings();
settings.setProperty("foo", " FOO ");
@@ -85,6 +126,7 @@ public class SettingsTest {
@Test
public void testGetDate() {
Settings settings = new Settings(definitions);
+ assertThat(settings.getDate("unknown")).isNull();
assertThat(settings.getDate("date").getDate()).isEqualTo(18);
assertThat(settings.getDate("date").getMonth()).isEqualTo(4);
}
@@ -98,19 +140,27 @@ public class SettingsTest {
@Test
public void testGetDateTime() {
Settings settings = new Settings(definitions);
+ assertThat(settings.getDateTime("unknown")).isNull();
assertThat(settings.getDateTime("datetime").getDate()).isEqualTo(18);
assertThat(settings.getDateTime("datetime").getMonth()).isEqualTo(4);
assertThat(settings.getDateTime("datetime").getMinutes()).isEqualTo(50);
}
@Test
- public void testGetArray() {
+ public void getStringArray() {
Settings settings = new Settings(definitions);
String[] array = settings.getStringArray("array");
assertThat(array).isEqualTo(new String[]{"one", "two", "three"});
}
@Test
+ public void getStringArray_no_value() {
+ Settings settings = new Settings();
+ String[] array = settings.getStringArray("array");
+ assertThat(array).isEmpty();
+ }
+
+ @Test
public void shouldTrimArray() {
Settings settings = new Settings();
settings.setProperty("foo", " one, two, three ");
@@ -221,6 +271,5 @@ public class SettingsTest {
assertThat(settings.getKeysStartingWith("sonar")).containsOnly("sonar.jdbc.url", "sonar.jdbc.username", "sonar.security");
assertThat(settings.getKeysStartingWith("sonar.jdbc")).containsOnly("sonar.jdbc.url", "sonar.jdbc.username");
assertThat(settings.getKeysStartingWith("other")).hasSize(0);
-
}
}