JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / test / java / com / threerings / getdown / cache / GarbageCollectorTest.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.cache;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.concurrent.TimeUnit;
11
12 import org.junit.*;
13 import org.junit.rules.TemporaryFolder;
14
15 import static org.junit.Assert.*;
16 import static org.junit.Assume.assumeTrue;
17
18 /**
19  * Validates that cache garbage is collected and deleted correctly.
20  */
21 public class GarbageCollectorTest
22 {
23     @Before public void setupFiles () throws IOException
24     {
25         _cachedFile = _folder.newFile("abc123.jar");
26         _lastAccessedFile = _folder.newFile("abc123.jar" + ResourceCache.LAST_ACCESSED_FILE_SUFFIX);
27     }
28
29     @Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached ()
30     {
31         gcNow();
32         assertFalse(_cachedFile.exists());
33         assertFalse(_lastAccessedFile.exists());
34     }
35
36     @Test public void shouldDeleteCacheFolderIfFolderIsEmpty ()
37     {
38         gcNow();
39         assertFalse(_folder.getRoot().exists());
40     }
41
42     private void gcNow() {
43         GarbageCollector.collect(_folder.getRoot(), -1);
44     }
45
46     @Test public void shouldKeepFilesInCacheIfRententionPeriodIsNotReached ()
47     {
48         GarbageCollector.collect(_folder.getRoot(), TimeUnit.DAYS.toMillis(1));
49         assertTrue(_cachedFile.exists());
50         assertTrue(_lastAccessedFile.exists());
51     }
52
53     @Test public void shouldDeleteCachedFileIfLastAccessedFileIsMissing ()
54     {
55         assumeTrue(_lastAccessedFile.delete());
56         gcNow();
57         assertFalse(_cachedFile.exists());
58     }
59
60     @Test public void shouldDeleteLastAccessedFileIfCachedFileIsMissing ()
61     {
62         assumeTrue(_cachedFile.delete());
63         gcNow();
64         assertFalse(_lastAccessedFile.exists());
65     }
66
67     @Rule public TemporaryFolder _folder = new TemporaryFolder();
68
69     private File _cachedFile;
70     private File _lastAccessedFile;
71 }