/*
 * Bach - Java Shell Builder
 * Copyright (C) 2017 Christian Stein
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(BachContext.class)
class TaskTests {

  private final BachContext context;
  private final Bach bach;

  TaskTests(BachContext context) {
    this.context = context;
    this.bach = context.bach;
  }

  @Test
  void compiler() {
    var project =
        Project.builder()
            .target(Paths.get("target/test/task/compiler"))
            .newModuleGroup("01-hello-world")
            .moduleSourcePath(List.of(Paths.get("demo/01-hello-world/src")))
            .end()
            .build();
    var compiler = new Task.CompilerTask(bach, project);
    assertEquals(0, (int) compiler.get());
  }

  @Test
  void runner() {
    var project =
        Project.builder()
            .entryPoint("world", "com.greetings.Main")
            .target(Paths.get("target/test/task/runner"))
            .newModuleGroup("01-hello-world")
            .moduleSourcePath(List.of(Paths.get("demo/01-hello-world/src")))
            .end()
            .build();
    var runner = new Task.RunnerTask(bach, project);
    var result = runner.get();
    assertTrue(context.bytes.toString().contains("world not found"));
    assertEquals(1, result.intValue());
  }
}

@@ -748,16 +748,44 @@
 tTrue;%0A%0A
+import java.nio.file.Files;%0A
 import j
@@ -1548,25 +1548,105 @@
 id runner() 
-%7B
+throws Exception %7B%0A    var temp = Files.createTempDirectory(%22TaskTests-runner-%22);
 %0A    var pro
@@ -1757,44 +1757,12 @@
 get(
-Paths.get(%22target/test/task/runner%22)
+temp
 )%0A  
@@ -1839,50 +1839,210 @@
 ath(
-List.of(Paths.get(%22demo/01-hello-world/src
+%0A                List.of(%0A                    Paths.get(%22demo/01-hello-world/src%22),%0A                    Paths.get(%22demo/01-hello-world/src-de%22),%0A                    Paths.get(%22demo/01-hello-world/src-fr
 %22)))
@@ -2150,16 +2150,29 @@
 r result
+BeforeCompile
  = runne
@@ -2176,24 +2176,77 @@
 nner.get();%0A
+    assertEquals(1, resultBeforeCompile.intValue());%0A
     assertTr
@@ -2299,24 +2299,108 @@
 t found%22));%0A
+    context.bytes.reset();%0A    var compiler = new Task.CompilerTask(bach, project);%0A
     assertEq
@@ -2408,28 +2408,568 @@
 als(
-1, result.intValue()
+0, (int) compiler.get());%0A    context.bytes.reset();%0A    var resultAfterCompile = runner.get();%0A    assertEquals(0, resultAfterCompile.intValue());%0A    var output = context.bytes.toString();%0A    assertTrue(output.contains(%22Greetings from 01-hello-world demo!%22));%0A    assertTrue(output.contains(%22'Hello world' from class hello.Hello in module hello%22));%0A    assertTrue(output.contains(%22'Hallo Welt' from class hallo.Hallo in module hallo%22));%0A    assertTrue(output.contains(%22'Salut monde' from class salut.Salut in module bonjour%22));%0A    bach.util.removeTree(temp
 );%0A 

Test successful runner task execution