1 package com.puppycrawl.tools.checkstyle.bcel; 2 3 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 4 import com.puppycrawl.tools.checkstyle.api.Configuration; 5 import com.puppycrawl.tools.checkstyle.*; 6 import java.io.ByteArrayInputStream ; 7 import java.io.ByteArrayOutputStream ; 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.InputStreamReader ; 11 import java.io.LineNumberReader ; 12 import java.io.OutputStream ; 13 import java.io.PrintStream ; 14 import java.util.Locale ; 15 import java.util.Properties ; 16 17 import junit.framework.TestCase; 18 19 public abstract class BcelCheckTestCase 20 extends TestCase 21 { 22 23 protected static class BriefLogger 24 extends DefaultLogger 25 { 26 public BriefLogger(OutputStream out) 27 { 28 super(out, true); 29 } 30 public void auditStarted(AuditEvent evt) {} 31 public void fileFinished(AuditEvent evt) {} 32 public void fileStarted(AuditEvent evt) {} 33 } 34 35 private final ByteArrayOutputStream mBAOS = new ByteArrayOutputStream (); 36 protected final PrintStream mStream = new PrintStream (mBAOS); 37 protected final Properties mProps = new Properties (); 38 39 public static DefaultConfiguration createCheckConfig(Class aClazz) 40 { 41 final DefaultConfiguration checkConfig = 42 new DefaultConfiguration(aClazz.getName()); 43 return checkConfig; 44 } 45 46 protected Checker createChecker(Configuration aCheckConfig) 47 throws Exception 48 { 49 final DefaultConfiguration dc = createCheckerConfig(aCheckConfig); 50 final Checker c = new Checker(); 51 final Locale locale = Locale.ENGLISH; 54 c.setLocaleCountry(locale.getCountry()); 55 c.setLocaleLanguage(locale.getLanguage()); 56 c.configure(dc); 57 c.addListener(new BriefLogger(mStream)); 58 return c; 59 } 60 61 protected DefaultConfiguration createCheckerConfig(Configuration aConfig) 62 { 63 final DefaultConfiguration dc = 64 new DefaultConfiguration("configuration"); 65 final DefaultConfiguration twConf = createCheckConfig(ClassFileSetCheck.class); 66 dc.addChild(twConf); 67 twConf.addChild(aConfig); 68 return dc; 69 } 70 71 protected static String getPath(String aFilename) 72 throws IOException 73 { 74 final File f = new File (System.getProperty("testinputs.compiled.dir"), 75 aFilename); 76 return f.getCanonicalPath(); 77 } 78 79 protected void verify(Configuration aConfig, String aFileName, String [] aExpected) 80 throws Exception 81 { 82 verify(createChecker(aConfig), aFileName, aFileName, aExpected); 83 } 84 85 protected void verify(Checker aC, String aFileName, String [] aExpected) 86 throws Exception 87 { 88 verify(aC, aFileName, aFileName, aExpected); 89 } 90 91 protected void verify(Checker aC, 92 String aProcessedFilename, 93 String aMessageFileName, 94 String [] aExpected) 95 throws Exception 96 { 97 verify(aC, 98 new File [] {new File (aProcessedFilename)}, 99 aMessageFileName, aExpected); 100 } 101 102 protected void verify(Checker aC, 103 File [] aProcessedFiles, 104 String aMessageFileName, 105 String [] aExpected) 106 throws Exception 107 { 108 mStream.flush(); 109 final int errs = aC.process(aProcessedFiles); 110 111 final ByteArrayInputStream bais = 113 new ByteArrayInputStream (mBAOS.toByteArray()); 114 final LineNumberReader lnr = 115 new LineNumberReader (new InputStreamReader (bais)); 116 117 for (int i = 0; i < aExpected.length; i++) { 118 final String expected = aMessageFileName + ":" + aExpected[i]; 119 final String actual = lnr.readLine(); 120 assertEquals("error message " + i, expected, actual); 121 } 122 123 assertEquals("unexpected output: " + lnr.readLine(), 124 aExpected.length, errs); 125 aC.destroy(); 126 } 127 } 128 | Popular Tags |