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.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
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.api.DetailAST;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  
33  public class FinalClassCheckTest
34      extends AbstractModuleTestSupport {
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/design/finalclass";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final FinalClassCheck checkObj = new FinalClassCheck();
43          final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.CTOR_DEF, TokenTypes.PACKAGE_DEF};
44          assertArrayEquals("Default required tokens are invalid",
45              expected, checkObj.getRequiredTokens());
46      }
47  
48      @Test
49      public void testFinalClass() throws Exception {
50          final DefaultConfiguration checkConfig =
51              createModuleConfig(FinalClassCheck.class);
52          final String[] expected = {
53              "7: " + getCheckMessage(MSG_KEY, "InputFinalClass"),
54              "15: " + getCheckMessage(MSG_KEY, "test4"),
55              "113: " + getCheckMessage(MSG_KEY, "someinnerClass"),
56          };
57          verify(checkConfig, getPath("InputFinalClass.java"), expected);
58      }
59  
60      @Test
61      public void testClassWithPrivateCtorAndNestedExtendingSubclass() throws Exception {
62          final DefaultConfiguration checkConfig =
63                  createModuleConfig(FinalClassCheck.class);
64          final String[] expected = {
65              "15: " + getCheckMessage(MSG_KEY, "C"),
66          };
67          verify(checkConfig,
68                  getNonCompilablePath(
69                          "InputFinalClassClassWithPrivateCtorWithNestedExtendingClass.java"),
70                  expected);
71      }
72  
73      @Test
74      public void testClassWithPrivateCtorAndNestedExtendingSubclassWithoutPackage()
75              throws Exception {
76          final DefaultConfiguration checkConfig =
77                  createModuleConfig(FinalClassCheck.class);
78          final String[] expected = {
79              "7: " + getCheckMessage(MSG_KEY, "C"),
80          };
81          verify(checkConfig,
82                  getNonCompilablePath(
83                  "InputFinalClassClassWithPrivateCtorWithNestedExtendingClassWithoutPackage.java"),
84                  expected);
85      }
86  
87      @Test
88      public void testImproperToken() {
89          final FinalClassCheck finalClassCheck = new FinalClassCheck();
90          final DetailAST badAst = new DetailAST();
91          final int unsupportedTokenByCheck = TokenTypes.EOF;
92          badAst.setType(unsupportedTokenByCheck);
93          try {
94              finalClassCheck.visitToken(badAst);
95              Assert.fail("IllegalStateException is expected");
96          }
97          catch (IllegalStateException ex) {
98              // it is OK
99          }
100     }
101 
102     @Test
103     public void testGetAcceptableTokens() {
104         final FinalClassCheck obj = new FinalClassCheck();
105         final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.CTOR_DEF, TokenTypes.PACKAGE_DEF};
106         assertArrayEquals("Default acceptable tokens are invalid",
107             expected, obj.getAcceptableTokens());
108     }
109 }