View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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      @Override
32      protected String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/descendanttoken";
34      }
35  
36      @Test
37      public void testExample1() throws Exception {
38          final String[] expected = {
39              "29:5: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
40              "63:5: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
41              "68:5: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
42              "70:9: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
43          };
44  
45          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
46      }
47  
48      @Test
49      public void testExample2() throws Exception {
50          final String[] expected = {
51              "47:5: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "LITERAL_SWITCH", "LITERAL_CASE"),
52          };
53  
54          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
55      }
56  
57      @Test
58      public void testExample3() throws Exception {
59          final String[] expected = {
60              "68:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_SWITCH", "LITERAL_SWITCH"),
61          };
62  
63          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
64      }
65  
66      @Test
67      public void testExample4() throws Exception {
68          final String[] expected = {
69              "21:12: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "FOR_CONDITION", "EXPR"),
70          };
71  
72          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
73      }
74  
75      @Test
76      public void testExample5() throws Exception {
77          final String[] expected = {
78              "21:10: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "FOR_INIT", "EXPR"),
79              "33:10: " + getCheckMessage(MSG_KEY_MIN, 0, 1, "FOR_INIT", "EXPR"),
80          };
81  
82          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
83      }
84  
85      @Test
86      public void testExample6() throws Exception {
87          final String[] expected = {
88              "18:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_CATCH", "LITERAL_RETURN"),
89              "29:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_CATCH", "LITERAL_RETURN"),
90          };
91  
92          verifyWithInlineConfigParser(getPath("Example6.java"), expected);
93      }
94  
95      @Test
96      public void testExample7() throws Exception {
97          final String[] expected = {
98              "37:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_CATCH", "LITERAL_TRY"),
99              "43:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_FINALLY", "LITERAL_TRY"),
100         };
101 
102         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
103     }
104 
105     @Test
106     public void testExample8() throws Exception {
107         final String[] expected = {
108             "20:3: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "METHOD_DEF", "VARIABLE_DEF"),
109         };
110 
111         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
112     }
113 
114     @Test
115     public void testExample9() throws Exception {
116         final String[] expected = {
117             "24:3: " + getCheckMessage(MSG_KEY_MAX, 3, 2, "METHOD_DEF", "LITERAL_RETURN"),
118             "33:3: " + getCheckMessage(MSG_KEY_MAX, 3, 2, "METHOD_DEF", "LITERAL_RETURN"),
119         };
120 
121         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
122     }
123 
124     @Test
125     public void testExample10() throws Exception {
126         final String[] expected = {
127             "20:22: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "LITERAL_THROWS", "IDENT"),
128         };
129 
130         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
131     }
132 
133     @Test
134     public void testExample11() throws Exception {
135         final String[] expected = {
136             "21:3: " + getCheckMessage(MSG_KEY_MAX, 3, 2, "METHOD_DEF", "EXPR"),
137         };
138 
139         verifyWithInlineConfigParser(getPath("Example11.java"), expected);
140     }
141 
142     @Test
143     public void testExample12() throws Exception {
144         final String[] expected = {
145             "23:7: " + "Empty statement is not allowed.",
146         };
147 
148         verifyWithInlineConfigParser(getPath("Example12.java"), expected);
149     }
150 
151     @Test
152     public void testExample13() throws Exception {
153         final String[] expected = {
154             "22:1: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "CLASS_DEF", "VARIABLE_DEF"),
155             "32:1: " + getCheckMessage(MSG_KEY_MAX, 2, 1, "INTERFACE_DEF", "VARIABLE_DEF"),
156         };
157 
158         verifyWithInlineConfigParser(getPath("Example13.java"), expected);
159     }
160 
161     @Test
162     public void testExample14() throws Exception {
163         final String[] expected = {
164             "20:14: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "EQUAL"),
165             "23:14: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "NOT_EQUAL"),
166         };
167 
168         verifyWithInlineConfigParser(getPath("Example14.java"), expected);
169     }
170 
171     @Test
172     public void testExample15() throws Exception {
173         final String[] expected = {
174             "22:13: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "EQUAL", "STRING_LITERAL"),
175         };
176 
177         verifyWithInlineConfigParser(getPath("Example15.java"), expected);
178     }
179 
180     @Test
181     public void testExample16() throws Exception {
182         final String[] expected = {
183             "22:5: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_ASSERT", "POST_INC"),
184         };
185 
186         verifyWithInlineConfigParser(getPath("Example16.java"), expected);
187     }
188 }