View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.checkstyle.test.chapter4formatting.rule4841indentation;
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_ERROR;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.google.checkstyle.test.base.AbstractIndentationTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.Configuration;
29  import com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class IndentationTest extends AbstractIndentationTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/google/checkstyle/test/chapter4formatting/rule4841indentation";
37      }
38  
39      @Test
40      public void testCorrectClass() throws Exception {
41          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
42  
43          final Configuration checkConfig = getModuleConfig("Indentation");
44          final String filePath = getPath("InputIndentationCorrectClass.java");
45  
46          final Integer[] warnList = getLinesWithWarn(filePath);
47          verify(checkConfig, filePath, expected, warnList);
48      }
49  
50      @Test
51      public void testCorrectField() throws Exception {
52          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
53  
54          final Configuration checkConfig = getModuleConfig("Indentation");
55          final String filePath = getPath("InputIndentationCorrectFieldAndParameter.java");
56  
57          final Integer[] warnList = getLinesWithWarn(filePath);
58          verify(checkConfig, filePath, expected, warnList);
59      }
60  
61      @Test
62      public void testCorrectFor() throws Exception {
63          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
64  
65          final Configuration checkConfig = getModuleConfig("Indentation");
66          final String filePath = getPath("InputIndentationCorrectForAndParameter.java");
67  
68          final Integer[] warnList = getLinesWithWarn(filePath);
69          verify(checkConfig, filePath, expected, warnList);
70      }
71  
72      @Test
73      public void testCorrectIf() throws Exception {
74          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
75  
76          final Configuration checkConfig = getModuleConfig("Indentation");
77          final String filePath = getPath("InputIndentationCorrectIfAndParameter.java");
78  
79          final Integer[] warnList = getLinesWithWarn(filePath);
80          verify(checkConfig, filePath, expected, warnList);
81      }
82  
83      @Test
84      public void testCorrectNewKeyword() throws Exception {
85          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
86  
87          final Configuration checkConfig = getModuleConfig("Indentation");
88          final String filePath = getPath("InputIndentationCorrectNewChildren.java");
89  
90          final Integer[] warnList = getLinesWithWarn(filePath);
91          verify(checkConfig, filePath, expected, warnList);
92      }
93  
94      @Test
95      public void testCorrect() throws Exception {
96          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
97  
98          final Configuration checkConfig = getModuleConfig("Indentation");
99          final String filePath = getPath("InputIndentationCorrect.java");
100 
101         final Integer[] warnList = getLinesWithWarn(filePath);
102         verify(checkConfig, filePath, expected, warnList);
103     }
104 
105     @Test
106     public void testCorrectReturn() throws Exception {
107         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
108 
109         final Configuration checkConfig = getModuleConfig("Indentation");
110         final String filePath = getPath("InputIndentationCorrectReturnAndParameter.java");
111 
112         final Integer[] warnList = getLinesWithWarn(filePath);
113         verify(checkConfig, filePath, expected, warnList);
114     }
115 
116     @Test
117     public void testCorrectWhile() throws Exception {
118         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
119 
120         final Configuration checkConfig = getModuleConfig("Indentation");
121         final String filePath = getPath("InputIndentationCorrectWhileDoWhileAndParameter.java");
122 
123         final Integer[] warnList = getLinesWithWarn(filePath);
124         verify(checkConfig, filePath, expected, warnList);
125     }
126 
127     @Test
128     public void testCorrectChained() throws Exception {
129         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
130 
131         final Configuration checkConfig = getModuleConfig("Indentation");
132         final String filePath = getPath("ClassWithChainedMethodsCorrect.java");
133 
134         final Integer[] warnList = getLinesWithWarn(filePath);
135         verify(checkConfig, filePath, expected, warnList);
136     }
137 
138     @Test
139     public void testWarnChained() throws Exception {
140         final String[] expected = {
141             "18:5: " + getCheckMessage(IndentationCheck.class,
142                     MSG_CHILD_ERROR, "method call", 4, 8),
143             "23:5: " + getCheckMessage(IndentationCheck.class, MSG_ERROR, ".", 4, 8),
144             "24:5: " + getCheckMessage(IndentationCheck.class, MSG_ERROR, ".", 4, 8),
145             "27:5: " + getCheckMessage(IndentationCheck.class, MSG_ERROR, "new", 4, 8),
146         };
147 
148         final Configuration checkConfig = getModuleConfig("Indentation");
149         final String filePath = getPath("ClassWithChainedMethods.java");
150 
151         final Integer[] warnList = getLinesWithWarn(filePath);
152         verify(checkConfig, filePath, expected, warnList);
153     }
154 
155     @Test
156     public void testCorrectAnnotationArrayInit() throws Exception {
157         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
158 
159         final Configuration checkConfig = getModuleConfig("Indentation");
160         final String filePath = getPath("InputIndentationCorrectAnnotationArrayInit.java");
161 
162         final Integer[] warnList = getLinesWithWarn(filePath);
163         verify(checkConfig, filePath, expected, warnList);
164     }
165 
166 }