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;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
25  
26  import java.io.File;
27  import java.util.List;
28  
29  import org.antlr.v4.runtime.CommonToken;
30  import org.junit.jupiter.api.Test;
31  
32  import com.google.common.collect.ImmutableMap;
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
38  
39  public class UncommentedMainCheckTest
40      extends AbstractModuleTestSupport {
41  
42      @Override
43      public String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/uncommentedmain";
45      }
46  
47      @Test
48      public void testDefaults()
49              throws Exception {
50          final String[] expected = {
51              "17:5: " + getCheckMessage(MSG_KEY),
52              "26:5: " + getCheckMessage(MSG_KEY),
53              "35:5: " + getCheckMessage(MSG_KEY),
54              "99:5: " + getCheckMessage(MSG_KEY),
55          };
56          verifyWithInlineConfigParser(
57                  getPath("InputUncommentedMain.java"), expected);
58      }
59  
60      @Test
61      public void testExcludedClasses()
62              throws Exception {
63          final String[] expected = {
64              "17:5: " + getCheckMessage(MSG_KEY),
65              "35:5: " + getCheckMessage(MSG_KEY),
66              "99:5: " + getCheckMessage(MSG_KEY),
67          };
68          verifyWithInlineConfigParser(
69                  getPath("InputUncommentedMain6.java"), expected);
70      }
71  
72      @Test
73      public void testTokens() {
74          final UncommentedMainCheck check = new UncommentedMainCheck();
75          assertWithMessage("Required tokens should not be null")
76              .that(check.getRequiredTokens())
77              .isNotNull();
78          assertWithMessage("Acceptable tokens should not be null")
79              .that(check.getAcceptableTokens())
80              .isNotNull();
81          assertWithMessage("Invalid default tokens")
82              .that(check.getAcceptableTokens())
83              .isEqualTo(check.getDefaultTokens());
84          assertWithMessage("Invalid acceptable tokens")
85              .that(check.getRequiredTokens())
86              .isEqualTo(check.getDefaultTokens());
87      }
88  
89      @Test
90      public void testDeepDepth() throws Exception {
91          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
92          verifyWithInlineConfigParser(
93                  getPath("InputUncommentedMain2.java"), expected);
94      }
95  
96      @Test
97      public void testVisitPackage() throws Exception {
98          final String[] expected = {
99              "21:5: " + getCheckMessage(MSG_KEY),
100         };
101         verifyWithInlineConfigParser(
102                 getPath("InputUncommentedMain5.java"), expected);
103     }
104 
105     @Test
106     public void testWrongName() throws Exception {
107         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
108         verifyWithInlineConfigParser(
109                 getPath("InputUncommentedMain3.java"), expected);
110     }
111 
112     @Test
113     public void testWrongArrayType() throws Exception {
114         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115         verifyWithInlineConfigParser(
116                 getPath("InputUncommentedMain4.java"), expected);
117     }
118 
119     @Test
120     public void testCompactSourceFile() throws Exception {
121         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
122         verifyWithInlineConfigParser(
123                 getNonCompilablePath("InputUncommentedMainCompactSourceFile.java"),
124                 expected);
125     }
126 
127     @Test
128     public void testCompactSourceFileTraditional() throws Exception {
129         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
130         verifyWithInlineConfigParser(
131                 getNonCompilablePath(
132                     "InputUncommentedMainCompactSourceFileTraditional.java"),
133                 expected);
134     }
135 
136     @Test
137     public void testIllegalStateException() {
138         final UncommentedMainCheck check = new UncommentedMainCheck();
139         final DetailAstImpl ast = new DetailAstImpl();
140         ast.initialize(new CommonToken(TokenTypes.CTOR_DEF, "ctor"));
141         final IllegalStateException exc = getExpectedThrowable(
142                 IllegalStateException.class,
143                 () -> check.visitToken(ast));
144         assertWithMessage("Error message is unexpected")
145             .that(exc.getMessage())
146             .isEqualTo(ast.toString());
147     }
148 
149     @Test
150     public void testRecords()
151             throws Exception {
152 
153         final String[] expected = {
154             "12:5: " + getCheckMessage(MSG_KEY),
155             "20:9: " + getCheckMessage(MSG_KEY),
156             "25:13: " + getCheckMessage(MSG_KEY),
157         };
158 
159         verifyWithInlineConfigParser(
160                 getPath(
161                 "InputUncommentedMainRecords.java"), expected);
162     }
163 
164     @Test
165     public void testStateIsClearedOnBeginTree() throws Exception {
166         final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
167         final String file1 = getPath(
168                 "InputUncommentedMainRecords2.java");
169         final String file2 = getPath(
170                 "InputUncommentedMainBeginTree2.java");
171         final List<String> expectedFirstInput = List.of(
172                 "12:5: " + getCheckMessage(MSG_KEY),
173                 "21:24: " + getCheckMessage(MSG_KEY)
174         );
175         final List<String> expectedSecondInput = List.of(
176                 "13:13: " + getCheckMessage(MSG_KEY)
177         );
178         final File[] inputs = {new File(file1), new File(file2)};
179 
180         verify(createChecker(checkConfig), inputs, ImmutableMap.of(
181             file1, expectedFirstInput,
182             file2, expectedSecondInput));
183     }
184 
185     @Test
186     public void testStateIsClearedOnBeginTree2() throws Exception {
187         final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
188         checkConfig.addProperty("excludedClasses",
189                 "uncommentedmain\\.InputUncommentedMainBeginTreePackage2");
190         final String file1 = getPath(
191                 "InputUncommentedMainBeginTreePackage1.java");
192         final String file2 = getPath(
193                 "InputUncommentedMainBeginTreePackage2.java");
194         final List<String> expectedFirstInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
195         final List<String> expectedSecondInput = List.of(
196                 "3:5: " + getCheckMessage(MSG_KEY),
197                 "12:5: " + getCheckMessage(MSG_KEY)
198         );
199         final File[] inputs = {new File(file1), new File(file2)};
200 
201         verify(createChecker(checkConfig), inputs, ImmutableMap.of(
202             file1, expectedFirstInput,
203             file2, expectedSecondInput));
204     }
205 
206 }