]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6571 update the metric key validation without dots nor semi-colon
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 23 Jul 2015 13:35:20 +0000 (15:35 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 24 Jul 2015 07:22:45 +0000 (09:22 +0200)
server/sonar-server/src/main/java/org/sonar/server/util/MetricKeyValidator.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/util/MetricKeyValidatorTest.java

index d077cf42cc7de6177647da08bbeb2d72c63f969b..7f3d77412b7e79cae937e99125004362170fd5e9 100644 (file)
@@ -27,9 +27,9 @@ public class MetricKeyValidator {
   }
 
   /*
-   * Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit
+   * Allowed characters are alphanumeric, '-', '_', with at least one non-digit
    */
-  private static final String VALID_MODULE_KEY_REGEXP = "[\\p{Alnum}\\-_.:]*[\\p{Alpha}\\-_.:]+[\\p{Alnum}\\-_.:]*";
+  private static final String VALID_METRIC_KEY_REGEXP = "[\\p{Alnum}\\-_]*[\\p{Alpha}\\-_]+[\\p{Alnum}\\-_]*";
 
   /**
    * <p>Test if given parameter is valid for a project/module. Valid format is:</p>
@@ -39,7 +39,7 @@ public class MetricKeyValidator {
    *      <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>
+   *      <li>Punctuation signs dash '-', underscore '_'</li>
    *    </ul>
    *  </li>
    *  <li>At least one non-digit</li>
@@ -48,12 +48,12 @@ public class MetricKeyValidator {
    * @return <code>true</code> if <code>candidateKey</code> can be used for a metric
    */
   public static boolean isMetricKeyValid(String candidateKey) {
-    return candidateKey.matches(VALID_MODULE_KEY_REGEXP);
+    return candidateKey.matches(VALID_METRIC_KEY_REGEXP);
   }
 
   public static String checkMetricKeyFormat(String candidateKey) {
     if (!isMetricKeyValid(candidateKey)) {
-      throw new IllegalArgumentException(String.format("Malformed metric key '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
+      throw new IllegalArgumentException(String.format("Malformed metric key '%s'. Allowed characters are alphanumeric, '-', '_', with at least one non-digit.",
         candidateKey));
     }
 
index 4d1d6833ff0dd0b7d4900fa781cb0e0faae3dae0..d844cdd171a70691ec68b7eb7aabccee35c917aa 100644 (file)
@@ -261,7 +261,7 @@ public class CreateActionTest {
     expectedException.expect(IllegalArgumentException.class);
 
     newRequest()
-      .setParam(PARAM_KEY, "(123:456)")
+      .setParam(PARAM_KEY, "123:456")
       .setParam(PARAM_NAME, DEFAULT_NAME)
       .setParam(PARAM_TYPE, DEFAULT_TYPE)
       .execute();
index c2ca240a249d1bab55a7128a27449e897720c45b..d835fd69d738f28dd3657caef1c58d9d5463aa81 100644 (file)
@@ -34,16 +34,16 @@ public class MetricKeyValidatorTest {
   @Test
   public void isMetricKeyValid() {
     assertThat(MetricKeyValidator.isMetricKeyValid("")).isFalse();
-    assertThat(MetricKeyValidator.isMetricKeyValid("1.2.3:ABC-1.2.3")).isTrue();
-    assertThat(MetricKeyValidator.isMetricKeyValid("123.321")).isTrue();
+    assertThat(MetricKeyValidator.isMetricKeyValid("1_2_3-ABC-1_2_3")).isTrue();
+    assertThat(MetricKeyValidator.isMetricKeyValid("123_321")).isTrue();
     assertThat(MetricKeyValidator.isMetricKeyValid("123456")).isFalse();
-    assertThat(MetricKeyValidator.isMetricKeyValid("(123.A.321)")).isFalse();
+    assertThat(MetricKeyValidator.isMetricKeyValid("1.2.3_A_3:2:1")).isFalse();
   }
 
   @Test
   public void checkMetricKeyFormat() {
     expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Malformed metric key '123456'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
+    expectedException.expectMessage("Malformed metric key '123456'. Allowed characters are alphanumeric, '-', '_', with at least one non-digit.");
 
     MetricKeyValidator.checkMetricKeyFormat("123456");
   }