diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-12-07 11:33:07 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-12-07 11:33:51 +0100 |
commit | a2d09b95e5f7fe719d24e47dd88795f1ccdcdd2b (patch) | |
tree | 33b5ea49c64939edd3d84cbb27f4e756825b8b0d | |
parent | e0d98a29f7250f19ebe3b6cf34ef6afd0e38830b (diff) | |
download | sonarqube-a2d09b95e5f7fe719d24e47dd88795f1ccdcdd2b.tar.gz sonarqube-a2d09b95e5f7fe719d24e47dd88795f1ccdcdd2b.zip |
SONAR-3062 API: org.sonar.api.config.Settings#getStringArray() should trim values
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java | 22 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java | 24 |
2 files changed, 42 insertions, 4 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 fc626bb5f9f..656e3f88ee6 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 @@ -106,14 +106,32 @@ public class Settings implements BatchComponent, ServerComponent { return null; } + /** + * Value is splitted by comma and trimmed. + * + * Examples : + * <ul> + * <li>"one,two,three " -> ["one", "two", "three"]</li> + * <li>" one, two, three " -> ["one", "two", "three"]</li> + * <li>"one, , three" -> ["one", "", "three"]</li> + * </ul> + */ public final String[] getStringArray(String key) { return getStringArrayBySeparator(key, ","); } + /** + * Value is splitted and trimmed. + */ public final String[] getStringArrayBySeparator(String key, String separator) { String value = getString(key); if (value != null) { - return StringUtils.splitByWholeSeparator(value, separator); + String[] strings = StringUtils.splitByWholeSeparator(value, separator); + String[] result = new String[strings.length]; + for (int index=0 ; index<strings.length ; index++) { + result[index]=StringUtils.trim(strings[index]); + } + return result; } return ArrayUtils.EMPTY_STRING_ARRAY; } @@ -128,8 +146,6 @@ public class Settings implements BatchComponent, ServerComponent { return result; } - - public final Settings appendProperty(String key, String value) { String newValue = properties.get(key); if (StringUtils.isEmpty(newValue)) { 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 c34f1278ca4..c8c3c4df23d 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 @@ -95,6 +95,28 @@ public class SettingsTest { } @Test + public void shouldTrimArray() { + Settings settings = new Settings(); + settings.setProperty("foo", " one, two, three "); + String[] array = settings.getStringArray("foo"); + assertThat(array.length, is(3)); + assertThat(array[0], is("one")); + assertThat(array[1], is("two")); + assertThat(array[2], is("three")); + } + + @Test + public void shouldKeepEmptyValuesWhenSplitting() { + Settings settings = new Settings(); + settings.setProperty("foo", " one, , two"); + String[] array = settings.getStringArray("foo"); + assertThat(array.length, is(3)); + assertThat(array[0], is("one")); + assertThat(array[1], is("")); + assertThat(array[2], is("two")); + } + + @Test public void testDefaultValueOfGetString() { Settings settings = new Settings(definitions); assertThat(settings.getString("hello"), is("world")); @@ -117,7 +139,7 @@ public class SettingsTest { assertThat(settings.getDefaultValue("foo"), is("bar")); } - @Property(key="foo", name="Foo", defaultValue = "bar") + @Property(key = "foo", name = "Foo", defaultValue = "bar") public static class MyComponent { } |