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