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.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 }