1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }