checkDoubleRegexpLineConstraints(line, fields);
pattern = new Pattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]);
} else {
+ checkWholeFileRegexp(fields[0]);
pattern = new Pattern().setAllFileRegexp(fields[0]);
}
}
}
- private void checkDoubleRegexpLineConstraints(String line, String[] fields) {
+ static void checkDoubleRegexpLineConstraints(String line, String[] fields) {
if (!isRegexp(fields[0])) {
throw new SonarException("Invalid format. The first field does not define a regular expression: " + line);
}
}
}
+ static void checkWholeFileRegexp(String regexp) {
+ if (!isRegexp(regexp)) {
+ throw new SonarException("Invalid format. The field does not define a regular expression: " + regexp);
+ }
+ }
+
public static void decodeRangeOfLines(Pattern pattern, String field) {
if (StringUtils.equals(field, "*")) {
pattern.setCheckLines(false);
String propPrefix = Constants.PATTERNS_BLOCK_KEY + "." + id + ".";
String beginBlockRegexp = settings.getString(propPrefix + Constants.BEGIN_BLOCK_REGEXP);
String endBlockRegexp = settings.getString(propPrefix + Constants.END_BLOCK_REGEXP);
+ String[] fields = new String[] { beginBlockRegexp, endBlockRegexp };
+ PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields);
Pattern pattern = new Pattern().setBeginBlockRegexp(nullToEmpty(beginBlockRegexp)).setEndBlockRegexp(nullToEmpty(endBlockRegexp));
blockPatterns.add(pattern);
}
for (String id : StringUtils.split(patternConf, ',')) {
String propPrefix = Constants.PATTERNS_ALLFILE_KEY + "." + id + ".";
String allFileRegexp = settings.getString(propPrefix + Constants.FILE_REGEXP);
+ PatternDecoder.checkWholeFileRegexp(allFileRegexp);
Pattern pattern = new Pattern().setAllFileRegexp(nullToEmpty(allFileRegexp));
allFilePatterns.add(pattern);
}
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;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
+import org.sonar.api.utils.SonarException;
import org.sonar.plugins.core.issue.ignore.Constants;
import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration;
assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
}
+ @Test(expected = SonarException.class)
+ public void shouldLogInvalidStartBlockPattern() {
+ settings.setProperty(Constants.PATTERNS_BLOCK_KEY, "1");
+ settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.BEGIN_BLOCK_REGEXP, "");
+ settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.END_BLOCK_REGEXP, "// SONAR-ON");
+ patternsInitializer.initPatterns();
+ }
+
+ @Test(expected = SonarException.class)
+ public void shouldLogInvalidEndBlockPattern() {
+ settings.setProperty(Constants.PATTERNS_BLOCK_KEY, "1");
+ settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
+ settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.END_BLOCK_REGEXP, "");
+ patternsInitializer.initPatterns();
+ }
+
@Test
public void shouldReturnAllFilePattern() {
settings.setProperty(Constants.PATTERNS_ALLFILE_KEY, "1,2");
assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2);
}
+ @Test(expected = SonarException.class)
+ public void shouldLogInvalidAllFilePattern() {
+ settings.setProperty(Constants.PATTERNS_ALLFILE_KEY, "1");
+ settings.setProperty(Constants.PATTERNS_ALLFILE_KEY + ".1." + Constants.FILE_REGEXP, "");
+ patternsInitializer.initPatterns();
+ }
+
@Test
public void shouldConfigurePatternsForComponents() {
String componentKey = "groupId:artifactId:org.foo.Bar";