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

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ExceptionUtilsTest {
    
    public ExceptionUtilsTest() {
    }

    @Test
    public void testCauseStream() {
        Exception e = new IOException(new RuntimeException(new IllegalArgumentException()));
        
        List<String> s = ExceptionUtils.getCauseStream(e).map(f -> f.getClass().getName()).collect(Collectors.toList());
        List<String> expected = Arrays.asList(
            "java.io.IOException", 
            "java.lang.RuntimeException", 
            "java.lang.IllegalArgumentException"
        );
        assertEquals(expected, s);
    }
    
    @Test
    public void testCauseList() {
        Exception e = new IOException(new RuntimeException(new IllegalArgumentException()));
        
        List<Throwable> s = ExceptionUtils.getCauseList(e);
        List<Throwable> expected = Arrays.asList(
            e, 
            e.getCause(), 
            e.getCause().getCause()
        );
        assertEquals(expected, s);
    }
    
    @Test
    public void testStacktrace() {
        String st = ExceptionUtils.getStackTrace(new IOException());
        assertTrue(st.contains("ExceptionUtilsTest"));
        assertTrue(st.contains("org.junit."));
        assertTrue(st.contains("org.apache.maven."));
    }
    
    @Test
    public void testRethrow() {
        assertThrows(IOException.class, this::rm);
    }
    
    private void rm() {
        ExceptionUtils.rethrow(new IOException());
    }
   
}
