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