package fr.umlv.monitor;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.EnumSet;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class PushPathMonitorTest2 {
  private Path tmpDir;

  @Before
  public void before() throws IOException {
    tmpDir = Files.createTempDirectory("path-monitor-test");
  }
  
  @After
  public void after() {
    tmpDir = null;
  }
  
  @Test(timeout=1000)
  public void testFilter() throws IOException {
    final Path directory = Files.createTempDirectory(tmpDir, null);
    PushPathMonitor pathMonitor = new PushPathMonitor(directory, new PathStateChangeListener() {
      @Override
      public void pathChanged(Path path) {
        fail();
      }
    });
    PathFilter filter = new PathFilter() {
      @Override
      public boolean accept(Path path) throws IOException {
        return path.getFileName().toString().endsWith(".txt");
      }
    };
    pathMonitor.setPathFilter(filter);
    final Thread mainThread = Thread.currentThread();
    Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(300);
          Path tmpFile = Files.createTempFile(directory, null, "foo");
          Files.delete(tmpFile);
          Thread.sleep(300);
        } catch(InterruptedException|IOException e) {
          throw new AssertionError("error in spawned thread", e);
        }
        mainThread.interrupt();
      }
    });
    thread.start();
    try {
      pathMonitor.monitor();
    } catch (InterruptedException e) {
      //ok
    }
  }
}
