package tutorial;



import io.reactors.japi.*;
import java.util.concurrent.*;
import org.junit.Assert;
import org.junit.Test;



public class reactors {

  /*!begin-include!*/
  /*!begin-code!*/
  ReactorSystem system = ReactorSystem.create("test-system");
  /*!end-code!*/
  /*!end-include(reactors-java-reactors-system.html)!*/

  class FakeSystem {
    public class Out {
      public LinkedTransferQueue<Object> queue = new LinkedTransferQueue<>();
      public void println(Object x) {
        queue.add(x);
      }
    }
    public Out out = new Out();
  }

  @Test
  public void runsAnonymousReactor() {
    FakeSystem System = new FakeSystem();

    /*!begin-include!*/
    /*!begin-code!*/
    Proto<String> proto = Reactor.apply(
      self -> self.main().events().onEvent(x -> System.out.println(x))
    );
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-anonymous.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    Channel<String> ch = system.spawn(proto);
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-spawn.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    ch.send("Hola!");
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-send.html)!*/

    try {
      Assert.assertEquals("Hola!", System.out.queue.take());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /*!begin-include!*/
  /*!begin-code!*/
  public static class HelloReactor extends Reactor<String> {
  }
  /*!end-code!*/
  /*!end-include(reactors-java-reactors-template.html)!*/

  @Test
  public void runsHelloReactor() {
    /*!begin-include!*/
    /*!begin-code!*/
    Proto<String> proto = Proto.create(HelloReactor.class);
    Channel<String> ch = system.spawn(proto);
    // ch.send("Howdee!");
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-spawn-template.html)!*/
  }
}

@@ -327,16 +327,23 @@
 )!*/%0A%0A  
+static 
 class Fa
@@ -565,16 +565,64 @@
 );%0A  %7D%0A%0A
+  static FakeSystem System = new FakeSystem();%0A%0A
   @Test%0A
@@ -664,51 +664,8 @@
 ) %7B%0A
-    FakeSystem System = new FakeSystem();%0A%0A
     
@@ -1465,16 +1465,125 @@
 ring%3E %7B%0A
+    public HelloReactor() %7B%0A      main().events().onEvent(%0A        x -%3E System.out.println(x)%0A      );%0A    %7D%0A
   %7D%0A  /*
@@ -1855,11 +1855,8 @@
 %0A   
- //
  ch.
@@ -1956,15 +1956,160 @@
 html)!*/
+%0A%0A    try %7B%0A      Assert.assertEquals(%22Howdee!%22, System.out.queue.take());%0A    %7D catch (Exception e) %7B%0A      throw new RuntimeException(e);%0A    %7D
 %0A  %7D%0A%7D%0A

Add test for Java reactor templates.