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