Example #1
0
  public void testSymlinks() throws Exception {
    File scratchDirectory = Files.createTempDir();
    File newFile = createFileIn("New.java", scratchDirectory);
    File rootDirectory = Files.createTempDir();
    File subDirectory = Files.createTempDir();

    ResourceAccumulator resourceAccumulator =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());

    assertTrue(getResources(resourceAccumulator).isEmpty());

    // Symlink in a subdirectory and then symlink in a contained file.
    java.nio.file.Files.createSymbolicLink(
            new File(rootDirectory, "sublink").toPath(), subDirectory.toPath())
        .toFile();
    java.nio.file.Files.createSymbolicLink(
            new File(subDirectory, "New.java").toPath(), newFile.toPath())
        .toFile();
    waitForFileEvents();

    List<AbstractResource> resources = getResources(resourceAccumulator);
    assertEquals(1, resources.size());
    assertTrue(resources.get(0).getPath().endsWith("sublink/New.java"));

    resourceAccumulator.shutdown();
  }
Example #2
0
  public void testMultipleListeners() throws Exception {
    File rootDirectory = Files.createTempDir();
    File subDirectory = createDirectoryIn("subdir", rootDirectory);

    ResourceAccumulator resourceAccumulator1 =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());
    ResourceAccumulator resourceAccumulator2 =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());

    assertTrue(getResources(resourceAccumulator1).isEmpty());
    assertTrue(getResources(resourceAccumulator2).isEmpty());

    createFileIn("New.java", subDirectory);
    waitForFileEvents();

    List<AbstractResource> resources1 = getResources(resourceAccumulator1);
    assertEquals(1, resources1.size());
    assertTrue(resources1.get(0).getPath().endsWith("New.java"));

    List<AbstractResource> resources2 = getResources(resourceAccumulator2);
    assertEquals(1, resources2.size());
    assertTrue(resources2.get(0).getPath().endsWith("New.java"));

    resourceAccumulator1.shutdown();
    resourceAccumulator2.shutdown();
  }
Example #3
0
  public void testSymlinkInfiniteLoop() throws Exception {
    File rootDirectory = Files.createTempDir();
    File subDirectory = Files.createTempDir();

    ResourceAccumulator resourceAccumulator =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());

    assertTrue(getResources(resourceAccumulator).isEmpty());

    // Symlink in a loop
    java.nio.file.Files.createSymbolicLink(
            new File(rootDirectory, "sublink").toPath(), subDirectory.toPath())
        .toFile();
    java.nio.file.Files.createSymbolicLink(
            new File(subDirectory, "sublink").toPath(), rootDirectory.toPath())
        .toFile();
    createFileIn("New.java", subDirectory);
    waitForFileEvents();

    try {
      // Should throw an error if resourceAccumulator got stuck in an infinite directory scan loop.
      getResources(resourceAccumulator);
      fail();
    } catch (FileSystemException expected) {
      // Expected
    }

    resourceAccumulator.shutdown();
  }
Example #4
0
  public void testRenameParentDirectory() throws Exception {
    File rootDirectory = Files.createTempDir();
    File parentDirectory = createDirectoryIn("original_dir", rootDirectory);
    File subDirectory = createDirectoryIn("subdir", parentDirectory);
    createFileIn("Name1.java", subDirectory);
    createFileIn("Name2.java", subDirectory);
    File renamedParentDirectory = new File(rootDirectory, "new_dir");

    ResourceAccumulator resourceAccumulator =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());

    List<AbstractResource> resources = getResources(resourceAccumulator);
    assertEquals(2, resources.size());
    assertTrue(resources.get(0).getPath().endsWith("original_dir/subdir/Name1.java"));
    assertTrue(resources.get(1).getPath().endsWith("original_dir/subdir/Name2.java"));

    parentDirectory.renameTo(renamedParentDirectory);
    waitForFileEvents();

    resources = getResources(resourceAccumulator);
    assertEquals(2, resources.size());
    assertTrue(resources.get(0).getPath().endsWith("new_dir/subdir/Name1.java"));
    assertTrue(resources.get(1).getPath().endsWith("new_dir/subdir/Name2.java"));

    resourceAccumulator.shutdown();
  }
Example #5
0
 private List<AbstractResource> getResources(ResourceAccumulator resourceAccumulator)
     throws IOException {
   resourceAccumulator.refreshResources();
   List<AbstractResource> list = Lists.newArrayList(resourceAccumulator.getResources().keySet());
   Collections.sort(
       list,
       new Comparator<AbstractResource>() {
         @Override
         public int compare(AbstractResource a, AbstractResource b) {
           return a.getLocation().compareTo(b.getLocation());
         }
       });
   return list;
 }
Example #6
0
  public void testDeleteFile() throws Exception {
    File rootDirectory = Files.createTempDir();
    File subDirectory = createDirectoryIn("subdir", rootDirectory);
    File originalFile = createFileIn("SomeFile.java", subDirectory);

    ResourceAccumulator resourceAccumulator =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());

    List<AbstractResource> resources = getResources(resourceAccumulator);
    assertEquals(1, resources.size());
    assertTrue(resources.get(0).getPath().endsWith("SomeFile.java"));

    originalFile.delete();
    waitForFileEvents();

    assertTrue(getResources(resourceAccumulator).isEmpty());

    resourceAccumulator.shutdown();
  }
Example #7
0
  public void testListensInNewDirectories() throws Exception {
    File rootDirectory = Files.createTempDir();

    ResourceAccumulator resourceAccumulator =
        new ResourceAccumulator(rootDirectory.toPath(), createInclusivePathPrefixSet());

    assertTrue(getResources(resourceAccumulator).isEmpty());

    // Create a new directory and contained file AFTER the root directory has started being listened
    // to.
    File subDirectory = createDirectoryIn("subdir", rootDirectory);
    createFileIn("New.java", subDirectory);
    waitForFileEvents();

    List<AbstractResource> resources = getResources(resourceAccumulator);
    assertEquals(1, resources.size());
    assertTrue(resources.get(0).getPath().endsWith("New.java"));

    resourceAccumulator.shutdown();
  }