1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }