/*
 * Bach - Java Shell Builder
 * Copyright (C) 2019 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

class UtilTests {

  @Test
  void currentJavaHome() {
    assertTrue(Files.isDirectory(Util.currentJavaHome()));
  }

  @Test
  void tempDir() {
    assertEquals("tools", Util.path(Property.PATH_TOOLS).getFileName().toString());
  }

  @Test
  void isJavaFile() {
    assertFalse(Util.isJavaFile(Path.of("")));
    assertFalse(Util.isJavaFile(Path.of("a/b")));
    assertTrue(Util.isJavaFile(Path.of("src/test/UtilTests.java")));
    assertFalse(Util.isJavaFile(Path.of("src/test-resources/Util.isJavaFile.java")));
  }

  @Test
  void last() {
    assertEquals("", last(""));
    assertEquals("a", last("a"));
    assertEquals("b", last("a", "b"));
    assertEquals(File.separator, last("/"));
    assertEquals("\\", last("\\"));
    assertEquals("C:\\", last("C:\\"));
    assertEquals("a", last("/a"));
    assertEquals("a", last("\\a"));
    assertEquals("b", last("a/b"));
    assertEquals("b", last("a\\b"));
    assertEquals("b", last("/a/b"));
    assertEquals("b", last("\\a\\b"));
  }

  @Test
  void removeTreeForNonExistingPathFails() {
    var path = Path.of("does not exist");
    var e = assertThrows(UncheckedIOException.class, () -> Util.removeTree(path));
    assertEquals("removing tree failed: does not exist", e.getMessage());
  }

  @Test
  void removeTreeForEmptyDirectoryWorks() throws Exception {
    var temp = Files.createTempDirectory("bach-UtilTests.downloadUsingHttps-");
    assertTrue(Files.exists(temp));
    Util.removeTree(temp, __ -> true);
    assertFalse(Files.exists(temp));
  }

  private static String last(String first, String... more) {
    return Util.last(Path.of(first, more)).toString();
  }
}

@@ -1689,143 +1689,157 @@
 als(
-File.separator, last(%22/%22));%0A    assertEquals(%22%5C%5C%22, last(%22%5C%5C%22));%0A    assertEquals(%22C:%5C%5C%22, last(%22C:%5C%5C%22));%0A    assertEquals(%22a%22, last(%22/a%22
+%22c%22, last(%22a%22, %22b%22, %22c%22));%0A    assertEquals(File.separator, last(File.separator));%0A    assertEquals(%22C:%22 + File.separator, last(%22C:%22 + File.separator
 ));%0A
@@ -1865,19 +1865,34 @@
 %22, last(
-%22%5C%5C
+File.separator + %22
 a%22));%0A  
@@ -1922,122 +1922,104 @@
 t(%22a
-/b%22));%0A    assertEquals(%22b%22, last(%22a%5C%5Cb%22));%0A    assertEquals(%22b%22, last(%22/a/b%22));%0A    assertEquals(%22b%22, last(%22%5C%5Ca%5C%5C
+%22 + File.separator + %22b%22));%0A    assertEquals(%22b%22, last(File.separator + %22a%22 + File.separator + %22
 b%22))

Make path tests platform agnostic