package jetbrains.jetpad.base;

import jetbrains.jetpad.test.BaseTestCase;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class SimpleAsyncTest extends BaseTestCase {
  private SimpleAsync<Void> async = new SimpleAsync<>();

  @Test
  public void exceptionInSuccessHandler() {
    async.onSuccess(new Handler<Void>() {
      @Override
      public void handle(Void item) {
        throw new RuntimeException();
      }
    });
    try {
      async.success(null);
    } catch (RuntimeException e) {
    }
    assertTrue(async.hasSucceeded());
  }

  @Test
  public void exceptionInFailureHandler() {
    async.onFailure(new Handler<Throwable>() {
      @Override
      public void handle(Throwable t) {
        throw new RuntimeException();
      }
    });
    try {
      async.failure(new Throwable());
    } catch (RuntimeException e) {
    }
    assertTrue(async.hasFailed());
  }

  @Test(expected = IllegalStateException.class)
  public void callSuccessInSuccessHandler() {
    async.onSuccess(new Handler<Void>() {
      @Override
      public void handle(Void item) {
        async.success(null);
      }
    });
    async.success(null);
  }

  @Test(expected = IllegalStateException.class)
  public void callSuccessInFailureHandler() {
    async.onFailure(new Handler<Throwable>() {
      @Override
      public void handle(Throwable item) {
        async.success(null);
      }
    });
    async.failure(new Throwable());
  }

  @Test(expected = IllegalStateException.class)
  public void callFailureInSuccessHandler() {
    async.onSuccess(new Handler<Void>() {
      @Override
      public void handle(Void item) {
        async.failure(null);
      }
    });
    async.success(null);
  }

  @Test(expected = IllegalStateException.class)
  public void callFailureInFailureHandler() {
    async.onFailure(new Handler<Throwable>() {
      @Override
      public void handle(Throwable item) {
        async.failure(null);
      }
    });
    async.failure(new Throwable());
  }
}

@@ -322,135 +322,25 @@
 ess(
-new Handler%3CVoid%3E() %7B%0A      @Override%0A      public void handle(Void item) %7B%0A        throw new RuntimeException();%0A      %7D%0A    %7D
+throwingHandler()
 );%0A 
@@ -535,142 +535,41 @@
 ure(
-new Handler%3CThrowable%3E() %7B%0A      @Override%0A      public void handle(Throwable t) %7B%0A        throw new RuntimeException();%0A      %7D%0A    %7D
+this.%3CThrowable%3EthrowingHandler()
 );%0A 
@@ -814,126 +814,32 @@
 ess(
-new Handler%3CVoid%3E() %7B%0A      @Override%0A      public void handle(Void item) %7B%0A        async.success(null);%0A      %7D%0A    %7D
+succeedingHandler(async)
 );%0A 
@@ -985,136 +985,48 @@
 ure(
-new Handler%3CThrowable%3E() %7B%0A      @Override%0A      public void handle(Throwable item) %7B%0A        async.success(null);%0A      %7D%0A    %7D
+this.%3CThrowable%3EsucceedingHandler(async)
 );%0A 
@@ -1183,126 +1183,29 @@
 ess(
-new Handler%3CVoid%3E() %7B%0A      @Override%0A      public void handle(Void item) %7B%0A        async.failure(null);%0A      %7D%0A    %7D
+failingHandler(async)
 );%0A 
@@ -1351,139 +1351,709 @@
 ure(
-new Handler%3CThrowable%3E() %7B%0A      @Override%0A      public void handle(Throwable item) %7B%0A        async.failure(null);%0A      %7D%0A    %7D);%0A
+this.%3CThrowable%3EfailingHandler(async));%0A    async.failure(new Throwable());%0A  %7D%0A%0A  private %3CResultT%3E Handler%3CResultT%3E throwingHandler() %7B%0A    return new Handler%3CResultT%3E() %7B%0A      @Override%0A      public void handle(ResultT item) %7B%0A        throw new RuntimeException();%0A      %7D%0A    %7D;%0A  %7D%0A%0A  private %3CResultT%3E Handler%3CResultT%3E succeedingHandler(final SimpleAsync%3CVoid%3E async) %7B%0A    return new Handler%3CResultT%3E() %7B%0A      @Override%0A      public void handle(ResultT item) %7B%0A        async.success(null);%0A      %7D%0A    %7D;%0A  %7D%0A%0A  private %3CResultT%3E Handler%3CResultT%3E failingHandler(final SimpleAsync%3CVoid%3E async) %7B%0A    return new Handler%3CResultT%3E() %7B%0A      @Override%0A      public void handle(ResultT item) %7B%0A    
     
@@ -2080,18 +2080,33 @@
 rowable());%0A
+      %7D%0A    %7D;%0A
   %7D%0A%7D%0A

fix duplication in SimpleAsyncTest