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.IllegalImportCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29
30 public class IllegalImportCheckTest extends AbstractModuleTestSupport {
31
32 @Override
33 protected String getPackageLocation() {
34 return "com/puppycrawl/tools/checkstyle/checks/imports/illegalimport";
35 }
36
37 @Test
38 public void testGetRequiredTokens() {
39 final IllegalImportCheck checkObj = new IllegalImportCheck();
40 final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
41 assertWithMessage("Default required tokens are invalid")
42 .that(checkObj.getRequiredTokens())
43 .isEqualTo(expected);
44 }
45
46 @Test
47 public void testWithSupplied()
48 throws Exception {
49 final String[] expected = {
50 "12:1: " + getCheckMessage(MSG_KEY, "java.io.*"),
51 "26:1: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
52 "30:1: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
53 };
54 verifyWithInlineConfigParser(
55 getPath("InputIllegalImportDefault1.java"), expected);
56 }
57
58 @Test
59 public void testWithDefault()
60 throws Exception {
61 final String[] expected = {};
62 verifyWithInlineConfigParser(
63 getPath("InputIllegalImportDefault2.java"), expected);
64 }
65
66 @Test
67 public void testCustomSunPackageWithRegexp()
68 throws Exception {
69 final String[] expected = {
70 "17:1: " + getCheckMessage(MSG_KEY, "sun.reflect.*"),
71 };
72 verifyWithInlineConfigParser(
73 getNonCompilablePath("InputIllegalImportDefault.java"), expected);
74 }
75
76 @Test
77 public void testGetAcceptableTokens() {
78 final IllegalImportCheck testCheckObject =
79 new IllegalImportCheck();
80 final int[] actual = testCheckObject.getAcceptableTokens();
81 final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
82
83 assertWithMessage("Default acceptable tokens are invalid")
84 .that(actual)
85 .isEqualTo(expected);
86 }
87
88 @Test
89 public void testIllegalClasses()
90 throws Exception {
91 final String[] expected = {
92 "14:1: " + getCheckMessage(MSG_KEY, "java.sql.Connection"),
93 "18:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
94 "31:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
95 };
96 verifyWithInlineConfigParser(
97 getPath("InputIllegalImportDefault3.java"), expected);
98 }
99
100 @Test
101 public void testIllegalClassesStarImport()
102 throws Exception {
103 final String[] expected = {
104 "12:1: " + getCheckMessage(MSG_KEY, "java.io.*"),
105 "18:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
106 "31:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
107 };
108 verifyWithInlineConfigParser(
109 getPath("InputIllegalImportDefault4.java"), expected);
110 }
111
112 @Test
113 public void testIllegalPackagesRegularExpression()
114 throws Exception {
115 final String[] expected = {
116 "15:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
117 "16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
118 "19:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
119 "20:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
120 "37:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
121 "38:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
122 "39:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
123 };
124 verifyWithInlineConfigParser(
125 getPath("InputIllegalImportDefault5.java"), expected);
126 }
127
128 @Test
129 public void testIllegalClassesRegularExpression()
130 throws Exception {
131 final String[] expected = {
132 "15:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
133 "16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
134 "20:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
135 };
136 verifyWithInlineConfigParser(
137 getPath("InputIllegalImportDefault6.java"), expected);
138 }
139
140 @Test
141 public void testIllegalPackagesAndClassesRegularExpression()
142 throws Exception {
143 final String[] expected = {
144 "15:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
145 "16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
146 "19:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
147 "20:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
148 "33:1: " + getCheckMessage(MSG_KEY, "java.awt.Component"),
149 "34:1: " + getCheckMessage(MSG_KEY, "java.awt.Graphics2D"),
150 "35:1: " + getCheckMessage(MSG_KEY, "java.awt.HeadlessException"),
151 "36:1: " + getCheckMessage(MSG_KEY, "java.awt.Label"),
152 "37:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
153 "38:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
154 "39:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
155 };
156 verifyWithInlineConfigParser(
157 getPath("InputIllegalImportDefault7.java"), expected);
158 }
159
160 }