View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck.MSG_KEY;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.fail;
25  
26  import org.junit.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.CommonUtils;
32  
33  public class FileLengthCheckTest
34      extends AbstractModuleTestSupport {
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 DefaultConfiguration checkConfig =
43              createModuleConfig(FileLengthCheck.class);
44          checkConfig.addAttribute("max", "20");
45          final String[] expected = {
46              "1: " + getCheckMessage(MSG_KEY, 225, 20),
47          };
48          verify(createChecker(checkConfig),
49                  getPath("InputFileLength.java"),
50                  getPath("InputFileLength.java"), expected);
51      }
52  
53      @Test
54      public void testFileLengthEqualToMaxLength() throws Exception {
55          final DefaultConfiguration checkConfig =
56                  createModuleConfig(FileLengthCheck.class);
57          checkConfig.addAttribute("max", "225");
58          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
59          verify(createChecker(checkConfig),
60                  getPath("InputFileLength.java"),
61                  getPath("InputFileLength.java"), expected);
62      }
63  
64      @Test
65      public void testOk() throws Exception {
66          final DefaultConfiguration checkConfig =
67              createModuleConfig(FileLengthCheck.class);
68          checkConfig.addAttribute("max", "1000");
69          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
70          verify(createChecker(checkConfig),
71                  getPath("InputFileLength.java"),
72                  getPath("InputFileLength.java"), expected);
73      }
74  
75      @Test
76      public void testArgs() throws Exception {
77          final DefaultConfiguration checkConfig =
78              createModuleConfig(FileLengthCheck.class);
79          try {
80              checkConfig.addAttribute("max", "abc");
81              createChecker(checkConfig);
82              fail("Should indicate illegal args");
83          }
84          catch (CheckstyleException ex) {
85              // Expected Exception because of illegal argument for "max"
86              assertEquals("Invalid exception message", "cannot initialize module"
87                  + " com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck"
88                  + " - illegal value 'abc' for property 'max' of module"
89                  + " com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck",
90                  ex.getMessage());
91          }
92      }
93  
94      @Test
95      public void testNoAlarmByExtension() throws Exception {
96          final DefaultConfiguration checkConfig =
97                  createModuleConfig(FileLengthCheck.class);
98          checkConfig.addAttribute("fileExtensions", "txt");
99          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
100 
101         verify(createChecker(checkConfig),
102                 getPath("InputFileLength.java"),
103                 getPath("InputFileLength.java"), expected);
104     }
105 
106     @Test
107     public void testExtensions() {
108         final FileLengthCheck check = new FileLengthCheck();
109         check.setFileExtensions("java");
110         assertEquals("extension should be the same", ".java", check.getFileExtensions()[0]);
111         check.setFileExtensions(".java");
112         assertEquals("extension should be the same", ".java", check.getFileExtensions()[0]);
113         try {
114             check.setFileExtensions((String[]) null);
115             fail("IllegalArgumentException is expected");
116         }
117         catch (IllegalArgumentException ex) {
118             assertEquals("Invalid exception message",
119                 "Extensions array can not be null", ex.getMessage());
120         }
121     }
122 }