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.puppycrawl.tools.checkstyle.checks.imports.AvoidModuleImportCheck.MSG_COUNT;
23 import static com.puppycrawl.tools.checkstyle.checks.imports.AvoidModuleImportCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29
30 public class AvoidModuleImportCheckTest extends AbstractModuleTestSupport {
31
32 @Override
33 public String getPackageLocation() {
34 return "com/puppycrawl/tools/checkstyle/checks/imports/avoidmoduleimport";
35 }
36
37 @Test
38 public void testDefault() throws Exception {
39 final String[] expected = {
40 "12:1: " + getCheckMessage(MSG_KEY, "java.base"),
41 "14:1: " + getCheckMessage(MSG_KEY, "java.xml"),
42 "16:1: " + getCheckMessage(MSG_KEY, "java.logging"),
43 };
44
45 verifyWithInlineConfigParser(
46 getNonCompilablePath("InputAvoidModuleImportDefault.java"), expected);
47 }
48
49 @Test
50 public void testExcludes() throws Exception {
51 final String[] expected = {
52 "12:1: " + getCheckMessage(MSG_KEY, "java.net.http"),
53 };
54
55 verifyWithInlineConfigParser(
56 getNonCompilablePath("InputAvoidModuleImportExcludes.java"), expected);
57 }
58
59 @Test
60 public void testMaxAllowed() throws Exception {
61 final String[] expected = {
62 "14:1: " + getCheckMessage(MSG_COUNT, 2),
63 };
64
65 verifyWithInlineConfigParser(
66 getNonCompilablePath("InputAvoidModuleImportMaxAllowed.java"), expected);
67 }
68
69 @Test
70 public void testMaxAllowedAndExcluded() throws Exception {
71 final String[] expected = {
72 "13:1: " + getCheckMessage(MSG_COUNT, 1),
73 };
74
75 verifyWithInlineConfigParser(
76 getNonCompilablePath("InputAvoidModuleImportMaxAllowedAndExcluded.java"),
77 expected);
78 }
79
80 @Test
81 public void testMaxAllowedMultipleFiles() throws Exception {
82 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
83 verifyWithInlineConfigParser(
84 getNonCompilablePath("InputAvoidModuleImportFile1.java"),
85 getNonCompilablePath("InputAvoidModuleImportFile2.java"),
86 expected);
87 }
88
89 @Test
90 public void testSingleNameModule() throws Exception {
91 final String[] expected = {
92 "12:1: " + getCheckMessage(MSG_KEY, "someModule"),
93 "14:1: " + getCheckMessage(MSG_KEY, "otherModule"),
94 };
95 verifyWithInlineConfigParser(
96 getNonCompilablePath("InputAvoidModuleImportSingleName.java"), expected);
97 }
98
99 @Test
100 public void testCompactSourceFileDefault() throws Exception {
101 final String[] expected = {
102 "10:1: " + getCheckMessage(MSG_KEY, "java.sql"),
103 "12:1: " + getCheckMessage(MSG_KEY, "java.base"),
104 };
105
106 final String filename = "compact/InputAvoidModuleImportDefaultCompactSource.java";
107 verifyWithInlineConfigParser(
108 getNonCompilablePath(filename), expected);
109 }
110
111 @Test
112 public void testCompactSourceFile() throws Exception {
113 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
114
115 final String filename = "compact/InputAvoidModuleImportCompactSource.java";
116 verifyWithInlineConfigParser(
117 getNonCompilablePath(filename), expected);
118 }
119
120 }