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.Arrays;
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.coding.MagicNumberCheck;
30  
31  public class XpathRegressionMagicNumberTest extends AbstractXpathTestSupport {
32  
33      @Override
34      protected String getCheckName() {
35          return MagicNumberCheck.class.getSimpleName();
36      }
37  
38      @Test
39      public void testVariable() throws Exception {
40          final File fileToProcess =
41                  new File(getPath("InputXpathMagicNumberVariable.java"));
42  
43          final DefaultConfiguration moduleConfig =
44                  createModuleConfig(MagicNumberCheck.class);
45  
46          final String[] expectedViolation = {
47              "5:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "5"),
48          };
49  
50          final List<String> expectedXpathQueries = Arrays.asList(
51              "/COMPILATION_UNIT/CLASS_DEF"
52                      + "[./IDENT[@text='InputXpathMagicNumberVariable']]"
53                      + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='d']]"
54                      + "/ASSIGN/EXPR[./NUM_INT[@text='5']]",
55              "/COMPILATION_UNIT/CLASS_DEF"
56                      + "[./IDENT[@text='InputXpathMagicNumberVariable']]"
57                      + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='d']]"
58                      + "/ASSIGN/EXPR/NUM_INT[@text='5']"
59          );
60  
61          runVerifications(moduleConfig, fileToProcess, expectedViolation,
62              expectedXpathQueries);
63      }
64  
65      @Test
66      public void testMethodDef() throws Exception {
67          final File fileToProcess =
68                  new File(getPath("InputXpathMagicNumberMethodDef.java"));
69  
70          final DefaultConfiguration moduleConfig =
71                  createModuleConfig(MagicNumberCheck.class);
72  
73          final String[] expectedViolation = {
74              "5:17: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "20"),
75          };
76  
77          final List<String> expectedXpathQueries = Arrays.asList(
78                  "/COMPILATION_UNIT/CLASS_DEF"
79                          + "[./IDENT[@text='InputXpathMagicNumberMethodDef']]"
80                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodWithMagicNumber']]"
81                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='x']]/ASSIGN/EXPR[./"
82                          + "NUM_INT[@text='20']]",
83                  "/COMPILATION_UNIT/CLASS_DEF"
84                          + "[./IDENT[@text='InputXpathMagicNumberMethodDef']]"
85                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodWithMagicNumber']]"
86                          + "/SLIST/VARIABLE_DEF[./IDENT[@text='x']]/ASSIGN/EXPR/NU"
87                          + "M_INT[@text='20']"
88          );
89          runVerifications(moduleConfig, fileToProcess, expectedViolation,
90                  expectedXpathQueries);
91      }
92  
93      @Test
94      public void testAnotherVariable() throws Exception {
95          final File fileToProcess =
96                  new File(getPath("InputXpathMagicNumberAnotherVariable.java"));
97  
98          final DefaultConfiguration moduleConfig =
99                  createModuleConfig(MagicNumberCheck.class);
100 
101         final String[] expectedViolation = {
102             "13:21: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "20"),
103         };
104 
105         final List<String> expectedXpathQueries = List.of(
106                 "/COMPILATION_UNIT/CLASS_DEF"
107                         + "[./IDENT[@text='InputXpathMagicNumberAnotherVariable']]"
108                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='performOperation']]"
109                         + "/SLIST/LITERAL_TRY/LITERAL_CATCH/SLIST/LITERAL_IF"
110                         + "/LITERAL_ELSE/SLIST/EXPR/ASSIGN"
111                         + "[./IDENT[@text='a']]/NUM_INT[@text='20']"
112         );
113         runVerifications(moduleConfig, fileToProcess, expectedViolation,
114                 expectedXpathQueries);
115     }
116 }