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