Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/org/apache/commons/io/FileCleaningTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ public synchronized void exitWhenFinished() {
}
}

/**
* Tests whether {@link #exitWhenFinished()} has been called, and therefore whether the file cleaner thread will terminate when there are no more objects
* being tracked for deletion.
*
* @return {@code true} if {@link #exitWhenFinished()} has been called, {@code false} otherwise.
* @since 2.23.0
*/
public synchronized boolean exitWhenFinishedTest() {
return exitWhenFinished;
}

/**
* Gets a copy of the file paths that failed to delete.
*
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ public void tearDown() {
fileCleaningTracker = null;
}

@Test
void testExitWhenFinishedTestMatchesField() {
// Before calling exitWhenFinished()
assertEquals(fileCleaningTracker.exitWhenFinished, fileCleaningTracker.exitWhenFinishedTest(),
"exitWhenFinishedTest() must reflect the exitWhenFinished field (before)");
fileCleaningTracker.exitWhenFinished();
// After calling exitWhenFinished()
assertEquals(fileCleaningTracker.exitWhenFinished, fileCleaningTracker.exitWhenFinishedTest(),
"exitWhenFinishedTest() must reflect the exitWhenFinished field (after)");
}

@Test
void testExitWhenFinishedTestReturnsFalseInitially() {
assertFalse(fileCleaningTracker.exitWhenFinishedTest(), "exitWhenFinishedTest() should return false before exitWhenFinished() is called");
}

@Test
void testExitWhenFinishedTestReturnsTrueAfterExitWhenFinished() {
fileCleaningTracker.exitWhenFinished();
assertTrue(fileCleaningTracker.exitWhenFinishedTest(), "exitWhenFinishedTest() should return true after exitWhenFinished() is called");
}

@Test
void testExitWhenFinishedTestWithActiveReaper() throws Exception {
final String path = testFile.getPath();
// Start the reaper by tracking a file
final RandomAccessFile raf = createRandomAccessFile();
fileCleaningTracker.track(path, raf);
assertFalse(fileCleaningTracker.exitWhenFinishedTest(), "exitWhenFinishedTest() should return false while reaper is still active");
assertTrue(fileCleaningTracker.reaper.isAlive(), "Reaper should be alive");
fileCleaningTracker.exitWhenFinished();
assertTrue(fileCleaningTracker.exitWhenFinishedTest(),
"exitWhenFinishedTest() should return true after exitWhenFinished() is called with active reaper");
raf.close();
}

@Test
void testFileCleanerDirectory_ForceStrategy_FileSource() throws Exception {
if (!testFile.getParentFile().exists()) {
Expand Down
Loading