1 package com.puppycrawl.tools.checkstyle.checks; 2 3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; 4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration; 5 6 import java.io.File ; 7 8 public class DescendantTokenCheckTest extends BaseCheckTestCase 9 { 10 public void testDefault() 11 throws Exception 12 { 13 final DefaultConfiguration checkConfig = 14 createCheckConfig(DescendantTokenCheck.class); 15 final String [] expected = {}; 16 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 17 } 18 19 public void testMaximumNumber() 20 throws Exception 21 { 22 final DefaultConfiguration checkConfig = 23 createCheckConfig(DescendantTokenCheck.class); 24 checkConfig.addAttribute("tokens", "LITERAL_NATIVE"); 25 checkConfig.addAttribute("limitedTokens", "LITERAL_NATIVE"); 26 checkConfig.addAttribute("maximumNumber", "0"); 27 final String [] expected = { 28 "20:12: Count of 1 for 'LITERAL_NATIVE' descendant 'LITERAL_NATIVE' exceeds maximum count 0.", 29 }; 30 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 31 } 32 33 public void testMessage() 34 throws Exception 35 { 36 final DefaultConfiguration checkConfig = 37 createCheckConfig(DescendantTokenCheck.class); 38 checkConfig.addAttribute("tokens", "LITERAL_NATIVE"); 39 checkConfig.addAttribute("limitedTokens", "LITERAL_NATIVE"); 40 checkConfig.addAttribute("maximumNumber", "0"); 41 checkConfig.addAttribute("maximumMessage", "Using ''native'' is not allowed."); 42 final String [] expected = { 43 "20:12: Using 'native' is not allowed.", 44 }; 45 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 46 } 47 48 public void testMinimumNumber() 49 throws Exception 50 { 51 final DefaultConfiguration checkConfig = 52 createCheckConfig(DescendantTokenCheck.class); 53 checkConfig.addAttribute("tokens", "LITERAL_SWITCH"); 54 checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT"); 55 checkConfig.addAttribute("minimumNumber", "2"); 56 final String [] expected = { 57 "11:9: Count of 1 for 'LITERAL_SWITCH' descendant 'LITERAL_DEFAULT' is less than minimum count 2.", 58 }; 59 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 60 } 61 62 public void testMinimumDepth() 63 throws Exception 64 { 65 final DefaultConfiguration checkConfig = 66 createCheckConfig(DescendantTokenCheck.class); 67 checkConfig.addAttribute("tokens", "LITERAL_SWITCH"); 68 checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT"); 69 checkConfig.addAttribute("maximumNumber", "0"); 70 checkConfig.addAttribute("minimumDepth", "3"); 71 final String [] expected = {}; 72 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 73 } 74 75 public void testMaximumDepth() 76 throws Exception 77 { 78 final DefaultConfiguration checkConfig = 79 createCheckConfig(DescendantTokenCheck.class); 80 checkConfig.addAttribute("tokens", "LITERAL_SWITCH"); 81 checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT"); 82 checkConfig.addAttribute("maximumNumber", "0"); 83 checkConfig.addAttribute("maximumDepth", "1"); 84 final String [] expected = {}; 85 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 86 } 87 88 public void testEmptyStatements() 89 throws Exception 90 { 91 final DefaultConfiguration checkConfig = 92 createCheckConfig(DescendantTokenCheck.class); 93 checkConfig.addAttribute("tokens", "EMPTY_STAT"); 94 checkConfig.addAttribute("limitedTokens", "EMPTY_STAT"); 95 checkConfig.addAttribute("maximumNumber", "0"); 96 checkConfig.addAttribute("maximumDepth", "0"); 97 checkConfig.addAttribute("maximumMessage", "Empty statement."); 98 99 final String [] expected = { 100 "12:7: Empty statement.", 101 "17:7: Empty statement.", 102 "22:19: Empty statement.", 103 "26:10: Empty statement.", 104 "29:16: Empty statement.", 105 "33:10: Empty statement.", 106 "43:10: Empty statement.", 107 "49:13: Empty statement.", 108 "51:13: Empty statement.", 109 "54:19: Empty statement.", 110 "58:10: Empty statement.", 111 "61:9: Empty statement.", 112 "66:10: Empty statement.", 113 "72:10: Empty statement.", 114 "76:10: Empty statement.", 115 "80:10: Empty statement.", 116 }; 117 118 verify(checkConfig, getPath("InputEmptyStatement.java"), expected); 119 } 120 121 public void testMissingSwitchDefault() throws Exception 122 { 123 final DefaultConfiguration checkConfig = 124 createCheckConfig(DescendantTokenCheck.class); 125 checkConfig.addAttribute("tokens", "LITERAL_SWITCH"); 126 checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT"); 127 checkConfig.addAttribute("minimumNumber", "1"); 128 checkConfig.addAttribute("maximumDepth", "2"); 129 checkConfig.addAttribute("minimumMessage", "switch without \"default\" clause."); 130 131 final String [] expected = { 132 "15:9: switch without \"default\" clause.", 133 }; 134 135 verify(checkConfig, getPath("InputMissingSwitchDefault.java"), expected); 136 } 137 138 public void testStringLiteralEquality() throws Exception 139 { 140 final DefaultConfiguration checkConfig = 141 createCheckConfig(DescendantTokenCheck.class); 142 checkConfig.addAttribute("tokens", "EQUAL,NOT_EQUAL"); 143 checkConfig.addAttribute("limitedTokens", "STRING_LITERAL"); 144 checkConfig.addAttribute("maximumNumber", "0"); 145 checkConfig.addAttribute("maximumDepth", "1"); 146 checkConfig.addAttribute("maximumMessage", "Literal Strings should be compared using equals(), not ''==''."); 147 148 final String [] expected = { 149 "11:18: Literal Strings should be compared using equals(), not '=='.", 150 "16:20: Literal Strings should be compared using equals(), not '=='.", 151 "21:22: Literal Strings should be compared using equals(), not '=='.", 152 }; 153 verify(checkConfig, getPath("coding" + File.separator + "InputStringLiteralEquality.java"), expected); 154 } 155 156 public void testIllegalTokenDefault() throws Exception 157 { 158 final DefaultConfiguration checkConfig = 159 createCheckConfig(DescendantTokenCheck.class); 160 checkConfig.addAttribute("tokens", "LITERAL_SWITCH, POST_INC, POST_DEC"); 161 checkConfig.addAttribute("limitedTokens", "LITERAL_SWITCH, POST_INC, POST_DEC"); 162 checkConfig.addAttribute("maximumNumber", "0"); 163 checkConfig.addAttribute("maximumDepth", "0"); 164 checkConfig.addAttribute("maximumMessage", "Using ''{2}'' is not allowed."); 165 166 final String [] expected = { 167 "11:9: Using 'LITERAL_SWITCH' is not allowed.", 168 "14:18: Using 'POST_DEC' is not allowed.", 169 "15:18: Using 'POST_INC' is not allowed.", 170 }; 171 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 172 } 173 174 public void testIllegalTokenNative() throws Exception 175 { 176 final DefaultConfiguration checkConfig = 177 createCheckConfig(DescendantTokenCheck.class); 178 checkConfig.addAttribute("tokens", "LITERAL_NATIVE"); 179 checkConfig.addAttribute("limitedTokens", "LITERAL_NATIVE"); 180 checkConfig.addAttribute("maximumNumber", "0"); 181 checkConfig.addAttribute("maximumDepth", "0"); 182 checkConfig.addAttribute("maximumMessage", "Using ''{2}'' is not allowed."); 183 184 final String [] expected = { 185 "20:12: Using 'LITERAL_NATIVE' is not allowed.", 186 }; 187 verify(checkConfig, getPath("InputIllegalTokens.java"), expected); 188 } 189 190 public void testReturnFromCatch() throws Exception 191 { 192 DefaultConfiguration checkConfig = createCheckConfig(DescendantTokenCheck.class); 193 194 checkConfig.addAttribute("tokens", "LITERAL_CATCH"); 195 checkConfig.addAttribute("limitedTokens", "LITERAL_RETURN"); 196 checkConfig.addAttribute("maximumNumber", "0"); 197 checkConfig.addAttribute("maximumMessage", "Return from catch is not allowed."); 198 199 String [] expected = { 200 "7:11: Return from catch is not allowed.", 201 "15:11: Return from catch is not allowed." 202 }; 203 204 verify(checkConfig, 205 getPath("coding" + File.separator + "InputReturnFromCatchCheck.java"), 206 expected); 207 } 208 209 public void testReturnFromFinally() throws Exception 210 { 211 DefaultConfiguration checkConfig = createCheckConfig(DescendantTokenCheck.class); 212 213 checkConfig.addAttribute("tokens", "LITERAL_FINALLY"); 214 checkConfig.addAttribute("limitedTokens", "LITERAL_RETURN"); 215 checkConfig.addAttribute("maximumNumber", "0"); 216 checkConfig.addAttribute("maximumMessage", "Return from finally is not allowed."); 217 218 String [] expected = { 219 "7:11: Return from finally is not allowed.", 220 "15:11: Return from finally is not allowed." 221 }; 222 223 verify(checkConfig, 224 getPath("coding" + File.separator + "InputReturnFromFinallyCheck.java"), 225 expected); 226 } 227 } 228 | Popular Tags |