1 5 package com.puppycrawl.tools.checkstyle.filters; 6 7 import java.util.ArrayList ; 8 import java.util.Arrays ; 9 import java.util.Collection ; 10 import java.util.Locale ; 11 12 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; 13 import com.puppycrawl.tools.checkstyle.Checker; 14 import com.puppycrawl.tools.checkstyle.DefaultConfiguration; 15 import com.puppycrawl.tools.checkstyle.TreeWalker; 16 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 17 import com.puppycrawl.tools.checkstyle.api.Configuration; 18 import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder; 19 import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck; 20 import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck; 21 import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; 22 23 public class SuppressionCommentFilterTest 24 extends BaseCheckTestCase 25 { 26 static String [] sAllMessages = { 27 "13:17: Name 'I' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 28 "16:17: Name 'J' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 29 "19:17: Name 'K' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 30 "22:17: Name 'L' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 31 "23:30: Name 'm' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 32 "27:17: Name 'M2' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 33 "28:30: Name 'n' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 34 "32:17: Name 'P' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 35 "35:17: Name 'Q' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 36 "38:17: Name 'R' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 37 "39:30: Name 's' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 38 "43:17: Name 'T' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 39 "64:23: Catching 'Exception' is not allowed.", 40 "71:11: Catching 'Exception' is not allowed.", 41 }; 42 43 public void testNone() 44 throws Exception 45 { 46 final DefaultConfiguration filterConfig = null; 47 final String [] suppressed = { 48 }; 49 verifySuppressed(filterConfig, suppressed); 50 } 51 52 public void testDefault() throws Exception 54 { 55 final DefaultConfiguration filterConfig = 56 createFilterConfig(SuppressionCommentFilter.class); 57 final String [] suppressed = { 58 "16:17: Name 'J' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 59 "43:17: Name 'T' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 60 "64:23: Catching 'Exception' is not allowed.", 61 "71:11: Catching 'Exception' is not allowed.", 62 }; 63 verifySuppressed(filterConfig, suppressed); 64 } 65 66 public void testCheckC() throws Exception 67 { 68 final DefaultConfiguration filterConfig = 69 createFilterConfig(SuppressionCommentFilter.class); 70 filterConfig.addAttribute("checkC", "false"); 71 final String [] suppressed = { 72 "43:17: Name 'T' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 73 "64:23: Catching 'Exception' is not allowed.", 74 "71:11: Catching 'Exception' is not allowed.", 75 }; 76 verifySuppressed(filterConfig, suppressed); 77 } 78 79 public void testCheckCPP() throws Exception 80 { 81 final DefaultConfiguration filterConfig = 82 createFilterConfig(SuppressionCommentFilter.class); 83 filterConfig.addAttribute("checkCPP", "false"); 84 final String [] suppressed = { 85 "16:17: Name 'J' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 86 }; 87 verifySuppressed(filterConfig, suppressed); 88 } 89 90 public void testOffFormat() throws Exception 92 { 93 final DefaultConfiguration filterConfig = 94 createFilterConfig(SuppressionCommentFilter.class); 95 filterConfig.addAttribute("offCommentFormat", "CS_OFF"); 96 filterConfig.addAttribute("onCommentFormat", "CS_ON"); 97 final String [] suppressed = { 98 "32:17: Name 'P' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 99 "38:17: Name 'R' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 100 "39:30: Name 's' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 101 "42:17: Name 'T' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 102 }; 103 verifySuppressed(filterConfig, suppressed); 104 } 105 106 public void testOffFormatCheck() throws Exception 109 { 110 final DefaultConfiguration filterConfig = 111 createFilterConfig(SuppressionCommentFilter.class); 112 filterConfig.addAttribute("offCommentFormat", "CS_OFF"); 113 filterConfig.addAttribute("onCommentFormat", "CS_ON"); 114 filterConfig.addAttribute("checkFormat", "ConstantNameCheck"); 115 final String [] suppressed = { 116 "39:30: Name 's' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 117 }; 118 verifySuppressed(filterConfig, suppressed); 119 } 120 121 122 public void testExpansion() 123 throws Exception 124 { 125 final DefaultConfiguration filterConfig = 126 createFilterConfig(SuppressionCommentFilter.class); 127 filterConfig.addAttribute("offCommentFormat", "CSOFF\\: ([\\w\\|]+)"); 128 filterConfig.addAttribute("onCommentFormat", "CSON\\: ([\\w\\|]+)"); 129 filterConfig.addAttribute("checkFormat", "$1"); 130 final String [] suppressed = { 131 "22:17: Name 'L' must match pattern '^[a-z][a-zA-Z0-9]*$'.", 132 "23:30: Name 'm' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 133 "28:30: Name 'n' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", 134 }; 135 verifySuppressed(filterConfig, suppressed); 136 } 137 138 public void testMessage() 139 throws Exception 140 { 141 final DefaultConfiguration filterConfig = 142 createFilterConfig(SuppressionCommentFilter.class); 143 filterConfig.addAttribute("onCommentFormat", "UNUSED ON\\: (\\w+)"); 144 filterConfig.addAttribute("offCommentFormat", "UNUSED OFF\\: (\\w+)"); 145 filterConfig.addAttribute("checkFormat", "Unused"); 146 filterConfig.addAttribute("messageFormat", "^Unused \\w+ '$1'.$"); 147 final String [] suppressed = { 148 "47:34: Unused parameter 'aInt'.", 149 }; 150 verifySuppressed(filterConfig, suppressed); 151 } 152 153 public static DefaultConfiguration createFilterConfig(Class aClass) 154 { 155 return new DefaultConfiguration(aClass.getName()); 156 } 157 158 protected void verifySuppressed(Configuration aFilterConfig, 159 String [] aSuppressed) 160 throws Exception 161 { 162 verify(createChecker(aFilterConfig), 163 getPath("filters/InputSuppressionCommentFilter.java"), 164 removeSuppressed(sAllMessages, aSuppressed)); 165 } 166 167 protected Checker createChecker(Configuration aFilterConfig) 168 throws CheckstyleException 169 { 170 final DefaultConfiguration checkerConfig = 171 new DefaultConfiguration("configuration"); 172 final DefaultConfiguration checksConfig = createCheckConfig(TreeWalker.class); 173 checksConfig.addChild(createCheckConfig(FileContentsHolder.class)); 174 checksConfig.addChild(createCheckConfig(MemberNameCheck.class)); 175 checksConfig.addChild(createCheckConfig(ConstantNameCheck.class)); 176 checksConfig.addChild(createCheckConfig(IllegalCatchCheck.class)); 177 checkerConfig.addChild(checksConfig); 178 if (aFilterConfig != null) { 179 checkerConfig.addChild(aFilterConfig); 180 } 181 final Checker checker = new Checker(); 182 final Locale locale = Locale.ENGLISH; 183 checker.setLocaleCountry(locale.getCountry()); 184 checker.setLocaleLanguage(locale.getLanguage()); 185 checker.configure(checkerConfig); 186 checker.addListener(new BriefLogger(mStream)); 187 return checker; 188 } 189 190 private String [] removeSuppressed(String [] aFrom, String [] aRemove) 191 { 192 final Collection coll = new ArrayList (Arrays.asList(aFrom)); 193 coll.removeAll(Arrays.asList(aRemove)); 194 return (String []) coll.toArray(new String [coll.size()]); 195 } 196 } 197 | Popular Tags |