package com.aol.cyclops.react.simple;

import static org.junit.Assert.*;

import org.junit.Test;

import com.aol.cyclops.control.LazyReact;
import com.aol.cyclops.control.SimpleReact;
import com.aol.cyclops.react.SimpleReactFailedStageException;

public class CaptureTest {

    private String exception(String in){
        throw new InternalException();
    }
    @Test
    public void capture() throws InterruptedException{
        t=null;
        new SimpleReact().of("hello","world")
                         .capture(e->t=e)
                         .peek(System.out::println)
                         .then(this::exception)
                         .peek(System.out::println)
                         .then(s->"hello"+s)
                         .run();
                         
        Thread.sleep(500);                 
        assertNotNull(t);
        assertFalse(t.toString(),t instanceof SimpleReactFailedStageException);
        assertTrue(t.toString(),t instanceof InternalException);
    }
    @Test
    public void captureLast() throws InterruptedException{
        t=null;
        new SimpleReact().of("hello","world")
                         .capture(e->t=e)
                         .peek(System.out::println)
                         .peek(System.out::println)
                         .then(s->"hello"+s)
                         .then(this::exception)
                         .run();
        
                         
        Thread.sleep(500);                 
        assertNotNull(t);
        assertFalse(t.toString(),t instanceof SimpleReactFailedStageException);
        assertTrue(t.toString(),t instanceof InternalException);
    }
    Throwable second = null;
    @Test
    public void captureErrorOnce() throws InterruptedException{
        t=null;
        second =null;
        new SimpleReact().of("hello","world")
                          .capture(e->t=e)
                         .peek(System.out::println)
                         .then(this::exception)
                         .peek(System.out::println)
                         .capture(e->second=t)
                         .then(s->"hello"+s)
                         .run();
                         
        Thread.sleep(500);                 
        assertNotNull(t);
        assertNull(second);
        assertFalse(t.toString(),t instanceof SimpleReactFailedStageException);
        assertTrue(t.toString(),t instanceof InternalException);
    }
    @Test
    public void captureBlock(){
        t=null;
        new SimpleReact().of("hello","world").capture(e->t=e)
                         .peek(System.out::println)
                         .then(this::exception)
                         .peek(System.out::println)
                         .block();
                         
        assertNotNull(t);
        t.printStackTrace();
        assertFalse(t.toString(),t instanceof SimpleReactFailedStageException);
        assertTrue(t.toString(),t instanceof InternalException);
    }
    Throwable t;
    @Test
    public void captureLazy(){
        t=null;
        new LazyReact().of("hello","world")
                        .capture(e->t=e)
                        .peek(System.out::println)
                        .then(this::exception)
                        .forEach(System.out::println);
                         
        assertNotNull(t);
        assertFalse(t.toString(),t instanceof SimpleReactFailedStageException);
        assertTrue(t.toString(),t instanceof InternalException);
    }
    
    private static class InternalException extends RuntimeException{
        
    }
}

@@ -1667,785 +1667,13 @@
     
-Throwable second = null;%0A    @Test%0A    public void captureErrorOnce() throws InterruptedException%7B%0A        t=null;%0A        second =null;%0A        new SimpleReact().of(%22hello%22,%22world%22)%0A                          .capture(e-%3Et=e)%0A                         .peek(System.out::println)%0A                         .then(this::exception)%0A                         .peek(System.out::println)%0A                         .capture(e-%3Esecond=t)%0A                         .then(s-%3E%22hello%22+s)%0A                         .run();%0A                         %0A        Thread.sleep(500);                 %0A        assertNotNull(t);%0A        assertNull(second);%0A        assertFalse(t.toString(),t instanceof SimpleReactFailedStageException);%0A        assertTrue(t.toString(),t instanceof InternalException);%0A    %7D
+%0A    
 %0A   

remove redundant test