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