@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(); }
private void assertAbsent(String key) throws Exception { DiskLruCache.Value value = cache.get(key); if (value != null) { Assert.fail(); } assertThat(getCleanFile(key, 0)).doesNotExist(); assertThat(getCleanFile(key, 1)).doesNotExist(); assertThat(getDirtyFile(key, 0)).doesNotExist(); assertThat(getDirtyFile(key, 1)).doesNotExist(); }
@Test public void openWithDirtyKeyDeletesAllFilesForThatKey() throws Exception { cache.close(); File cleanFile0 = getCleanFile("k1", 0); File cleanFile1 = getCleanFile("k1", 1); File dirtyFile0 = getDirtyFile("k1", 0); File dirtyFile1 = getDirtyFile("k1", 1); writeFile(cleanFile0, "A"); writeFile(cleanFile1, "B"); writeFile(dirtyFile0, "C"); writeFile(dirtyFile1, "D"); createJournal("CLEAN k1 1 1", "DIRTY k1"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertThat(cleanFile0.exists()).isFalse(); assertThat(cleanFile1.exists()).isFalse(); assertThat(dirtyFile0.exists()).isFalse(); assertThat(dirtyFile1.exists()).isFalse(); assertThat(cache.get("k1")).isNull(); }
@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(); }