View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_MAX;
23  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_MIN;
24  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_SUM_MAX;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
29  
30  public class DescendantTokenCheckExamplesTest extends AbstractExamplesModuleTestSupport {
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/descendanttoken";
35      }
36  
37      @Test
38      public void testExample1() throws Exception {
39          final String[] expected = {
40              "23:5: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
41          };
42  
43          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
44      }
45  
46      @Test
47      public void testExample2() throws Exception {
48          final String[] expected = {
49              "23:5: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "LITERAL_SWITCH", "LITERAL_CASE"),
50          };
51  
52          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
53      }
54  
55      @Test
56      public void testExample3() throws Exception {
57          final String[] expected = {
58          };
59  
60          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
61      }
62  
63      @Test
64      public void testExample4() throws Exception {
65          final String[] expected = {
66              "40:11: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "FOR_CONDITION", "EXPR"),
67          };
68  
69          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
70      }
71  
72      @Test
73      public void testExample5() throws Exception {
74          final String[] expected = {
75              "29:5: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_TRY", "LITERAL_SWITCH"),
76              "31:7: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_TRY", "LITERAL_SWITCH"),
77              "36:7: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_TRY", "LITERAL_SWITCH"),
78          };
79  
80          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
81      }
82  
83      @Test
84      public void testUseCase1() throws Exception {
85          final String[] expected = {
86              "40:10: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "FOR_INIT", "EXPR"),
87          };
88  
89          verifyWithInlineConfigParser(getPath("UseCase1.java"), expected);
90      }
91  
92      @Test
93      public void testExample6() throws Exception {
94          final String[] expected = {
95              "30:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_CATCH", "LITERAL_RETURN"),
96          };
97  
98          verifyWithInlineConfigParser(getPath("Example6.java"), expected);
99      }
100 
101     @Test
102     public void testExample7() throws Exception {
103         final String[] expected = {
104             "29:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_CATCH", "LITERAL_TRY"),
105             "34:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_FINALLY", "LITERAL_TRY"),
106         };
107 
108         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
109     }
110 
111     @Test
112     public void testUseCase2() throws Exception {
113         final String[] expected = {
114             "20:3: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "METHOD_DEF", "VARIABLE_DEF"),
115         };
116 
117         verifyWithInlineConfigParser(getPath("UseCase2.java"), expected);
118     }
119 
120     @Test
121     public void testUseCase3() throws Exception {
122         final String[] expected = {
123             "19:3: " + getCheckMessage(MSG_KEY_MAX, 3, 2, "METHOD_DEF", "LITERAL_RETURN"),
124         };
125 
126         verifyWithInlineConfigParser(getPath("UseCase3.java"), expected);
127     }
128 
129     @Test
130     public void testUseCase4() throws Exception {
131         final String[] expected = {
132             "20:11: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "LITERAL_THROWS", "IDENT"),
133         };
134 
135         verifyWithInlineConfigParser(getPath("UseCase4.java"), expected);
136     }
137 
138     @Test
139     public void testUseCase5() throws Exception {
140         final String[] expected = {
141             "19:3: " + getCheckMessage(MSG_KEY_MAX, 10, 2, "METHOD_DEF", "EXPR"),
142         };
143 
144         verifyWithInlineConfigParser(getPath("UseCase5.java"), expected);
145     }
146 
147     @Test
148     public void testExample8() throws Exception {
149         final String[] expected = {
150             "51:5: " + "Empty statement is not allowed.",
151         };
152 
153         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
154     }
155 
156     @Test
157     public void testUseCase6() throws Exception {
158         final String[] expected = {
159             "16:1: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "CLASS_DEF", "VARIABLE_DEF"),
160         };
161 
162         verifyWithInlineConfigParser(getPath("UseCase6.java"), expected);
163     }
164 
165     @Test
166     public void testExample9() throws Exception {
167         final String[] expected = {
168             "47:14: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "EQUAL"),
169         };
170 
171         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
172     }
173 
174     @Test
175     public void testUseCase7() throws Exception {
176         final String[] expected = {
177             "46:29: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "EQUAL", "STRING_LITERAL"),
178         };
179 
180         verifyWithInlineConfigParser(getPath("UseCase7.java"), expected);
181     }
182 
183     @Test
184     public void testUseCase8() throws Exception {
185         final String[] expected = {
186             "50:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_ASSERT", "POST_INC"),
187         };
188 
189         verifyWithInlineConfigParser(getPath("UseCase8.java"), expected);
190     }
191 
192     @Test
193     public void testExample10() throws Exception {
194         final String[] expected = {
195             "24:5: Switch must contain at least one default branch.",
196         };
197 
198         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
199     }
200 
201 }