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.MutableExceptionCheck.MSG_KEY;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.antlr.v4.runtime.CommonToken;
30  import org.junit.jupiter.api.Test;
31  
32  import com.google.common.collect.ImmutableMap;
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
38  
39  public class MutableExceptionCheckTest extends AbstractModuleTestSupport {
40  
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  
49          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
50  
51          verifyWithInlineConfigParser(
52                  getPath("InputMutableExceptionClassExtendsGenericClass.java"),
53                  expected);
54      }
55  
56      @Test
57      public void testDefault() throws Exception {
58  
59          final String[] expected = {
60              "14:9: " + getCheckMessage(MSG_KEY, "errorCode"),
61              "31:9: " + getCheckMessage(MSG_KEY, "errorCode"),
62              "54:9: " + getCheckMessage(MSG_KEY, "errorCode"),
63          };
64  
65          verifyWithInlineConfigParser(
66                  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("InputMutableException2.java");
73          final String filePath2 = getPath("InputMutableExceptionMultipleInputs.java");
74  
75          final List<String> expected1 = Arrays.asList(
76              "14:9: " + getCheckMessage(MSG_KEY, "errorCode"),
77              "31:9: " + getCheckMessage(MSG_KEY, "errorCode"),
78              "54:9: " + getCheckMessage(MSG_KEY, "errorCode"));
79          final List<String> expected2 = Arrays.asList(
80              "14:9: " + getCheckMessage(MSG_KEY, "errorCode"),
81              "18: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 String[] expected = {
92              "42:13: " + getCheckMessage(MSG_KEY, "errorCode"),
93          };
94  
95          verifyWithInlineConfigParser(
96                  getPath("InputMutableException3.java"), expected);
97      }
98  
99      @Test
100     public void testNested() throws Exception {
101 
102         final String[] expected = {
103             "15:9: " + getCheckMessage(MSG_KEY, "code"),
104         };
105 
106         verifyWithInlineConfigParser(
107                 getPath("InputMutableExceptionNested.java"), expected);
108     }
109 
110     @Test
111     public void testGetAcceptableTokens() {
112         final MutableExceptionCheck obj = new MutableExceptionCheck();
113         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
114         assertWithMessage("Default acceptable tokens are invalid")
115                 .that(obj.getAcceptableTokens())
116                 .isEqualTo(expected);
117     }
118 
119     @Test
120     public void testGetRequiredTokens() {
121         final MutableExceptionCheck obj = new MutableExceptionCheck();
122         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
123         assertWithMessage("Default required tokens are invalid")
124                 .that(obj.getRequiredTokens())
125                 .isEqualTo(expected);
126     }
127 
128     @Test
129     public void testWrongTokenType() {
130         final MutableExceptionCheck obj = new MutableExceptionCheck();
131         final DetailAstImpl ast = new DetailAstImpl();
132         ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
133         try {
134             obj.visitToken(ast);
135             assertWithMessage("IllegalStateException is expected")
136                     .fail();
137         }
138         catch (IllegalStateException ex) {
139             // exception is expected
140             assertWithMessage("Invalid exception message")
141                     .that(ex.getMessage())
142                     .isEqualTo("interface[0x-1]");
143         }
144     }
145 
146 }