1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }