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);
}
}
@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);
}
}
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);
}
@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
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;
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();
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");