/*
 * Bach - Java Shell Builder
 * Copyright (C) 2020 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.
 */

package de.sormuras.bach;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import de.sormuras.bach.util.Logbook;
import java.net.http.HttpClient;
import java.util.List;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;

class BachTests {

  static Bach zero() {
    var logbook = new Logbook(__ -> {}, true, false);
    var zero = Project.newProject("Zero", "0").build();
    return new Bach(logbook, zero, HttpClient.newBuilder()::build);
  }

  @Test
  void defaults() {
    var bach = zero();
    var expectedStringRepresentation = "Bach.java " + Bach.VERSION;
    assertNotNull(bach.getLogger());
    assertEquals("0", bach.getProject().info().version().toString());
    assertNotNull(bach.getHttpClient());
    assertEquals(expectedStringRepresentation, bach.toString());

    var logbook = ((Logbook) bach.getLogger());
    var initialMessage = "Initialized " + expectedStringRepresentation;
    var projectMessage = "Zero 0";
    assertLinesMatch(List.of(initialMessage, projectMessage), logbook.messages());
    assertLinesMatch(
        List.of("T|" + initialMessage, "D|" + projectMessage), logbook.lines(Object::toString));
  }

  @Test
  void executeTool() {
    var bach = zero();
    var task = Task.runTool("javac", "--version");
    bach.execute(task);
    var logbook = ((Logbook) bach.getLogger());
    assertLinesMatch(
        List.of(
            ">> INIT >>",
            "T|* javac --version",
            "D|javac --version",
            Pattern.quote("D|javac ") + ".+"),
        logbook.lines(Object::toString));
  }
}

@@ -881,16 +881,44 @@
 ogbook;%0A
+import java.io.PrintWriter;%0A
 import j
@@ -986,21 +986,24 @@
 til.
-regex.Patt
+spi.ToolProvid
 er
-n
 ;%0Aim
@@ -1978,16 +1978,21 @@
  execute
+Local
 Tool() %7B
@@ -1988,24 +1988,325 @@
 calTool() %7B%0A
+    class Local implements ToolProvider %7B%0A%0A      @Override%0A      public String name() %7B%0A        return %22local%22;%0A      %7D%0A%0A      @Override%0A      public int run(PrintWriter out, PrintWriter err, String... args) %7B%0A        out.printf(%22args -%3E %25s%22, String.join(%22-%22, args));%0A        return 0;%0A      %7D%0A    %7D%0A%0A
     var bach
@@ -2334,41 +2334,51 @@
 sk =
+ new
  Task.
-r
+R
 unTool(
-%22javac%22, %22--version
+new Local(), %221%22, %222%22, %223
 %22);%0A
@@ -2491,146 +2491,74 @@
 .of(
-%0A            %22%3E%3E INIT %3E%3E%22,%0A            %22T%7C* javac --version%22,%0A            %22D%7Cjavac --version%22,%0A            Pattern.quote(%22D%7Cjavac %22) + %22.+
+%22%3E%3E INIT %3E%3E%22, %22T%7C* local 1 2 3%22, %22D%7Clocal 1 2 3%22, %22D%7Cargs -%3E 1-2-3
 %22),%0A

Use tool declared in local class