aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-07-10 17:02:44 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-07-21 17:42:34 +0200
commitfa6d3bdff62c8c12ea3c910b871b6eb0461752b2 (patch)
treef8e6cb36a53d8d6e4f8f5a13e66d39433b728025 /sonar-plugin-api/src
parent2cb0710049f3cc946ba51b1ab6e797e73b1da390 (diff)
downloadsonarqube-fa6d3bdff62c8c12ea3c910b871b6eb0461752b2.tar.gz
sonarqube-fa6d3bdff62c8c12ea3c910b871b6eb0461752b2.zip
SONAR-9283 support trailing/heading whitespaces in properties
for numbers and boolean types
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/Configuration.java10
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/ConfigurationTest.java104
2 files changed, 109 insertions, 5 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Configuration.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Configuration.java
index f91996a0734..9ca07781246 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Configuration.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Configuration.java
@@ -125,7 +125,7 @@ public interface Configuration {
* If the property does not have value nor default value, then {@code empty} is returned.
*/
default Optional<Boolean> getBoolean(String key) {
- return get(key).map(Boolean::parseBoolean);
+ return get(key).map(String::trim).map(Boolean::parseBoolean);
}
/**
@@ -135,7 +135,7 @@ public interface Configuration {
*/
default Optional<Integer> getInt(String key) {
try {
- return get(key).map(Integer::parseInt);
+ return get(key).map(String::trim).map(Integer::parseInt);
} catch (NumberFormatException e) {
throw new IllegalStateException(String.format("The property '%s' is not an int value: %s", key, e.getMessage()));
}
@@ -148,7 +148,7 @@ public interface Configuration {
*/
default Optional<Long> getLong(String key) {
try {
- return get(key).map(Long::parseLong);
+ return get(key).map(String::trim).map(Long::parseLong);
} catch (NumberFormatException e) {
throw new IllegalStateException(String.format("The property '%s' is not an long value: %s", key, e.getMessage()));
}
@@ -161,7 +161,7 @@ public interface Configuration {
*/
default Optional<Float> getFloat(String key) {
try {
- return get(key).map(Float::valueOf);
+ return get(key).map(String::trim).map(Float::valueOf);
} catch (NumberFormatException e) {
throw new IllegalStateException(String.format("The property '%s' is not an float value: %s", key, e.getMessage()));
}
@@ -174,7 +174,7 @@ public interface Configuration {
*/
default Optional<Double> getDouble(String key) {
try {
- return get(key).map(Double::valueOf);
+ return get(key).map(String::trim).map(Double::valueOf);
} catch (NumberFormatException e) {
throw new IllegalStateException(String.format("The property '%s' is not an double value: %s", key, e.getMessage()));
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/ConfigurationTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/ConfigurationTest.java
new file mode 100644
index 00000000000..1f712844412
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/ConfigurationTest.java
@@ -0,0 +1,104 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.config;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Random;
+import java.util.function.BiFunction;
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.commons.lang.StringUtils;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ConfigurationTest {
+
+ private DumpMapConfiguration underTest = new DumpMapConfiguration();
+
+ @Test
+ public void getBoolean_supports_heading_and_or_trailing_whitespaces() {
+ boolean value = new Random().nextBoolean();
+
+ verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getBoolean);
+ }
+
+ @Test
+ public void getInt() {
+ int value = new Random().nextInt();
+
+ verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getInt);
+ }
+
+ @Test
+ public void getLong() {
+ long value = new Random().nextLong();
+
+ verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getLong);
+ }
+
+ @Test
+ public void getFloat() {
+ float value = new Random().nextFloat();
+
+ verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getFloat);
+ }
+
+ @Test
+ public void getDouble() {
+ double value = new Random().nextDouble();
+
+ verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getDouble);
+ }
+
+ private <T> void verifySupportHeadAndOrTrailingWhitespaces(T value, BiFunction<Configuration, String, Optional<T>> t) {
+ String randomKey = RandomStringUtils.randomAlphabetic(3);
+ String randomNumberOfWhitespaces = StringUtils.repeat(" ", 1 + new Random().nextInt(10));
+
+ assertThat(t.apply(underTest.put(randomKey, randomNumberOfWhitespaces + String.valueOf(value)), randomKey)).contains(value);
+ assertThat(t.apply(underTest.put(randomKey, String.valueOf(value) + randomNumberOfWhitespaces), randomKey)).contains(value);
+ assertThat(t.apply(underTest.put(randomKey, randomNumberOfWhitespaces + String.valueOf(value) + randomNumberOfWhitespaces), randomKey)).contains(value);
+ }
+
+ private static class DumpMapConfiguration implements Configuration {
+ private final Map<String, String> keyValues = new HashMap<>();
+
+ public Configuration put(String key, String value) {
+ keyValues.put(key, value);
+ return this;
+ }
+
+ @Override
+ public Optional<String> get(String key) {
+ return Optional.ofNullable(keyValues.get(key));
+ }
+
+ @Override
+ public boolean hasKey(String key) {
+ throw new UnsupportedOperationException("hasKey not implemented");
+ }
+
+ @Override
+ public String[] getStringArray(String key) {
+ throw new UnsupportedOperationException("getStringArray not implemented");
+ }
+ }
+}