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.EqualsAvoidNullCheck.MSG_EQUALS_AVOID_NULL;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck.MSG_EQUALS_IGNORE_CASE_AVOID_NULL;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  
31  public class EqualsAvoidNullCheckTest extends AbstractModuleTestSupport {
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/coding/equalsavoidnull";
35      }
36  
37      @Test
38      public void testEqualsWithDefault() throws Exception {
39          final DefaultConfiguration checkConfig =
40              createModuleConfig(EqualsAvoidNullCheck.class);
41  
42          final String[] expected = {
43              "37:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
44              "39:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
45              "41:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
46              "43:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
47              "45:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
48              "47:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
49              "69:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
50              "71:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
51              "73:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
52              "75:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
53              "77:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
54              "79:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
55              "238:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
56              "244:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
57              "245:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
58              "247:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
59              "248:27: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
60              "249:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
61              "250:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
62              "251:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
63              "268:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
64              "269:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
65              "270:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
66              "272:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
67              "273:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
68              "274:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
69              "295:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
70              "300:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
71              "301:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
72              "308:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
73              "311:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
74              "312:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
75              "314:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
76              "315:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
77              "316:33: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
78              "319:70: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
79              "320:51: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
80              "320:77: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
81              "321:55: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
82              "335:28: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
83              "336:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
84              "337:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
85              "338:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
86              "339:37: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
87              "343:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
88              "344:44: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
89              "345:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
90              "346:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
91              "357:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
92              "368:30: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
93              "394:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
94              "415:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
95              "416:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
96              "417:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
97          };
98          verify(checkConfig, getPath("InputEqualsAvoidNull.java"), expected);
99      }
100 
101     @Test
102     public void testEqualsWithoutEqualsIgnoreCase() throws Exception {
103         final DefaultConfiguration checkConfig =
104                 createModuleConfig(EqualsAvoidNullCheck.class);
105         checkConfig.addAttribute("ignoreEqualsIgnoreCase", "true");
106 
107         final String[] expected = {
108             "238:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
109             "244:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
110             "245:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
111             "247:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
112             "248:27: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
113             "249:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
114             "250:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
115             "251:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
116             "268:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
117             "269:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
118             "270:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
119             "272:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
120             "273:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
121             "274:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
122             "295:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
123             "300:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
124             "301:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
125             "308:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
126             "311:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
127             "312:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
128             "314:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
129             "315:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
130             "316:33: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
131             "319:70: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
132             "320:51: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
133             "320:77: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
134             "321:55: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
135             "335:28: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
136             "336:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
137             "337:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
138             "338:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
139             "339:37: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
140             "343:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
141             "344:44: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
142             "345:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
143             "346:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
144             "357:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
145             "368:30: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
146             "394:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
147             "415:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
148             "416:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
149             "417:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
150         };
151         verify(checkConfig, getPath("InputEqualsAvoidNull.java"), expected);
152     }
153 
154     @Test
155     public void testEqualsOnTheSameLine() throws Exception {
156         final DefaultConfiguration checkConfig =
157             createModuleConfig(EqualsAvoidNullCheck.class);
158 
159         final String[] expected = {
160             "7:28: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
161         };
162         verify(checkConfig, getPath("InputEqualsAvoidNullOnTheSameLine.java"), expected);
163     }
164 
165     @Test
166     public void testEqualsNested() throws Exception {
167         final DefaultConfiguration checkConfig =
168                 createModuleConfig(EqualsAvoidNullCheck.class);
169 
170         final String[] expected = {
171             "18:44: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
172             "19:48: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
173             "20:48: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
174         };
175         verify(checkConfig, getPath("InputEqualsAvoidNullNested.java"), expected);
176     }
177 
178     @Test
179     public void testTokensNotNull() {
180         final EqualsAvoidNullCheck check = new EqualsAvoidNullCheck();
181         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
182         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
183         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
184     }
185 }