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.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class IllegalThrowsCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/coding/illegalthrows";
35      }
36  
37      @Test
38      public void testDefault() throws Exception {
39  
40          final String[] expected = {
41              "19:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
42              "24:45: " + getCheckMessage(MSG_KEY, "java.lang.RuntimeException"),
43              "25:22: " + getCheckMessage(MSG_KEY, "java.lang.Error"),
44          };
45  
46          verifyWithInlineConfigParser(
47                  getPath("InputIllegalThrowsTestDefault.java"), expected);
48      }
49  
50      @Test
51      public void testIllegalClassNames() throws Exception {
52          // check that incorrect names don't break the Check
53          final String[] expected = {
54              "15:33: " + getCheckMessage(MSG_KEY, "NullPointerException"),
55              "24:73: " + getCheckMessage(MSG_KEY, "java.lang.Error"),
56          };
57  
58          verifyWithInlineConfigParser(
59                  getPath("InputIllegalThrowsTestIllegalClassNames.java"), expected);
60      }
61  
62      /**
63       * Test to validate the IllegalThrowsCheck with ignoredMethodNames attribute.
64       */
65      @Test
66      public void testIgnoreMethodNames() throws Exception {
67  
68          final String[] expected = {
69              "19:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
70              "28:35: " + getCheckMessage(MSG_KEY, "Throwable"),
71          };
72  
73          verifyWithInlineConfigParser(
74                  getPath("InputIllegalThrowsIgnoreMethodNames.java"), expected);
75      }
76  
77      /**
78       * Test to validate the IllegalThrowsCheck with both the attributes specified.
79       */
80      @Test
81      public void testIllegalClassNamesWithIgnoreMethodNames() throws Exception {
82          final String[] expected = {
83              "14:33: " + getCheckMessage(MSG_KEY, "NullPointerException"),
84              "27:35: " + getCheckMessage(MSG_KEY, "Throwable"),
85          };
86  
87          verifyWithInlineConfigParser(
88                  getPath("InputIllegalThrowsTestClassNames.java"), expected);
89      }
90  
91      /**
92       * Test to validate the IllegalThrowsCheck with <b>ignoreOverriddenMethods</b>
93       * property.
94       */
95      @Test
96      public void testIgnoreOverriddenMethods() throws Exception {
97  
98          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
99  
100         verifyWithInlineConfigParser(
101                 getPath("InputIllegalThrowsIgnoreOverriddenMethods.java"), expected);
102     }
103 
104     /**
105      * Test to validate the IllegalThrowsCheck without <b>ignoreOverriddenMethods</b>
106      * property.
107      */
108     @Test
109     public void testNotIgnoreOverriddenMethods() throws Exception {
110 
111         final String[] expected = {
112             "17:36: " + getCheckMessage(MSG_KEY, "RuntimeException"),
113             "22:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
114         };
115 
116         verifyWithInlineConfigParser(
117                 getPath("InputIllegalThrowsNotIgnoreOverriddenMethod.java"), expected);
118     }
119 
120     @Test
121     public void testTokensNotNull() {
122         final IllegalThrowsCheck check = new IllegalThrowsCheck();
123         assertWithMessage("Acceptable tokens should not be null")
124             .that(check.getAcceptableTokens())
125             .isNotNull();
126         assertWithMessage("Default tokens should not be null")
127             .that(check.getDefaultTokens())
128             .isNotNull();
129         assertWithMessage("Required tokens should not be null")
130             .that(check.getRequiredTokens())
131             .isNotNull();
132     }
133 
134 }