001: package com.puppycrawl.tools.checkstyle.checks;
002:
003: import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
004: import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
005:
006: public class GenericIllegalRegexpCheckTest extends BaseCheckTestCase {
007: private DefaultConfiguration mCheckConfig;
008:
009: public void setUp() {
010: mCheckConfig = createCheckConfig(GenericIllegalRegexpCheck.class);
011: }
012:
013: public void testIt() throws Exception {
014: final String illegal = "System\\.(out)|(err)\\.print(ln)?\\(";
015: mCheckConfig.addAttribute("format", illegal);
016: final String[] expected = { "69: Line matches the illegal pattern '"
017: + illegal + "'." };
018: verify(mCheckConfig, getPath("InputSemantic.java"), expected);
019: }
020:
021: public void testMessageProperty() throws Exception {
022: final String illegal = "System\\.(out)|(err)\\.print(ln)?\\(";
023: final String message = "Bad line :(";
024: mCheckConfig.addAttribute("format", illegal);
025: mCheckConfig.addAttribute("message", message);
026: final String[] expected = { "69: " + message, };
027: verify(mCheckConfig, getPath("InputSemantic.java"), expected);
028: }
029:
030: public void testIgnoreCaseTrue() throws Exception {
031: final String illegal = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
032: mCheckConfig.addAttribute("format", illegal);
033: mCheckConfig.addAttribute("ignoreCase", "true");
034: final String[] expected = { "69: Line matches the illegal pattern '"
035: + illegal + "'." };
036: verify(mCheckConfig, getPath("InputSemantic.java"), expected);
037: }
038:
039: public void testIgnoreCaseFalse() throws Exception {
040: final String illegal = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
041: mCheckConfig.addAttribute("format", illegal);
042: mCheckConfig.addAttribute("ignoreCase", "false");
043: final String[] expected = {};
044: verify(mCheckConfig, getPath("InputSemantic.java"), expected);
045: }
046:
047: public void testIgnoreCommentsCppStyle() throws Exception {
048: // See if the comment is removed properly
049: final String illegal = "don't use trailing comments";
050: mCheckConfig.addAttribute("format", illegal);
051: mCheckConfig.addAttribute("ignoreComments", "true");
052: final String[] expected = {};
053: verify(mCheckConfig, getPath("InputTrailingComment.java"),
054: expected);
055: }
056:
057: public void testIgnoreCommentsFalseCppStyle() throws Exception {
058: // See if the comment is removed properly
059: final String illegal = "don't use trailing comments";
060: mCheckConfig.addAttribute("format", illegal);
061: mCheckConfig.addAttribute("ignoreComments", "false");
062: final String[] expected = { "2: Line matches the illegal pattern '"
063: + illegal + "'." };
064: verify(mCheckConfig, getPath("InputTrailingComment.java"),
065: expected);
066: }
067:
068: public void testIgnoreCommentsCStyle() throws Exception {
069: // See if the comment is removed properly
070: final String illegal = "c-style 1";
071: mCheckConfig.addAttribute("format", illegal);
072: mCheckConfig.addAttribute("ignoreComments", "true");
073: final String[] expected = {};
074: verify(mCheckConfig, getPath("InputTrailingComment.java"),
075: expected);
076: }
077:
078: public void testIgnoreCommentsFalseCStyle() throws Exception {
079: final String illegal = "c-style 1";
080: mCheckConfig.addAttribute("format", illegal);
081: mCheckConfig.addAttribute("ignoreComments", "false");
082: final String[] expected = { "17: Line matches the illegal pattern '"
083: + illegal + "'." };
084: verify(mCheckConfig, getPath("InputTrailingComment.java"),
085: expected);
086: }
087:
088: public void testIgnoreCommentsMultipleCStyle() throws Exception {
089: // See if a second comment on the same line is removed properly
090: final String illegal = "c-style 2";
091: mCheckConfig.addAttribute("format", illegal);
092: mCheckConfig.addAttribute("ignoreComments", "true");
093: final String[] expected = {};
094: verify(mCheckConfig, getPath("InputTrailingComment.java"),
095: expected);
096: }
097:
098: public void testIgnoreCommentsMultiLine() throws Exception {
099: final String illegal = "Let's check multi-line comments";
100: mCheckConfig.addAttribute("format", illegal);
101: mCheckConfig.addAttribute("ignoreComments", "true");
102: final String[] expected = {};
103: verify(mCheckConfig, getPath("InputTrailingComment.java"),
104: expected);
105: }
106:
107: public void testIgnoreCommentsInlineStart() throws Exception {
108: final String illegal = "long ms /";
109: mCheckConfig.addAttribute("format", illegal);
110: mCheckConfig.addAttribute("ignoreComments", "true");
111: final String[] expected = {};
112: verify(mCheckConfig, getPath("InputTrailingComment.java"),
113: expected);
114: }
115:
116: public void testIgnoreCommentsInlineEnd() throws Exception {
117: final String illegal = "int z";
118: mCheckConfig.addAttribute("format", illegal);
119: mCheckConfig.addAttribute("ignoreComments", "true");
120: final String[] expected = { "20: Line matches the illegal pattern '"
121: + illegal + "'." };
122: verify(mCheckConfig, getPath("InputTrailingComment.java"),
123: expected);
124: }
125:
126: public void testIgnoreCommentsInlineMiddle() throws Exception {
127: final String illegal = "int y";
128: mCheckConfig.addAttribute("format", illegal);
129: mCheckConfig.addAttribute("ignoreComments", "true");
130: final String[] expected = { "21: Line matches the illegal pattern '"
131: + illegal + "'." };
132: verify(mCheckConfig, getPath("InputTrailingComment.java"),
133: expected);
134: }
135:
136: public void testIgnoreCommentsNoSpaces() throws Exception {
137: // make sure the comment is not turned into spaces
138: final String illegal = "long ms ";
139: mCheckConfig.addAttribute("format", illegal);
140: mCheckConfig.addAttribute("ignoreComments", "true");
141: final String[] expected = {};
142: verify(mCheckConfig, getPath("InputTrailingComment.java"),
143: expected);
144: }
145:
146: public void test1371588() throws Exception {
147: // StackOverflowError with trailing space and ignoreComments
148: final String illegal = "\\s+$";
149: mCheckConfig.addAttribute("format", illegal);
150: mCheckConfig.addAttribute("ignoreComments", "true");
151: final String[] expected = {};
152: verify(mCheckConfig, getPath("InputTrailingComment.java"),
153: expected);
154: }
155: }
|