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.sizes;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32  
33  public class FileLengthCheckTest
34      extends AbstractModuleTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/sizes/filelength";
39      }
40  
41      @Test
42      public void testAlarm() throws Exception {
43          final String[] expected = {
44              "1: " + getCheckMessage(MSG_KEY, 228, 20),
45          };
46          verifyWithInlineConfigParser(
47                  getPath("InputFileLength.java"), expected);
48      }
49  
50      @Test
51      public void testAlarmDefault() throws Exception {
52          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
53          verifyWithInlineConfigParser(
54                  getPath("InputFileLengthDefault.java"), expected);
55      }
56  
57      @Test
58      public void testFileLengthEqualToMaxLength() throws Exception {
59          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
60          verifyWithInlineConfigParser(
61                  getPath("InputFileLength2.java"), expected);
62      }
63  
64      @Test
65      public void testOk() throws Exception {
66          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
67          verifyWithInlineConfigParser(
68                  getPath("InputFileLength3.java"), expected);
69      }
70  
71      @Test
72      public void testArgs() {
73          final DefaultConfiguration checkConfig =
74              createModuleConfig(FileLengthCheck.class);
75          final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class,
76                  () -> {
77                      checkConfig.addProperty("max", "abc");
78                      createChecker(checkConfig);
79                  });
80          assertWithMessage("Invalid exception message")
81              .that(exc.getMessage())
82              .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.checks."
83                  + "sizes.FileLengthCheck - "
84                  + "illegal value 'abc' for property 'max'");
85      }
86  
87      @Test
88      public void testNoAlarmByExtension() throws Exception {
89          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
90  
91          verifyWithInlineConfigParser(
92                  getPath("InputFileLength4.java"), expected);
93      }
94  
95      @Test
96      public void testExtensions() {
97          final FileLengthCheck check = new FileLengthCheck();
98          check.setFileExtensions("java");
99          assertWithMessage("extension should be the same")
100             .that(check.getFileExtensions()[0])
101             .isEqualTo(".java");
102         check.setFileExtensions(".java");
103         assertWithMessage("extension should be the same")
104             .that(check.getFileExtensions()[0])
105             .isEqualTo(".java");
106         final IllegalArgumentException exc = getExpectedThrowable(IllegalArgumentException.class,
107                 () -> check.setFileExtensions((String[]) null));
108         assertWithMessage("Invalid exception message")
109             .that(exc.getMessage())
110             .isEqualTo("Extensions array can not be null");
111     }
112 
113 }