// Copyright © 2010-2012, Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package org.specsy.scala.examples;

import fi.jumi.core.config.*;
import fi.jumi.launcher.*;
import fi.jumi.launcher.ui.*;
import org.junit.Test;

import java.io.*;
import java.nio.file.*;
import java.util.*;

public class ExamplesBootstrapTest {

    // TODO: remove this boostrap when Jumi can be run without it

    private static final boolean DEBUG = false;
    private static final long TIMEOUT = 15 * 1000;

    @Test(timeout = TIMEOUT)
    public void run_using_Jumi() throws Exception {
        List<Class<?>> testClasses = Arrays.<Class<?>>asList(
                DeferBlocksExample2Spec.class,
                DeferBlocksExampleSpec.class,
                EnvironmentFilterExampleSpec.class,
                FibonacciSpec.class,
                HelloWorldSpec.class,
                ParameterizedExampleSpec.class,
                PendingUntilFixedExampleSpec.class,
                ShareSideEffectsExampleSpec.class,
                StackSpec.class
        );
        for (Class<?> testClass : testClasses) {
            runTests(testClass);
            System.out.println();
        }
    }

    private static void runTests(Class<?> testClass) throws IOException, InterruptedException {
        Path javaHome = Paths.get(System.getProperty("java.home"));

        SuiteConfigurationBuilder suite = new SuiteConfigurationBuilder()
                .addJvmOptions("-ea")
                .testClasses(testClass.getName());

        for (String library : System.getProperty("java.class.path").split(";")) {
            Path libraryPath = Paths.get(library);
            if (!libraryPath.startsWith(javaHome)) {
                suite.addToClassPath(libraryPath);
            }
        }

        DaemonConfiguration daemon = new DaemonConfigurationBuilder()
                .logActorMessages(DEBUG)
                .freeze();

        try (JumiLauncher launcher = createLauncher()) {
            launcher.start(suite.freeze(), daemon);

            TextUI ui = new TextUI(launcher.getEventStream(), new PlainTextPrinter(System.out));
            ui.updateUntilFinished();

            launcher.close();
        }
    }

    private static JumiLauncher createLauncher() {
        return new JumiLauncherBuilder() {
            @Override
            protected OutputStream createDaemonOutputListener() {
                if (DEBUG) {
                    return System.err;
                } else {
                    return super.createDaemonOutputListener();
                }
            }
        }.build();
    }
}

@@ -237,164 +237,53 @@
 umi.
-core.config.*;%0Aimport fi.jumi.launcher.*;%0Aimport fi.jumi.launcher.ui.*;%0Aimport org.junit.Test;%0A%0Aimport java.io.*;%0Aimport java.nio.file.*;%0Aimport java.util.*
+launcher.JumiBootstrap;%0Aimport org.junit.Test
 ;%0A%0Ap
@@ -390,56 +390,8 @@
 it%0A%0A
-    private static final boolean DEBUG = false;%0A
     
@@ -531,60 +531,171 @@
     
-List%3CClass%3C?%3E%3E testClasses = Arrays.%3CClass%3C?%3E%3EasList
+JumiBootstrap bootstrap = new JumiBootstrap()%0A                //.enableDebugMode()%0A                .setPassingTestsVisible(true);%0A%0A        bootstrap.runTestClasses
 (%0A  
@@ -1110,1553 +1110,8 @@
  );%0A
-        for (Class%3C?%3E testClass : testClasses) %7B%0A            runTests(testClass);%0A            System.out.println();%0A        %7D%0A    %7D%0A%0A    private static void runTests(Class%3C?%3E testClass) throws IOException, InterruptedException %7B%0A        Path javaHome = Paths.get(System.getProperty(%22java.home%22));%0A%0A        SuiteConfigurationBuilder suite = new SuiteConfigurationBuilder()%0A                .addJvmOptions(%22-ea%22)%0A                .testClasses(testClass.getName());%0A%0A        for (String library : System.getProperty(%22java.class.path%22).split(%22;%22)) %7B%0A            Path libraryPath = Paths.get(library);%0A            if (!libraryPath.startsWith(javaHome)) %7B%0A                suite.addToClassPath(libraryPath);%0A            %7D%0A        %7D%0A%0A        DaemonConfiguration daemon = new DaemonConfigurationBuilder()%0A                .logActorMessages(DEBUG)%0A                .freeze();%0A%0A        try (JumiLauncher launcher = createLauncher()) %7B%0A            launcher.start(suite.freeze(), daemon);%0A%0A            TextUI ui = new TextUI(launcher.getEventStream(), new PlainTextPrinter(System.out));%0A            ui.updateUntilFinished();%0A%0A            launcher.close();%0A        %7D%0A    %7D%0A%0A    private static JumiLauncher createLauncher() %7B%0A        return new JumiLauncherBuilder() %7B%0A            @Override%0A            protected OutputStream createDaemonOutputListener() %7B%0A                if (DEBUG) %7B%0A                    return System.err;%0A                %7D else %7B%0A                    return super.createDaemonOutputListener();%0A                %7D%0A            %7D%0A        %7D.build();%0A
     

Use JumiBootstrap