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