View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.regexp;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_ILLEGAL_REGEXP;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  public class RegexpMultilineCheckExamplesTest extends AbstractExamplesModuleTestSupport {
30  
31      @Override
32      public String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexpmultiline";
34      }
35  
36      @Test
37      public void testExample1() throws Exception {
38          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
39          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
40      }
41  
42      @Test
43      public void testExample2() throws Exception {
44          final String[] expected = {
45              "14: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
46              "16: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
47              "20: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
48              "24: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
49              "29: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
50              "31: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
51              "33: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
52              "36: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
53              "38: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
54              "40: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
55          };
56  
57          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
58      }
59  
60      @Test
61      public void testExample3() throws Exception {
62          final String[] expected = {
63              "15: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.out.*?print\\("),
64          };
65  
66          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
67      }
68  
69      @Test
70      public void testExample4() throws Exception {
71          final String[] expected = {
72              "40: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test #[0-9]+:[A-Za-z ]+"),
73              "42: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test #[0-9]+:[A-Za-z ]+"),
74          };
75  
76          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
77      }
78  
79      @Test
80      public void testExample5() throws Exception {
81          final String[] expected = {
82              "30: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test #[0-9]+:[A-Za-z ]+"),
83              "39: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test #[0-9]+:[A-Za-z ]+"),
84          };
85  
86          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
87      }
88  
89      @Test
90      public void testExample7() throws Exception {
91          final String[] expected = {
92              "4: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test.*string"),
93              "30: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test.*string"),
94              "39: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test.*string"),
95          };
96  
97          verifyWithInlineConfigParser(getPath("Example7.java"), expected);
98      }
99  
100     @Test
101     public void testExample8() throws Exception {
102         final String[] expected = {
103             "15: Avoid using System.out/err for printing.",
104             "17: Avoid using System.out/err for printing.",
105             "30: Avoid using System.out/err for printing.",
106             "32: Avoid using System.out/err for printing.",
107             "34: Avoid using System.out/err for printing.",
108             "37: Avoid using System.out/err for printing.",
109             "39: Avoid using System.out/err for printing.",
110             "41: Avoid using System.out/err for printing.",
111         };
112 
113         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
114     }
115 
116     @Test
117     public void testExample9() throws Exception {
118         final String[] expected = {
119             "39: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Test #[0-9]+:[A-Za-z ]+"),
120         };
121 
122         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
123     }
124 
125 }