1 package com.puppycrawl.tools.checkstyle; 2 3 import java.io.File ; 4 5 import junit.framework.TestCase; 6 7 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 8 import com.puppycrawl.tools.checkstyle.api.AuditListener; 9 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 10 import com.puppycrawl.tools.checkstyle.api.Filter; 11 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; 12 13 public class CheckerTest extends TestCase 14 { 15 public void testDosBasedir() throws Exception 16 { 17 Checker c = new Checker(); 18 19 c.setBasedir("c:/a\\b/./c\\..\\d"); 20 assertEquals("C:\\a\\b\\d", c.getBasedir()); 21 } 22 23 public void testOsBasedir() throws Exception 24 { 25 Checker c = new Checker(); 26 27 String testinputs_dir = System.getProperty("testinputs.dir"); 30 31 if (!testinputs_dir.endsWith(File.separator)) { 32 testinputs_dir += File.separator; 33 } 34 35 c.setBasedir(testinputs_dir + "indentation/./..\\coding\\"); 36 37 38 assertEquals(testinputs_dir + "coding", c.getBasedir()); 39 } 40 41 42 public void testDestroy() throws Exception 43 { 44 DebugChecker c= new DebugChecker(); 45 DebugAuditAdapter aa = new DebugAuditAdapter(); 46 c.addListener(aa); 47 DebugFilter f = new DebugFilter(); 48 c.addFilter(f); 49 50 c.destroy(); 52 c.fireAuditStarted(); 54 c.fireAuditFinished(); 55 c.fireFileStarted("Some File Name"); 56 c.fireFileFinished("Some File Name"); 57 58 LocalizedMessage[] msgs = new LocalizedMessage[1]; 59 msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key", 60 new Object [] {"arg"}, null, 61 getClass()); 62 c.fireErrors("Some File Name", msgs); 63 64 assertFalse("Checker.destroy() doesn't remove listeners.", aa.wasCalled()); 65 assertFalse("Checker.destroy() doesn't remove filters.", f.wasCalled()); 66 } 67 68 public void testAddListener() throws Exception 69 { 70 DebugChecker c= new DebugChecker(); 71 DebugAuditAdapter aa = new DebugAuditAdapter(); 72 c.addListener(aa); 73 74 c.fireAuditStarted(); 76 assertTrue("Checker.fireAuditStarted() doesn't call listener", aa.wasCalled()); 77 78 aa.resetListener(); 79 c.fireAuditFinished(); 80 assertTrue("Checker.fireAuditFinished() doesn't call listener", aa.wasCalled()); 81 82 aa.resetListener(); 83 c.fireFileStarted("Some File Name"); 84 assertTrue("Checker.fireFileStarted() doesn't call listener", aa.wasCalled()); 85 86 aa.resetListener(); 87 c.fireFileFinished("Some File Name"); 88 assertTrue("Checker.fireFileFinished() doesn't call listener", aa.wasCalled()); 89 90 aa.resetListener(); 91 LocalizedMessage[] msgs = new LocalizedMessage[1]; 92 msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key", 93 new Object [] {"arg"}, null, 94 getClass()); 95 c.fireErrors("Some File Name", msgs); 96 assertTrue("Checker.fireErrors() doesn't call listener", aa.wasCalled()); 97 } 98 99 public void testRemoveListener() throws Exception 100 { 101 DebugChecker c= new DebugChecker(); 102 DebugAuditAdapter aa = new DebugAuditAdapter(); 103 DebugAuditAdapter aa2 = new DebugAuditAdapter(); 104 c.addListener(aa); 105 c.addListener(aa2); 106 c.removeListener(aa); 107 108 c.fireAuditStarted(); 110 assertTrue("Checker.fireAuditStarted() doesn't call listener", aa2.wasCalled()); 111 assertFalse("Checker.fireAuditStarted() does call removed listener", aa.wasCalled()); 112 113 aa2.resetListener(); 114 c.fireAuditFinished(); 115 assertTrue("Checker.fireAuditFinished() doesn't call listener", aa2.wasCalled()); 116 assertFalse("Checker.fireAuditFinished() does call removed listener", aa.wasCalled()); 117 118 aa2.resetListener(); 119 c.fireFileStarted("Some File Name"); 120 assertTrue("Checker.fireFileStarted() doesn't call listener", aa2.wasCalled()); 121 assertFalse("Checker.fireFileStarted() does call removed listener", aa.wasCalled()); 122 123 aa2.resetListener(); 124 c.fireFileFinished("Some File Name"); 125 assertTrue("Checker.fireFileFinished() doesn't call listener", aa2.wasCalled()); 126 assertFalse("Checker.fireFileFinished() does call removed listener", aa.wasCalled()); 127 128 aa2.resetListener(); 129 LocalizedMessage[] msgs = new LocalizedMessage[1]; 130 msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key", 131 new Object [] {"arg"}, null, 132 getClass()); 133 c.fireErrors("Some File Name", msgs); 134 assertTrue("Checker.fireErrors() doesn't call listener", aa2.wasCalled()); 135 assertFalse("Checker.fireErrors() does call removed listener", aa.wasCalled()); 136 137 } 138 139 public void testAddFilter() throws Exception 140 { 141 DebugChecker c= new DebugChecker(); 142 DebugFilter f = new DebugFilter(); 143 144 c.addFilter(f); 145 146 151 156 161 166 f.resetFilter(); 167 LocalizedMessage[] msgs = new LocalizedMessage[1]; 168 msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key", 169 new Object [] {"arg"}, null, 170 getClass()); 171 c.fireErrors("Some File Name", msgs); 172 assertTrue("Checker.fireErrors() doesn't call filter", f.wasCalled()); 173 } 174 175 public void testRemoveFilter() throws Exception 176 { 177 DebugChecker c= new DebugChecker(); 178 DebugFilter f = new DebugFilter(); 179 DebugFilter f2 = new DebugFilter(); 180 c.addFilter(f); 181 c.addFilter(f2); 182 c.removeFilter(f); 183 184 190 196 202 208 f2.resetFilter(); 209 LocalizedMessage[] msgs = new LocalizedMessage[1]; 210 msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key", 211 new Object [] {"arg"}, null, 212 getClass()); 213 c.fireErrors("Some File Name", msgs); 214 assertTrue("Checker.fireErrors() doesn't call filter", f2.wasCalled()); 215 assertFalse("Checker.fireErrors() does call removed filter", f.wasCalled()); 216 217 } 218 } 219 220 class DebugChecker extends Checker 222 { 223 public DebugChecker() throws CheckstyleException 224 { 225 super(); 226 } 227 228 public void fireAuditFinished() 229 { 230 super.fireAuditFinished(); 231 } 232 233 public void fireAuditStarted() 234 { 235 super.fireAuditStarted(); 236 } 237 } 238 class DebugAuditAdapter implements AuditListener 239 { 240 241 private boolean mCalled; 242 243 public boolean wasCalled() 244 { 245 return mCalled; 246 } 247 248 public void resetListener() 249 { 250 mCalled = false; 251 } 252 253 254 public void addError(AuditEvent aEvt) 255 { 256 mCalled = true; 257 } 258 259 260 public void addException(AuditEvent aEvt, Throwable aThrowable) 261 { 262 mCalled = true; 263 } 264 265 266 public void auditStarted(AuditEvent aEvt) 267 { 268 mCalled = true; 269 } 270 271 272 public void fileStarted(AuditEvent aEvt) 273 { 274 mCalled = true; 275 } 276 277 278 public void auditFinished(AuditEvent aEvt) 279 { 280 mCalled = true; 281 } 282 283 284 public void fileFinished(AuditEvent aEvt) 285 { 286 mCalled = true; 287 } 288 } 289 290 class DebugFilter implements Filter 291 { 292 private boolean mCalled; 293 294 public boolean accept(AuditEvent aEvent) 295 { 296 mCalled = true; 297 return true; 298 } 299 300 public boolean wasCalled() 301 { 302 return mCalled; 303 } 304 305 public void resetFilter() 306 { 307 mCalled = false; 308 } 309 } 310 | Popular Tags |