View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }