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.indentation;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_CHILD_ERROR;
23  import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_CHILD_ERROR_MULTI;
24  import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_ERROR;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
29  
30  public class IndentationCheckExamplesTest extends AbstractExamplesModuleTestSupport {
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/indentation/indentation";
35      }
36  
37      @Test
38      public void testExample1() throws Exception {
39          final String[] expected = {
40              "20:3: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 2, 8),
41          };
42          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
43      }
44  
45      @Test
46      public void testExample2() throws Exception {
47          final String[] expected = {
48              "22:3: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 2, 8),
49              "28:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 8),
50              "29:17: " + getCheckMessage(MSG_CHILD_ERROR, "block", 16, 12),
51              "30:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 8),
52              "31:17: " + getCheckMessage(MSG_CHILD_ERROR, "block", 16, 12),
53          };
54          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
55      }
56  
57      @Test
58      public void testExample3() throws Exception {
59          final String[] expected = {
60              "22:3: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 2, 8),
61              "35:18: " + getCheckMessage(MSG_ERROR, "int", 17, 8),
62              "37:17: " + getCheckMessage(MSG_ERROR, "&&", 16, 12),
63              "43:22: " + getCheckMessage(MSG_ERROR, "int", 21, 8),
64              "49:17: " + getCheckMessage(MSG_ERROR, "||", 16, 12),
65          };
66          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
67      }
68  
69      @Test
70      public void testExample4() throws Exception {
71          final String[] expected = {
72              "13:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
73              "17:11: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 10, 8),
74          };
75          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
76      }
77  
78      @Test
79      public void testExample5() throws Exception {
80          final String[] expected = {
81              "15:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 2),
82              "16:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 2),
83              "17:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 2),
84              "18:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 2),
85              "21:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 2),
86              "22:3: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
87                      "array initialization", 2, "8, 23, 24"),
88              "25:5: " + getCheckMessage(MSG_ERROR, "method def modifier", 4, 2),
89              "27:9: " + getCheckMessage(MSG_ERROR, "switch", 8, 4),
90              "28:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 8),
91              "29:17: " + getCheckMessage(MSG_CHILD_ERROR, "block", 16, 10),
92              "30:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 8),
93              "31:17: " + getCheckMessage(MSG_CHILD_ERROR, "block", 16, 10),
94              "32:9: " + getCheckMessage(MSG_ERROR, "switch rcurly", 8, 4),
95              "33:5: " + getCheckMessage(MSG_ERROR, "method def rcurly", 4, 2),
96              "34:5: " + getCheckMessage(MSG_ERROR, "method def modifier", 4, 2),
97              "36:9: " + getCheckMessage(MSG_ERROR, "if", 8, 4),
98              "38:13: " + getCheckMessage(MSG_CHILD_ERROR, "if", 12, 6),
99              "39:9: " + getCheckMessage(MSG_ERROR, "if rcurly", 8, 4),
100             "40:5: " + getCheckMessage(MSG_ERROR, "method def rcurly", 4, 2),
101             "42:5: " + getCheckMessage(MSG_ERROR, "method def modifier", 4, 2),
102             "45:9: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 8, 4),
103             "47:9: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 8, 4),
104             "48:9: " + getCheckMessage(MSG_ERROR, "if", 8, 4),
105             "50:13: " + getCheckMessage(MSG_CHILD_ERROR, "if", 12, 6),
106             "52:9: " + getCheckMessage(MSG_ERROR, "if rcurly", 8, 4),
107             "53:5: " + getCheckMessage(MSG_ERROR, "method def rcurly", 4, 2),
108             "55:5: " + getCheckMessage(MSG_ERROR, "method def modifier", 4, 2),
109             "56:5: " + getCheckMessage(MSG_ERROR, "method def lcurly", 4, 2),
110             "57:5: " + getCheckMessage(MSG_ERROR, "method def rcurly", 4, 2),
111         };
112         verifyWithInlineConfigParser(getPath("Example5.java"), expected);
113     }
114 
115     @Test
116     public void testExample6() throws Exception {
117         final String[] expected = {
118             "22:3: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 2, "8, 12"),
119         };
120         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
121     }
122 
123     @Test
124     public void testExample7() throws Exception {
125         final String[] expected = {
126             "22:3: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 2, 8),
127             "26:9: " + getCheckMessage(MSG_ERROR, "throws", 8, 12),
128         };
129         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
130     }
131 
132     @Test
133     public void testExample8() throws Exception {
134         final String[] expected = {
135             "22:3: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 2, "6, 8"),
136         };
137         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
138     }
139 
140     @Test
141     public void testExample9() throws Exception {
142         final String[] expected = {
143             "22:3: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 2, 8),
144             "56:5: " + getCheckMessage(MSG_ERROR, "method def lcurly", 4, 6),
145             "57:5: " + getCheckMessage(MSG_ERROR, "method def rcurly", 4, 6),
146         };
147         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
148     }
149 }