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.Collections;
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.UnnecessarySemicolonInTryWithResourcesCheck;
30
31 public class XpathRegressionUnnecessarySemicolonInTryWithResourcesTest
32 extends AbstractXpathTestSupport {
33
34 private final String checkName =
35 UnnecessarySemicolonInTryWithResourcesCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Test
43 public void testDefault() throws Exception {
44 final File fileToProcess = new File(
45 getPath("InputXpathUnnecessarySemicolonInTryWithResourcesDefault.java"));
46 final DefaultConfiguration moduleConfig =
47 createModuleConfig(UnnecessarySemicolonInTryWithResourcesCheck.class);
48 final String[] expectedViolation = {
49 "11:76: " + getCheckMessage(UnnecessarySemicolonInTryWithResourcesCheck.class,
50 UnnecessarySemicolonInTryWithResourcesCheck.MSG_SEMI),
51 };
52 final List<String> expectedXpathQueries = Collections.singletonList(
53 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
54 + "'InputXpathUnnecessarySemicolonInTryWithResourcesDefault']]"
55 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='m']]/SLIST/LITERAL_TRY"
56 + "/RESOURCE_SPECIFICATION/SEMI"
57 );
58 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
59 }
60
61 @Test
62 public void testNoBrace() throws Exception {
63 final File fileToProcess = new File(getPath(
64 "InputXpathUnnecessarySemicolonInTryWithResourcesNoBrace.java"
65 ));
66
67 final DefaultConfiguration moduleConfig =
68 createModuleConfig(UnnecessarySemicolonInTryWithResourcesCheck.class);
69 moduleConfig.addProperty("allowWhenNoBraceAfterSemicolon", "false");
70
71 final String[] expectedViolation = {
72 "8:44: " + getCheckMessage(UnnecessarySemicolonInTryWithResourcesCheck.class,
73 UnnecessarySemicolonInTryWithResourcesCheck.MSG_SEMI),
74 };
75
76 final List<String> expectedXpathQueries = Collections.singletonList(
77 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
78 + "'InputXpathUnnecessarySemicolonInTryWithResourcesNoBrace']]"
79 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
80 + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/SEMI"
81 );
82 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
83 }
84 }