11package com .danikula .videocache .file ;
22
33import com .danikula .android .garden .io .Files ;
4- import com .danikula .android .garden .io .IoUtils ;
54import com .danikula .videocache .BaseTest ;
65import com .danikula .videocache .Cache ;
76import com .danikula .videocache .ProxyCacheException ;
1110import org .junit .Test ;
1211
1312import java .io .File ;
13+ import java .io .IOException ;
1414import java .util .Arrays ;
1515
1616import static com .danikula .videocache .support .ProxyCacheTestUtils .ASSETS_DATA_NAME ;
1919import static com .danikula .videocache .support .ProxyCacheTestUtils .getTempFile ;
2020import static com .danikula .videocache .support .ProxyCacheTestUtils .loadAssetFile ;
2121import static com .danikula .videocache .support .ProxyCacheTestUtils .newCacheFile ;
22+ import static com .google .common .io .Files .write ;
2223import static org .fest .assertions .api .Assertions .assertThat ;
2324
2425/**
@@ -95,7 +96,8 @@ public void testAppendDiscCache() throws Exception {
9596 public void testIsFileCacheCompleted () throws Exception {
9697 File file = newCacheFile ();
9798 File partialFile = new File (file .getParentFile (), file .getName () + ".download" );
98- IoUtils .saveToFile (loadAssetFile (ASSETS_DATA_NAME ), partialFile );
99+ write (loadAssetFile (ASSETS_DATA_NAME ), partialFile );
100+ write (loadAssetFile (ASSETS_DATA_NAME ), partialFile );
99101 Cache fileCache = new FileCache (partialFile );
100102
101103 assertThat (file .exists ()).isFalse ();
@@ -114,7 +116,7 @@ public void testIsFileCacheCompleted() throws Exception {
114116 @ Test (expected = ProxyCacheException .class )
115117 public void testErrorWritingCompletedCache () throws Exception {
116118 File file = newCacheFile ();
117- IoUtils . saveToFile (loadAssetFile (ASSETS_DATA_NAME ), file );
119+ write (loadAssetFile (ASSETS_DATA_NAME ), file );
118120 FileCache fileCache = new FileCache (file );
119121 fileCache .append (generate (100 ), 20 );
120122 Assert .fail ();
@@ -124,7 +126,7 @@ public void testErrorWritingCompletedCache() throws Exception {
124126 public void testErrorWritingAfterCompletion () throws Exception {
125127 File file = newCacheFile ();
126128 File partialFile = new File (file .getParentFile (), file .getName () + ".download" );
127- IoUtils . saveToFile (loadAssetFile (ASSETS_DATA_NAME ), partialFile );
129+ write (loadAssetFile (ASSETS_DATA_NAME ), partialFile );
128130 FileCache fileCache = new FileCache (partialFile );
129131 fileCache .complete ();
130132 fileCache .append (generate (100 ), 20 );
@@ -140,4 +142,45 @@ public void testFileErrorForDiscCache() throws Exception {
140142 fileCache .available ();
141143 Assert .fail ();
142144 }
145+
146+ @ Test
147+ public void testTrimAfterCompletionForTotalCountLru () throws Exception {
148+ File cacheDir = newCacheFile ();
149+ DiskUsage diskUsage = new TotalCountLruDiskUsage (2 );
150+ byte [] data = loadAssetFile (ASSETS_DATA_NAME );
151+ saveAndCompleteCache (diskUsage , data ,
152+ new File (cacheDir , "0.dat" ),
153+ new File (cacheDir , "1.dat" ),
154+ new File (cacheDir , "2.dat" )
155+ );
156+ waitForAsyncTrimming ();
157+ assertThat (new File (cacheDir , "0.dat" )).doesNotExist ();
158+ }
159+
160+ @ Test
161+ public void testTrimAfterCompletionForTotalSizeLru () throws Exception {
162+ File cacheDir = newCacheFile ();
163+ byte [] data = loadAssetFile (ASSETS_DATA_NAME );
164+ DiskUsage diskUsage = new TotalSizeLruDiskUsage (data .length *3 -1 );
165+ saveAndCompleteCache (diskUsage , data ,
166+ new File (cacheDir , "0.dat" ),
167+ new File (cacheDir , "1.dat" ),
168+ new File (cacheDir , "2.dat" )
169+ );
170+ waitForAsyncTrimming ();
171+ assertThat (new File (cacheDir , "0.dat" )).doesNotExist ();
172+ }
173+
174+ private void saveAndCompleteCache (DiskUsage diskUsage , byte [] data , File ... files ) throws ProxyCacheException , IOException {
175+ for (File file : files ) {
176+ FileCache fileCache = new FileCache (file , diskUsage );
177+ fileCache .append (data , data .length );
178+ fileCache .complete ();
179+ assertThat (file ).exists ();
180+ }
181+ }
182+
183+ private void waitForAsyncTrimming () throws InterruptedException {
184+ Thread .sleep (500 );
185+ }
143186}
0 commit comments