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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheck.MSG_KEY;
24 import static com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheck.MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31 public class DefaultComesLastCheckTest extends AbstractModuleTestSupport {
32
33 @Override
34 protected String getPackageLocation() {
35 return "com/puppycrawl/tools/checkstyle/checks/coding/defaultcomeslast";
36 }
37
38 @Test
39 public void testSkipIfLastAndSharedWithCaseOne() throws Exception {
40 final String[] expected = {
41 "23:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
42 "31:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
43 "39:21: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
44 "43:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
45 "63:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
46 "83:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
47 "95:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
48 };
49
50 verifyWithInlineConfigParser(
51 getPath("InputDefaultComesLastSkipIfLastAndSharedWithCaseOne.java"),
52 expected);
53 }
54
55 @Test
56 public void testSkipIfLastAndSharedWithCaseTwo() throws Exception {
57 final String[] expected = {
58 "16:13: " + getCheckMessage(MSG_KEY),
59 };
60
61 verifyWithInlineConfigParser(
62 getPath("InputDefaultComesLastSkipIfLastAndSharedWithCaseTwo.java"),
63 expected);
64 }
65
66 @Test
67 public void testDefaultOne() throws Exception {
68 final String[] expected = {
69 "31:9: " + getCheckMessage(MSG_KEY),
70 "38:24: " + getCheckMessage(MSG_KEY),
71 "43:13: " + getCheckMessage(MSG_KEY),
72 "51:13: " + getCheckMessage(MSG_KEY),
73 "59:13: " + getCheckMessage(MSG_KEY),
74 "67:21: " + getCheckMessage(MSG_KEY),
75 "71:13: " + getCheckMessage(MSG_KEY),
76 "75:21: " + getCheckMessage(MSG_KEY),
77 "80:13: " + getCheckMessage(MSG_KEY),
78 "91:13: " + getCheckMessage(MSG_KEY),
79 "102:13: " + getCheckMessage(MSG_KEY),
80 "112:13: " + getCheckMessage(MSG_KEY),
81 };
82 verifyWithInlineConfigParser(
83 getPath("InputDefaultComesLastOne.java"),
84 expected);
85 }
86
87 @Test
88 public void testDefaultTwo() throws Exception {
89 final String[] expected = {
90 "15:13: " + getCheckMessage(MSG_KEY),
91 "26:13: " + getCheckMessage(MSG_KEY),
92 };
93 verifyWithInlineConfigParser(
94 getPath("InputDefaultComesLastTwo.java"),
95 expected);
96 }
97
98 @Test
99 public void testDefaultMethodsInJava8()
100 throws Exception {
101 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
102 verifyWithInlineConfigParser(
103 getPath("InputDefaultComesLastDefaultMethodsInInterface.java"),
104 expected);
105 }
106
107 @Test
108 public void testDefaultComesLastSwitchExpressions() throws Exception {
109 final String[] expected = {
110 "16:13: " + getCheckMessage(MSG_KEY),
111 "32:13: " + getCheckMessage(MSG_KEY),
112 "46:13: " + getCheckMessage(MSG_KEY),
113 };
114 verifyWithInlineConfigParser(
115 getNonCompilablePath("InputDefaultComesLastSwitchExpressions.java"),
116 expected);
117 }
118
119 @Test
120 public void testDefaultComesLastSwitchExpressionsSkipIfLast() throws Exception {
121
122 final String[] expected = {
123 "33:13: " + getCheckMessage(MSG_KEY),
124 "48:13: " + getCheckMessage(MSG_KEY),
125 };
126 verifyWithInlineConfigParser(
127 getNonCompilablePath("InputDefaultComesLastSwitchExpressionsSkipIfLast.java"),
128 expected);
129 }
130
131 @Test
132 public void testTokensNotNull() {
133 final DefaultComesLastCheck check = new DefaultComesLastCheck();
134 assertWithMessage("Acceptable tokens should not be null")
135 .that(check.getAcceptableTokens())
136 .isNotNull();
137 assertWithMessage("Default tokens should not be null")
138 .that(check.getDefaultTokens())
139 .isNotNull();
140 assertWithMessage("Required tokens should not be null")
141 .that(check.getRequiredTokens())
142 .isNotNull();
143 }
144
145 @Test
146 public void testDefaultMethodsInJava8Interface2()
147 throws Exception {
148 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
149 verifyWithInlineConfigParser(
150 getPath("InputDefaultComesLastDefaultMethodsInInterface2.java"),
151 expected);
152 }
153
154 }