View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.design.SealedShouldHavePermitsListCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  public class SealedShouldHavePermitsListCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/design/sealedshouldhavepermitslist";
35      }
36  
37      @Test
38      public void testGetRequiredTokens() {
39          final SealedShouldHavePermitsListCheck checkObj = new SealedShouldHavePermitsListCheck();
40          final int[] expected = {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
41          assertWithMessage("Default required tokens are invalid")
42              .that(checkObj.getRequiredTokens())
43              .isEqualTo(expected);
44          assertWithMessage("Default acceptable tokens are invalid")
45              .that(checkObj.getAcceptableTokens())
46              .isEqualTo(expected);
47          assertWithMessage("Default tokens are invalid")
48              .that(checkObj.getDefaultTokens())
49              .isEqualTo(expected);
50      }
51  
52      @Test
53      public void testInnerSealedClass() throws Exception {
54          final String[] expected = {
55              "10:5: " + getCheckMessage(MSG_KEY),
56              "15:5: " + getCheckMessage(MSG_KEY),
57          };
58          verifyWithInlineConfigParser(
59                  getNonCompilablePath("InputSealedShouldHavePermitsListInnerClass.java"),
60                  expected);
61      }
62  
63      @Test
64      public void testInnerSealedInterface() throws Exception {
65          final String[] expected = {
66              "10:5: " + getCheckMessage(MSG_KEY),
67          };
68          verifyWithInlineConfigParser(
69                  getNonCompilablePath("InputSealedShouldHavePermitsListInnerInterface.java"),
70                  expected);
71      }
72  
73      @Test
74      public void testTopLevelSealedClass() throws Exception {
75          final String[] expected = {
76              "9:1: " + getCheckMessage(MSG_KEY),
77          };
78          verifyWithInlineConfigParser(
79                  getNonCompilablePath("InputSealedShouldHavePermitsListTopLevelSealedClass.java"),
80                  expected);
81      }
82  
83      @Test
84      public void testTopLevelSealedInterface() throws Exception {
85          final String[] expected = {
86              "9:1: " + getCheckMessage(MSG_KEY),
87          };
88          verifyWithInlineConfigParser(
89                  getNonCompilablePath(
90                          "InputSealedShouldHavePermitsListTopLevelSealedInterface.java"),
91                  expected);
92      }
93  
94      @Test
95      public void testJepExample() throws Exception {
96          final String[] expected = {
97              "10:1: " + getCheckMessage(MSG_KEY),
98              "24:1: " + getCheckMessage(MSG_KEY),
99          };
100         verifyWithInlineConfigParser(
101                 getNonCompilablePath(
102                         "InputSealedShouldHavePermitsListJepExample.java"),
103                 expected);
104     }
105 }