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