@Test public void shouldReturnZeroWhenCacheNotExpiredButSizeExceeded() { List<CacheHelper.FolderToManage> folders = new LinkedList<>(); CacheHelper.FolderToManage mockedFolder = mock(CacheHelper.FolderToManage.class); File mockedFile = mock(File.class); when(mockedFolder.getFolder()).thenReturn(mockedFile); when(mockedFolder.getCacheTime()).thenReturn(1000L); folders.add(mockedFolder); FileUtils fileUtilsMock = mock(FileUtils.class); when(fileUtilsMock.dirSize(mockedFile)).thenReturn(10L); cacheHelper = new CacheHelper(9L, folders, fileUtilsMock); TestSubscriber subscriber = TestSubscriber.create(); cacheHelper.cleanCache() .subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertCompleted(); subscriber.assertNoErrors(); subscriber.assertValueCount(1); subscriber.assertValue(0L); }
public Observable<Long> cleanCache() { long now = System.currentTimeMillis(); return Observable.just(foldersToCleanPath) .filter(folderToManages -> shouldClean(folderToManages, maxCacheSize)) .flatMapIterable(folders -> folders) .filter(folder -> folder.getFolder() .exists()) .map(folder -> removeOldFiles(folder.getFolder(), folder.getCacheTime(), now)) .toList() .map(sizes -> { long size = 0; for (int i = 0; i < sizes.size(); i++) { size += sizes.get(i); } Logger.getInstance() .d(TAG, "Cache cleaned: " + AptoideUtils.StringU.formatBytes(size, false)); return size; }) .subscribeOn(Schedulers.io()); }
CacheHelper implements CacheManager { public Observable<Long> cleanCache() { long now = System.currentTimeMillis(); return Observable.just(foldersToCleanPath) .filter(folderToManages -> shouldClean(folderToManages, maxCacheSize)) .flatMapIterable(folders -> folders) .filter(folder -> folder.getFolder() .exists()) .map(folder -> removeOldFiles(folder.getFolder(), folder.getCacheTime(), now)) .toList() .map(sizes -> { long size = 0; for (int i = 0; i < sizes.size(); i++) { size += sizes.get(i); } Logger.getInstance() .d(TAG, "Cache cleaned: " + AptoideUtils.StringU.formatBytes(size, false)); return size; }) .subscribeOn(Schedulers.io()); } }
CacheHelper implements CacheManager { public Observable<Long> cleanCache() { long now = System.currentTimeMillis(); return Observable.just(foldersToCleanPath) .filter(folderToManages -> shouldClean(folderToManages, maxCacheSize)) .flatMapIterable(folders -> folders) .filter(folder -> folder.getFolder() .exists()) .map(folder -> removeOldFiles(folder.getFolder(), folder.getCacheTime(), now)) .toList() .map(sizes -> { long size = 0; for (int i = 0; i < sizes.size(); i++) { size += sizes.get(i); } Logger.getInstance() .d(TAG, "Cache cleaned: " + AptoideUtils.StringU.formatBytes(size, false)); return size; }) .subscribeOn(Schedulers.io()); } CacheHelper(long maxCacheSize, List<FolderToManage> foldersToCleanPath,
      FileUtils fileUtils); }
CacheHelper implements CacheManager { public Observable<Long> cleanCache() { long now = System.currentTimeMillis(); return Observable.just(foldersToCleanPath) .filter(folderToManages -> shouldClean(folderToManages, maxCacheSize)) .flatMapIterable(folders -> folders) .filter(folder -> folder.getFolder() .exists()) .map(folder -> removeOldFiles(folder.getFolder(), folder.getCacheTime(), now)) .toList() .map(sizes -> { long size = 0; for (int i = 0; i < sizes.size(); i++) { size += sizes.get(i); } Logger.getInstance() .d(TAG, "Cache cleaned: " + AptoideUtils.StringU.formatBytes(size, false)); return size; }) .subscribeOn(Schedulers.io()); } CacheHelper(long maxCacheSize, List<FolderToManage> foldersToCleanPath,
      FileUtils fileUtils); Observable<Long> cleanCache(); }
CacheHelper implements CacheManager { public Observable<Long> cleanCache() { long now = System.currentTimeMillis(); return Observable.just(foldersToCleanPath) .filter(folderToManages -> shouldClean(folderToManages, maxCacheSize)) .flatMapIterable(folders -> folders) .filter(folder -> folder.getFolder() .exists()) .map(folder -> removeOldFiles(folder.getFolder(), folder.getCacheTime(), now)) .toList() .map(sizes -> { long size = 0; for (int i = 0; i < sizes.size(); i++) { size += sizes.get(i); } Logger.getInstance() .d(TAG, "Cache cleaned: " + AptoideUtils.StringU.formatBytes(size, false)); return size; }) .subscribeOn(Schedulers.io()); } CacheHelper(long maxCacheSize, List<FolderToManage> foldersToCleanPath,
      FileUtils fileUtils); Observable<Long> cleanCache(); static final int VALUE_TO_CONVERT_MB_TO_BYTES; static String TAG; }