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