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.DefaultComesLastCheck.MSG_KEY;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheck.MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE;
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  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class DefaultComesLastCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/coding/defaultcomeslast";
37      }
38  
39      @Test
40      public void testSkipIfLastAndSharedWithCase() throws Exception {
41          final DefaultConfiguration checkConfig = createModuleConfig(DefaultComesLastCheck.class);
42          checkConfig.addAttribute("skipIfLastAndSharedWithCase", "true");
43          final String[] expected = {
44              "17:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
45              "25:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
46              "33:21: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
47              "37:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
48              "57:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
49              "77:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
50              "89:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
51              "98:13: " + getCheckMessage(MSG_KEY),
52          };
53  
54          verify(checkConfig, getPath("InputDefaultComesLastSkipIfLastAndSharedWithCase.java"),
55                  expected);
56      }
57  
58      @Test
59      public void testDefault() throws Exception {
60          final DefaultConfiguration checkConfig = createModuleConfig(DefaultComesLastCheck.class);
61          final String[] expected = {
62              "25:9: " + getCheckMessage(MSG_KEY),
63              "32:24: " + getCheckMessage(MSG_KEY),
64              "37:13: " + getCheckMessage(MSG_KEY),
65              "45:13: " + getCheckMessage(MSG_KEY),
66              "53:13: " + getCheckMessage(MSG_KEY),
67              "61:21: " + getCheckMessage(MSG_KEY),
68              "65:13: " + getCheckMessage(MSG_KEY),
69              "69:21: " + getCheckMessage(MSG_KEY),
70              "74:13: " + getCheckMessage(MSG_KEY),
71              "85:13: " + getCheckMessage(MSG_KEY),
72              "96:13: " + getCheckMessage(MSG_KEY),
73              "106:13: " + getCheckMessage(MSG_KEY),
74              "114:13: " + getCheckMessage(MSG_KEY),
75              "125:13: " + getCheckMessage(MSG_KEY),
76          };
77          verify(checkConfig,
78                 getPath("InputDefaultComesLast.java"),
79                 expected);
80      }
81  
82      @Test
83      public void testDefaultMethodsInJava8()
84              throws Exception {
85          final DefaultConfiguration checkConfig = createModuleConfig(DefaultComesLastCheck.class);
86          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
87          verify(checkConfig,
88                  getPath("InputDefaultComesLastDefaultMethodsInInterface.java"),
89                  expected);
90      }
91  
92      @Test
93      public void testTokensNotNull() {
94          final DefaultComesLastCheck check = new DefaultComesLastCheck();
95          Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
96          Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
97          Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
98      }
99  }