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.MutableExceptionCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  import static org.junit.Assert.fail;
25  
26  import java.io.File;
27  import java.util.Arrays;
28  import java.util.List;
29  
30  import org.junit.Test;
31  
32  import antlr.CommonHiddenStreamToken;
33  import com.google.common.collect.ImmutableMap;
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.DetailAST;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  
40  public class MutableExceptionCheckTest extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/design/mutableexception";
44      }
45  
46      @Test
47      public void testClassExtendsGenericClass() throws Exception {
48          final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
49  
50          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
51  
52          verify(checkConfig, getPath("InputMutableExceptionClassExtendsGenericClass.java"),
53              expected);
54      }
55  
56      @Test
57      public void testDefault() throws Exception {
58          final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
59  
60          final String[] expected = {
61              "6:9: " + getCheckMessage(MSG_KEY, "errorCode"),
62              "23:9: " + getCheckMessage(MSG_KEY, "errorCode"),
63              "46:9: " + getCheckMessage(MSG_KEY, "errorCode"),
64          };
65  
66          verify(checkConfig, getPath("InputMutableException.java"), expected);
67      }
68  
69      @Test
70      public void testMultipleInputs() throws Exception {
71          final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
72          final String filePath1 = getPath("InputMutableException.java");
73          final String filePath2 = getPath("InputMutableExceptionMultipleInputs.java");
74  
75          final List<String> expected1 = Arrays.asList(
76              "6:9: " + getCheckMessage(MSG_KEY, "errorCode"),
77              "23:9: " + getCheckMessage(MSG_KEY, "errorCode"),
78              "46:9: " + getCheckMessage(MSG_KEY, "errorCode"));
79          final List<String> expected2 = Arrays.asList(
80              "6:9: " + getCheckMessage(MSG_KEY, "errorCode"),
81              "10:9: " + getCheckMessage(MSG_KEY, "errorCode"));
82  
83          final File[] inputs = {new File(filePath1), new File(filePath2)};
84  
85          verify(createChecker(checkConfig), inputs,
86                  ImmutableMap.of(filePath1, expected1, filePath2, expected2));
87      }
88  
89      @Test
90      public void testFormat() throws Exception {
91          final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
92          checkConfig.addAttribute("format", "^.*Failure$");
93          checkConfig.addAttribute("extendedClassNameFormat", "^.*ThreadDeath$");
94          final String[] expected = {
95              "34:13: " + getCheckMessage(MSG_KEY, "errorCode"),
96          };
97  
98          verify(checkConfig, getPath("InputMutableException.java"), expected);
99      }
100 
101     @Test
102     public void testGetAcceptableTokens() {
103         final MutableExceptionCheck obj = new MutableExceptionCheck();
104         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
105         assertArrayEquals("Default acceptable tokens are invalid",
106             expected, obj.getAcceptableTokens());
107     }
108 
109     @Test
110     public void testGetRequiredTokens() {
111         final MutableExceptionCheck obj = new MutableExceptionCheck();
112         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
113         assertArrayEquals("Default required tokens are invalid",
114             expected, obj.getRequiredTokens());
115     }
116 
117     @Test
118     public void testWrongTokenType() {
119         final MutableExceptionCheck obj = new MutableExceptionCheck();
120         final DetailAST ast = new DetailAST();
121         ast.initialize(new CommonHiddenStreamToken(TokenTypes.INTERFACE_DEF, "interface"));
122         try {
123             obj.visitToken(ast);
124             fail("IllegalStateException is expected");
125         }
126         catch (IllegalStateException ex) {
127             //expected
128         }
129     }
130 }