/*
 * @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.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.reflection.util.ClassUtils;
import net.ranides.assira.text.Charsets;
import net.ranides.assira.text.IOStrings;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ResourceUtilsTest {

    @Test
    public void testReader() throws IOException {
        ClassLoader loader = ClassUtils.loader(ClassUtils.loader(), ContractTesters.JAR_TEST);
        
        Reader reader1 = ResourceUtils.reader(loader, "text/info.txt", Charsets.UTF8);
        assertEquals("Hello world", IOStrings.read(reader1));
        
        assertThrows(IOException.class, ()->{
            ResourceUtils.reader("text/info.txt", Charsets.UTF8);
        });
        
        ClassLoader prev = replace(loader);
        try {
            Reader reader2 = ResourceUtils.reader("text/info.txt", Charsets.UTF8);
            assertEquals("Hello world", IOStrings.read(reader2));
        } finally {
            replace(prev);
        }
    }
    
    @Test
    public void testStream() throws IOException {
        ClassLoader loader = ClassUtils.loader(ClassUtils.loader(), ContractTesters.JAR_TEST);
        
        InputStream istream1 = ResourceUtils.stream(loader, "text/info.txt");
        assertEquals("Hello world", IOStrings.read(istream1, Charsets.UTF8));
        
        assertThrows(IOException.class, ()->{
            ResourceUtils.stream("text/info.txt");
        });
        
        ClassLoader prev = replace(loader);
        try {
            InputStream istream2 = ResourceUtils.stream("text/info.txt");
            assertEquals("Hello world", IOStrings.read(istream2, Charsets.UTF8));
        } finally {
            replace(prev);
        }
    }
    
    @Test
    public void testExtract() throws IOException {
        ClassLoader loader = ClassUtils.loader(ClassUtils.loader(), ContractTesters.JAR_TEST);
        
        File out1 = File.createTempFile("assira.ResourceUtils", ".txt");
        ResourceUtils.extract(loader, "text/info.txt", out1);
        assertEquals("Hello world", IOStrings.read(out1, Charsets.UTF8));
        
        assertThrows(IOException.class, ()->{
            ResourceUtils.extract("text/info.txt", out1);
        });
        
        ClassLoader prev = replace(loader);
        try {
            File out2 = File.createTempFile("assira.ResourceUtils", ".txt");
            ResourceUtils.extract("text/info.txt", out2);
            assertEquals("Hello world", IOStrings.read(out2, Charsets.UTF8));
        } finally {
            replace(prev);
        }
        
    }
    
    private static ClassLoader replace(ClassLoader loader) {
		ClassLoader prev = Thread.currentThread().getContextClassLoader();
		Thread.currentThread().setContextClassLoader(loader);
		return prev;
	}
    
}
