package water;

import hex.*;
import hex.nn.Dropout;
import hex.nn.NN;
import org.junit.internal.TextListener;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import water.deploy.NodeCL;
import water.util.Log;
import water.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class JUnitRunnerDebug {
  public static final int NODES = 1;

  public static void main(String[] args) throws Exception {
    int[] ports = new int[NODES];
    for( int i = 0; i < ports.length; i++ )
      ports[i] = 54321 + i * 2;

    String flat = "";
    for( int i = 0; i < ports.length; i++ )
      flat += "127.0.0.1:" + ports[i] + "\n";
    flat = Utils.writeFile(flat).getAbsolutePath();

    for( int i = 0; i < ports.length; i++ ) {
      Class c = i == 0 ? UserCode.class : H2O.class;
      new NodeCL(c, ("-ip 127.0.0.1 -port " + ports[i] + " -flatfile " + flat).split(" ")).start();
    }
  }

  public static class UserCode {
    public static void userMain(String[] args) {
      H2O.main(args);
      TestUtil.stall_till_cloudsize(NODES);

      List<Class> tests = new ArrayList<Class>();

      // Classes to test:
      // tests = JUnitRunner.all();
      tests.add(NNvsNeuralNet.class);
      tests.add(NeuralNetSpiralsTest.class);
      tests.add(NeuralNetSpiralsTest2.class);
      tests.add(NeuralNetIrisTest.class);
      tests.add(NeuralNetIrisTest2.class);
      tests.add(Dropout.class);
      tests.add(NN.class);

      // Uncomment this to sleep here and use the browser.
      // try { Thread.sleep(10000000); } catch (Exception _) {}

      JUnitCore junit = new JUnitCore();
      junit.addListener(new LogListener());
      Result result = junit.run(tests.toArray(new Class[0]));
      if( result.getFailures().size() == 0 ) {
        Log.info("SUCCESS!");
        System.exit(0);
      }
      else {
        Log.info("FAIL!");
        System.exit(1);
      }
    }
  }

  static class LogListener extends TextListener {
    LogListener() {
      super(System.out);
    }

    @Override public void testRunFinished(Result result) {
      printHeader(result.getRunTime());
      printFailures(result);
      printFooter(result);
    }

    @Override public void testStarted(Description description) {
      Log.info("");
      Log.info("Starting test " + description);
    }

    @Override public void testFailure(Failure failure) {
      Log.info("Test failed " + failure);
    }

    @Override public void testIgnored(Description description) {
      Log.info("Ignoring test " + description);
    }
  }
}

@@ -449,17 +449,17 @@
 NODES = 
-1
+3
 ;%0A%0A  pub
@@ -1257,24 +1257,26 @@
 nner.all();%0A
+//
       tests.
@@ -1292,32 +1292,73 @@
 euralNet.class);
+ //only passes for NODES=1, not clear why
 %0A      tests.add
@@ -1378,32 +1378,65 @@
 ralsTest.class);
+ //compare NeuralNet vs reference
 %0A      tests.add
@@ -1449,28 +1449,24 @@
 lNet
-Spiral
+Iri
 sTest
-2
 .class);
 %0A   
@@ -1453,32 +1453,65 @@
 IrisTest.class);
+ //compare NeuralNet vs reference
 %0A      tests.add
@@ -1520,32 +1520,36 @@
 euralNet
-Iri
+Spiral
 sTest
+2
 .class);
 %0A      t
@@ -1532,32 +1532,58 @@
 alsTest2.class);
+ //compare NN vs reference
 %0A      tests.add
@@ -1601,32 +1601,58 @@
 risTest2.class);
+ //compare NN vs reference
 %0A      tests.add
@@ -1667,16 +1667,34 @@
 .class);
+ //test NN Dropput
 %0A      t
@@ -1712,16 +1712,26 @@
 .class);
+ //test NN
 %0A%0A      

Set JUnitRunnerDebug to use 3 nodes, and disable the NNvsNeuralNet test (fails currently).