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.blocks;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck.MSG_KEY_CATCH_BLOCK_EMPTY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class EmptyCatchBlockCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/blocks/emptycatchblock";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final EmptyCatchBlockCheck checkObj = new EmptyCatchBlockCheck();
41          final int[] expected = {TokenTypes.LITERAL_CATCH};
42          assertWithMessage("Default required tokens are invalid")
43                  .that(checkObj.getRequiredTokens())
44                  .isEqualTo(expected);
45      }
46  
47      @Test
48      public void testDefault() throws Exception {
49          final String[] expected = {
50              "25:31: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
51              "32:83: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
52          };
53          verifyWithInlineConfigParser(
54                  getPath("InputEmptyCatchBlockDefault.java"), expected);
55      }
56  
57      @Test
58      public void testWithUserSetValues() throws Exception {
59          final String[] expected = {
60              "26:31: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
61              "54:78: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
62              "88:29: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
63          };
64          verifyWithInlineConfigParser(
65                  getPath("InputEmptyCatchBlockDefault2.java"), expected);
66      }
67  
68      @Test
69      public void testLinesAreProperlySplitSystemIndependently() throws Exception {
70          final String[] expected = {
71              "25:31: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
72              "53:78: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
73              "87:29: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
74          };
75          final String originalLineSeparator = System.lineSeparator();
76          try {
77              System.setProperty("line.separator", "\r\n");
78              verifyWithInlineConfigParser(
79                      getPath("InputEmptyCatchBlockDefaultLF.java"), expected);
80          }
81          finally {
82              System.setProperty("line.separator", originalLineSeparator);
83          }
84      }
85  
86      @Test
87      public void testGetAcceptableTokens() {
88          final EmptyCatchBlockCheck constantNameCheckObj = new EmptyCatchBlockCheck();
89          final int[] actual = constantNameCheckObj.getAcceptableTokens();
90          final int[] expected = {TokenTypes.LITERAL_CATCH };
91          assertWithMessage("Default acceptable tokens are invalid")
92                  .that(actual)
93                  .isEqualTo(expected);
94      }
95  
96      @Test
97      public void testNonEmptyCatch() throws Exception {
98          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
99          verifyWithInlineConfigParser(
100                 getPath("InputEmptyCatchBlockNonEmptyCatch.java"), expected);
101     }
102 
103     @Test
104     public void testCommentRecognition() throws Exception {
105         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
106         verifyWithInlineConfigParser(
107                 getPath("InputEmptyCatchBlockCommentRecognition.java"), expected);
108     }
109 
110     @Test
111     public void testNonEmptyCatch2() throws Exception {
112         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
113         verifyWithInlineConfigParser(
114                 getPath("InputEmptyCatchBlockNonEmptyCatch2.java"), expected);
115     }
116 
117     @Test
118     public void testCommentRecognition2() throws Exception {
119         final String[] expected = {
120             "17:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
121             "26:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
122             "45:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
123             "61:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
124             "70:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
125         };
126         verifyWithInlineConfigParser(
127                 getPath("InputEmptyCatchBlockCommentRecognition2.java"), expected);
128     }
129 
130     @Test
131     public void testNonEmptyCatchLf() throws Exception {
132         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
133         verifyWithInlineConfigParser(
134                 getPath("InputEmptyCatchBlockNonEmptyCatchLF.java"), expected);
135     }
136 
137     @Test
138     public void testCommentRecognitionLf() throws Exception {
139         final String[] expected = {
140             "17:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
141             "26:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
142             "45:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
143             "61:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
144             "70:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
145         };
146         verifyWithInlineConfigParser(
147                 getPath("InputEmptyCatchBlockCommentRecognitionLF.java"), expected);
148     }
149 
150 }