summaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2013-10-01 16:52:32 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2013-10-02 10:38:16 +0200
commit59afc718cbc197ed4f67f80af9ca40df4edbf51b (patch)
tree340ff8f6c48eb788d94ac704904ce9d8affd0d00 /sonar-batch
parentd908112cc1fe908d8f8925706d3398a499386802 (diff)
downloadsonarqube-59afc718cbc197ed4f67f80af9ca40df4edbf51b.tar.gz
sonarqube-59afc718cbc197ed4f67f80af9ca40df4edbf51b.zip
Simplify component key validation (one regex, less branches in validateKey)
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java13
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java11
2 files changed, 16 insertions, 8 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java
index 539da5974e7..927976cf0f1 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java
@@ -19,15 +19,14 @@
*/
package org.sonar.batch.scan;
-import org.sonar.core.resource.ResourceDao;
import com.google.common.base.Joiner;
-import org.apache.commons.lang.math.NumberUtils;
import org.codehaus.plexus.util.StringUtils;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.SonarException;
+import org.sonar.core.resource.ResourceDao;
import java.util.ArrayList;
import java.util.List;
@@ -38,7 +37,11 @@ import java.util.List;
*/
public class ProjectReactorValidator {
- private static final String VALID_MODULE_KEY_REGEXP = "[0-9a-zA-Z\\-_\\.:]+";
+ /*
+ * Allowed characters are alphanumeric, dash '-', underscore '_', period '.' and colon ':',
+ * with at least one non-digit
+ */
+ private static final String VALID_MODULE_KEY_REGEXP = "[\\p{Alnum}\\-_.:]*[\\p{Alpha}\\-_.:]+[\\p{Alnum}\\-_.:]*";
private final Settings settings;
private final ResourceDao resourceDao;
@@ -72,9 +75,7 @@ public class ProjectReactorValidator {
}
private void validateKey(ProjectDefinition def, List<String> validationMessages) {
- if (!def.getKey().matches(VALID_MODULE_KEY_REGEXP) ||
- // SONAR-4629 project key must not only contain digits
- NumberUtils.isDigits(def.getKey())) {
+ if (!def.getKey().matches(VALID_MODULE_KEY_REGEXP)) {
validationMessages.add(String.format("%s is not a valid project or module key", def.getKey()));
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java
index 34887e22188..2d5398fc176 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java
@@ -70,8 +70,15 @@ public class ProjectReactorValidatorTest {
@Test
public void not_fail_with_valid_key() {
- ProjectReactor reactor = createProjectReactor("foo");
- validator.validate(reactor);
+ validator.validate(createProjectReactor("foo"));
+ validator.validate(createProjectReactor("123foo"));
+ validator.validate(createProjectReactor("foo123"));
+ validator.validate(createProjectReactor("1Z3"));
+ validator.validate(createProjectReactor("a123"));
+ validator.validate(createProjectReactor("123a"));
+ validator.validate(createProjectReactor("1:2"));
+ validator.validate(createProjectReactor("3-3"));
+ validator.validate(createProjectReactor("-:"));
}
@Test