PollingScanDiskSpaceMonitor implements DiskSpaceMonitor { static long getSpaceUsed(Set<Path> paths) { ArrayDeque<Path> pathStack = new ArrayDeque<>(); for (Path path : paths) { pathStack.push(path); } Set<Path> visited = new HashSet<>(); long totalBytes = 0; while (!pathStack.isEmpty()) { try { Path current = pathStack.pop().toRealPath(); if (visited.contains(current)) { continue; } visited.add(current); BasicFileAttributes currentAttrs = Files.readAttributes(current, BasicFileAttributes.class); if (currentAttrs.isDirectory()) { try (DirectoryStream<Path> directoryListing = Files.newDirectoryStream(current)) { for (Path child : directoryListing) { pathStack.push(child); } } } else if (currentAttrs.isRegularFile()) { totalBytes += currentAttrs.size(); } } catch (IOException e) { } } return totalBytes; } PollingScanDiskSpaceMonitor(Set<Path> watchPaths, long pollingIntervalMillis); @Override void start(); @Override void stop(); @Override boolean registerListener(Listener listener);  }
@Test public void testSizeOfSingleFile() throws IOException { writeFile(testDir, "single-file", new byte[1024]); assertEquals(1024, PollingScanDiskSpaceMonitor.getSpaceUsed(Collections.singleton(testDir))); }
@Test public void testSizeOfDisjointDirectoriesFromRoot() throws IOException { Path child1Dir = createDirectory(testDir, "child1"); writeFile(child1Dir, "foo", new byte[1024]); Path child2Dir = createDirectory(testDir, "child2"); writeFile(child2Dir, "bar", new byte[4096]); assertEquals(1024 + 4096, PollingScanDiskSpaceMonitor.getSpaceUsed(Collections.singleton(testDir))); }
@Test public void testSizeOfDisjointDirectoriesFromChildDirs() throws IOException { Path child1Dir = createDirectory(testDir, "child1"); writeFile(child1Dir, "foo", new byte[1024]); Path child2Dir = createDirectory(testDir, "child2"); writeFile(child2Dir, "bar", new byte[4096]); Set<Path> pathSet = new HashSet<>(Arrays.asList(child1Dir, child2Dir)); assertEquals(1024 + 4096, PollingScanDiskSpaceMonitor.getSpaceUsed(pathSet)); }
@Test public void testSizeOfOverlappedDirectories() throws IOException { Path childDir = createDirectory(testDir, "child"); writeFile(childDir, "foo", new byte[1024]); Path grandchildDir = createDirectory(childDir, "grandchild"); writeFile(grandchildDir, "bar", new byte[4096]); Set<Path> pathSet = new HashSet<>(Arrays.asList(childDir, grandchildDir)); assertEquals(1024 + 4096, PollingScanDiskSpaceMonitor.getSpaceUsed(pathSet)); }
@Test public void testSizeOfDirectoryAccessedWithDifferentPaths() throws IOException { Path childDir = createDirectory(testDir, "child1"); writeFile(childDir, "foo", new byte[1024]); Path otherPath = childDir.resolve("..").resolve(childDir.getFileName()); Set<Path> pathSet = new HashSet<>(Arrays.asList(childDir, otherPath)); assertEquals(1024, PollingScanDiskSpaceMonitor.getSpaceUsed(pathSet)); }
@Test public void testSizeOfAlreadyCountedSymlinkedFile() throws IOException { writeFile(testDir, "regular-file", new byte[1024]); Files.createSymbolicLink(testDir.resolve("symlink"), testDir.resolve("regular-file")); assertEquals(1024, PollingScanDiskSpaceMonitor.getSpaceUsed(Collections.singleton(testDir))); }
@Test public void testSizeOfUncountedSymlinkedFile() throws IOException { Path childDir = createDirectory(testDir, "child"); writeFile(testDir, "regular-file", new byte[1024]); Files.createSymbolicLink(childDir.resolve("symlink"), testDir.resolve("regular-file")); assertEquals(1024, PollingScanDiskSpaceMonitor.getSpaceUsed(Collections.singleton(testDir))); }
@Test public void testFollowSymlinkedDirectory() throws IOException { Path childDir = createDirectory(testDir, "child"); writeFile(childDir, "regular-file", new byte[1024]); Path dirSymlink = testDir.resolve("symlink"); Files.createSymbolicLink(dirSymlink, childDir); assertEquals(1024, PollingScanDiskSpaceMonitor.getSpaceUsed(Collections.singleton(dirSymlink))); }
@Test public void testHandleCyclicalSymlink() throws IOException { Path childDir = createDirectory(testDir, "child"); writeFile(childDir, "regular-file", new byte[1024]); Files.createSymbolicLink(childDir.resolve("symlink"), testDir); assertEquals(1024, PollingScanDiskSpaceMonitor.getSpaceUsed(Collections.singleton(childDir))); }
@Test public void testMissingDirectory() throws IOException { Set<Path> pathSet = Collections.singleton(testDir.resolve("non-existant-child")); assertEquals(0, PollingScanDiskSpaceMonitor.getSpaceUsed(pathSet)); }