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 org.junit.jupiter.api.Test;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
25  
26  public class RegexpCheckExamplesTest extends AbstractExamplesModuleTestSupport {
27  
28      @Override
29      public String getPackageLocation() {
30          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexp";
31      }
32  
33      @Test
34      public void testExample1() throws Exception {
35          final String[] expected = {
36  
37          };
38  
39          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
40      }
41  
42      @Test
43      public void testExample2() throws Exception {
44          final String[] expected = {};
45  
46          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
47      }
48  
49      @Test
50      public void testExample3() throws Exception {
51          final String[] expected = {
52              "15: Found duplicate pattern '// \\(c\\) MyCompany'.",
53          };
54  
55          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
56      }
57  
58      @Test
59      public void testExample4() throws Exception {
60          final String[] expected = {
61              "7: Line matches the illegal pattern 'Avoid using System.out.println'.",
62              "21: Line matches the illegal pattern 'Avoid using System.out.println'.",
63              "22: Line matches the illegal pattern 'Avoid using System.out.println'.",
64          };
65  
66          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
67      }
68  
69      @Test
70      public void testExample5() throws Exception {
71          final String[] expected = {
72              "22: Line matches the illegal pattern"
73                  + " 'The error limit has been exceeded, the check is aborting,"
74                  + " there may be more unreported errors.fix me\\.'.",
75          };
76  
77          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
78      }
79  
80      @Test
81      public void testUseCase1() throws Exception {
82          final String[] expected = {
83              "1: Required pattern '// This code is copyrighted\\.' missing in file.",
84          };
85  
86          verifyWithInlineConfigParser(getPath("UseCase1.java"), expected);
87      }
88  
89      @Test
90      public void testUseCase2() throws Exception {
91          final String[] expected = {
92  
93          };
94  
95          verifyWithInlineConfigParser(getPath("UseCase2.java"), expected);
96      }
97  
98      @Test
99      public void testUseCase3() throws Exception {
100         final String[] expected = {
101             "20: Found duplicate pattern '// This code is copyrighted\\n// \\(c\\) MyCompany'.",
102         };
103 
104         verifyWithInlineConfigParser(getPath("UseCase3.java"), expected);
105     }
106 
107     @Test
108     public void testUseCase4() throws Exception {
109         final String[] expected = {
110             "1: Required pattern 'Copyright' missing in file.",
111         };
112 
113         verifyWithInlineConfigParser(getPath("UseCase4.java"), expected);
114     }
115 
116     @Test
117     public void testUseCase9() throws Exception {
118         final String[] expected = {
119             "23: Line matches the illegal pattern 'System\\.out\\.println'.",
120             "24: Line matches the illegal pattern 'System\\.out\\.println'.",
121         };
122 
123         verifyWithInlineConfigParser(getPath("UseCase9.java"), expected);
124     }
125 
126     @Test
127     public void testExample6() throws Exception {
128         final String[] expected = {
129             "20: Line matches the illegal pattern 'System\\.out\\.println'.",
130         };
131 
132         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
133     }
134 
135     @Test
136     public void testExample7() throws Exception {
137         final String[] expected = {
138             "19: Line matches the illegal pattern 'System\\.out\\.println'.",
139             "20: Line matches the illegal pattern 'System\\.out\\.println'.",
140         };
141 
142         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
143     }
144 
145     @Test
146     public void testUseCase5() throws Exception {
147         final String[] expected = {
148             "17: Line matches the illegal pattern 'Trailing whitespace'.",
149         };
150 
151         verifyWithInlineConfigParser(getPath("UseCase5.java"), expected);
152     }
153 
154     @Test
155     public void testUseCase6() throws Exception {
156         final String[] expected = {
157             "17: Line matches the illegal pattern '(?i)fix me\\.'.",
158         };
159 
160         verifyWithInlineConfigParser(getPath("UseCase6.java"), expected);
161     }
162 
163     @Test
164     public void testUseCase8() throws Exception {
165         final String[] expected = {
166             "26: Line matches the illegal pattern"
167                 + " 'The error limit has been exceeded, the check is aborting,"
168                 + " there may be more unreported errors.(?i)fix me\\.'.",
169         };
170 
171         verifyWithInlineConfigParser(getPath("UseCase8.java"), expected);
172     }
173 
174     @Test
175     public void testUseCase7() throws Exception {
176         final String[] expected = {
177 
178         };
179 
180         verifyWithInlineConfigParser(getPath("UseCase7.java"), expected);
181     }
182 
183 }