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.AvoidStarImportCheck.MSG_COUNT;
24 import static com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck.MSG_KEY;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32 public class AvoidStarImportCheckTest
33 extends AbstractModuleTestSupport {
34
35 @Override
36 public String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/imports/avoidstarimport";
38 }
39
40 @Test
41 public void testDefaultOperation()
42 throws Exception {
43 final String[] expected = {
44 "12:15: " + getCheckMessage(MSG_KEY, "java.io.*"),
45 "13:17: " + getCheckMessage(MSG_KEY, "java.lang.*"),
46 "28:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
47 "29:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
48 "31:27: " + getCheckMessage(MSG_KEY, "java.io.File.*"),
49 };
50
51 verifyWithInlineConfigParser(
52 getPath("InputAvoidStarImportDefault.java"),
53 expected);
54 }
55
56 @Test
57 public void testExcludes()
58 throws Exception {
59
60 final String[] expected2 = {
61 "31:27: " + getCheckMessage(MSG_KEY, "java.io.File.*"),
62 };
63 verifyWithInlineConfigParser(
64 getPath("InputAvoidStarImportExcludes.java"),
65 expected2);
66 }
67
68 @Test
69 public void testAllowClassImports() throws Exception {
70
71 final String[] expected2 = {
72 "28:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
73 "29:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
74 "31:27: " + getCheckMessage(MSG_KEY, "java.io.File.*"), };
75 verifyWithInlineConfigParser(
76 getPath("InputAvoidStarImportAllowClass.java"), expected2);
77 }
78
79 @Test
80 public void testAllowStaticMemberImports() throws Exception {
81
82 final String[] expected2 = {
83 "12:15: " + getCheckMessage(MSG_KEY, "java.io.*"),
84 "13:17: " + getCheckMessage(MSG_KEY, "java.lang.*"),
85 };
86 verifyWithInlineConfigParser(
87 getPath("InputAvoidStarImportAllowStaticMember.java"), expected2);
88 }
89
90 @Test
91 public void testGetAcceptableTokens() {
92 final AvoidStarImportCheck testCheckObject =
93 new AvoidStarImportCheck();
94 final int[] actual = testCheckObject.getAcceptableTokens();
95 final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
96 assertWithMessage("Default acceptable tokens are invalid")
97 .that(actual)
98 .isEqualTo(expected);
99 }
100
101 @Test
102 public void testGetRequiredTokens() {
103 final AvoidStarImportCheck testCheckObject =
104 new AvoidStarImportCheck();
105 final int[] actual = testCheckObject.getRequiredTokens();
106 final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
107
108 assertWithMessage("Default required tokens are invalid")
109 .that(actual)
110 .isEqualTo(expected);
111 }
112
113 @Test
114 public void testMaxAllowedStarImports() throws Exception {
115 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
116
117 verifyWithInlineConfigParser(
118 getPath("InputAvoidStarImportMaxAllowed.java"), expected);
119 }
120
121 @Test
122 public void testMaxAllowedStarImports3() throws Exception {
123 final String[] expected = {
124 "13:17: " + getCheckMessage(MSG_COUNT, 1),
125 };
126
127 verifyWithInlineConfigParser(
128 getPath("InputAvoidStarImportMaxAllowed3.java"), expected);
129 }
130
131 @Test
132 public void testMaxAllowedMultipleFiles() throws Exception {
133 final String[] excepted = CommonUtil.EMPTY_STRING_ARRAY;
134
135 verifyWithInlineConfigParser(
136 getPath("InputAvoidStarImportMaxAllowed.java"),
137 getPath("InputAvoidStarImportMaxAllowed2.java"),
138 excepted);
139 }
140
141 }