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
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 }