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 org.checkstyle.suppressionxpathfilter;
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCaseDefaultColonCheck;
30  
31  public class XpathRegressionNoWhitespaceBeforeCaseDefaultColonTest
32          extends AbstractXpathTestSupport {
33  
34      private final String checkName = NoWhitespaceBeforeCaseDefaultColonCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Test
42      public void testOne() throws Exception {
43          final File fileToProcess =
44                  new File(getPath(
45                          "InputXpathNoWhitespaceBeforeCaseDefaultColonOne.java"));
46  
47          final DefaultConfiguration moduleConfig =
48                  createModuleConfig(NoWhitespaceBeforeCaseDefaultColonCheck.class);
49  
50          final String[] expectedViolation = {
51              "6:20: " + getCheckMessage(NoWhitespaceBeforeCaseDefaultColonCheck.class,
52                      NoWhitespaceBeforeCaseDefaultColonCheck.MSG_KEY, ":"),
53          };
54  
55          final List<String> expectedXpathQueries = Collections.singletonList(
56              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
57                      + "'InputXpathNoWhitespaceBeforeCaseDefaultColonOne']]"
58                      + "/OBJBLOCK/INSTANCE_INIT/SLIST/LITERAL_SWITCH/CASE_GROUP/LITERAL_CASE/COLON"
59          );
60  
61          runVerifications(moduleConfig, fileToProcess, expectedViolation,
62                  expectedXpathQueries);
63      }
64  
65      @Test
66      public void testTwo() throws Exception {
67          final File fileToProcess =
68                  new File(getPath(
69                          "InputXpathNoWhitespaceBeforeCaseDefaultColonTwo.java"));
70  
71          final DefaultConfiguration moduleConfig =
72                  createModuleConfig(NoWhitespaceBeforeCaseDefaultColonCheck.class);
73  
74          final String[] expectedViolation = {
75              "10:21: " + getCheckMessage(NoWhitespaceBeforeCaseDefaultColonCheck.class,
76                      NoWhitespaceBeforeCaseDefaultColonCheck.MSG_KEY, ":"),
77          };
78  
79          final List<String> expectedXpathQueries = Collections.singletonList(
80              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
81                      + "'InputXpathNoWhitespaceBeforeCaseDefaultColonTwo']]"
82                      + "/OBJBLOCK/INSTANCE_INIT/SLIST/LITERAL_SWITCH/CASE_GROUP"
83                      + "/LITERAL_DEFAULT/COLON"
84          );
85  
86          runVerifications(moduleConfig, fileToProcess, expectedViolation,
87                  expectedXpathQueries);
88      }
89  
90      @Test
91      public void testThree() throws Exception {
92          final File fileToProcess =
93                  new File(getPath(
94                          "InputXpathNoWhitespaceBeforeCaseDefaultColonThree.java"));
95  
96          final DefaultConfiguration moduleConfig =
97                  createModuleConfig(NoWhitespaceBeforeCaseDefaultColonCheck.class);
98  
99          final String[] expectedViolation = {
100             "7:21: " + getCheckMessage(NoWhitespaceBeforeCaseDefaultColonCheck.class,
101                         NoWhitespaceBeforeCaseDefaultColonCheck.MSG_KEY, ":"),
102         };
103 
104         final List<String> expectedXpathQueries = Collections.singletonList(
105             "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
106                     + "'InputXpathNoWhitespaceBeforeCaseDefaultColonThree']]"
107                     + "/OBJBLOCK/INSTANCE_INIT/SLIST/LITERAL_SWITCH/CASE_GROUP"
108                     + "/LITERAL_CASE/COLON"
109         );
110 
111         runVerifications(moduleConfig, fileToProcess, expectedViolation,
112                 expectedXpathQueries);
113     }
114 
115     @Test
116     public void testFour() throws Exception {
117         final File fileToProcess =
118                 new File(getPath(
119                         "InputXpathNoWhitespaceBeforeCaseDefaultColonFour.java"));
120 
121         final DefaultConfiguration moduleConfig =
122                 createModuleConfig(NoWhitespaceBeforeCaseDefaultColonCheck.class);
123 
124         final String[] expectedViolation = {
125             "11:21: " + getCheckMessage(NoWhitespaceBeforeCaseDefaultColonCheck.class,
126                         NoWhitespaceBeforeCaseDefaultColonCheck.MSG_KEY, ":"),
127         };
128 
129         final List<String> expectedXpathQueries = Collections.singletonList(
130             "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
131                     + "'InputXpathNoWhitespaceBeforeCaseDefaultColonFour']]"
132                     + "/OBJBLOCK/INSTANCE_INIT/SLIST/LITERAL_SWITCH/CASE_GROUP"
133                     + "/LITERAL_DEFAULT/COLON"
134         );
135 
136         runVerifications(moduleConfig, fileToProcess, expectedViolation,
137                 expectedXpathQueries);
138     }
139 }