*/
package org.sonar.api.config;
+import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.ArrayUtils;
/**
* Project Settings on batch side, Global Settings on server side. This component does not access to database, so
* property changed via setter methods are not persisted.
- *
+ * <p/>
* <p>
* This component replaces the deprecated org.apache.commons.configuration.Configuration
* </p>
/**
* Clone settings. Actions are not propagated to cloned settings.
+ *
* @since 3.1
*/
public Settings(Settings other) {
}
/**
- * Value is splitted by comma and trimmed.
+ * Value is split by comma and trimmed.
* <p/>
* Examples :
* <ul>
return getStringArrayBySeparator(key, ",");
}
+ /**
+ * Value is split by carriage returns.
+ *
+ * @return non-null array of lines. The line termination characters are excluded.
+ * @since 3.2
+ */
+ public final String[] getStringLines(String key) {
+ String value = getString(key);
+ if (Strings.isNullOrEmpty(value)) {
+ return ArrayUtils.EMPTY_STRING_ARRAY;
+ }
+ return value.split("\r?\n|\r", -1);
+ }
+
/**
* Value is splitted and trimmed.
*/
private PropertyDefinitions definitions;
@Properties({
- @Property(key = "hello", name = "Hello", defaultValue = "world"),
- @Property(key = "date", name = "Date", defaultValue = "2010-05-18"),
- @Property(key = "boolean", name = "Boolean", defaultValue = "true"),
- @Property(key = "falseboolean", name = "False Boolean", defaultValue = "false"),
- @Property(key = "integer", name = "Integer", defaultValue = "12345"),
- @Property(key = "array", name = "Array", defaultValue = "one,two,three")
+ @Property(key = "hello", name = "Hello", defaultValue = "world"),
+ @Property(key = "date", name = "Date", defaultValue = "2010-05-18"),
+ @Property(key = "datetime", name = "DateTime", defaultValue = "2010-05-18T15:50:45+0100"),
+ @Property(key = "boolean", name = "Boolean", defaultValue = "true"),
+ @Property(key = "falseboolean", name = "False Boolean", defaultValue = "false"),
+ @Property(key = "integer", name = "Integer", defaultValue = "12345"),
+ @Property(key = "array", name = "Array", defaultValue = "one,two,three")
})
static class Init {
}
assertThat(settings.getDate("unknown")).isNull();
}
+ @Test
+ public void testGetDateTime() {
+ Settings settings = new Settings(definitions);
+ 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() {
Settings settings = new Settings(definitions);
assertThat(target.getString("foo")).isEqualTo("bar");
assertThat(target.getString("new")).isNull();
}
+
+ @Test
+ public void getStringLines_no_value() {
+ assertThat(new Settings().getStringLines("foo")).hasSize(0);
+ }
+
+ @Test
+ public void getStringLines_single_line() {
+ Settings settings = new Settings();
+ settings.setProperty("foo", "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"});
+
+ settings.setProperty("foo", "one\ntwo\n");
+ 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"});
+
+ settings.setProperty("foo", "one\r\ntwo\r\n");
+ 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"});
+ }
+
+ @Test
+ public void getKeysStartingWith() {
+ Settings settings = new Settings();
+ settings.setProperty("sonar.jdbc.url", "foo");
+ settings.setProperty("sonar.jdbc.username", "bar");
+ settings.setProperty("sonar.security", "admin");
+
+ 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);
+
+ }
}