/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.MissingResourceException;
import java.util.Random;
import net.ranides.assira.math.LFSR;
import net.ranides.assira.text.StringLoader;
import net.ranides.assira.text.Strings;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
public class FileHelperTest {
    
    @Test
    public void testExtractResource() throws IOException {
        File file1 = FileHelper.createTempFile("resource", FileHelperTest.class);
        FileHelper.extractResource("/native-types.txt", file1);
        String expected = Strings.raw(
/*Native types (8)
-------------------
byte
short
int
long
float
double
char
boolean

Boxed types (8)
-------------------
Byte
Short
Integer
Long
Float
Double
Character
Boolean*/);
        assertEquals(expected, StringLoader.fromFile(file1)); 
        
        try {
            FileHelper.extractResource("/not-found.resource", file1);
            fail("MissingResourceException expected");
        } catch(MissingResourceException ex) {
            assertTrue(true);
        }
    }
    
    @Test
    public void testCopyEquals() throws IOException {
        File file1 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
        File file2 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
        File file3 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
        File file4 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
        File file5 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
        
        DataOutputStream out1 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file1)));
        DataOutputStream out4 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file4)));
        DataOutputStream out5 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file5)));
        
        Random gen1 = new LFSR(0x45454545);
        for(int i=0; i<256*1024; i++) {
            out1.writeInt(gen1.nextInt());
            out4.writeInt(gen1.nextInt());
            out5.writeLong(gen1.nextLong());
        }
        FileHelper.close(out1);
        FileHelper.close(out4);
        
        FileHelper.copy(file2, file1);
        FileHelper.copy(file3.toString(), file1.toString());
        
        assertTrue(FileHelper.equals(file1, file2));
        assertTrue(FileHelper.equals(file1, file3));
        assertFalse(FileHelper.equals(file1, file4));
        assertFalse(FileHelper.equals(file1, file5));
    }

    @Test
    public void testFileInfo() throws IOException {
        File file1 = FileHelper.createTempFile("io", FileHelperTest.class);
        File file2 = FileHelper.createTempFile("io", "copy", FileHelperTest.class);
        
        Writer writer = null; 
        BufferedReader reader = null;
        
        Random gen1 = new LFSR(0x45454545);
        Random gen2 = new LFSR(0x45454545);
        
        try {
            writer = LocalWriter.toFile(file1);
            for(int i=0; i<10; i++) {
                String line = Long.toString(Math.abs(gen1.nextLong()), 7);
                writer.append(line).append('\n');
            }
        } finally {
            FileHelper.close(writer);
        }
        
        FileHelper.copy(file2, file1);
        
        try {
            reader = new BufferedReader(LocalReader.fromFile(file2));
            for(int i=0; i<10; i++) {
                long line = Long.parseLong(reader.readLine(), 7);
                assertEquals(Math.abs(gen2.nextLong()), line);
            }
        } finally {
            FileHelper.close(reader);
        }
    }
}
