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.naming;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
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 LocalFinalVariableNameCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename";
37 }
38
39 @Test
40 public void testGetRequiredTokens() {
41 final LocalFinalVariableNameCheck checkObj =
42 new LocalFinalVariableNameCheck();
43 assertWithMessage("LocalFinalVariableNameCheck#getRequiredTokens should return empty array "
44 + "by default")
45 .that(checkObj.getRequiredTokens())
46 .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
47 }
48
49 @Test
50 public void testDefault()
51 throws Exception {
52
53 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
54
55 final String[] expected = {
56 "126:19: " + getCheckMessage(MSG_INVALID_PATTERN, "CDE", pattern),
57 };
58 verifyWithInlineConfigParser(
59 getPath("InputLocalFinalVariableName.java"), expected);
60 }
61
62 @Test
63 public void testSet()
64 throws Exception {
65
66 final String pattern = "[A-Z]+";
67
68 final String[] expected = {
69 "125:19: " + getCheckMessage(MSG_INVALID_PATTERN, "cde", pattern),
70 };
71 verifyWithInlineConfigParser(
72 getPath("InputLocalFinalVariableName1.java"), expected);
73 }
74
75 @Test
76 public void testInnerClass()
77 throws Exception {
78 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
79 verifyWithInlineConfigParser(
80 getPath("InputLocalFinalVariableNameInnerClass.java"), expected);
81 }
82
83 @Test
84 public void testGetAcceptableTokens() {
85 final LocalFinalVariableNameCheck localFinalVariableNameCheckObj =
86 new LocalFinalVariableNameCheck();
87 final int[] actual = localFinalVariableNameCheckObj.getAcceptableTokens();
88 final int[] expected = {
89 TokenTypes.VARIABLE_DEF,
90 TokenTypes.PARAMETER_DEF,
91 TokenTypes.RESOURCE,
92 };
93 assertWithMessage("Default acceptable tokens are invalid")
94 .that(actual)
95 .isEqualTo(expected);
96 }
97
98 @Test
99 public void testTryWithResources() throws Exception {
100
101 final String pattern = "[A-Z]+";
102
103 final String[] expected = {
104 "32:30: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
105 "43:29: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
106 "63:22: " + getCheckMessage(MSG_INVALID_PATTERN, "zf", pattern),
107 "82:30: " + getCheckMessage(MSG_INVALID_PATTERN, "fis8859_1", pattern),
108 "86:32: " + getCheckMessage(MSG_INVALID_PATTERN, "isrutf8", pattern),
109 };
110 verifyWithInlineConfigParser(
111 getPath("InputLocalFinalVariableNameTryResources.java"), expected);
112 }
113
114 @Test
115 public void testTryWithResourcesJava9() throws Exception {
116
117 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
118 verifyWithInlineConfigParser(
119 getPath("InputLocalFinalVariableNameTryResourcesJava9.java"), expected);
120 }
121
122 @Test
123 public void testUnnamedVariables() throws Exception {
124 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
125
126 final String[] expected = {
127 "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
128 "21:32: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
129 "29:24: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
130 "31:24: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
131 "37:19: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
132 "38:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
133 };
134 verifyWithInlineConfigParser(
135 getNonCompilablePath("InputLocalFinalVariableNameUnnamedVariables.java"),
136 expected);
137 }
138
139 }