/*
 *
 * Copyright (c) 2010 by Joel Uckelman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */

package VASSAL.tools.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

import static org.junit.Assert.*;

public class TailerTest {

  private static final String EXISTS = "src/test/resources/TailerTest.txt";
  private static final String NOTEXISTS = "src/test/resources/notexists";

  @Test
  public void testGetFile() {
    final File file = new File(EXISTS);
    final Tailer tailer = new Tailer(file);
    assertEquals(file, tailer.getFile());
  }

  @Test
  public void testIsTailingTrue() throws IOException {
    final File file = new File(EXISTS);
    final Tailer tailer = new Tailer(file);
    tailer.start();
    assertTrue(tailer.isTailing());
    tailer.stop();
  }

  @Test
  public void testIsTailingFalse() {
    final File file = new File(EXISTS);
    final Tailer tailer = new Tailer(file);
    assertFalse(tailer.isTailing());
  }

  @Test(expected=IOException.class)
  public void testNoFile() throws IOException {
    final File file = new File(NOTEXISTS);
    final Tailer tailer = new Tailer(file);
    tailer.start();
  }

  @Test(expected=IOException.class)
  public void testDirectory() throws IOException {
    final File file = new File(".");
    final Tailer tailer = new Tailer(file);
    tailer.start();
  }

  @Test
  public void testTailer() throws InterruptedException, IOException {
    final File file = new File(EXISTS);

    final StringBuilder sb_tailer = new StringBuilder();

    final Tailer tailer = new Tailer(file);
    tailer.addEventListener((src, str) -> sb_tailer.append(str));
    tailer.start();

    // give the Tailer time to work
    Thread.sleep(1000L);

    tailer.stop();

    final String actual = sb_tailer.toString();

    String expected;
    try (FileInputStream in = new FileInputStream(file)) {
      expected = IOUtils.toString(in, StandardCharsets.UTF_8).substring(0, actual.length());
    }

    // compare whatever the Tailer had time to read
    assertEquals(expected, actual);
  }
}

@@ -792,31 +792,17 @@
 nio.
-charset.StandardCharset
+file.File
 s;%0A%0A
@@ -2432,20 +2432,25 @@
 ring();%0A
-%0A
+ 
    
+final
  String 
@@ -2461,129 +2461,41 @@
 cted
-;%0A    try (FileInputStream in = new FileInputStream(file)) %7B%0A      expected = IOUtils.toString(in, StandardCharsets.UTF_8
+ = Files.readString(file.toPath()
 ).su
@@ -2526,14 +2526,8 @@
 ());
-%0A    %7D
 %0A%0A  

Use Files.readString() for readaing the test file, as its docs claim that it does not do newline translation.