View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.internal.utils.TestUtil;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
39  
40  public class MutableExceptionCheckTest extends AbstractModuleTestSupport {
41  
42      @Override
43      public String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/design/mutableexception";
45      }
46  
47      @Test
48      public void testClassExtendsGenericClass() throws Exception {
49  
50          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
51  
52          verifyWithInlineConfigParser(
53                  getPath("InputMutableExceptionClassExtendsGenericClass.java"),
54                  expected);
55      }
56  
57      @Test
58      public void testDefault() throws Exception {
59  
60          final String[] expected = {
61              "14:9: " + getCheckMessage(MSG_KEY, "errorCode"),
62              "31:9: " + getCheckMessage(MSG_KEY, "errorCode"),
63              "54:9: " + getCheckMessage(MSG_KEY, "errorCode"),
64          };
65  
66          verifyWithInlineConfigParser(
67                  getPath("InputMutableException.java"), expected);
68      }
69  
70      @Test
71      public void testMultipleInputs() throws Exception {
72          final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
73          final String filePath1 = getPath("InputMutableException2.java");
74          final String filePath2 = getPath("InputMutableExceptionMultipleInputs.java");
75  
76          final List<String> expected1 = Arrays.asList(
77              "14:9: " + getCheckMessage(MSG_KEY, "errorCode"),
78              "31:9: " + getCheckMessage(MSG_KEY, "errorCode"),
79              "54:9: " + getCheckMessage(MSG_KEY, "errorCode"));
80          final List<String> expected2 = Arrays.asList(
81              "14:9: " + getCheckMessage(MSG_KEY, "errorCode"),
82              "18:9: " + getCheckMessage(MSG_KEY, "errorCode"));
83  
84          final File[] inputs = {new File(filePath1), new File(filePath2)};
85  
86          verify(createChecker(checkConfig), inputs,
87                  ImmutableMap.of(filePath1, expected1, filePath2, expected2));
88      }
89  
90      @Test
91      public void testFormat() throws Exception {
92          final String[] expected = {
93              "42:13: " + getCheckMessage(MSG_KEY, "errorCode"),
94          };
95  
96          verifyWithInlineConfigParser(
97                  getPath("InputMutableException3.java"), expected);
98      }
99  
100     @Test
101     public void testNested() throws Exception {
102 
103         final String[] expected = {
104             "15:9: " + getCheckMessage(MSG_KEY, "code"),
105         };
106 
107         verifyWithInlineConfigParser(
108                 getPath("InputMutableExceptionNested.java"), expected);
109     }
110 
111     @Test
112     public void testGetAcceptableTokens() {
113         final MutableExceptionCheck obj = new MutableExceptionCheck();
114         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
115         assertWithMessage("Default acceptable tokens are invalid")
116                 .that(obj.getAcceptableTokens())
117                 .isEqualTo(expected);
118     }
119 
120     @Test
121     public void testGetRequiredTokens() {
122         final MutableExceptionCheck obj = new MutableExceptionCheck();
123         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF};
124         assertWithMessage("Default required tokens are invalid")
125                 .that(obj.getRequiredTokens())
126                 .isEqualTo(expected);
127     }
128 
129     @Test
130     public void testWrongTokenType() {
131         final MutableExceptionCheck obj = new MutableExceptionCheck();
132         final DetailAstImpl ast = new DetailAstImpl();
133         ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
134         final IllegalStateException exc =
135             TestUtil.getExpectedThrowable(
136                 IllegalStateException.class, () -> {
137                     obj.visitToken(ast);
138                 });
139         // exception is expected
140         assertWithMessage("Invalid exception message")
141                 .that(exc.getMessage())
142                 .isEqualTo("interface[0x-1]");
143     }
144 
145 }