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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.ExplicitInitializationCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28
29 public class ExplicitInitializationCheckTest extends AbstractModuleTestSupport {
30
31 @Override
32 protected String getPackageLocation() {
33 return "com/puppycrawl/tools/checkstyle/checks/coding/explicitinitialization";
34 }
35
36 @Test
37 public void testDefault() throws Exception {
38 final String[] expected = {
39 "11:17: " + getCheckMessage(MSG_KEY, "x", 0),
40 "12:20: " + getCheckMessage(MSG_KEY, "bar", "null"),
41 "16:18: " + getCheckMessage(MSG_KEY, "y4", 0),
42 "17:21: " + getCheckMessage(MSG_KEY, "b1", "false"),
43 "21:22: " + getCheckMessage(MSG_KEY, "str1", "null"),
44 "21:35: " + getCheckMessage(MSG_KEY, "str3", "null"),
45 "22:9: " + getCheckMessage(MSG_KEY, "ar1", "null"),
46 "25:11: " + getCheckMessage(MSG_KEY, "f1", 0),
47 "26:12: " + getCheckMessage(MSG_KEY, "d1", 0),
48 "29:17: " + getCheckMessage(MSG_KEY, "ch1", "\\0"),
49 "30:17: " + getCheckMessage(MSG_KEY, "ch2", "\\0"),
50 "46:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
51 "47:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
52 "54:21: " + getCheckMessage(MSG_KEY, "x", 0),
53 "55:29: " + getCheckMessage(MSG_KEY, "bar", "null"),
54 "56:31: " + getCheckMessage(MSG_KEY, "barArray", "null"),
55 "59:17: " + getCheckMessage(MSG_KEY, "x", 0),
56 "60:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
57 "61:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
58 "96:19: " + getCheckMessage(MSG_KEY, "shortVariable", "0"),
59 "97:18: " + getCheckMessage(MSG_KEY, "bite", "0"),
60 "98:12: " + getCheckMessage(MSG_KEY, "d", "0"),
61 };
62 verifyWithInlineConfigParser(
63 getPath("InputExplicitInitialization.java"),
64 expected);
65 }
66
67 @Test
68 public void testTokensNotNull() {
69 final ExplicitInitializationCheck check = new ExplicitInitializationCheck();
70 assertWithMessage("Acceptable tokens should not be null")
71 .that(check.getAcceptableTokens())
72 .isNotNull();
73 assertWithMessage("Default tokens should not be null")
74 .that(check.getDefaultTokens())
75 .isNotNull();
76 assertWithMessage("Required tokens should not be null")
77 .that(check.getRequiredTokens())
78 .isNotNull();
79 }
80
81 @Test
82 public void testOnlyObjectReferences() throws Exception {
83 final String[] expected = {
84 "12:20: " + getCheckMessage(MSG_KEY, "bar", "null"),
85 "21:22: " + getCheckMessage(MSG_KEY, "str1", "null"),
86 "21:35: " + getCheckMessage(MSG_KEY, "str3", "null"),
87 "22:9: " + getCheckMessage(MSG_KEY, "ar1", "null"),
88 "46:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
89 "47:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
90 "55:29: " + getCheckMessage(MSG_KEY, "bar", "null"),
91 "56:31: " + getCheckMessage(MSG_KEY, "barArray", "null"),
92 "60:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
93 "61:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
94 };
95 verifyWithInlineConfigParser(
96 getPath("InputExplicitInitializationOnlyObjectReference.java"),
97 expected);
98 }
99
100 }