1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }