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.FallThroughCheck.MSG_FALL_THROUGH;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.FallThroughCheck.MSG_FALL_THROUGH_LAST;
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 FallThroughCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/coding/fallthrough";
36      }
37  
38      @Test
39      public void testDefault() throws Exception {
40          final DefaultConfiguration checkConfig = createModuleConfig(FallThroughCheck.class);
41          final String[] expected = {
42              "14:13: " + getCheckMessage(MSG_FALL_THROUGH),
43              "38:13: " + getCheckMessage(MSG_FALL_THROUGH),
44              "47:13: " + getCheckMessage(MSG_FALL_THROUGH),
45              "53:13: " + getCheckMessage(MSG_FALL_THROUGH),
46              "70:13: " + getCheckMessage(MSG_FALL_THROUGH),
47              "87:13: " + getCheckMessage(MSG_FALL_THROUGH),
48              "123:13: " + getCheckMessage(MSG_FALL_THROUGH),
49              "179:11: " + getCheckMessage(MSG_FALL_THROUGH),
50              "369:11: " + getCheckMessage(MSG_FALL_THROUGH),
51              "372:11: " + getCheckMessage(MSG_FALL_THROUGH),
52              "374:40: " + getCheckMessage(MSG_FALL_THROUGH),
53              "416:13: " + getCheckMessage(MSG_FALL_THROUGH),
54              "424:9: " + getCheckMessage(MSG_FALL_THROUGH),
55              "436:9: " + getCheckMessage(MSG_FALL_THROUGH),
56              "446:9: " + getCheckMessage(MSG_FALL_THROUGH),
57              "555:9: " + getCheckMessage(MSG_FALL_THROUGH),
58              "556:9: " + getCheckMessage(MSG_FALL_THROUGH),
59              "557:9: " + getCheckMessage(MSG_FALL_THROUGH),
60          };
61          verify(checkConfig,
62                 getPath("InputFallThrough.java"),
63                 expected);
64      }
65  
66      @Test
67      public void testLastCaseGroup() throws Exception {
68          final DefaultConfiguration checkConfig = createModuleConfig(FallThroughCheck.class);
69          checkConfig.addAttribute("checkLastCaseGroup", "true");
70          final String[] expected = {
71              "14:13: " + getCheckMessage(MSG_FALL_THROUGH),
72              "38:13: " + getCheckMessage(MSG_FALL_THROUGH),
73              "47:13: " + getCheckMessage(MSG_FALL_THROUGH),
74              "53:13: " + getCheckMessage(MSG_FALL_THROUGH),
75              "70:13: " + getCheckMessage(MSG_FALL_THROUGH),
76              "87:13: " + getCheckMessage(MSG_FALL_THROUGH),
77              "123:13: " + getCheckMessage(MSG_FALL_THROUGH),
78              "123:13: " + getCheckMessage(MSG_FALL_THROUGH_LAST),
79              "179:11: " + getCheckMessage(MSG_FALL_THROUGH),
80              "369:11: " + getCheckMessage(MSG_FALL_THROUGH),
81              "372:11: " + getCheckMessage(MSG_FALL_THROUGH),
82              "374:40: " + getCheckMessage(MSG_FALL_THROUGH),
83              "376:11: " + getCheckMessage(MSG_FALL_THROUGH_LAST),
84              "416:13: " + getCheckMessage(MSG_FALL_THROUGH),
85              "424:9: " + getCheckMessage(MSG_FALL_THROUGH),
86              "436:9: " + getCheckMessage(MSG_FALL_THROUGH),
87              "446:9: " + getCheckMessage(MSG_FALL_THROUGH),
88              "555:9: " + getCheckMessage(MSG_FALL_THROUGH),
89              "556:9: " + getCheckMessage(MSG_FALL_THROUGH),
90              "557:9: " + getCheckMessage(MSG_FALL_THROUGH),
91          };
92          verify(checkConfig,
93                 getPath("InputFallThrough.java"),
94                 expected);
95      }
96  
97      @Test
98      public void testOwnPattern() throws Exception {
99          final DefaultConfiguration checkConfig =
100             createModuleConfig(FallThroughCheck.class);
101         checkConfig.addAttribute("reliefPattern", "Continue with next case");
102 
103         final String[] expected = {
104             "14:13: " + getCheckMessage(MSG_FALL_THROUGH),
105             "38:13: " + getCheckMessage(MSG_FALL_THROUGH),
106             "47:13: " + getCheckMessage(MSG_FALL_THROUGH),
107             "53:13: " + getCheckMessage(MSG_FALL_THROUGH),
108             "70:13: " + getCheckMessage(MSG_FALL_THROUGH),
109             "87:13: " + getCheckMessage(MSG_FALL_THROUGH),
110             "123:13: " + getCheckMessage(MSG_FALL_THROUGH),
111             "145:11: " + getCheckMessage(MSG_FALL_THROUGH),
112             "170:11: " + getCheckMessage(MSG_FALL_THROUGH),
113             "179:11: " + getCheckMessage(MSG_FALL_THROUGH),
114             "186:11: " + getCheckMessage(MSG_FALL_THROUGH),
115             "204:11: " + getCheckMessage(MSG_FALL_THROUGH),
116             "222:11: " + getCheckMessage(MSG_FALL_THROUGH),
117             "252:26: " + getCheckMessage(MSG_FALL_THROUGH),
118             "266:11: " + getCheckMessage(MSG_FALL_THROUGH),
119             "281:11: " + getCheckMessage(MSG_FALL_THROUGH),
120             "284:11: " + getCheckMessage(MSG_FALL_THROUGH),
121             "288:11: " + getCheckMessage(MSG_FALL_THROUGH),
122             "290:25: " + getCheckMessage(MSG_FALL_THROUGH),
123             "306:11: " + getCheckMessage(MSG_FALL_THROUGH),
124             "309:11: " + getCheckMessage(MSG_FALL_THROUGH),
125             "311:25: " + getCheckMessage(MSG_FALL_THROUGH),
126             "327:11: " + getCheckMessage(MSG_FALL_THROUGH),
127             "330:11: " + getCheckMessage(MSG_FALL_THROUGH),
128             "332:23: " + getCheckMessage(MSG_FALL_THROUGH),
129             "348:11: " + getCheckMessage(MSG_FALL_THROUGH),
130             "351:11: " + getCheckMessage(MSG_FALL_THROUGH),
131             "353:30: " + getCheckMessage(MSG_FALL_THROUGH),
132             "416:13: " + getCheckMessage(MSG_FALL_THROUGH),
133             "424:9: " + getCheckMessage(MSG_FALL_THROUGH),
134             "436:9: " + getCheckMessage(MSG_FALL_THROUGH),
135             "446:9: " + getCheckMessage(MSG_FALL_THROUGH),
136             "491:9: " + getCheckMessage(MSG_FALL_THROUGH),
137             "495:9: " + getCheckMessage(MSG_FALL_THROUGH),
138             "501:9: " + getCheckMessage(MSG_FALL_THROUGH),
139             "507:9: " + getCheckMessage(MSG_FALL_THROUGH),
140             "514:9: " + getCheckMessage(MSG_FALL_THROUGH),
141             "546:12: " + getCheckMessage(MSG_FALL_THROUGH),
142             "555:9: " + getCheckMessage(MSG_FALL_THROUGH),
143             "556:9: " + getCheckMessage(MSG_FALL_THROUGH),
144             "557:9: " + getCheckMessage(MSG_FALL_THROUGH),
145         };
146         verify(checkConfig,
147                getPath("InputFallThrough.java"),
148                expected);
149     }
150 
151     @Test
152     public void testTokensNotNull() {
153         final FallThroughCheck check = new FallThroughCheck();
154         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
155         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
156         Assert.assertNotNull("Rrequired tokens should not be null", check.getRequiredTokens());
157     }
158 
159     @Test
160     public void testFallThroughNoElse() throws Exception {
161         final DefaultConfiguration checkConfig = createModuleConfig(FallThroughCheck.class);
162         final String[] expected = {
163             "20:13: " + getCheckMessage(MSG_FALL_THROUGH),
164             "35:13: " + getCheckMessage(MSG_FALL_THROUGH),
165             "39:13: " + getCheckMessage(MSG_FALL_THROUGH),
166             "46:13: " + getCheckMessage(MSG_FALL_THROUGH),
167             "60:13: " + getCheckMessage(MSG_FALL_THROUGH),
168             "67:13: " + getCheckMessage(MSG_FALL_THROUGH),
169             "80:21: " + getCheckMessage(MSG_FALL_THROUGH),
170             "86:13: " + getCheckMessage(MSG_FALL_THROUGH),
171             "88:13: " + getCheckMessage(MSG_FALL_THROUGH),
172         };
173         verify(checkConfig,
174             getPath("InputFallThrough2.java"),
175             expected);
176     }
177 
178 }