View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck.MSG_KEY;
23  import static org.junit.Assert.assertEquals;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import antlr.CommonHiddenStreamToken;
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class UncommentedMainCheckTest
36      extends AbstractModuleTestSupport {
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/uncommentedmain";
40      }
41  
42      @Test
43      public void testDefaults()
44              throws Exception {
45          final DefaultConfiguration checkConfig =
46              createModuleConfig(UncommentedMainCheck.class);
47          final String[] expected = {
48              "14: " + getCheckMessage(MSG_KEY),
49              "23: " + getCheckMessage(MSG_KEY),
50              "32: " + getCheckMessage(MSG_KEY),
51              "96: " + getCheckMessage(MSG_KEY),
52          };
53          verify(checkConfig, getPath("InputUncommentedMain.java"), expected);
54      }
55  
56      @Test
57      public void testExcludedClasses()
58              throws Exception {
59          final DefaultConfiguration checkConfig =
60              createModuleConfig(UncommentedMainCheck.class);
61          checkConfig.addAttribute("excludedClasses", "\\.Main.*$");
62          final String[] expected = {
63              "14: " + getCheckMessage(MSG_KEY),
64              "32: " + getCheckMessage(MSG_KEY),
65              "96: " + getCheckMessage(MSG_KEY),
66          };
67          verify(checkConfig, getPath("InputUncommentedMain.java"), expected);
68      }
69  
70      @Test
71      public void testTokens() {
72          final UncommentedMainCheck check = new UncommentedMainCheck();
73          Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
74          Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
75          Assert.assertArrayEquals("Invalid default tokens", check.getDefaultTokens(),
76                  check.getAcceptableTokens());
77          Assert.assertArrayEquals("Invalid acceptable tokens", check.getDefaultTokens(),
78                  check.getRequiredTokens());
79      }
80  
81      @Test
82      public void testDeepDepth() throws Exception {
83          final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
84          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
85          verify(checkConfig, getPath("InputUncommentedMain2.java"), expected);
86      }
87  
88      @Test
89      public void testVisitPackage() throws Exception {
90          final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
91          checkConfig.addAttribute("excludedClasses", "uncommentedmain\\.InputUncommentedMain5");
92          final String[] expected = {
93              "14: " + getCheckMessage(MSG_KEY),
94          };
95          verify(checkConfig, getPath("InputUncommentedMain5.java"), expected);
96      }
97  
98      @Test
99      public void testWrongName() throws Exception {
100         final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
101         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
102         verify(checkConfig, getPath("InputUncommentedMain3.java"), expected);
103     }
104 
105     @Test
106     public void testWrongArrayType() throws Exception {
107         final DefaultConfiguration checkConfig = createModuleConfig(UncommentedMainCheck.class);
108         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
109         verify(checkConfig, getPath("InputUncommentedMain4.java"), expected);
110     }
111 
112     @Test
113     public void testIllegalStateException() {
114         final UncommentedMainCheck check = new UncommentedMainCheck();
115         final DetailAST ast = new DetailAST();
116         ast.initialize(new CommonHiddenStreamToken(TokenTypes.CTOR_DEF, "ctor"));
117         try {
118             check.visitToken(ast);
119             Assert.fail("IllegalStateException is expected");
120         }
121         catch (IllegalStateException ex) {
122             assertEquals("Error message is unexpected",
123                     ast.toString(), ex.getMessage());
124         }
125 
126     }
127 }