View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.coding.ExplicitInitializationCheck.MSG_KEY;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  
30  public class ExplicitInitializationCheckTest extends AbstractModuleTestSupport {
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 DefaultConfiguration checkConfig =
39              createModuleConfig(ExplicitInitializationCheck.class);
40          final String[] expected = {
41              "4:17: " + getCheckMessage(MSG_KEY, "x", 0),
42              "5:20: " + getCheckMessage(MSG_KEY, "bar", "null"),
43              "9:18: " + getCheckMessage(MSG_KEY, "y4", 0),
44              "10:21: " + getCheckMessage(MSG_KEY, "b1", "false"),
45              "14:22: " + getCheckMessage(MSG_KEY, "str1", "null"),
46              "14:35: " + getCheckMessage(MSG_KEY, "str3", "null"),
47              "15:9: " + getCheckMessage(MSG_KEY, "ar1", "null"),
48              "18:11: " + getCheckMessage(MSG_KEY, "f1", 0),
49              "19:12: " + getCheckMessage(MSG_KEY, "d1", 0),
50              "22:17: " + getCheckMessage(MSG_KEY, "ch1", "\\0"),
51              "23:17: " + getCheckMessage(MSG_KEY, "ch2", "\\0"),
52              "39:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
53              "40:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
54              "47:21: " + getCheckMessage(MSG_KEY, "x", 0),
55              "48:29: " + getCheckMessage(MSG_KEY, "bar", "null"),
56              "49:31: " + getCheckMessage(MSG_KEY, "barArray", "null"),
57              "52:17: " + getCheckMessage(MSG_KEY, "x", 0),
58              "53:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
59              "54:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
60          };
61          verify(checkConfig,
62                 getPath("InputExplicitInitialization.java"),
63                 expected);
64      }
65  
66      @Test
67      public void testTokensNotNull() {
68          final ExplicitInitializationCheck check = new ExplicitInitializationCheck();
69          Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
70          Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
71          Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
72      }
73  
74      @Test
75      public void testOnlyObjectReferences() throws Exception {
76          final DefaultConfiguration checkConfig =
77              createModuleConfig(ExplicitInitializationCheck.class);
78          checkConfig.addAttribute("onlyObjectReferences", "true");
79          final String[] expected = {
80              "5:20: " + getCheckMessage(MSG_KEY, "bar", "null"),
81              "14:22: " + getCheckMessage(MSG_KEY, "str1", "null"),
82              "14:35: " + getCheckMessage(MSG_KEY, "str3", "null"),
83              "15:9: " + getCheckMessage(MSG_KEY, "ar1", "null"),
84              "39:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
85              "40:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
86              "48:29: " + getCheckMessage(MSG_KEY, "bar", "null"),
87              "49:31: " + getCheckMessage(MSG_KEY, "barArray", "null"),
88              "53:25: " + getCheckMessage(MSG_KEY, "bar", "null"),
89              "54:27: " + getCheckMessage(MSG_KEY, "barArray", "null"),
90          };
91          verify(checkConfig, getPath("InputExplicitInitialization.java"), expected);
92      }
93  }