1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.imports;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_DUPLICATE;
24 import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_LANG;
25 import static com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck.MSG_SAME;
26
27 import java.io.File;
28 import java.util.Arrays;
29 import java.util.List;
30
31 import org.junit.jupiter.api.Test;
32
33 import com.google.common.collect.ImmutableMap;
34 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
38
39 public class RedundantImportCheckTest
40 extends AbstractModuleTestSupport {
41
42 @Override
43 protected String getPackageLocation() {
44 return "com/puppycrawl/tools/checkstyle/checks/imports/redundantimport";
45 }
46
47 @Test
48 public void testGetRequiredTokens() {
49 final RedundantImportCheck checkObj = new RedundantImportCheck();
50 final int[] expected = {
51 TokenTypes.IMPORT,
52 TokenTypes.STATIC_IMPORT,
53 TokenTypes.PACKAGE_DEF,
54 TokenTypes.MODULE_IMPORT,
55 };
56 assertWithMessage("Default required tokens are invalid")
57 .that(checkObj.getRequiredTokens())
58 .isEqualTo(expected);
59 }
60
61 @Test
62 public void testStateIsClearedOnBeginTree1()
63 throws Exception {
64 final DefaultConfiguration checkConfig = createModuleConfig(RedundantImportCheck.class);
65 final String inputWithWarnings = getPath("InputRedundantImportCheckClearState.java");
66 final String inputWithoutWarnings = getPath("InputRedundantImportWithoutWarnings.java");
67 final List<String> expectedFirstInput = Arrays.asList(
68 "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.Arrays.asList"),
69 "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.util.List")
70 );
71 final List<String> expectedSecondInput = Arrays.asList(CommonUtil.EMPTY_STRING_ARRAY);
72 final File[] inputs = {new File(inputWithWarnings), new File(inputWithoutWarnings)};
73
74 verify(createChecker(checkConfig), inputs, ImmutableMap.of(
75 inputWithWarnings, expectedFirstInput,
76 inputWithoutWarnings, expectedSecondInput));
77 }
78
79 @Test
80 public void testWithChecker()
81 throws Exception {
82 final String[] expected = {
83 "9:1: " + getCheckMessage(MSG_SAME,
84 "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*"),
85 "10:1: " + getCheckMessage(MSG_SAME,
86 "com.puppycrawl.tools.checkstyle.checks.imports.redundantimport."
87 + "InputRedundantImportBug"),
88 "12:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
89 "13:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
90 "16:1: " + getCheckMessage(MSG_DUPLICATE, 15, "java.util.List"),
91 "28:1: " + getCheckMessage(MSG_DUPLICATE, 27, "javax.swing.WindowConstants.*"),
92 };
93 verifyWithInlineConfigParser(
94 getPath("InputRedundantImportWithChecker.java"), expected);
95 }
96
97 @Test
98 public void testUnnamedPackage()
99 throws Exception {
100 final String[] expected = {
101 "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.List"),
102 "12:1: " + getCheckMessage(MSG_LANG, "java.lang.String"),
103 };
104 verifyWithInlineConfigParser(
105 getNonCompilablePath("InputRedundantImport_UnnamedPackage.java"),
106 expected);
107 }
108
109 @Test
110 public void testGetAcceptableTokens() {
111 final RedundantImportCheck testCheckObject =
112 new RedundantImportCheck();
113 final int[] actual = testCheckObject.getAcceptableTokens();
114 final int[] expected = {
115 TokenTypes.IMPORT,
116 TokenTypes.STATIC_IMPORT,
117 TokenTypes.PACKAGE_DEF,
118 TokenTypes.MODULE_IMPORT,
119 };
120
121 assertWithMessage("Default acceptable tokens are invalid")
122 .that(actual)
123 .isEqualTo(expected);
124 }
125
126 @Test
127 public void testBeginTreePackage() throws Exception {
128 final String file1 = getPath("InputRedundantImportCheckClearState.java");
129 final String file2 = getPath("InputRedundantImportWithoutPackage.java");
130 final List<String> expectedFirstInput = Arrays.asList(
131 "10:1: " + getCheckMessage(MSG_DUPLICATE, 9, "java.util.Arrays.asList"),
132 "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.util.List")
133 );
134 final List<String> expectedSecondInput = Arrays.asList(
135 "9:1: " + getCheckMessage(MSG_LANG, "java.lang.*"),
136 "10:1: " + getCheckMessage(MSG_LANG, "java.lang.String")
137 );
138 verifyWithInlineConfigParser(file1, file2, expectedFirstInput, expectedSecondInput);
139 }
140
141 @Test
142 public void testModuleImportBuiltInModules() throws Exception {
143 final String[] expected = {
144 "11:1: " + getCheckMessage(MSG_DUPLICATE, 10, "java.base"),
145 "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "java.logging"),
146 };
147 verifyWithInlineConfigParser(
148 getNonCompilablePath("InputRedundantImportBuiltInModules.java"), expected);
149 }
150
151 @Test
152 public void testModuleImportCustomModules() throws Exception {
153 final String[] expected = {
154 "11:1: " + getCheckMessage(MSG_DUPLICATE, 10, "moduleA"),
155 "13:1: " + getCheckMessage(MSG_DUPLICATE, 12, "moduleB"),
156 };
157 verifyWithInlineConfigParser(
158 getNonCompilablePath("InputRedundantImportCustomModules.java"), expected);
159 }
160 }