Skip to content

Commit

Permalink
Near-full test coverage for DBC, but some tests fail!
Browse files Browse the repository at this point in the history
  • Loading branch information
joebowbeer committed Jul 7, 2017
1 parent ad63150 commit fba8125
Show file tree
Hide file tree
Showing 2 changed files with 341 additions and 47 deletions.
43 changes: 22 additions & 21 deletions src/main/java/com/android/volley/toolbox/DiskBasedCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -84,9 +86,6 @@ public DiskBasedCache(File rootDirectory) {
this(rootDirectory, DEFAULT_DISK_USAGE_BYTES);
}

/**
* Clears the cache. Deletes all cached files from disk.
*/
@Override
public synchronized void clear() {
File[] files = mRootDirectory.listFiles();
Expand Down Expand Up @@ -114,7 +113,7 @@ public synchronized Entry get(String key) {
File file = getFileForKey(key);
CountingInputStream cis = null;
try {
cis = new CountingInputStream(new BufferedInputStream(new FileInputStream(file)));
cis = new CountingInputStream(new BufferedInputStream(createInputStream(file)));
CacheHeader.readHeader(cis); // eat header
byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
return entry.toCacheEntry(data);
Expand Down Expand Up @@ -157,7 +156,7 @@ public synchronized void initialize() {
for (File file : files) {
BufferedInputStream fis = null;
try {
fis = new BufferedInputStream(new FileInputStream(file));
fis = new BufferedInputStream(createInputStream(file));
CacheHeader entry = CacheHeader.readHeader(fis);
entry.size = file.length();
putEntry(entry.key, entry);
Expand Down Expand Up @@ -201,7 +200,7 @@ public synchronized void put(String key, Entry entry) {
pruneIfNeeded(entry.data.length);
File file = getFileForKey(key);
try {
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
BufferedOutputStream fos = new BufferedOutputStream(createOutputStream(file));
CacheHeader e = new CacheHeader(key, entry);
boolean success = e.writeHeader(fos);
if (!success) {
Expand Down Expand Up @@ -322,24 +321,27 @@ private void removeEntry(String key) {

/**
* Reads the contents of an InputStream into a byte[].
* */
*/
private static byte[] streamToBytes(InputStream in, int length) throws IOException {
byte[] bytes = new byte[length];
int count;
int pos = 0;
while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
pos += count;
}
if (pos != length) {
throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
}
new DataInputStream(in).readFully(bytes);
return bytes;
}

//VisibleForTesting
InputStream createInputStream(File file) throws FileNotFoundException {
return new FileInputStream(file);
}

//VisibleForTesting
OutputStream createOutputStream(File file) throws FileNotFoundException {
return new FileOutputStream(file);
}

/**
* Handles holding onto the cache headers for an entry.
*/
// Visible for testing.
//VisibleForTesting
static class CacheHeader {
/** The size of the data identified by this CacheHeader. (This is not
* serialized to disk. */
Expand Down Expand Up @@ -373,7 +375,7 @@ private CacheHeader() { }
* @param key The key that identifies the cache entry
* @param entry The cache entry.
*/
public CacheHeader(String key, Entry entry) {
CacheHeader(String key, Entry entry) {
this.key = key;
this.size = entry.data.length;
this.etag = entry.etag;
Expand All @@ -389,7 +391,7 @@ public CacheHeader(String key, Entry entry) {
* @param is The InputStream to read from.
* @throws IOException
*/
public static CacheHeader readHeader(InputStream is) throws IOException {
static CacheHeader readHeader(InputStream is) throws IOException {
CacheHeader entry = new CacheHeader();
int magic = readInt(is);
if (magic != CACHE_MAGIC) {
Expand All @@ -413,7 +415,7 @@ public static CacheHeader readHeader(InputStream is) throws IOException {
/**
* Creates a cache entry for the specified data.
*/
public Entry toCacheEntry(byte[] data) {
Entry toCacheEntry(byte[] data) {
Entry e = new Entry();
e.data = data;
e.etag = etag;
Expand All @@ -429,7 +431,7 @@ public Entry toCacheEntry(byte[] data) {
/**
* Writes the contents of this CacheHeader to the specified OutputStream.
*/
public boolean writeHeader(OutputStream os) {
boolean writeHeader(OutputStream os) {
try {
writeInt(os, CACHE_MAGIC);
writeString(os, key);
Expand Down Expand Up @@ -578,5 +580,4 @@ static Map<String, String> readStringStringMap(InputStream is) throws IOExceptio
return result;
}


}
Loading

0 comments on commit fba8125

Please sign in to comment.