diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-09-09 11:28:13 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-09-12 12:26:49 +0200 |
commit | 132d938e7a91b61ce436b7bb40f616151a08306c (patch) | |
tree | 395e8caf5efa50474ee184455f7fb7bb197a0d02 /sonar-plugin-api | |
parent | 2cff1d865d72af290b61eeb9dc13e81b63679f09 (diff) | |
download | sonarqube-132d938e7a91b61ce436b7bb40f616151a08306c.tar.gz sonarqube-132d938e7a91b61ce436b7bb40f616151a08306c.zip |
SONAR-8050 Validate settings of type LONG
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java | 24 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java | 13 |
2 files changed, 29 insertions, 8 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java index 0100cf61d36..c6ef17c80af 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java @@ -38,6 +38,7 @@ import org.sonar.api.server.ServerSide; import org.sonarsource.api.sonarlint.SonarLintSide; import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.commons.lang.StringUtils.isBlank; import static org.sonar.api.PropertyType.PROPERTY_SET; /** @@ -150,18 +151,25 @@ public final class PropertyDefinition { } public static Result validate(PropertyType type, @Nullable String value, List<String> options) { - if (StringUtils.isNotBlank(value)) { - if (type == PropertyType.BOOLEAN) { + if (isBlank(value)) { + return Result.SUCCESS; + } + + switch (type) { + case BOOLEAN: return validateBoolean(value); - } else if (type == PropertyType.INTEGER) { + case INTEGER: + case LONG: return validateInteger(value); - } else if (type == PropertyType.FLOAT) { + case FLOAT: return validateFloat(value); - } else if (type == PropertyType.SINGLE_SELECT_LIST && !options.contains(value)) { - return Result.newError("notInOptions"); - } + case SINGLE_SELECT_LIST: + if (!options.contains(value)) { + return Result.newError("notInOptions"); + } + default: + return Result.SUCCESS; } - return Result.SUCCESS; } private static Result validateBoolean(String value) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java index cb4b67f242f..4f7a860a1c4 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java @@ -227,6 +227,19 @@ public class PropertyDefinitionTest { } @Test + public void should_validate_long() { + PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.LONG).build(); + + assertThat(def.validate(null).isValid()).isTrue(); + assertThat(def.validate("").isValid()).isTrue(); + assertThat(def.validate(" ").isValid()).isTrue(); + assertThat(def.validate("123456").isValid()).isTrue(); + + assertThat(def.validate("foo").isValid()).isFalse(); + assertThat(def.validate("foo").getErrorKey()).isEqualTo("notInteger"); + } + + @Test public void should_validate_float() { PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.FLOAT).build(); |