]> source.dussan.org Git - sonarqube.git/commitdiff
Add some tests to org.sonar.api.config.Settings
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 6 Jul 2012 16:55:46 +0000 (18:55 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 6 Jul 2012 16:56:12 +0000 (18:56 +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 584e00d74b7a72707b1ad82af8a7861de199101a..2b8423a67d7f436a6679888fcad95279e6791b3a 100644 (file)
@@ -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)) {
index e821ff6a8c09bcabfee3311f68ebf4fbff63d2ca..00757d04646da2f724df97ad76104b7ab9bbd47a 100644 (file)
@@ -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();
@@ -55,6 +60,42 @@ public class SettingsTest {
     assertThat(settings.getDefaultValue("hello")).isEqualTo("world");
   }
 
+  @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();
@@ -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,18 +140,26 @@ 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();
@@ -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);
-
   }
 }