/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the file is null */ public void track(final File file, final Object marker) { track(file, marker, null); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the path is null */ public void track(final String path, final Object marker) { track(path, marker, null); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the file is null * @deprecated Use {@link FileCleaningTracker#track(File, Object)}. */ @Deprecated public static void track(final File file, final Object marker) { theInstance.track(file, marker); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the path is null * @deprecated Use {@link FileCleaningTracker#track(String, Object)}. */ @Deprecated public static void track(final String path, final Object marker) { theInstance.track(path, marker); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The specified deletion strategy is used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @param deleteStrategy the strategy to delete the file, null means normal * @throws NullPointerException if the path is null * @deprecated Use {@link FileCleaningTracker#track(String, Object, FileDeleteStrategy)}. */ @Deprecated public static void track(final String path, final Object marker, final FileDeleteStrategy deleteStrategy) { theInstance.track(path, marker, deleteStrategy); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The specified deletion strategy is used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @param deleteStrategy the strategy to delete the file, null means normal * @throws NullPointerException if the file is null * @deprecated Use {@link FileCleaningTracker#track(File, Object, FileDeleteStrategy)}. */ @Deprecated public static void track(final File file, final Object marker, final FileDeleteStrategy deleteStrategy) { theInstance.track(file, marker, deleteStrategy); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the path is null */ public static void track(String path, Object marker) { theInstance.track(path, marker); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the path is null */ public void track(String path, Object marker) { track(path, marker, (FileDeleteStrategy) null); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the file is null */ public static void track(File file, Object marker) { theInstance.track(file, marker); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The {@link FileDeleteStrategy#NORMAL normal} deletion strategy will be used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @throws NullPointerException if the file is null */ public void track(File file, Object marker) { track(file, marker, (FileDeleteStrategy) null); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The speified deletion strategy is used. * * @param file the file to be tracked, not null * @param marker the marker object used to track the file, not null * @param deleteStrategy the strategy to delete the file, null means normal * @throws NullPointerException if the file is null */ public static void track(File file, Object marker, FileDeleteStrategy deleteStrategy) { theInstance.track(file, marker, deleteStrategy); }
/** * Track the specified file, using the provided marker, deleting the file * when the marker instance is garbage collected. * The speified deletion strategy is used. * * @param path the full path to the file to be tracked, not null * @param marker the marker object used to track the file, not null * @param deleteStrategy the strategy to delete the file, null means normal * @throws NullPointerException if the path is null */ public static void track(String path, Object marker, FileDeleteStrategy deleteStrategy) { theInstance.track(path, marker, deleteStrategy); }
@Test public void testFileCleanerNull() throws Exception { try { theInstance.track((File) null, new Object()); fail(); } catch (final NullPointerException ex) { // expected } try { theInstance.track((File) null, new Object(), FileDeleteStrategy.NORMAL); fail(); } catch (final NullPointerException ex) { // expected } try { theInstance.track((String) null, new Object()); fail(); } catch (final NullPointerException ex) { // expected } try { theInstance.track((String) null, new Object(), FileDeleteStrategy.NORMAL); fail(); } catch (final NullPointerException ex) { // expected } }
@Test public void testFileCleanerFile() throws Exception { final String path = testFile.getPath(); assertFalse(testFile.exists()); RandomAccessFile r = new RandomAccessFile(testFile, "rw"); assertTrue(testFile.exists()); assertEquals(0, theInstance.getTrackCount()); theInstance.track(path, r); assertEquals(1, theInstance.getTrackCount()); r.close(); testFile = null; r = null; waitUntilTrackCount(); pauseForDeleteToComplete(new File(path)); assertEquals(0, theInstance.getTrackCount()); assertEquals(showFailures(), false, new File(path).exists()); }
@Test public void testFileCleanerDirectory_ForceStrategy() throws Exception { if (!testFile.getParentFile().exists()) { throw new IOException("Cannot create file " + testFile + " as the parent directory does not exist"); } try (final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(testFile))) { TestUtils.generateTestData(output, 100); } assertTrue(testFile.exists()); assertTrue(getTestDirectory().exists()); Object obj = new Object(); assertEquals(0, theInstance.getTrackCount()); theInstance.track(getTestDirectory(), obj, FileDeleteStrategy.FORCE); assertEquals(1, theInstance.getTrackCount()); obj = null; waitUntilTrackCount(); pauseForDeleteToComplete(testFile.getParentFile()); assertEquals(0, theInstance.getTrackCount()); assertEquals(showFailures(), false, new File(testFile.getPath()).exists()); assertEquals(showFailures(), false, testFile.getParentFile().exists()); }
@Test public void testFileCleanerDirectory() throws Exception { TestUtils.createFile(testFile, 100); assertTrue(testFile.exists()); assertTrue(getTestDirectory().exists()); Object obj = new Object(); assertEquals(0, theInstance.getTrackCount()); theInstance.track(getTestDirectory(), obj); assertEquals(1, theInstance.getTrackCount()); obj = null; waitUntilTrackCount(); assertEquals(0, theInstance.getTrackCount()); assertTrue(testFile.exists()); // not deleted, as dir not empty assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty }
@Test public void testFileCleanerDirectory_NullStrategy() throws Exception { TestUtils.createFile(testFile, 100); assertTrue(testFile.exists()); assertTrue(getTestDirectory().exists()); Object obj = new Object(); assertEquals(0, theInstance.getTrackCount()); theInstance.track(getTestDirectory(), obj, null); assertEquals(1, theInstance.getTrackCount()); obj = null; waitUntilTrackCount(); assertEquals(0, theInstance.getTrackCount()); assertTrue(testFile.exists()); // not deleted, as dir not empty assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty }
@Test public void testFileCleanerExitWhenFinished_NoTrackAfter() throws Exception { assertFalse(theInstance.exitWhenFinished); theInstance.exitWhenFinished(); assertTrue(theInstance.exitWhenFinished); assertEquals(null, theInstance.reaper); final String path = testFile.getPath(); final Object marker = new Object(); try { theInstance.track(path, marker); fail(); } catch (final IllegalStateException ex) { // expected } assertTrue(theInstance.exitWhenFinished); assertEquals(null, theInstance.reaper); }
@Test public void testFileCleanerExitWhenFinished1() throws Exception { final String path = testFile.getPath(); assertEquals("1-testFile exists: " + testFile, false, testFile.exists()); RandomAccessFile r = new RandomAccessFile(testFile, "rw"); assertEquals("2-testFile exists", true, testFile.exists()); assertEquals("3-Track Count", 0, theInstance.getTrackCount()); theInstance.track(path, r); assertEquals("4-Track Count", 1, theInstance.getTrackCount()); assertEquals("5-exitWhenFinished", false, theInstance.exitWhenFinished); assertEquals("6-reaper.isAlive", true, theInstance.reaper.isAlive()); assertEquals("7-exitWhenFinished", false, theInstance.exitWhenFinished); theInstance.exitWhenFinished(); assertEquals("8-exitWhenFinished", true, theInstance.exitWhenFinished); assertEquals("9-reaper.isAlive", true, theInstance.reaper.isAlive()); r.close(); testFile = null; r = null; waitUntilTrackCount(); pauseForDeleteToComplete(new File(path)); assertEquals("10-Track Count", 0, theInstance.getTrackCount()); assertEquals("11-testFile exists " + showFailures(), false, new File(path).exists()); assertEquals("12-exitWhenFinished", true, theInstance.exitWhenFinished); assertEquals("13-reaper.isAlive", false, theInstance.reaper.isAlive()); }
@Test public void testFileCleanerExitWhenFinished2() throws Exception { final String path = testFile.getPath(); assertFalse(testFile.exists()); RandomAccessFile r = new RandomAccessFile(testFile, "rw"); assertTrue(testFile.exists()); assertEquals(0, theInstance.getTrackCount()); theInstance.track(path, r); assertEquals(1, theInstance.getTrackCount()); assertFalse(theInstance.exitWhenFinished); assertTrue(theInstance.reaper.isAlive()); r.close(); testFile = null; r = null; waitUntilTrackCount(); pauseForDeleteToComplete(new File(path)); assertEquals(0, theInstance.getTrackCount()); assertEquals(showFailures(), false, new File(path).exists()); assertFalse(theInstance.exitWhenFinished); assertTrue(theInstance.reaper.isAlive()); assertFalse(theInstance.exitWhenFinished); theInstance.exitWhenFinished(); for (int i = 0; i < 20 && theInstance.reaper.isAlive(); i++) { TestUtils.sleep(500L); // allow reaper thread to die } assertTrue(theInstance.exitWhenFinished); assertFalse(theInstance.reaper.isAlive()); }