package com.puppycrawl.tools.checkstyle;

import com.puppycrawl.tools.checkstyle.api.Configuration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties;

import junit.framework.TestCase;

public abstract class BaseCheckTestCase
    extends TestCase
{
    /** a brief logger that only display info about errors */
    protected static class BriefLogger
        extends DefaultLogger
    {
        public BriefLogger(OutputStream out)
        {
            super(out, true);
        }
        public void auditStarted(AuditEvent evt) {}
        public void fileFinished(AuditEvent evt) {}
        public void fileStarted(AuditEvent evt) {}
    }

    private final ByteArrayOutputStream mBAOS = new ByteArrayOutputStream();
    protected final PrintStream mStream = new PrintStream(mBAOS);
    protected final Properties mProps = new Properties();

    public static DefaultConfiguration createCheckConfig(Class aClazz)
    {
        final DefaultConfiguration checkConfig =
            new DefaultConfiguration("test");
        checkConfig.addAttribute("classname", aClazz.getName());
        return checkConfig;
    }

    protected Checker createChecker(Configuration aConfig)
        throws Exception
    {
        throw new IllegalStateException("not implemented yet");
//        final DefaultConfiguration dc = new DefaultConfiguration("root");
//        dc.addChild(aConfig);
//        final Checker c = new Checker();
//        c.configure(aConfig);
//        c.addListener(new BriefLogger(mStream));
//        return c;
    }

    protected static String getPath(String aFilename)
        throws IOException
    {
        final File f = new File(System.getProperty("testinputs.dir"),
                                aFilename);
        return f.getCanonicalPath();
    }

    protected void verify(Checker aC, String aFileName, String[] aExpected)
            throws Exception
    {
        verify(aC, aFileName, aFileName, aExpected);
    }

    protected void verify(Checker aC, String aProcessedFilename,
            String aMessageFileName, String[] aExpected)
        throws Exception
    {
        mStream.flush();
        final int errs = aC.process(new File[] {new File(aProcessedFilename)});

        // process each of the lines
        final ByteArrayInputStream bais =
            new ByteArrayInputStream(mBAOS.toByteArray());
        final LineNumberReader lnr =
            new LineNumberReader(new InputStreamReader(bais));

        for (int i = 0; i < aExpected.length; i++) {
            assertEquals(aMessageFileName + ":" + aExpected[i], lnr.readLine());
        }
        assertEquals(aExpected.length, errs);
        aC.destroy();
    }
}

@@ -1392,15 +1392,20 @@
 n aC
+heckC
 onfig)%0A
-
     
@@ -1443,66 +1443,74 @@
     
-throw new IllegalStateException(%22not implemented ye
+final DefaultConfiguration dc = new DefaultConfiguration(%22roo
 t%22);%0A
-//
     
@@ -1544,49 +1544,54 @@
 ion 
-dc = new DefaultConfiguration(%22root%22
+twConf = createCheckConfig(TreeWalker.class
 );%0A
-//
     
@@ -1606,17 +1606,55 @@
 ddChild(
-a
+twConf);%0A        twConf.addChild(aCheck
 Config);
@@ -1646,34 +1646,32 @@
 (aCheckConfig);%0A
-//
         final Ch
@@ -1695,18 +1695,16 @@
 cker();%0A
-//
         
@@ -1719,20 +1719,13 @@
 ure(
-aConfig
+dc
 );%0A
-//
     
@@ -1769,18 +1769,16 @@
 ream));%0A
-//
         

implement createChecker for real, most tests are running now