/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/2">Issue #2</a> */ @Test public void aggressiveClearingHandlesRead() throws Exception { FileUtils.deleteDirectory(cacheDir); assertThat(cache.get("a")).isNull(); }
@Test public void allow_null_results_when_get_result_is_used() throws Exception { Object result = elementCreator.getResult(); assertThat(result).isNull(); }
@Test public void allow_null_results_when_get_result_is_popped() throws Exception { Object result = elementCreator.popResult(); assertThat(result).isNull(); }
@Test public void readAttributesInvalid() throws IOException { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final File tempFile = File.createTempFile( "foo", "bar" ); final Path path = GeneralPathImpl.newFromFile( fsProvider.getFileSystem( URI.create( "file:///" ) ), tempFile ); assertThat( fsProvider.readAttributes( path, MyAttrs.class ) ).isNull(); }
@Test public void getFileAttributeViewInvalidView() throws IOException { final SimpleFileSystemProvider fsProvider = new SimpleFileSystemProvider(); final File tempFile = File.createTempFile( "foo", "bar" ); final Path path = GeneralPathImpl.newFromFile( fsProvider.getFileSystem( URI.create( "file:///" ) ), tempFile ); assertThat( fsProvider.getFileAttributeView( path, MyAttrsView.class ) ).isNull(); }
@Test public void fileDeletedExternally() throws Exception { set("a", "a", "a"); getCleanFile("a", 1).delete(); assertThat(cache.get("a")).isNull(); }
@Test public void not_cache_result_after_a_result_is_popped() throws Exception { elementCreator.onParsed("ignore"); elementCreator.popResult(); Object actual = elementCreator.popResult(); assertThat(actual).isNull(); }
@Test public void revertWithTooFewValues() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(1, "A"); creator.abort(); assertThat(getCleanFile("k1", 0).exists()).isFalse(); assertThat(getCleanFile("k1", 1).exists()).isFalse(); assertThat(getDirtyFile("k1", 0).exists()).isFalse(); assertThat(getDirtyFile("k1", 1).exists()).isFalse(); assertThat(cache.get("k1")).isNull(); }
@Test public void testNullValues() throws Exception { try { insertExtraRow(null, null, null, null, null); DataSet result = dataSetManager.lookupDataSet( DataSetLookupFactory.newDataSetLookupBuilder() .dataset(DataSetGroupTest.EXPENSE_REPORTS) .filter(equalsTo(ID.getName(), 9999)) .buildLookup()); assertThat(result.getRowCount()).isEqualTo(1); assertThat(result.getValueAt(0, 1)).isNull(); assertThat(result.getValueAt(0, 2)).isNull(); assertThat(result.getValueAt(0, 3)).isNull(); assertThat(result.getValueAt(0, 4)).isNull(); // Skip next since some DBs like Mysql return the current date when the value inserted is null, // assertThat(result.getValueAt(0, 5)).isNull(); } finally { deleteExtraRow(); } }
@Test public void openWithTruncatedLineDiscardsThatLine() throws Exception { cache.close(); writeFile(getCleanFile("k1", 0), "A"); writeFile(getCleanFile("k1", 1), "B"); Writer writer = new FileWriter(journalFile); writer.write(MAGIC + "\n" + VERSION_1 + "\n100\n2\n\nCLEAN k1 1 1"); // no trailing newline writer.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertThat(cache.get("k1")).isNull(); // The journal is not corrupt when editing after a truncated line. set("k1", "C", "D"); cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertValue("k1", "C", "D"); }
/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/2">Issue #2</a> */ @Test public void aggressiveClearingHandlesPartialEdit() throws Exception { set("a", "a", "a"); set("b", "b", "b"); DiskLruCache.Editor a = cache.get("a").edit(); a.set(0, "a1"); FileUtils.deleteDirectory(cacheDir); a.set(1, "a2"); a.commit(); assertThat(cache.get("a")).isNull(); }
@Test public void editSnapshotAfterChangeCommitted() throws Exception { set("a", "a", "a"); DiskLruCache.Value value = cache.get("a"); DiskLruCache.Editor toAbort = value.edit(); toAbort.set(0, "b"); toAbort.commit(); assertThat(value.edit()).isNull(); }
@Test public void openWithInvalidJournalLineClearsDirectory() throws Exception { cache.close(); generateSomeGarbageFiles(); createJournal("CLEAN k1 1 1", "BOGUS"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(cache.get("k1")).isNull(); }
@Test public void openWithInvalidFileSizeClearsDirectory() throws Exception { cache.close(); generateSomeGarbageFiles(); createJournal("CLEAN k1 0000x001 1"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(cache.get("k1")).isNull(); }
@Test public void openWithTooManyFileSizesClearsDirectory() throws Exception { cache.close(); generateSomeGarbageFiles(); createJournal("CLEAN k1 1 1 1"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(cache.get("k1")).isNull(); }
@Test public void journalDoesNotIncludeReadOfYetUnpublishedValue() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); assertThat(cache.get("k1")).isNull(); creator.set(0, "A"); creator.set(1, "BC"); creator.commit(); cache.close(); assertJournalEquals("DIRTY k1", "CLEAN k1 1 2"); }
@Test public void createNewEntryWithTooFewValuesFails() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(1, "A"); try { creator.commit(); Assert.fail(); } catch (IllegalStateException expected) { } assertThat(getCleanFile("k1", 0).exists()).isFalse(); assertThat(getCleanFile("k1", 1).exists()).isFalse(); assertThat(getDirtyFile("k1", 0).exists()).isFalse(); assertThat(getDirtyFile("k1", 1).exists()).isFalse(); assertThat(cache.get("k1")).isNull(); DiskLruCache.Editor creator2 = cache.edit("k1"); creator2.set(0, "B"); creator2.set(1, "C"); creator2.commit(); }
@Test public void editSinceEvicted() throws Exception { cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("a", "aa", "aaa"); // size 5 DiskLruCache.Value value = cache.get("a"); set("b", "bb", "bbb"); // size 5 set("c", "cc", "ccc"); // size 5; will evict 'A' cache.flush(); assertThat(value.edit()).isNull(); }
@Test public void editSinceEvictedAndRecreated() throws Exception { cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("a", "aa", "aaa"); // size 5 DiskLruCache.Value value = cache.get("a"); set("b", "bb", "bbb"); // size 5 set("c", "cc", "ccc"); // size 5; will evict 'A' set("a", "a", "aaaa"); // size 5; will evict 'B' cache.flush(); assertThat(value.edit()).isNull(); }
@Test public void simpleTests() { final FileStore fileStore = new SimpleUnixFileStore( null ); assertThat( fileStore.name() ).isNotNull().isEqualTo( "/" ); assertThat( fileStore.type() ).isNull(); assertThat( fileStore.isReadOnly() ).isFalse(); assertThat( fileStore.getTotalSpace() ).isEqualTo( File.listRoots()[ 0 ].getTotalSpace() ); assertThat( fileStore.getUsableSpace() ).isEqualTo( File.listRoots()[ 0 ].getUsableSpace() ); assertThat( fileStore.supportsFileAttributeView( BasicFileAttributeView.class ) ).isTrue(); assertThat( fileStore.supportsFileAttributeView( MyFileAttributeView.class ) ).isFalse(); assertThat( fileStore.supportsFileAttributeView( MyAlsoInvalidFileAttributeView.class ) ).isFalse(); assertThat( fileStore.supportsFileAttributeView( "basic" ) ).isTrue(); assertThat( fileStore.supportsFileAttributeView( "any" ) ).isFalse(); assertThat( fileStore.supportsFileAttributeView( BasicFileAttributeView.class.getName() ) ).isFalse(); assertThat( fileStore.supportsFileAttributeView( MyAlsoInvalidFileAttributeView.class.getName() ) ).isFalse(); assertThat( fileStore.getFileStoreAttributeView( FileStoreAttributeView.class ) ).isNull(); assertThat( fileStore.getAttribute( "name" ) ).isNotNull().isEqualTo( fileStore.name() ); assertThat( fileStore.getAttribute( "totalSpace" ) ).isNotNull().isEqualTo( fileStore.getTotalSpace() ); assertThat( fileStore.getAttribute( "usableSpace" ) ).isNotNull().isEqualTo( fileStore.getUsableSpace() ); assertThat( fileStore.getAttribute( "readOnly" ) ).isNotNull().isEqualTo( fileStore.isReadOnly() ); }