
package com.ecyrd.jspwiki;

import junit.framework.*;
import java.io.*;
import java.util.*;
import org.apache.log4j.*;

public class FileUtilTest extends TestCase
{
    static
    {
        BasicConfigurator.configure();
    }

    public FileUtilTest( String s )
    {
        super( s );
    }

    public void setUp()
        throws Exception
    {
    }

    public void tearDown()
    {
    }

    public void testReadContentsLatin1()
        throws Exception
    {
        String src = "abc";

        String res = FileUtil.readContents( new ByteArrayInputStream( src.getBytes("ISO-8859-1") ),
                                            "ISO-8859-1" );

        assertEquals( src, res );
    }

    /**
     *  Check that fallbacks to ISO-Latin1 still work.
     */
    public void testReadContentsLatin1_2()
        throws Exception
    {
        String src = "abcdef";

        String res = FileUtil.readContents( new ByteArrayInputStream( src.getBytes("ISO-8859-1") ),
                                            "UTF-8" );

        assertEquals( src, res );
    }

    /**
       ISO Latin 1 from a pipe.

       Works only on UNIX systems now.
    */
    public void testReadContentsFromPipe()
        throws Exception
    {
        String src = "abc\n123456\n\nfoobar.\n";

        // Make a very long string.

        for( int i = 0; i < 10; i++ )
        {
            src += src;
        }

        src += "";

        File f = FileUtil.newTmpFile( src, "ISO-8859-1" );

        String[] envp = {};

        try
        {
            Process process = Runtime.getRuntime().exec( "cat "+f.getAbsolutePath(), 
                                                         envp, 
                                                         f.getParentFile() );

            String result = FileUtil.readContents( process.getInputStream(), "UTF-8" );

            f.delete();

            assertEquals( src,
                          result );
        }
        catch( IOException e ) {}
    }

    public static Test suite()
    {
        return new TestSuite( FileUtilTest.class );
    }
}

@@ -1117,16 +1117,23 @@
 %0A%0A      
+ FIXME:
  Works o

Comment check.