aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorBenoît Gianinetti <benoit.gianinetti@sonarsource.com>2019-05-16 14:18:42 +0200
committerSonarTech <sonartech@sonarsource.com>2019-05-20 20:21:07 +0200
commitbf7776ff7da0661c5a966a869e7dae1760fce038 (patch)
treef3651d50fbc426694a6094cbd1fca1af644195cf /sonar-core
parent919604b531633e97a29964f4e4cf72bf1f058f78 (diff)
downloadsonarqube-bf7776ff7da0661c5a966a869e7dae1760fce038.tar.gz
sonarqube-bf7776ff7da0661c5a966a869e7dae1760fce038.zip
SONARCLOUD-628 Allow UTF-8 characters in project key
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java36
-rw-r--r--sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java35
2 files changed, 31 insertions, 40 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
index a3bb4fb1453..e4d59ea1dd8 100644
--- a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
+++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
@@ -30,11 +30,12 @@ public final class ComponentKeys {
public static final int MAX_COMPONENT_KEY_LENGTH = 400;
/*
- * Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit
+ * Must not be blank or empty
*/
- private static final String VALID_PROJECT_KEY_REGEXP = "[\\p{Alnum}\\-_.:]*[\\p{Alpha}\\-_.:]+[\\p{Alnum}\\-_.:]*";
+ private static final String VALID_PROJECT_KEY_REGEXP = "[^\\p{javaWhitespace}]+";
private static final String VALID_PROJECT_KEY_ISSUES_MODE_REGEXP = "[\\p{Alnum}\\-_.:/]*[\\p{Alpha}\\-_.:/]+[\\p{Alnum}\\-_.:/]*";
+
/*
* Allowed characters are alphanumeric, '-', '_', '.' and '/'
*/
@@ -60,18 +61,7 @@ public final class ComponentKeys {
}
/**
- * <p>Test if given parameter is valid for a project. Valid format is:</p>
- * <ul>
- * <li>Allowed characters:
- * <ul>
- * <li>Uppercase ASCII letters A-Z</li>
- * <li>Lowercase ASCII letters a-z</li>
- * <li>ASCII digits 0-9</li>
- * <li>Punctuation signs dash '-', underscore '_', period '.' and colon ':'</li>
- * </ul>
- * </li>
- * <li>At least one non-digit</li>
- * </ul>
+ * Test if given parameter is valid for a project. A key is valid if it doesn't contain whitespaces.
*
* @return <code>true</code> if <code>keyCandidate</code> can be used for a project
*/
@@ -85,12 +75,24 @@ public final class ComponentKeys {
* @throws IllegalArgumentException if the format is incorrect
*/
public static void checkProjectKey(String keyCandidate) {
- checkArgument(isValidProjectKey(keyCandidate), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
- keyCandidate);
+ checkArgument(isValidProjectKey(keyCandidate), "Malformed key for '%s'. %s", keyCandidate, "Project key cannot be empty nor contain whitespaces.");
}
/**
- * Same as {@link #isValidProjectKey(String)}, but allows additionally '/'.
+ * <p>Test if given parameter is valid for a project. Valid format is:</p>
+ * <ul>
+ * <li>Allowed characters:
+ * <ul>
+ * <li>Uppercase ASCII letters A-Z</li>
+ * <li>Lowercase ASCII letters a-z</li>
+ * <li>ASCII digits 0-9</li>
+ * <li>Punctuation signs dash '-', underscore '_', period '.', colon ':' and slash '/'</li>
+ * </ul>
+ * </li>
+ * <li>At least one non-digit</li>
+ * </ul>
+ *
+ * @return <code>true</code> if <code>keyCandidate</code> can be used for a project in issues mode
*/
public static boolean isValidProjectKeyIssuesMode(String keyCandidate) {
return keyCandidate.matches(VALID_PROJECT_KEY_ISSUES_MODE_REGEXP);
diff --git a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
index a328a0c586b..7f555664e29 100644
--- a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
@@ -47,13 +47,17 @@ public class ComponentKeysTest {
}
@Test
- public void isValidModuleKey() {
- assertThat(ComponentKeys.isValidProjectKey("")).isFalse();
+ public void isValidProjectKey() {
assertThat(ComponentKeys.isValidProjectKey("abc")).isTrue();
- assertThat(ComponentKeys.isValidProjectKey("0123")).isFalse();
- assertThat(ComponentKeys.isValidProjectKey("ab 12")).isFalse();
+ assertThat(ComponentKeys.isValidProjectKey("0123")).isTrue();
assertThat(ComponentKeys.isValidProjectKey("ab_12")).isTrue();
- assertThat(ComponentKeys.isValidProjectKey("ab/12")).isFalse();
+ assertThat(ComponentKeys.isValidProjectKey("ab/12")).isTrue();
+ assertThat(ComponentKeys.isValidProjectKey("코드품질")).isTrue();
+ assertThat(ComponentKeys.isValidProjectKey("")).isFalse();
+ assertThat(ComponentKeys.isValidProjectKey(" ")).isFalse();
+ assertThat(ComponentKeys.isValidProjectKey("ab 12")).isFalse();
+ assertThat(ComponentKeys.isValidProjectKey(" ab")).isFalse();
+ assertThat(ComponentKeys.isValidProjectKey("ab ")).isFalse();
}
@Test
@@ -79,37 +83,22 @@ public class ComponentKeysTest {
}
@Test
- public void checkModuleKey_with_correct_keys() {
+ public void checkProjectKey_with_correct_keys() {
ComponentKeys.checkProjectKey("abc");
ComponentKeys.checkProjectKey("a-b_1.:2");
}
@Test
- public void checkModuleKey_fail_if_only_digit() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Malformed key for '0123'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
-
- ComponentKeys.checkProjectKey("0123");
- }
-
- @Test
- public void checkModuleKey_fail_if_key_is_empty() {
+ public void checkProjectKey_fail_if_key_is_empty() {
expectedException.expect(IllegalArgumentException.class);
ComponentKeys.checkProjectKey("");
}
@Test
- public void checkModuleKey_fail_if_space() {
+ public void checkProjectKey_fail_if_space() {
expectedException.expect(IllegalArgumentException.class);
ComponentKeys.checkProjectKey("ab 12");
}
-
- @Test
- public void checkModuleKey_fail_if_special_characters_not_allowed() {
- expectedException.expect(IllegalArgumentException.class);
-
- ComponentKeys.checkProjectKey("ab/12");
- }
}