From: Sébastien Lesaint Date: Mon, 10 Jul 2017 15:02:44 +0000 (+0200) Subject: SONAR-9283 support trailing/heading whitespaces in properties X-Git-Tag: 6.6-RC1~852 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fa6d3bdff62c8c12ea3c910b871b6eb0461752b2;p=sonarqube.git SONAR-9283 support trailing/heading whitespaces in properties for numbers and boolean types --- 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 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 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 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 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 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 void verifySupportHeadAndOrTrailingWhitespaces(T value, BiFunction> 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 keyValues = new HashMap<>(); + + public Configuration put(String key, String value) { + keyValues.put(key, value); + return this; + } + + @Override + public Optional 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"); + } + } +}