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.imports.RedundantImportCheck;
30
31 public class XpathRegressionRedundantImportTest extends AbstractXpathTestSupport {
32
33 private final String checkName = RedundantImportCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Test
41 public void testInternal() throws Exception {
42 final File fileToProcess =
43 new File(getPath("InputXpathRedundantImportInternal.java"));
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(RedundantImportCheck.class);
46 final String[] expectedViolation = {
47 "3:1: " + getCheckMessage(RedundantImportCheck.class,
48 RedundantImportCheck.MSG_SAME, "org.checkstyle.suppressionxpathfilter"
49 + ".redundantimport.InputXpathRedundantImportInternal"),
50 };
51 final List<String> expectedXpathQueries = Collections.singletonList(
52 "/COMPILATION_UNIT/IMPORT");
53
54 runVerifications(moduleConfig, fileToProcess, expectedViolation,
55 expectedXpathQueries);
56 }
57
58 @Test
59 public void testLibrary() throws Exception {
60 final File fileToProcess =
61 new File(getPath("InputXpathRedundantImportLibrary.java"));
62 final DefaultConfiguration moduleConfig =
63 createModuleConfig(RedundantImportCheck.class);
64 final String[] expectedViolation = {
65 "3:1: " + getCheckMessage(RedundantImportCheck.class,
66 RedundantImportCheck.MSG_LANG, "java.lang.String"),
67 };
68 final List<String> expectedXpathQueries = Collections.singletonList(
69 "/COMPILATION_UNIT/IMPORT");
70
71 runVerifications(moduleConfig, fileToProcess, expectedViolation,
72 expectedXpathQueries);
73 }
74
75 @Test
76 public void testDuplicate() throws Exception {
77 final File fileToProcess =
78 new File(getPath("InputXpathRedundantImportDuplicate.java"));
79 final DefaultConfiguration moduleConfig =
80 createModuleConfig(RedundantImportCheck.class);
81 final String[] expectedViolation = {
82 "4:1: " + getCheckMessage(RedundantImportCheck.class,
83 RedundantImportCheck.MSG_DUPLICATE, 3, "java.util.Scanner"),
84 };
85 final List<String> expectedXpathQueries = Collections.singletonList(
86 "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='Scanner']]");
87
88 runVerifications(moduleConfig, fileToProcess, expectedViolation,
89 expectedXpathQueries);
90 }
91
92 }