]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3644 Check input format of configuration entries for multicriteria
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 10 Sep 2013 13:53:04 +0000 (15:53 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 10 Sep 2013 13:53:04 +0000 (15:53 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoderTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java

index b31ecdc0555f6426b9ddb915f201fa782c64ccc4..70c18e1b9c444e979d29b451e8aec0b54f5b8aad 100644 (file)
@@ -72,7 +72,7 @@ public class PatternDecoder {
     return pattern;
   }
 
-  private void checkRegularLineConstraints(String line, String[] fields) {
+  static void checkRegularLineConstraints(String line, String[] fields) {
     if (!isResource(fields[0])) {
       throw new SonarException("Invalid format. The first field does not define a resource pattern: " + line);
     }
@@ -112,27 +112,27 @@ public class PatternDecoder {
   }
 
   @VisibleForTesting
-  boolean isLinesRange(String field) {
+  static boolean isLinesRange(String field) {
     return StringUtils.equals(field, "*") || java.util.regex.Pattern.matches(LINE_RANGE_REGEXP, field);
   }
 
   @VisibleForTesting
-  boolean isBlankOrComment(String line) {
+  static boolean isBlankOrComment(String line) {
     return StringUtils.isBlank(line) ^ StringUtils.startsWith(line, "#");
   }
 
   @VisibleForTesting
-  boolean isResource(String field) {
+  static boolean isResource(String field) {
     return StringUtils.isNotBlank(field);
   }
 
   @VisibleForTesting
-  boolean isRule(String field) {
+  static boolean isRule(String field) {
     return StringUtils.isNotBlank(field);
   }
 
   @VisibleForTesting
-  boolean isRegexp(String field) {
+  static boolean isRegexp(String field) {
     return StringUtils.isNotBlank(field);
   }
 }
index 5681d23d9dd5636ef8ea18baf2a8d65966828274..742943335c6b83746243d65ccf2c4a5c0b910404 100644 (file)
@@ -90,8 +90,10 @@ public class PatternsInitializer implements BatchExtension {
       String propPrefix = Constants.PATTERNS_MULTICRITERIA_KEY + "." + id + ".";
       String resourceKeyPattern = settings.getString(propPrefix + Constants.RESOURCE_KEY);
       String ruleKeyPattern = settings.getString(propPrefix + Constants.RULE_KEY);
-      Pattern pattern = new Pattern(firstNonNull(resourceKeyPattern, "*"), firstNonNull(ruleKeyPattern, "*"));
       String lineRange = settings.getString(propPrefix + Constants.LINE_RANGE_KEY);
+      String[] fields = new String[] { resourceKeyPattern, ruleKeyPattern, lineRange };
+      PatternDecoder.checkRegularLineConstraints(StringUtils.join(fields, ","), fields);
+      Pattern pattern = new Pattern(firstNonNull(resourceKeyPattern, "*"), firstNonNull(ruleKeyPattern, "*"));
       PatternDecoder.decodeRangeOfLines(pattern, firstNonNull(lineRange, "*"));
       multicriteriaPatterns.add(pattern);
     }
index c527412a5d695ef15c011c9205f78b93bb3c7db1..c6d4a0d8a5b40ea1febe5b817670aa33f787e215 100644 (file)
@@ -56,32 +56,32 @@ public class PatternDecoderTest {
 
   @Test
   public void shouldCheckFormatOfResource() {
-    assertThat(decoder.isResource("")).isFalse();
-    assertThat(decoder.isResource("*")).isTrue();
-    assertThat(decoder.isResource("com.foo.*")).isTrue();
+    assertThat(PatternDecoder.isResource("")).isFalse();
+    assertThat(PatternDecoder.isResource("*")).isTrue();
+    assertThat(PatternDecoder.isResource("com.foo.*")).isTrue();
   }
 
   @Test
   public void shouldCheckFormatOfRule() {
-    assertThat(decoder.isRule("")).isFalse();
-    assertThat(decoder.isRule("*")).isTrue();
-    assertThat(decoder.isRule("com.foo.*")).isTrue();
+    assertThat(PatternDecoder.isRule("")).isFalse();
+    assertThat(PatternDecoder.isRule("*")).isTrue();
+    assertThat(PatternDecoder.isRule("com.foo.*")).isTrue();
   }
 
   @Test
   public void shouldCheckFormatOfLinesRange() {
-    assertThat(decoder.isLinesRange("")).isFalse();
-    assertThat(decoder.isLinesRange("   ")).isFalse();
-    assertThat(decoder.isLinesRange("12")).isFalse();
-    assertThat(decoder.isLinesRange("12,212")).isFalse();
-
-    assertThat(decoder.isLinesRange("*")).isTrue();
-    assertThat(decoder.isLinesRange("[]")).isTrue();
-    assertThat(decoder.isLinesRange("[13]")).isTrue();
-    assertThat(decoder.isLinesRange("[13,24]")).isTrue();
-    assertThat(decoder.isLinesRange("[13,24,25-500]")).isTrue();
-    assertThat(decoder.isLinesRange("[24-65]")).isTrue();
-    assertThat(decoder.isLinesRange("[13,24-65,84-89,122]")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("")).isFalse();
+    assertThat(PatternDecoder.isLinesRange("   ")).isFalse();
+    assertThat(PatternDecoder.isLinesRange("12")).isFalse();
+    assertThat(PatternDecoder.isLinesRange("12,212")).isFalse();
+
+    assertThat(PatternDecoder.isLinesRange("*")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("[]")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("[13]")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("[13,24]")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("[13,24,25-500]")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("[24-65]")).isTrue();
+    assertThat(PatternDecoder.isLinesRange("[13,24-65,84-89,122]")).isTrue();
   }
 
   @Test
index 09ad7fe348262877e01ae351f619ed23621de2cc..fe4df90f3ccf0ef73f3a142f92a2317a5dc8c2e8 100644 (file)
@@ -20,6 +20,9 @@
 
 package org.sonar.plugins.core.issue.ignore.pattern;
 
+import org.sonar.api.utils.SonarException;
+
+import org.junit.Ignore;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.junit.Before;
@@ -81,8 +84,8 @@ public class PatternsInitializerTest {
     settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1,2");
     settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org/foo/Bar.java");
     settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "*");
-    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "*");
-    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "org/foo/Hello.java");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.LINE_RANGE_KEY, "*");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.RESOURCE_KEY, "org/foo/Hello.java");
     settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.RULE_KEY, "checkstyle:MagicNumber");
     settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "[15-200]");
     patternsInitializer.initPatterns();
@@ -95,6 +98,33 @@ public class PatternsInitializerTest {
     assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
   }
 
+  @Test(expected = SonarException.class)
+  public void shouldLogInvalidResourceKey() {
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "*");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.LINE_RANGE_KEY, "*");
+    patternsInitializer.initPatterns();
+  }
+
+  @Test(expected = SonarException.class)
+  public void shouldLogInvalidRuleKey() {
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "*");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.LINE_RANGE_KEY, "*");
+    patternsInitializer.initPatterns();
+  }
+
+  @Test(expected = SonarException.class)
+  public void shouldLogInvalidLineRange() {
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org/foo/Bar.java");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "*");
+    settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.LINE_RANGE_KEY, "notALineRange");
+    patternsInitializer.initPatterns();
+  }
+
   @Test
   public void shouldReturnBlockPattern() {
     settings.setProperty(Constants.PATTERNS_BLOCK_KEY, "1,2");