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.filters;
21  
22  import org.junit.jupiter.api.Test;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck;
26  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
27  
28  public class SuppressionSingleFilterTest extends AbstractModuleTestSupport {
29  
30      private static final String FORMAT = "TODO$";
31      private static final String MESSAGE = getCheckMessage(RegexpSinglelineCheck.class,
32          "regexp.exceeded", FORMAT);
33      private static final String[] ALL_MESSAGES = {
34          "25: " + MESSAGE,
35      };
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/filters/suppressionsinglefilter";
40      }
41  
42      @Test
43      public void testDefault() throws Exception {
44          final String[] suppressed = {
45              "25: " + MESSAGE,
46          };
47          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter2.java"), suppressed);
48      }
49  
50      @Test
51      public void testMatching() throws Exception {
52          final String[] suppressed = {
53              "25: " + MESSAGE,
54          };
55          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter3.java"), suppressed);
56      }
57  
58      @Test
59      public void testNonMatchingLineNumber() throws Exception {
60          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
61          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter4.java"), suppressed);
62      }
63  
64      @Test
65      public void testNonMatchingColumnNumber() throws Exception {
66          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
67          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter5.java"), suppressed);
68      }
69  
70      @Test
71      public void testNonMatchingFileRegexp() throws Exception {
72          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
73          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter6.java"), suppressed);
74      }
75  
76      @Test
77      public void testNonMatchingModuleId() throws Exception {
78          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
79          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter7.java"), suppressed);
80      }
81  
82      @Test
83      public void testMatchingModuleId() throws Exception {
84          final String[] suppressed = {
85              "25: " + MESSAGE,
86          };
87          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter10.java"), suppressed);
88      }
89  
90      @Test
91      public void testNonMatchingChecks() throws Exception {
92          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
93          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter8.java"), suppressed);
94      }
95  
96      @Test
97      public void testNotMatchingMessage() throws Exception {
98          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
99          verifySuppressedWithParser(getPath("InputSuppressionSingleFilter9.java"), suppressed);
100     }
101 
102     @Test
103     public void testMatchMessage() throws Exception {
104         final String[] suppressed = {
105             "25: " + MESSAGE,
106         };
107         verifySuppressedWithParser(getPath("InputSuppressionSingleFilter.java"), suppressed);
108     }
109 
110     private void verifySuppressedWithParser(String fileName, String... suppressed)
111             throws Exception {
112         verifyFilterWithInlineConfigParser(fileName, ALL_MESSAGES,
113                                            removeSuppressed(ALL_MESSAGES, suppressed));
114     }
115 
116 }