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.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  
30  public class EmptyCatchBlockCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/blocks/emptycatchblock";
35      }
36  
37      @Test
38      public void testGetRequiredTokens() {
39          final EmptyCatchBlockCheck checkObj = new EmptyCatchBlockCheck();
40          final int[] expected = {TokenTypes.LITERAL_CATCH};
41          assertWithMessage("Default required tokens are invalid")
42                  .that(checkObj.getRequiredTokens())
43                  .isEqualTo(expected);
44      }
45  
46      @Test
47      public void testDefault() throws Exception {
48          final String[] expected = {
49              "25:31: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
50              "32:83: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
51          };
52          verifyWithInlineConfigParser(
53                  getPath("InputEmptyCatchBlockDefault.java"), expected);
54      }
55  
56      @Test
57      public void testWithUserSetValues() throws Exception {
58          final String[] expected = {
59              "26:31: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
60              "54:78: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
61              "88:29: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
62              "177:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
63              "186:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
64              "205:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
65              "221:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
66              "230:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
67          };
68          verifyWithInlineConfigParser(
69                  getPath("InputEmptyCatchBlockDefault2.java"), expected);
70      }
71  
72      @Test
73      public void testLinesAreProperlySplitSystemIndependently() throws Exception {
74          final String[] expected = {
75              "25:31: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
76              "53:78: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
77              "87:29: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
78              "176:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
79              "185:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
80              "204:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
81              "220:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
82              "229:33: " + getCheckMessage(MSG_KEY_CATCH_BLOCK_EMPTY),
83          };
84          final String originalLineSeparator = System.getProperty("line.separator");
85          try {
86              System.setProperty("line.separator", "\r\n");
87              verifyWithInlineConfigParser(
88                      getPath("InputEmptyCatchBlockDefaultLF.java"), expected);
89          }
90          finally {
91              System.setProperty("line.separator", originalLineSeparator);
92          }
93      }
94  
95      @Test
96      public void testGetAcceptableTokens() {
97          final EmptyCatchBlockCheck constantNameCheckObj = new EmptyCatchBlockCheck();
98          final int[] actual = constantNameCheckObj.getAcceptableTokens();
99          final int[] expected = {TokenTypes.LITERAL_CATCH };
100         assertWithMessage("Default acceptable tokens are invalid")
101                 .that(actual)
102                 .isEqualTo(expected);
103     }
104 
105 }