Source code: com/puppycrawl/tools/checkstyle/checks/HeaderCheckTest.java
1 package com.puppycrawl.tools.checkstyle.checks;
2
3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
5 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
6
7 public class HeaderCheckTest extends BaseCheckTestCase
8 {
9 public void testStaticHeader()
10 throws Exception
11 {
12 final DefaultConfiguration checkConfig =
13 createCheckConfig(HeaderCheck.class);
14 checkConfig.addAttribute("headerFile", getPath("java.header"));
15 checkConfig.addAttribute("ignoreLines", "");
16 final String[] expected = {
17 "1: Missing a header - not enough lines in file."
18 };
19 verify(checkConfig, getPath("inputHeader.java"), expected);
20 }
21
22 public void testRegexpHeader()
23 throws Exception
24 {
25 final DefaultConfiguration checkConfig =
26 createCheckConfig(RegexpHeaderCheck.class);
27 checkConfig.addAttribute("headerFile", getPath("regexp.header"));
28 checkConfig.addAttribute("ignoreLines", "4,5");
29 final String[] expected = {
30 "3: Line does not match expected header line of '// Created: 2002'."
31 };
32 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
33 }
34
35 public void testRegexpHeaderIgnore()
36 throws Exception
37 {
38 final DefaultConfiguration checkConfig =
39 createCheckConfig(RegexpHeaderCheck.class);
40 checkConfig.addAttribute("headerFile", getPath("regexp.header"));
41 checkConfig.addAttribute("ignoreLines", "3,4,5");
42 final String[] expected = {
43 };
44 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
45 }
46
47 public void testNoHeader()
48 throws Exception
49 {
50 final DefaultConfiguration checkConfig =
51 createCheckConfig(HeaderCheck.class);
52 // No header file specified
53 try {
54 createChecker(checkConfig);
55 fail();
56 }
57 catch (CheckstyleException ex) {
58 // expected exception
59 }
60 }
61
62 public void testIllegalArgs()
63 throws Exception
64 {
65 final DefaultConfiguration checkConfig =
66 createCheckConfig(HeaderCheck.class);
67 checkConfig.addAttribute("headerFile", getPath("nonexisting.file"));
68 try {
69 createChecker(checkConfig);
70 fail();
71 }
72 catch (CheckstyleException ex) {
73 // expected exception
74 }
75 }
76
77 public void testEmptyFilename()
78 throws Exception
79 {
80 final DefaultConfiguration checkConfig =
81 createCheckConfig(HeaderCheck.class);
82 checkConfig.addAttribute("headerFile", "");
83 try {
84 createChecker(checkConfig);
85 fail("Checker creation should not succeed with invalid headerFile");
86 }
87 catch (CheckstyleException ex) {
88 // expected exception
89 }
90 }
91 }