001: package com.puppycrawl.tools.checkstyle.checks.header;
002:
003: import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
004: import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
005: import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
006:
007: public class HeaderCheckTest extends BaseCheckTestCase {
008: public void testStaticHeader() throws Exception {
009: final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
010: checkConfig.addAttribute("headerFile", getPath("java.header"));
011: checkConfig.addAttribute("ignoreLines", "");
012: final String[] expected = { "1: Missing a header - not enough lines in file." };
013: verify(checkConfig, getPath("inputHeader.java"), expected);
014: }
015:
016: public void testRegexpHeader() throws Exception {
017: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
018: checkConfig
019: .addAttribute("headerFile", getPath("regexp.header"));
020: final String[] expected = { "3: Line does not match expected header line of '// Created: 2002'." };
021: verify(checkConfig, getPath("InputScopeAnonInner.java"),
022: expected);
023: }
024:
025: public void testInlineRegexpHeader() throws Exception {
026: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
027: checkConfig.addAttribute("header",
028: "^/*$\\n// .*\\n// Created: 2002\\n^//.*\\n^//.*");
029: final String[] expected = { "3: Line does not match expected header line of '// Created: 2002'." };
030: verify(checkConfig, getPath("InputScopeAnonInner.java"),
031: expected);
032: }
033:
034: public void testFailureForMultilineRegexp() throws Exception {
035: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
036: checkConfig.addAttribute("header", "^(.*\\n.*)");
037: try {
038: createChecker(checkConfig);
039: fail("Checker creation should not succeed when regexp spans multiple lines");
040: } catch (CheckstyleException ex) {
041: // expected exception
042: }
043: }
044:
045: public void testRegexpHeaderIgnore() throws Exception {
046: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
047: checkConfig.addAttribute("headerFile",
048: getPath("regexp.header1"));
049: final String[] expected = {};
050: verify(checkConfig, getPath("InputScopeAnonInner.java"),
051: expected);
052: }
053:
054: public void testRegexpHeaderMulti1() throws Exception {
055: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
056: checkConfig.addAttribute("headerFile",
057: getPath("regexp.header2"));
058: checkConfig.addAttribute("multiLines", "3, 6");
059: final String[] expected = {};
060: verify(checkConfig, getPath("InputRegexpHeader1.java"),
061: expected);
062: }
063:
064: public void testRegexpHeaderMulti2() throws Exception {
065: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
066: checkConfig.addAttribute("headerFile",
067: getPath("regexp.header2"));
068: checkConfig.addAttribute("multiLines", "3, 6");
069: final String[] expected = {};
070: verify(checkConfig, getPath("InputRegexpHeader2.java"),
071: expected);
072: }
073:
074: public void testRegexpHeaderMulti3() throws Exception {
075: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
076: checkConfig.addAttribute("headerFile",
077: getPath("regexp.header2"));
078: checkConfig.addAttribute("multiLines", "3, 7");
079: final String[] expected = {};
080: verify(checkConfig, getPath("InputRegexpHeader1.java"),
081: expected);
082: }
083:
084: public void testRegexpHeaderMulti4() throws Exception {
085: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
086: checkConfig.addAttribute("headerFile",
087: getPath("regexp.header2"));
088: checkConfig.addAttribute("multiLines", "3, 5, 6, 7");
089: final String[] expected = {};
090: verify(checkConfig, getPath("InputRegexpHeader3.java"),
091: expected);
092: }
093:
094: public void testRegexpHeaderMulti5() throws Exception {
095: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
096: checkConfig.addAttribute("headerFile",
097: getPath("regexp.header2"));
098: checkConfig.addAttribute("multiLines", "3");
099: final String[] expected = { "1: Missing a header - not enough lines in file." };
100: verify(checkConfig, getPath("InputRegexpHeader4.java"),
101: expected);
102: }
103:
104: public void testRegexpHeaderSmallHeader() throws Exception {
105: final DefaultConfiguration checkConfig = createCheckConfig(RegexpHeaderCheck.class);
106: checkConfig.addAttribute("headerFile",
107: getPath("regexp.header2"));
108: checkConfig.addAttribute("multiLines", "3, 6");
109: final String[] expected = {};
110: verify(checkConfig, getPath("InputRegexpSmallHeader.java"),
111: expected);
112: }
113:
114: public void testNoHeader() throws Exception {
115: final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
116: // No header file specified
117: try {
118: createChecker(checkConfig);
119: fail();
120: } catch (CheckstyleException ex) {
121: // expected exception
122: }
123: }
124:
125: public void testNonExistingHeaderFile() throws Exception {
126: final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
127: checkConfig.addAttribute("headerFile",
128: getPath("nonexisting.file"));
129: try {
130: createChecker(checkConfig);
131: fail();
132: } catch (CheckstyleException ex) {
133: // expected exception
134: }
135: }
136:
137: public void testEmptyFilename() throws Exception {
138: final DefaultConfiguration checkConfig = createCheckConfig(HeaderCheck.class);
139: checkConfig.addAttribute("headerFile", "");
140: try {
141: createChecker(checkConfig);
142: fail("Checker creation should not succeed with invalid headerFile");
143: } catch (CheckstyleException ex) {
144: // expected exception
145: }
146: }
147: }
|