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