1 package com.puppycrawl.tools.checkstyle.checks.header; 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() throws Exception 23 { 24 final DefaultConfiguration checkConfig = 25 createCheckConfig(RegexpHeaderCheck.class); 26 checkConfig.addAttribute("headerFile", getPath("regexp.header")); 27 final String [] expected = { 28 "3: Line does not match expected header line of '// Created: 2002'." 29 }; 30 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected); 31 } 32 33 public void testInlineRegexpHeader() 34 throws Exception 35 { 36 final DefaultConfiguration checkConfig = 37 createCheckConfig(RegexpHeaderCheck.class); 38 checkConfig.addAttribute("header", "^/*$\\n// .*\\n// Created: 2002\\n^//.*\\n^//.*"); 39 final String [] expected = { 40 "3: Line does not match expected header line of '// Created: 2002'." 41 }; 42 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected); 43 } 44 45 public void testFailureForMultilineRegexp() 46 throws Exception 47 { 48 final DefaultConfiguration checkConfig = 49 createCheckConfig(RegexpHeaderCheck.class); 50 checkConfig.addAttribute("header", "^(.*\\n.*)"); 51 try { 52 createChecker(checkConfig); 53 fail("Checker creation should not succeed when regexp spans multiple lines"); 54 } 55 catch (CheckstyleException ex) { 56 } 58 } 59 60 public void testRegexpHeaderIgnore() throws Exception 61 { 62 final DefaultConfiguration checkConfig = 63 createCheckConfig(RegexpHeaderCheck.class); 64 checkConfig.addAttribute("headerFile", getPath("regexp.header1")); 65 final String [] expected = { 66 }; 67 verify(checkConfig, getPath("InputScopeAnonInner.java"), expected); 68 } 69 70 public void testRegexpHeaderMulti1() throws Exception 71 { 72 final DefaultConfiguration checkConfig = 73 createCheckConfig(RegexpHeaderCheck.class); 74 checkConfig.addAttribute("headerFile", getPath("regexp.header2")); 75 checkConfig.addAttribute("multiLines", "3, 6"); 76 final String [] expected = { 77 }; 78 verify(checkConfig, getPath("InputRegexpHeader1.java"), expected); 79 } 80 81 public void testRegexpHeaderMulti2() throws Exception 82 { 83 final DefaultConfiguration checkConfig = 84 createCheckConfig(RegexpHeaderCheck.class); 85 checkConfig.addAttribute("headerFile", getPath("regexp.header2")); 86 checkConfig.addAttribute("multiLines", "3, 6"); 87 final String [] expected = { 88 }; 89 verify(checkConfig, getPath("InputRegexpHeader2.java"), expected); 90 } 91 92 public void testRegexpHeaderMulti3() throws Exception 93 { 94 final DefaultConfiguration checkConfig = 95 createCheckConfig(RegexpHeaderCheck.class); 96 checkConfig.addAttribute("headerFile", getPath("regexp.header2")); 97 checkConfig.addAttribute("multiLines", "3, 7"); 98 final String [] expected = { 99 }; 100 verify(checkConfig, getPath("InputRegexpHeader1.java"), expected); 101 } 102 103 public void testRegexpHeaderMulti4() throws Exception 104 { 105 final DefaultConfiguration checkConfig = 106 createCheckConfig(RegexpHeaderCheck.class); 107 checkConfig.addAttribute("headerFile", getPath("regexp.header2")); 108 checkConfig.addAttribute("multiLines", "3, 5, 6, 7"); 109 final String [] expected = { 110 }; 111 verify(checkConfig, getPath("InputRegexpHeader3.java"), expected); 112 } 113 114 public void testRegexpHeaderMulti5() throws Exception 115 { 116 final DefaultConfiguration checkConfig = 117 createCheckConfig(RegexpHeaderCheck.class); 118 checkConfig.addAttribute("headerFile", getPath("regexp.header2")); 119 checkConfig.addAttribute("multiLines", "3"); 120 final String [] expected = { 121 "1: Missing a header - not enough lines in file." 122 }; 123 verify(checkConfig, getPath("InputRegexpHeader4.java"), expected); 124 } 125 126 public void testRegexpHeaderSmallHeader() throws Exception 127 { 128 final DefaultConfiguration checkConfig = 129 createCheckConfig(RegexpHeaderCheck.class); 130 checkConfig.addAttribute("headerFile", getPath("regexp.header2")); 131 checkConfig.addAttribute("multiLines", "3, 6"); 132 final String [] expected = { 133 }; 134 verify(checkConfig, getPath("InputRegexpSmallHeader.java"), expected); 135 } 136 137 public void testNoHeader() 138 throws Exception 139 { 140 final DefaultConfiguration checkConfig = 141 createCheckConfig(HeaderCheck.class); 142 try { 144 createChecker(checkConfig); 145 fail(); 146 } 147 catch (CheckstyleException ex) { 148 } 150 } 151 152 public void testNonExistingHeaderFile() 153 throws Exception 154 { 155 final DefaultConfiguration checkConfig = 156 createCheckConfig(HeaderCheck.class); 157 checkConfig.addAttribute("headerFile", getPath("nonexisting.file")); 158 try { 159 createChecker(checkConfig); 160 fail(); 161 } 162 catch (CheckstyleException ex) { 163 } 165 } 166 167 public void testEmptyFilename() 168 throws Exception 169 { 170 final DefaultConfiguration checkConfig = 171 createCheckConfig(HeaderCheck.class); 172 checkConfig.addAttribute("headerFile", ""); 173 try { 174 createChecker(checkConfig); 175 fail("Checker creation should not succeed with invalid headerFile"); 176 } 177 catch (CheckstyleException ex) { 178 } 180 } 181 } 182 | Popular Tags |