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