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 static com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck;
32
33 public class XpathRegressionVisibilityModifierTest extends AbstractXpathTestSupport {
34
35 private final String checkName = VisibilityModifierCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Test
43 public void testDefaultModifier() throws Exception {
44 final File fileToProcess =
45 new File(getPath("InputXpathVisibilityModifierDefault.java"));
46
47 final DefaultConfiguration moduleConfig =
48 createModuleConfig(VisibilityModifierCheck.class);
49
50 final String[] expectedViolation = {
51 "6:9: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field"),
52 };
53
54 final List<String> expectedXpathQueries = Collections.singletonList(
55 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
56 + "@text='InputXpathVisibilityModifierDefault']]"
57 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='field']"
58 );
59
60 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
61 }
62
63 @Test
64 public void testAnnotation() throws Exception {
65 final File fileToProcess =
66 new File(getPath("InputXpathVisibilityModifierAnnotation.java"));
67
68 final DefaultConfiguration moduleConfig =
69 createModuleConfig(VisibilityModifierCheck.class);
70 moduleConfig.addProperty("ignoreAnnotationCanonicalNames", "Deprecated");
71
72 final String[] expectedViolation = {
73 "5:12: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY,
74 "annotatedString"),
75 };
76
77 final List<String> expectedXpathQueries = Collections.singletonList(
78 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
79 + "@text='InputXpathVisibilityModifierAnnotation']]"
80 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='annotatedString']"
81 );
82
83 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
84 }
85
86 @Test
87 public void testAnonymousClass() throws Exception {
88 final File fileToProcess =
89 new File(getPath("InputXpathVisibilityModifierAnonymous.java"));
90
91 final DefaultConfiguration moduleConfig =
92 createModuleConfig(VisibilityModifierCheck.class);
93
94 final String[] expectedViolation = {
95 "6:23: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field1"),
96 };
97
98 final List<String> expectedXpathQueries = Collections.singletonList(
99 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
100 + "@text='InputXpathVisibilityModifierAnonymous']]"
101 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='runnable']]"
102 + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Runnable']]"
103 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='field1']"
104 );
105
106 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
107 }
108
109 @Test
110 public void testInnerClass() throws Exception {
111 final File fileToProcess =
112 new File(getPath("InputXpathVisibilityModifierInner.java"));
113
114 final DefaultConfiguration moduleConfig =
115 createModuleConfig(VisibilityModifierCheck.class);
116
117 final String[] expectedViolation = {
118 "7:20: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field2"),
119 };
120
121 final List<String> expectedXpathQueries = Collections.singletonList(
122 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
123 + "@text='InputXpathVisibilityModifierInner']]"
124 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
125 + "VARIABLE_DEF/IDENT[@text='field2']"
126 );
127
128 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
129 }
130 }