001: package com.puppycrawl.tools.checkstyle.bcel;
002:
003: import com.puppycrawl.tools.checkstyle.api.AuditEvent;
004: import com.puppycrawl.tools.checkstyle.api.Configuration;
005: import com.puppycrawl.tools.checkstyle.*;
006: import java.io.ByteArrayInputStream;
007: import java.io.ByteArrayOutputStream;
008: import java.io.File;
009: import java.io.IOException;
010: import java.io.InputStreamReader;
011: import java.io.LineNumberReader;
012: import java.io.OutputStream;
013: import java.io.PrintStream;
014: import java.util.Locale;
015: import java.util.Properties;
016:
017: import junit.framework.TestCase;
018:
019: public abstract class BcelCheckTestCase extends TestCase {
020: /** a brief logger that only display info about errors */
021: protected static class BriefLogger extends DefaultLogger {
022: public BriefLogger(OutputStream out) {
023: super (out, true);
024: }
025:
026: public void auditStarted(AuditEvent evt) {
027: }
028:
029: public void fileFinished(AuditEvent evt) {
030: }
031:
032: public void fileStarted(AuditEvent evt) {
033: }
034: }
035:
036: private final ByteArrayOutputStream mBAOS = new ByteArrayOutputStream();
037: protected final PrintStream mStream = new PrintStream(mBAOS);
038: protected final Properties mProps = new Properties();
039:
040: public static DefaultConfiguration createCheckConfig(Class aClazz) {
041: final DefaultConfiguration checkConfig = new DefaultConfiguration(
042: aClazz.getName());
043: return checkConfig;
044: }
045:
046: protected Checker createChecker(Configuration aCheckConfig)
047: throws Exception {
048: final DefaultConfiguration dc = createCheckerConfig(aCheckConfig);
049: final Checker c = new Checker();
050: // make sure the tests always run with english error messages
051: // so the tests don't fail in supported locales like german
052: final Locale locale = Locale.ENGLISH;
053: c.setLocaleCountry(locale.getCountry());
054: c.setLocaleLanguage(locale.getLanguage());
055: c.configure(dc);
056: c.addListener(new BriefLogger(mStream));
057: return c;
058: }
059:
060: protected DefaultConfiguration createCheckerConfig(
061: Configuration aConfig) {
062: final DefaultConfiguration dc = new DefaultConfiguration(
063: "configuration");
064: final DefaultConfiguration twConf = createCheckConfig(ClassFileSetCheck.class);
065: dc.addChild(twConf);
066: twConf.addChild(aConfig);
067: return dc;
068: }
069:
070: protected static String getPath(String aFilename)
071: throws IOException {
072: final File f = new File(System
073: .getProperty("testinputs.compiled.dir"), aFilename);
074: return f.getCanonicalPath();
075: }
076:
077: protected void verify(Configuration aConfig, String aFileName,
078: String[] aExpected) throws Exception {
079: verify(createChecker(aConfig), aFileName, aFileName, aExpected);
080: }
081:
082: protected void verify(Checker aC, String aFileName,
083: String[] aExpected) throws Exception {
084: verify(aC, aFileName, aFileName, aExpected);
085: }
086:
087: protected void verify(Checker aC, String aProcessedFilename,
088: String aMessageFileName, String[] aExpected)
089: throws Exception {
090: verify(aC, new File[] { new File(aProcessedFilename) },
091: aMessageFileName, aExpected);
092: }
093:
094: protected void verify(Checker aC, File[] aProcessedFiles,
095: String aMessageFileName, String[] aExpected)
096: throws Exception {
097: mStream.flush();
098: final int errs = aC.process(aProcessedFiles);
099:
100: // process each of the lines
101: final ByteArrayInputStream bais = new ByteArrayInputStream(
102: mBAOS.toByteArray());
103: final LineNumberReader lnr = new LineNumberReader(
104: new InputStreamReader(bais));
105:
106: for (int i = 0; i < aExpected.length; i++) {
107: final String expected = aMessageFileName + ":"
108: + aExpected[i];
109: final String actual = lnr.readLine();
110: assertEquals("error message " + i, expected, actual);
111: }
112:
113: assertEquals("unexpected output: " + lnr.readLine(),
114: aExpected.length, errs);
115: aC.destroy();
116: }
117: }
|