private static final int THREE_FIELDS_PER_LINE = 3;
private static final String LINE_RANGE_REGEXP = "\\[((\\d+|\\d+-\\d+),?)*\\]";
+ private static final String CONFIG_FORMAT_ERROR_PREFIX = "Exclusions > Issues : Invalid format. ";
public List<Pattern> decode(String patternsList) {
List<Pattern> patterns = Lists.newLinkedList();
String[] fields = StringUtils.splitPreserveAllTokens(line, ';');
if (fields.length > THREE_FIELDS_PER_LINE) {
- throw new SonarException("Invalid format. The following line has more than 3 fields separated by comma: " + line);
+ throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The following line has more than 3 fields separated by comma: " + line);
}
Pattern pattern;
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);
+ throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The first field does not define a resource pattern: " + line);
}
if (!isRule(fields[1])) {
- throw new SonarException("Invalid format. The second field does not define a rule pattern: " + line);
+ throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The second field does not define a rule pattern: " + line);
}
if (!isLinesRange(fields[2])) {
- throw new SonarException("Invalid format. The third field does not define a range of lines: " + line);
+ throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The third field does not define a range of lines: " + line);
}
}
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);
- }
- if (!isRegexp(fields[1])) {
- throw new SonarException("Invalid format. The second field does not define a regular expression: " + line);
+ throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The first field does not define a regular expression: " + line);
}
+ // As per configuration help, missing second field means: from start regexp to EOF
}
static void checkWholeFileRegexp(String regexp) {
if (!isRegexp(regexp)) {
- throw new SonarException("Invalid format. The field does not define a regular expression: " + regexp);
+ throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The field does not define a regular expression: " + regexp);
}
}
package org.sonar.plugins.core.issue.ignore.scanner;
+import org.apache.commons.lang.StringUtils;
+
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;
checkDoubleRegexps(line, lineIndex);
}
+ if (currentMatcher != null && !currentMatcher.hasSecondPattern()) {
+ // this will happen when there is a start block regexp but no end block regexp
+ endExclusion(lineIndex + 1);
+ }
+
// now create the new line-based pattern for this file if there are exclusions
fileLength = lineIndex;
if (!lineExclusions.isEmpty()) {
}
boolean matchesSecondPattern(String line) {
- return secondPattern.matcher(line).find();
+ return hasSecondPattern() && secondPattern.matcher(line).find();
}
+ boolean hasSecondPattern() {
+ return StringUtils.isNotEmpty(secondPattern.toString());
+ }
}
}
@Test
public void shouldFailToReadUncorrectLine1() {
thrown.expect(SonarException.class);
- thrown.expectMessage("Invalid format. The following line has more than 3 fields separated by comma");
+ thrown.expectMessage("Exclusions > Issues : Invalid format. The following line has more than 3 fields separated by comma");
decoder.decode(";;;;");
}
@Test
public void shouldFailToReadUncorrectLine3() {
thrown.expect(SonarException.class);
- thrown.expectMessage("Invalid format. The first field does not define a resource pattern");
+ thrown.expectMessage("Exclusions > Issues : Invalid format. The first field does not define a resource pattern");
decoder.decode(";*;*");
}
@Test
public void shouldFailToReadUncorrectLine4() {
thrown.expect(SonarException.class);
- thrown.expectMessage("Invalid format. The second field does not define a rule pattern");
+ thrown.expectMessage("Exclusions > Issues : Invalid format. The second field does not define a rule pattern");
decoder.decode("*;;*");
}
@Test
public void shouldFailToReadUncorrectLine5() {
thrown.expect(SonarException.class);
- thrown.expectMessage("Invalid format. The third field does not define a range of lines");
+ thrown.expectMessage("Exclusions > Issues : Invalid format. The third field does not define a range of lines");
decoder.decode("*;*;blabla");
}
@Test
public void shouldFailToReadUncorrectLine6() {
thrown.expect(SonarException.class);
- thrown.expectMessage("Invalid format. The first field does not define a regular expression");
+ thrown.expectMessage("Exclusions > Issues : Invalid format. The first field does not define a regular expression");
decoder.decode(";ON");
}
@Test
- public void shouldFailToReadUncorrectLine7() {
- thrown.expect(SonarException.class);
- thrown.expectMessage("Invalid format. The second field does not define a regular expression");
+ public void shouldAcceptEmptyEndBlockRegexp() {
+ Pattern pattern = decoder.decodeLine("OFF;");
- decoder.decode("OFF;");
+ assertThat(pattern.getResourcePattern()).isNull();
+ assertThat(pattern.getBeginBlockRegexp()).isEqualTo("OFF");
+ assertThat(pattern.getEndBlockRegexp()).isEmpty();
}
}
@Test
public void shouldReturnBlockPattern() {
- settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY, "1,2");
+ settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY, "1,2,3");
settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "// SONAR-ON");
settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".2." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// FOO-OFF");
settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".2." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "// FOO-ON");
+ settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".3." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// IGNORE-TO-EOF");
+ settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".3." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "");
patternsInitializer.initPatterns();
assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
- assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(2);
+ assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(3);
assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
}
patternsInitializer.initPatterns();
}
- @Test(expected = SonarException.class)
- public void shouldLogInvalidEndBlockPattern() {
- settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY, "1");
- settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP, "// SONAR-OFF");
- settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + ".1." + IgnoreIssuesConfiguration.END_BLOCK_REGEXP, "");
- patternsInitializer.initPatterns();
- }
-
@Test
public void shouldReturnAllFilePattern() {
settings.setProperty(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY, "1,2");