diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-09-09 14:14:16 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-09-12 12:26:50 +0200 |
commit | fc81d57b127f84b5871b0811d31f565a3cc0955f (patch) | |
tree | c2f2476a6943bcb2937979f5a7ca1f0f4c44dc98 /sonar-plugin-api | |
parent | 132d938e7a91b61ce436b7bb40f616151a08306c (diff) | |
download | sonarqube-fc81d57b127f84b5871b0811d31f565a3cc0955f.tar.gz sonarqube-fc81d57b127f84b5871b0811d31f565a3cc0955f.zip |
SONAR-8047 Validate settings of type REGULAR_EXPRESSION
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java | 93 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java | 15 |
2 files changed, 76 insertions, 32 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 c6ef17c80af..a80801499d9 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 @@ -21,10 +21,14 @@ package org.sonar.api.config; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.Arrays; +import java.util.EnumMap; import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; @@ -39,7 +43,13 @@ 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.BOOLEAN; +import static org.sonar.api.PropertyType.FLOAT; +import static org.sonar.api.PropertyType.INTEGER; +import static org.sonar.api.PropertyType.LONG; import static org.sonar.api.PropertyType.PROPERTY_SET; +import static org.sonar.api.PropertyType.REGULAR_EXPRESSION; +import static org.sonar.api.PropertyType.SINGLE_SELECT_LIST; /** * Declare a plugin property. Values are available at runtime through the component {@link Settings}. @@ -155,44 +165,60 @@ public final class PropertyDefinition { return Result.SUCCESS; } - switch (type) { - case BOOLEAN: - return validateBoolean(value); - case INTEGER: - case LONG: - return validateInteger(value); - case FLOAT: - return validateFloat(value); - case SINGLE_SELECT_LIST: - if (!options.contains(value)) { - return Result.newError("notInOptions"); - } - default: - return Result.SUCCESS; - } + EnumMap<PropertyType, Validation> validations = createValidations(options); + return validations.getOrDefault(type, aValue -> Result.SUCCESS).validate(value); } - private static Result validateBoolean(String value) { - if (!StringUtils.equalsIgnoreCase(value, "true") && !StringUtils.equalsIgnoreCase(value, "false")) { - return Result.newError("notBoolean"); - } - return Result.SUCCESS; + private static EnumMap<PropertyType, Validation> createValidations(List<String> options) { + return new EnumMap<>(ImmutableMap.<PropertyType, Validation>builder() + .put(BOOLEAN, validateBoolean()) + .put(INTEGER, validateInteger()) + .put(LONG, validateInteger()) + .put(FLOAT, validateFloat()) + .put(REGULAR_EXPRESSION, validateRegexp()) + .put(SINGLE_SELECT_LIST, + aValue -> options.contains(aValue) ? Result.SUCCESS : Result.newError("notInOptions")) + .build()); } - private static Result validateInteger(String value) { - if (!NumberUtils.isDigits(value)) { - return Result.newError("notInteger"); - } - return Result.SUCCESS; + private static Validation validateBoolean() { + return value -> { + if (!StringUtils.equalsIgnoreCase(value, "true") && !StringUtils.equalsIgnoreCase(value, "false")) { + return Result.newError("notBoolean"); + } + return Result.SUCCESS; + }; } - private static Result validateFloat(String value) { - try { - Double.parseDouble(value); + private static Validation validateInteger() { + return value -> { + if (!NumberUtils.isDigits(value)) { + return Result.newError("notInteger"); + } return Result.SUCCESS; - } catch (NumberFormatException e) { - return Result.newError("notFloat"); - } + }; + } + + private static Validation validateFloat() { + return value -> { + try { + Double.parseDouble(value); + return Result.SUCCESS; + } catch (NumberFormatException e) { + return Result.newError("notFloat"); + } + }; + } + + private static Validation validateRegexp() { + return value -> { + try { + Pattern.compile(value); + return Result.SUCCESS; + } catch (PatternSyntaxException e) { + return Result.newError("notRegexp"); + } + }; } public Result validate(@Nullable String value) { @@ -541,4 +567,9 @@ public final class PropertyDefinition { } } } + + @FunctionalInterface + private interface Validation { + Result validate(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 4f7a860a1c4..dfdd21c8340 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,7 +227,7 @@ public class PropertyDefinitionTest { } @Test - public void should_validate_long() { + public void validate_long() { PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.LONG).build(); assertThat(def.validate(null).isValid()).isTrue(); @@ -254,6 +254,19 @@ public class PropertyDefinitionTest { } @Test + public void validate_regular_expression() { + PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.REGULAR_EXPRESSION).build(); + + assertThat(def.validate(null).isValid()).isTrue(); + assertThat(def.validate("").isValid()).isTrue(); + assertThat(def.validate(" ").isValid()).isTrue(); + assertThat(def.validate("[a-zA-Z]").isValid()).isTrue(); + + assertThat(def.validate("[a-zA-Z").isValid()).isFalse(); + assertThat(def.validate("[a-zA-Z").getErrorKey()).isEqualTo("notRegexp"); + } + + @Test public void should_validate_single_select_list() { PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.SINGLE_SELECT_LIST).options("de", "en").build(); |