View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.SinglelineDetector.MSG_REGEXP_EXCEEDED;
24  import static com.puppycrawl.tools.checkstyle.checks.regexp.SinglelineDetector.MSG_REGEXP_MINIMUM;
25  
26  import java.io.File;
27  import java.nio.charset.StandardCharsets;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.api.FileText;
33  import com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35  
36  public class RegexpSinglelineCheckTest extends AbstractModuleTestSupport {
37  
38      private static final String[] EMPTY = {};
39  
40      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexpsingleline";
43      }
44  
45      @Test
46      public void testIt() throws Exception {
47          final String[] expected = {
48              "77: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "System\\.(out)|(err)\\.print(ln)?\\("),
49          };
50          verifyWithInlineConfigParser(
51                  getPath("InputRegexpSinglelineSemantic.java"), expected);
52      }
53  
54      @Test
55      public void testMessageProperty()
56              throws Exception {
57          final String[] expected = {
58              "78: Bad line :(",
59          };
60          verifyWithInlineConfigParser(
61                  getPath("InputRegexpSinglelineSemantic2.java"), expected);
62      }
63  
64      @Test
65      public void testIgnoreCaseTrue() throws Exception {
66  
67          final String[] expected = {
68              "78: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
69          };
70          verifyWithInlineConfigParser(
71                  getPath("InputRegexpSinglelineSemantic3.java"), expected);
72      }
73  
74      @Test
75      public void testIgnoreCaseFalse() throws Exception {
76          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
77          verifyWithInlineConfigParser(
78                  getPath("InputRegexpSinglelineSemantic4.java"), expected);
79      }
80  
81      @Test
82      public void testMinimum() throws Exception {
83          final String[] expected = {
84              "1: " + getCheckMessage(MSG_REGEXP_MINIMUM, "500", "\\r"),
85          };
86  
87          verifyWithInlineConfigParser(
88                  getPath("InputRegexpSinglelineSemantic5.java"), expected);
89      }
90  
91      @Test
92      public void testSetMessage() throws Exception {
93          final String[] expected = {
94              "1: someMessage",
95          };
96  
97          verifyWithInlineConfigParser(
98                  getPath("InputRegexpSinglelineSemantic6.java"), expected);
99      }
100 
101     @Test
102     public void testMaximum() throws Exception {
103         verifyWithInlineConfigParser(
104                 getPath("InputRegexpSinglelineSemantic7.java"), EMPTY);
105     }
106 
107     /**
108      * Done as a UT cause new instance of Detector is created each time 'verify' executed.
109      *
110      * @throws Exception some Exception
111      */
112     @Test
113     public void testStateIsBeingReset() throws Exception {
114         final String illegal = "System\\.(out)|(err)\\.print(ln)?\\(";
115         final TestLoggingReporter reporter = new TestLoggingReporter();
116         final DetectorOptions detectorOptions = DetectorOptions.newBuilder()
117                 .reporter(reporter)
118                 .format(illegal)
119                 .maximum(1)
120                 .build();
121 
122         final SinglelineDetector detector =
123                 new SinglelineDetector(detectorOptions);
124         final File file = new File(getPath("InputRegexpSinglelineSemantic8.java"));
125 
126         detector.processLines(new FileText(file, StandardCharsets.UTF_8.name()));
127         detector.processLines(new FileText(file, StandardCharsets.UTF_8.name()));
128         assertWithMessage("Logged unexpected amount of issues")
129                 .that(reporter.getLogCount())
130                 .isEqualTo(0);
131     }
132 
133     @Test
134     public void testDefault() throws Exception {
135         verifyWithInlineConfigParser(
136                 getPath("InputRegexpSinglelineSemantic9.java"), EMPTY);
137     }
138 
139     @Test
140     public void testMessage() throws Exception {
141 
142         final String[] expected = {
143             "17: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
144         };
145         verifyWithInlineConfigParser(
146                 getPath("InputRegexpSinglelineSemantic10.java"), expected);
147     }
148 }