View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.ThrowsCountCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.fail;
26  
27  import org.junit.Test;
28  
29  import antlr.CommonHiddenStreamToken;
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.api.DetailAST;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  
35  public class ThrowsCountCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/design/throwscount";
40      }
41  
42      @Test
43      public void testDefault() throws Exception {
44          final DefaultConfiguration checkConfig = createModuleConfig(ThrowsCountCheck.class);
45  
46          final String[] expected = {
47              "17:20: " + getCheckMessage(MSG_KEY, 5, 4),
48              "22:20: " + getCheckMessage(MSG_KEY, 5, 4),
49              "27:20: " + getCheckMessage(MSG_KEY, 6, 4),
50              "55:43: " + getCheckMessage(MSG_KEY, 5, 4),
51          };
52  
53          verify(checkConfig, getPath("InputThrowsCount.java"), expected);
54      }
55  
56      @Test
57      public void testMax() throws Exception {
58          final DefaultConfiguration checkConfig = createModuleConfig(ThrowsCountCheck.class);
59          checkConfig.addAttribute("max", "5");
60  
61          final String[] expected = {
62              "27:20: " + getCheckMessage(MSG_KEY, 6, 5),
63          };
64  
65          verify(checkConfig, getPath("InputThrowsCount.java"), expected);
66      }
67  
68      @Test
69      public void testGetAcceptableTokens() {
70          final ThrowsCountCheck obj = new ThrowsCountCheck();
71          final int[] expected = {TokenTypes.LITERAL_THROWS};
72          assertArrayEquals("Default acceptable tokens are invalid",
73              expected, obj.getAcceptableTokens());
74      }
75  
76      @Test
77      public void testGetRequiredTokens() {
78          final ThrowsCountCheck obj = new ThrowsCountCheck();
79          final int[] expected = {TokenTypes.LITERAL_THROWS};
80          assertArrayEquals("Default required tokens are invalid",
81              expected, obj.getRequiredTokens());
82      }
83  
84      @Test
85      public void testWrongTokenType() {
86          final ThrowsCountCheck obj = new ThrowsCountCheck();
87          final DetailAST ast = new DetailAST();
88          ast.initialize(new CommonHiddenStreamToken(TokenTypes.CLASS_DEF, "class"));
89          try {
90              obj.visitToken(ast);
91              fail("IllegalStateException is expected");
92          }
93          catch (IllegalStateException ex) {
94              assertEquals("Invalid exception message", ast.toString(), ex.getMessage());
95          }
96      }
97  
98      @Test
99      public void testNotIgnorePrivateMethod() throws Exception {
100         final DefaultConfiguration checkConfig = createModuleConfig(ThrowsCountCheck.class);
101         checkConfig.addAttribute("ignorePrivateMethods", "false");
102         final String[] expected = {
103             "17:20: " + getCheckMessage(MSG_KEY, 5, 4),
104             "22:20: " + getCheckMessage(MSG_KEY, 5, 4),
105             "27:20: " + getCheckMessage(MSG_KEY, 6, 4),
106             "35:28: " + getCheckMessage(MSG_KEY, 5, 4),
107             "55:43: " + getCheckMessage(MSG_KEY, 5, 4),
108         };
109         verify(checkConfig, getPath("InputThrowsCount.java"), expected);
110     }
111 
112     @Test
113     public void testMethodWithAnnotation() throws Exception {
114         final DefaultConfiguration checkConfig = createModuleConfig(ThrowsCountCheck.class);
115         final String[] expected = {
116             "18:26: " + getCheckMessage(MSG_KEY, 5, 4),
117         };
118         verify(checkConfig, getPath("InputThrowsCountMethodWithAnnotation.java"), expected);
119     }
120 
121 }