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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck.MSG_KEY;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
30  
31  public class IllegalThrowsCheckTest extends AbstractModuleTestSupport {
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          final DefaultConfiguration checkConfig = createModuleConfig(IllegalThrowsCheck.class);
40  
41          final String[] expected = {
42              "9:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
43              "14:45: " + getCheckMessage(MSG_KEY, "java.lang.RuntimeException"),
44              "14:73: " + getCheckMessage(MSG_KEY, "java.lang.Error"),
45          };
46  
47          verify(checkConfig, getPath("InputIllegalThrows.java"), expected);
48      }
49  
50      @Test
51      public void testIllegalClassNames() throws Exception {
52          final DefaultConfiguration checkConfig = createModuleConfig(IllegalThrowsCheck.class);
53          checkConfig.addAttribute("illegalClassNames",
54                                   "java.lang.Error, java.lang.Exception, NullPointerException");
55  
56          // check that incorrect names don't break the Check
57          checkConfig.addAttribute("illegalClassNames",
58                  "java.lang.IOException.");
59  
60          final String[] expected = {
61              "5:33: " + getCheckMessage(MSG_KEY, "NullPointerException"),
62              "14:73: " + getCheckMessage(MSG_KEY, "java.lang.Error"),
63          };
64  
65          verify(checkConfig, getPath("InputIllegalThrows.java"), expected);
66      }
67  
68      /**
69       * Test to validate the IllegalThrowsCheck with ignoredMethodNames attribute.
70       */
71      @Test
72      public void testIgnoreMethodNames() throws Exception {
73          final DefaultConfiguration checkConfig = createModuleConfig(IllegalThrowsCheck.class);
74          checkConfig.addAttribute("ignoredMethodNames", "methodTwo");
75  
76          final String[] expected = {
77              "9:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
78              "18:35: " + getCheckMessage(MSG_KEY, "Throwable"),
79          };
80  
81          verify(checkConfig, getPath("InputIllegalThrows.java"), expected);
82      }
83  
84      /**
85       * Test to validate the IllegalThrowsCheck with both the attributes specified.
86       */
87      @Test
88      public void testIllegalClassNamesWithIgnoreMethodNames() throws Exception {
89          final DefaultConfiguration checkConfig = createModuleConfig(IllegalThrowsCheck.class);
90          checkConfig.addAttribute("illegalClassNames",
91              "java.lang.Error, java.lang.Exception, NullPointerException, Throwable");
92          checkConfig.addAttribute("ignoredMethodNames", "methodTwo");
93  
94          final String[] expected = {
95              "5:33: " + getCheckMessage(MSG_KEY, "NullPointerException"),
96              "18:35: " + getCheckMessage(MSG_KEY, "Throwable"),
97          };
98  
99          verify(checkConfig, getPath("InputIllegalThrows.java"), expected);
100     }
101 
102     /**
103      * Test to validate the IllegalThrowsCheck with <b>ignoreOverriddenMethods</b>
104      * property.
105      */
106     @Test
107     public void testIgnoreOverriddenMethods() throws Exception {
108         final DefaultConfiguration checkConfig = createModuleConfig(IllegalThrowsCheck.class);
109         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
110 
111         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
112 
113         verify(checkConfig, getPath("InputIllegalThrowsIgnoreOverriddenMethods.java"), expected);
114     }
115 
116     /**
117      * Test to validate the IllegalThrowsCheck without <b>ignoreOverriddenMethods</b>
118      * property.
119      */
120     @Test
121     public void testNotIgnoreOverriddenMethods() throws Exception {
122         final DefaultConfiguration checkConfig = createModuleConfig(IllegalThrowsCheck.class);
123         checkConfig.addAttribute("ignoreOverriddenMethods", "false");
124 
125         final String[] expected = {
126             "7:36: " + getCheckMessage(MSG_KEY, "RuntimeException"),
127             "12:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
128         };
129 
130         verify(checkConfig, getPath("InputIllegalThrowsIgnoreOverriddenMethods.java"), expected);
131     }
132 
133     @Test
134     public void testTokensNotNull() {
135         final IllegalThrowsCheck check = new IllegalThrowsCheck();
136         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
137         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
138         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
139     }
140 }