pmd: Fixed bug 1048: CommentContent Rule, String Index out of range Exception

This commit is contained in:
Andreas Dangel
2012-12-07 10:49:39 +01:00
parent 63c1a8fe47
commit 637006674f
3 changed files with 40 additions and 2 deletions

View File

@ -1,6 +1,7 @@
???? ??, 2012 - 5.0.2: ???? ??, 2012 - 5.0.2:
Fixed bug 1044: Unknown option: -excludemarker Fixed bug 1044: Unknown option: -excludemarker
Fixed bug 1048: CommentContent Rule, String Index out of range Exception
November 28, 2012 - 5.0.1: November 28, 2012 - 5.0.1:

View File

@ -1,3 +1,6 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.comments; package net.sourceforge.pmd.lang.java.rule.comments;
import java.util.ArrayList; import java.util.ArrayList;
@ -95,7 +98,7 @@ public abstract class AbstractCommentRule extends AbstractJavaRule {
continue; continue;
} }
if (line.charAt(0) == '*') { if (line.length() > 0 && line.charAt(0) == '*') {
filteredLines.add(line.substring(1)); filteredLines.add(line.substring(1));
continue; continue;
} }
@ -122,7 +125,7 @@ public abstract class AbstractCommentRule extends AbstractJavaRule {
continue; continue;
} }
if (line.charAt(0) == '*') { if (line.length() > 0 && line.charAt(0) == '*') {
filteredLines.add(line.substring(1)); filteredLines.add(line.substring(1));
continue; continue;
} }

View File

@ -0,0 +1,34 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.comments;
import static org.junit.Assert.assertEquals;
import net.sourceforge.pmd.lang.java.ast.FormalComment;
import net.sourceforge.pmd.lang.java.ast.MultiLineComment;
import net.sourceforge.pmd.lang.java.ast.Token;
import org.junit.Test;
public class AbstractCommentRuleTest {
private AbstractCommentRule testSubject = new AbstractCommentRule() {};
/**
* Blank lines in comments should not raise an exception.
* See bug #1048.
*/
@Test
public void testFilteredCommentIn() {
Token token = new Token();
token.image = "/* multi line comment with blank lines\n\n\n */";
String filtered = testSubject.filteredCommentIn(new MultiLineComment(token));
assertEquals("multi line comment with blank lines", filtered);
token.image = "/** a formal comment with blank lines\n\n\n */";
filtered = testSubject.filteredCommentIn(new FormalComment(token));
assertEquals("a formal comment with blank lines", filtered);
}
}