2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.cache;
9 import java.io.IOException;
10 import java.util.concurrent.TimeUnit;
13 import org.junit.rules.TemporaryFolder;
15 import static org.junit.Assert.*;
16 import static org.junit.Assume.assumeTrue;
19 * Validates that cache garbage is collected and deleted correctly.
21 public class GarbageCollectorTest
23 @Before public void setupFiles () throws IOException
25 _cachedFile = _folder.newFile("abc123.jar");
26 _lastAccessedFile = _folder.newFile("abc123.jar" + ResourceCache.LAST_ACCESSED_FILE_SUFFIX);
29 @Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached ()
32 assertFalse(_cachedFile.exists());
33 assertFalse(_lastAccessedFile.exists());
36 @Test public void shouldDeleteCacheFolderIfFolderIsEmpty ()
39 assertFalse(_folder.getRoot().exists());
42 private void gcNow() {
43 GarbageCollector.collect(_folder.getRoot(), -1);
46 @Test public void shouldKeepFilesInCacheIfRententionPeriodIsNotReached ()
48 GarbageCollector.collect(_folder.getRoot(), TimeUnit.DAYS.toMillis(1));
49 assertTrue(_cachedFile.exists());
50 assertTrue(_lastAccessedFile.exists());
53 @Test public void shouldDeleteCachedFileIfLastAccessedFileIsMissing ()
55 assumeTrue(_lastAccessedFile.delete());
57 assertFalse(_cachedFile.exists());
60 @Test public void shouldDeleteLastAccessedFileIfCachedFileIsMissing ()
62 assumeTrue(_cachedFile.delete());
64 assertFalse(_lastAccessedFile.exists());
67 @Rule public TemporaryFolder _folder = new TemporaryFolder();
69 private File _cachedFile;
70 private File _lastAccessedFile;