View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.coding;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.coding.UnusedPrivateFieldCheck;
30  
31  public class XpathRegressionUnusedPrivateFieldTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = UnusedPrivateFieldCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Override
41      public String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/unusedprivatefield";
43      }
44  
45      @Test
46      public void testPrivateField() throws Exception {
47          final File fileToProcess = new File(getPath(
48                  "InputXpathUnusedPrivateFieldOne.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(UnusedPrivateFieldCheck.class);
52  
53          final String[] expectedViolation = {
54              "4:17: " + getCheckMessage(UnusedPrivateFieldCheck.class,
55                      UnusedPrivateFieldCheck.MSG_PRIVATE_FIELD, "unused"),
56          };
57  
58          final List<String> expectedXpathQueries = List.of(
59                 "/COMPILATION_UNIT/CLASS_DEF"
60                        + "[./IDENT[@text='InputXpathUnusedPrivateFieldOne']]"
61                        + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='unused']"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65      }
66  
67      @Test
68      public void testFullCoverage() throws Exception {
69          final File fileToProcess = new File(getPath(
70                  "InputXpathUnusedPrivateFieldTwo.java"));
71  
72          final DefaultConfiguration moduleConfig =
73                  createModuleConfig(UnusedPrivateFieldCheck.class);
74  
75          final String[] expectedViolation = {
76              "12:21: " + getCheckMessage(UnusedPrivateFieldCheck.class,
77                      UnusedPrivateFieldCheck.MSG_PRIVATE_FIELD, "unused"),
78          };
79  
80          final List<String> expectedXpathQueries = List.of(
81                  "/COMPILATION_UNIT/CLASS_DEF"
82                         + "[./IDENT[@text='InputXpathUnusedPrivateFieldTwo']]"
83                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
84                         + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='unused']"
85          );
86  
87          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
88      }
89  
90      @Test
91      public void testInnerUse() throws Exception {
92          final File fileToProcess = new File(getPath(
93                  "InputXpathUnusedPrivateFieldThree.java"));
94  
95          final DefaultConfiguration moduleConfig =
96                  createModuleConfig(UnusedPrivateFieldCheck.class);
97  
98          final String[] expectedViolation = {
99              "5:17: " + getCheckMessage(UnusedPrivateFieldCheck.class,
100                     UnusedPrivateFieldCheck.MSG_PRIVATE_FIELD, "unused"),
101         };
102 
103         final List<String> expectedXpathQueries = List.of(
104                "/COMPILATION_UNIT/ENUM_DEF"
105                       + "[./IDENT[@text='InputXpathUnusedPrivateFieldThree']]"
106                       + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='unused']"
107         );
108 
109         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
110     }
111 
112 }