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 testIllegalStateException() {
121         final UncommentedMainCheck check = new UncommentedMainCheck();
122         final DetailAstImpl ast = new DetailAstImpl();
123         ast.initialize(new CommonToken(TokenTypes.CTOR_DEF, "ctor"));
124         final IllegalStateException exc = getExpectedThrowable(
125                 IllegalStateException.class,
126                 () -> check.visitToken(ast));
127         assertWithMessage("Error message is unexpected")
128             .that(exc.getMessage())
129             .isEqualTo(ast.toString());
130     }
131 
132     @Test
133     public void testRecords()
134             throws Exception {
135 
136         final String[] expected = {
137             "12:5: " + getCheckMessage(MSG_KEY),
138             "20:9: " + getCheckMessage(MSG_KEY),
139             "25:13: " + getCheckMessage(MSG_KEY),
140         };
141 
142         verifyWithInlineConfigParser(
143                 getPath(
144                 "InputUncommentedMainRecords.java"), expected);
145     }
146 
147     @Test
148     public void testStateIsClearedOnBeginTree() throws Exception {
149         final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
150         final String file1 = getPath(
151                 "InputUncommentedMainRecords2.java");
152         final String file2 = getPath(
153                 "InputUncommentedMainBeginTree2.java");
154         final List<String> expectedFirstInput = List.of(
155                 "12:5: " + getCheckMessage(MSG_KEY),
156                 "21:24: " + getCheckMessage(MSG_KEY)
157         );
158         final List<String> expectedSecondInput = List.of(
159                 "13:13: " + getCheckMessage(MSG_KEY)
160         );
161         final File[] inputs = {new File(file1), new File(file2)};
162 
163         verify(createChecker(checkConfig), inputs, ImmutableMap.of(
164             file1, expectedFirstInput,
165             file2, expectedSecondInput));
166     }
167 
168     @Test
169     public void testStateIsClearedOnBeginTree2() throws Exception {
170         final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
171         checkConfig.addProperty("excludedClasses",
172                 "uncommentedmain\\.InputUncommentedMainBeginTreePackage2");
173         final String file1 = getPath(
174                 "InputUncommentedMainBeginTreePackage1.java");
175         final String file2 = getPath(
176                 "InputUncommentedMainBeginTreePackage2.java");
177         final List<String> expectedFirstInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
178         final List<String> expectedSecondInput = List.of(
179                 "3:5: " + getCheckMessage(MSG_KEY),
180                 "12:5: " + getCheckMessage(MSG_KEY)
181         );
182         final File[] inputs = {new File(file1), new File(file2)};
183 
184         verify(createChecker(checkConfig), inputs, ImmutableMap.of(
185             file1, expectedFirstInput,
186             file2, expectedSecondInput));
187     }
188 }