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.NoFinalizerCheck;
30
31 public class XpathRegressionNoFinalizerTest extends AbstractXpathTestSupport {
32
33 @Override
34 protected String getCheckName() {
35 return NoFinalizerCheck.class.getSimpleName();
36 }
37
38 @Test
39 public void testMain() throws Exception {
40 final File fileToProcess = new File(
41 getPath("InputXpathNoFinalizerMain.java"));
42
43 final DefaultConfiguration moduleConfig =
44 createModuleConfig(NoFinalizerCheck.class);
45
46 final String[] expectedViolation = {
47 "8:5: " + getCheckMessage(NoFinalizerCheck.class,
48 NoFinalizerCheck.MSG_KEY),
49 };
50
51 final List<String> expectedXpathQueries = Arrays.asList(
52 "/COMPILATION_UNIT/CLASS_DEF"
53 + "[./IDENT[@text='InputXpathNoFinalizerMain']]"
54 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]",
55 "/COMPILATION_UNIT/CLASS_DEF"
56 + "[./IDENT[@text='InputXpathNoFinalizerMain']]"
57 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS",
58 "/COMPILATION_UNIT/CLASS_DEF"
59 + "[./IDENT[@text='InputXpathNoFinalizerMain']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/LITERAL_PROTECTED"
61 );
62
63 runVerifications(moduleConfig, fileToProcess, expectedViolation,
64 expectedXpathQueries);
65 }
66
67 @Test
68 public void testInner() throws Exception {
69 final File fileToProcess = new File(
70 getPath("InputXpathNoFinalizerInner.java"));
71
72 final DefaultConfiguration moduleConfig =
73 createModuleConfig(NoFinalizerCheck.class);
74
75 final String[] expectedViolation = {
76 "10:9: " + getCheckMessage(NoFinalizerCheck.class,
77 NoFinalizerCheck.MSG_KEY),
78 };
79
80 final List<String> expectedXpathQueries = Arrays.asList(
81 "/COMPILATION_UNIT/CLASS_DEF"
82 + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
83 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
84 + "METHOD_DEF[./IDENT[@text='finalize']]",
85 "/COMPILATION_UNIT/CLASS_DEF"
86 + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
87 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
88 + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS",
89 "/COMPILATION_UNIT/CLASS_DEF"
90 + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
91 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
92 + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/"
93 + "ANNOTATION[./IDENT[@text='Override']]",
94 "/COMPILATION_UNIT/CLASS_DEF"
95 + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
96 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
97 + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/"
98 + "ANNOTATION[./IDENT[@text='Override']]/AT"
99 );
100
101 runVerifications(moduleConfig, fileToProcess, expectedViolation,
102 expectedXpathQueries);
103 }
104
105 }