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.design;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.design.ThrowsCountCheck.MSG_KEY;
24  
25  import org.antlr.v4.runtime.CommonToken;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  
32  public class ThrowsCountCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/design/throwscount";
37      }
38  
39      @Test
40      public void testThrowsCountDefault() throws Exception {
41  
42          final String[] expected = {
43              "25:20: " + getCheckMessage(MSG_KEY, 5, 4),
44              "30:20: " + getCheckMessage(MSG_KEY, 5, 4),
45              "35:20: " + getCheckMessage(MSG_KEY, 6, 4),
46              "63:43: " + getCheckMessage(MSG_KEY, 5, 4),
47          };
48  
49          verifyWithInlineConfigParser(
50                  getPath("InputThrowsCountDefault.java"), expected);
51      }
52  
53      @Test
54      public void testThrowsCountCustomMaxCount() throws Exception {
55  
56          final String[] expected = {
57              "35:20: " + getCheckMessage(MSG_KEY, 6, 5),
58          };
59  
60          verifyWithInlineConfigParser(
61                  getPath("InputThrowsCountCustomMaxCount.java"), expected);
62      }
63  
64      @Test
65      public void testGetAcceptableTokens() {
66          final ThrowsCountCheck obj = new ThrowsCountCheck();
67          final int[] expected = {TokenTypes.LITERAL_THROWS};
68          assertWithMessage("Default acceptable tokens are invalid")
69              .that(obj.getAcceptableTokens())
70              .isEqualTo(expected);
71      }
72  
73      @Test
74      public void testGetRequiredTokens() {
75          final ThrowsCountCheck obj = new ThrowsCountCheck();
76          final int[] expected = {TokenTypes.LITERAL_THROWS};
77          assertWithMessage("Default required tokens are invalid")
78              .that(obj.getRequiredTokens())
79              .isEqualTo(expected);
80      }
81  
82      @Test
83      public void testWrongTokenType() {
84          final ThrowsCountCheck obj = new ThrowsCountCheck();
85          final DetailAstImpl ast = new DetailAstImpl();
86          ast.initialize(new CommonToken(TokenTypes.CLASS_DEF, "class"));
87          try {
88              obj.visitToken(ast);
89              assertWithMessage("IllegalStateException is expected").fail();
90          }
91          catch (IllegalStateException ex) {
92              assertWithMessage("Invalid exception message")
93                  .that(ex.getMessage())
94                  .isEqualTo(ast.toString());
95          }
96      }
97  
98      @Test
99      public void testThrowsCountNotIgnorePrivateMethods() throws Exception {
100         final String[] expected = {
101             "25:20: " + getCheckMessage(MSG_KEY, 5, 4),
102             "30:20: " + getCheckMessage(MSG_KEY, 5, 4),
103             "35:20: " + getCheckMessage(MSG_KEY, 6, 4),
104             "43:28: " + getCheckMessage(MSG_KEY, 5, 4),
105             "63:43: " + getCheckMessage(MSG_KEY, 5, 4),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputThrowsCountNotIgnorePrivateMethods.java"), expected);
109     }
110 
111     @Test
112     public void testThrowsCountMethodWithAnnotation() throws Exception {
113         final String[] expected = {
114             "26:26: " + getCheckMessage(MSG_KEY, 5, 4),
115         };
116         verifyWithInlineConfigParser(
117                 getPath("InputThrowsCountMethodWithAnnotation.java"), expected);
118     }
119 
120     @Test
121     public void testThrowsCountMaxAllowZero() throws Exception {
122         final String[] expected = {
123             "17:20: " + getCheckMessage(MSG_KEY, 1, 0),
124             "21:20: " + getCheckMessage(MSG_KEY, 1, 0),
125             "25:20: " + getCheckMessage(MSG_KEY, 5, 0),
126             "31:20: " + getCheckMessage(MSG_KEY, 5, 0),
127             "37:20: " + getCheckMessage(MSG_KEY, 6, 0),
128             "46:28: " + getCheckMessage(MSG_KEY, 5, 0),
129             "67:43: " + getCheckMessage(MSG_KEY, 5, 0),
130         };
131         verifyWithInlineConfigParser(
132                 getPath("InputThrowsCountMaxAllowZero.java"), expected);
133     }
134 
135 }